MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / monte_carlo_barrier

Function monte_carlo_barrier

examples/financial/monte_carlo_options.cpp:30–48  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29template<class ty, bool use_barrier>
30static ty monte_carlo_barrier(int N, ty K, ty t, ty vol, ty r, ty strike,
31 int steps, ty B) {
32 dtype pres = get_dtype<ty>();
33 array payoff = constant(0, N, 1, pres);
34
35 ty dt = t / (ty)(steps - 1);
36 array s = constant(strike, N, 1, pres);
37
38 array randmat = randn(N, steps - 1, pres);
39 randmat = exp((r - (vol * vol * 0.5)) * dt + vol * sqrt(dt) * randmat);
40
41 array S = product(join(1, s, randmat), 1);
42
43 if (use_barrier) { S = S * allTrue(S < B, 1); }
44
45 payoff = max(0.0, S - K);
46 ty P = mean<ty>(payoff) * exp(-r * t);
47 return P;
48}
49
50template<class ty, bool use_barrier>
51double monte_carlo_bench(int N) {

Callers

nothing calls this directly

Calls 8

constantFunction · 0.85
randnFunction · 0.85
expFunction · 0.85
sqrtFunction · 0.85
productFunction · 0.85
allTrueFunction · 0.85
joinFunction · 0.50
maxFunction · 0.50

Tested by

no test coverage detected