forked from keithalewis/FRE6233
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfms_binomial.h
55 lines (42 loc) · 1.28 KB
/
fms_binomial.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// fms_binomial.h - binomial model
#pragma once
#include <cmath>
#include <functional>
// indicate error
#define ensure(e) if (!(e)) { return std::numeric_limits<double>::quiet_NaN(); }
namespace fms::binomial {
// F_j = F e^{s W_j/sqrt(n)}/cosh(s/sqrt(n))^n
// v_j(i) = E_j[nu(F_n) | F_j(i) = S, tau >= t_j]
inline double value(int i, int j, int n, double f, double s,
const std::function<double(double)>& nu, bool american = false)
{
ensure(n > 0);
double sn = s / sqrt(n);
double F_j = f * exp(sn * (j - 2. * i)) / pow(cosh(sn), j);
if (j == n) {
return nu(F_j);
}
double v = (value(i, j + 1, n, f, s, nu, american)
+ value(i + 1, j + 1, n, f, s, nu, american)) / 2;
if (american) {
//!!! optimal exercise code goes here !!!
}
return v;
}
// American put (k < 0) or call (p > 0) value at time j given W_j = i
inline double value(int i, int j, int n, double f, double s, double k, bool american = false)
{
std::function<double(double)> nu;
if (k < 0) { // put
nu = [k](double F) { return std::max(-k - F, 0.); };
}
else if (k > 0) { // call
nu = [k](double F) { return std::max(F - k, 0.); };
}
else {
return std::numeric_limits<double>::quiet_NaN();
}
return value(i, j, n, f, s, nu, american);
}
} // namespace fms
#undef ensure