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
|
#include <vector>
#include <gsl/gsl_cdf.h>
#include "var_alea.hpp"
#include <algorithm>
#include <iostream>
typedef struct option_param {
double r;
double T;
double S0;
double V;
int d;
double K;
}option_param;
std::vector<double> path_gen(std::vector<double> X, option_param *p){
std::vector<double> S(p->d);
S[0]= p->S0*exp((p->r-p->V*p->V/2)*(p->T/p->d)+p->V*sqrt(p->T/p->d)*X[0]);
for(int i=1;i<p->d;i++){
S[i]=S[i-1]*exp((p->r-p->V*p->V/2)*(p->T/p->d)+p->V*sqrt(p->T/p->d)*X[i]);
}
return S;
}
double pos (double x){
return x>0?x:0;
}
double pay_off (double mean, option_param* p){
return exp(-p->r*p->T)*pos(mean-p->K);
}
std::pair<double,double> monte_carl(option_param* p, int N){
double moyenne=0;
double variance=0;
gaussian G(0,1);
double temp=0;
std::vector<double> S(p->d);
std::vector<double> X(p->d);
for(int i=0; i<N; i++){
for(int j=0;j<p->d;j++){
X[j]=G();
}
S=path_gen(X, p);
temp = pay_off(std::accumulate(S.begin(), S.end(), 0.)/p->d, p);
moyenne+=temp;
variance+=temp*temp;
}
return {moyenne/N, variance/N-(moyenne/N)*(moyenne/N)};
}
int main(){
init_alea(1);
option_param p = {.r=0.05, .T=1.0, .S0=50.0, .V=0.1, .d=16, .K=45};
int N=1000000;
std::pair<double,double> meanvar = monte_carl(&p, N);
std::cout<<"espérance "<<meanvar.first<<" IC "<<1.64*sqrt(meanvar.second)/sqrt(N)<<std::endl;
return 0;
}
|