-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMonteCarlo4DD.cpp
192 lines (162 loc) · 4.56 KB
/
MonteCarlo4DD.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Monte Carlo simulations to find the Price and Greeks of a simple
// Vanilla Call Option.
//
// David Dallaire - Example from FADBAD++ but modified
// - to include dividend yield
// - use C++14 random generator that default to Mersenne Twister
// Answer trivial but coding not so
/*
Answer from xterm
BadSimpleMonteCarlo
Price=9.11906
dPriceSpot=0.579618
dPriceVol=37.4675
dPriceR=48.8427
dDividend=-57.9618
FADBADSimpleMonteCarlo
Price=9.11906
dPriceSpot=0.579618
dPriceVol=37.4675
dPriceR=48.8427
dDividend=-57.9618
dPrice2Spot=0
*/
#include <cstdlib>
#include <cmath>
#include "badiff.h"
#include "fadiff.h"
#include <iostream>
#include <random>
//compile with following
//g++ -I/media/ddu/HD1/MyMain/usr/FADBAD++ -std=c++14 -O3 -o MonteCarlo4DD MonteCarlo4DD.cpp -lm
using namespace std;
using namespace fadbad;
template <typename ArithT>
ArithT SimpleMonteCarlo(const double Expiry,
const double Strike,
const ArithT& Spot,
const ArithT& Dividend,
const ArithT& Vol,
const ArithT& r,
unsigned long NumberOfPaths)
{
random_device rd;
//mt19937 gen(rd());
//set constant see
mt19937 gen(1);
normal_distribution<> d(0,1);
ArithT variance = Vol*Vol*Expiry;
ArithT rootVariance = sqrt(variance);
ArithT itoCorrection = -0.5*variance;
ArithT movedSpot = Spot*exp((r-Dividend)*Expiry +itoCorrection);
ArithT thisSpot;
ArithT runningSum=0;
for (unsigned long i=0; i < NumberOfPaths; i++)
{
thisSpot = movedSpot*exp( rootVariance*d(gen));
ArithT thisPayoff = thisSpot - Strike;
thisPayoff = thisPayoff >0 ? thisPayoff : 0;
runningSum += thisPayoff;
}
ArithT mean = runningSum / double(NumberOfPaths);
mean *= exp(-r*Expiry);
return mean;
}
template <typename ArithT>
void BADSimpleMonteCarlo(const double Expiry,
const double Strike,
const ArithT& Spot,
const ArithT& Dividend,
const ArithT& Vol,
const ArithT& r,
unsigned long NumberOfPaths,
ArithT& Price,
ArithT& dPriceSpot,
ArithT& dPriceVol,
ArithT& dPriceR,
ArithT& dDividend)
{
B< ArithT > BSpot(Spot);
B< ArithT > BVol(Vol);
B< ArithT > Br(r);
B< ArithT > BDividend(Dividend);
B< ArithT > BPrice=SimpleMonteCarlo(Expiry,Strike,BSpot,BDividend,BVol,Br,NumberOfPaths);
BPrice.diff(0,1);
Price=BPrice.x();
dPriceSpot=BSpot.d(0);
dPriceVol=BVol.d(0);
dPriceR=Br.d(0);
dDividend = BDividend.d(0);
}
template <typename ArithT>
void FADBADSimpleMonteCarlo(const double Expiry,
const double Strike,
const ArithT& Spot,
const ArithT& Dividend,
const ArithT& Vol,
const ArithT& r,
unsigned long NumberOfPaths,
ArithT& Price,
ArithT& dPriceSpot,
ArithT& dPriceVol,
ArithT& dPriceR,
ArithT& dDividend,
ArithT& dPrice2Spot)
{
F< ArithT > FSpot(Spot);
F< ArithT > FVol(Vol);
F< ArithT > Fr(r);
F< ArithT > FDividend(Dividend);
FSpot.diff(0,1);
F< ArithT > FPrice;
F< ArithT > FdPriceSpot;
F< ArithT > FdPriceVol;
F< ArithT > FdPriceR;
BADSimpleMonteCarlo(Expiry,Strike,FSpot,FDividend,FVol,Fr,NumberOfPaths,
FPrice, FdPriceSpot, FdPriceVol, FdPriceR, FDividend);
Price=FPrice.x();
dPriceSpot=FdPriceSpot.x();
dPriceVol=FdPriceVol.x();
dPriceR=FdPriceR.x();
dDividend = FDividend.x();
dPrice2Spot=FdPriceSpot.d(0);
}
int main()
{
double Strike=100;
double Expiry=1.0;
double Dividend = 0.03;
double Spot=100;
double Vol=.2;
double r=0.06;
double Price;
double dPriceSpot;
double dPriceVol;
double dPriceR;
double dPrice2Spot;
double dDividend;
srand(0);
BADSimpleMonteCarlo(Expiry,Strike,Spot,Dividend,Vol,r,100000,
Price,dPriceSpot,dPriceVol,dPriceR,dDividend);
cout << "\nBadSimpleMonteCarlo" << endl;
cout << "\nPrice=" << Price << endl;
cout << "dPriceSpot=" << dPriceSpot << endl;
cout << "dPriceVol=" << dPriceVol << endl;
cout << "dPriceR=" << dPriceR << endl;
cout << "dDividend=" << dDividend << endl;
srand(0);
// NOTICE: that the gamma-value is wrong since the
// derivative of the intrinsic value as a function of
// spot is not Lipschitz.
srand(0);
FADBADSimpleMonteCarlo(Expiry,Strike,Spot,Dividend,Vol,r,100000,
Price,dPriceSpot,dPriceVol,dPriceR,dDividend,dPrice2Spot);
cout << "\nFADBADSimpleMonteCarlo" << endl;
cout << "\nPrice=" << Price << endl;
cout << "dPriceSpot=" << dPriceSpot << endl;
cout << "dPriceVol=" << dPriceVol << endl;
cout << "dPriceR=" << dPriceR << endl;
cout << "dDividend=" << dDividend << endl;
cout << "dPrice2Spot=" << dPrice2Spot << endl;
return 0;
}