aboutsummaryrefslogtreecommitdiffstats
path: root/option.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'option.cpp')
-rw-r--r--option.cpp47
1 files changed, 33 insertions, 14 deletions
diff --git a/option.cpp b/option.cpp
index 87aeca6..a104c19 100644
--- a/option.cpp
+++ b/option.cpp
@@ -4,45 +4,64 @@
#include <algorithm>
#include <iostream>
-struct option_param {
+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;
+ 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 mean_monte_carl(option_param* p, int N){
+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;
- gaussian G;
+ 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=1; i<N; i++){
- for(int j=1;j<p->d;i++){
- X[i]=G();
+ for(int i=0; i<N; i++){
+ for(int j=0;j<p->d;j++){
+ X[j]=G();
}
S=path_gen(X, p);
- moyenne+=std::accumulate(S.begin(), S.end(), 0 );
+ temp = pay_off(std::accumulate(S.begin(), S.end(), 0.)/p->d, p);
+ moyenne+=temp;
+ variance+=temp*temp;
}
- return moyenne/N;
+ return {moyenne/N, variance/N-(moyenne/N)*(moyenne/N)};
}
+
+
+
+
+
int main(){
- init_alea(0);
- option_param* p = {.r=0.05, .T=1.0, .S0=50.0, .V=0.1, .d=16};
+ 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;
- double moyenne = mean_monte_carl(p, N);
- std::cout<<moyenne<<std::endl;
+ 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;