diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/opti.cpp | 2 | ||||
| -rw-r--r-- | src/option.hpp | 16 | ||||
| -rw-r--r-- | src/projet.cpp | 13 |
3 files changed, 17 insertions, 14 deletions
diff --git a/src/opti.cpp b/src/opti.cpp index 18759cf..8a9b34e 100644 --- a/src/opti.cpp +++ b/src/opti.cpp @@ -11,7 +11,7 @@ double f (const std::vector<double> &X, std::vector<double> &grad, void *params) for(int i=0; i<d; i++){ norm+=X[i]*X[i]; } - asian_option A(r, T, S0, V, d, K); + asian_option A(r, T, S0, V, K); return log(A(X)) - 0.5*norm; }; diff --git a/src/option.hpp b/src/option.hpp index 49d1900..12c1043 100644 --- a/src/option.hpp +++ b/src/option.hpp @@ -5,17 +5,23 @@ double pos (double x); struct asian_option : public std::unary_function<std::vector<double>, double> { - asian_option(double r, double T, double S0, double V, int d, double K) - : r(r), T(T), S0(S0), V(V), d(d), K(K) {}; + asian_option(double r, double T, double S0, double V, double K, bool call = true) + : r(r), T(T), S0(S0), V(V), K(K) {}; - double operator()(std::vector<double> X) const { + double operator()(std::vector<double> &X) const { + int d= X.size(); std::vector<double> S(d); S[0]= S0*exp((r-V*V/2)*(T/d)+V*sqrt(T/d)*X[0]); for(int i=1;i<d;i++){ S[i]=S[i-1]*exp((r-V*V/2)*(T/d)+V*sqrt(T/d)*X[i]); } double temp = std::accumulate(S.begin(), S.end(), 0.)/d; - return exp(-r*T)*pos(temp-K); + if (call) { + return exp(-r*T)*pos(temp-K); + } + else { + return exp(-r*T)*pos(K-temp); + } }; private: @@ -23,8 +29,8 @@ struct asian_option : public std::unary_function<std::vector<double>, double> double T; double S0; double V; - int d; double K; + bool call; }; diff --git a/src/projet.cpp b/src/projet.cpp index af322ed..9d3ac15 100644 --- a/src/projet.cpp +++ b/src/projet.cpp @@ -82,7 +82,7 @@ std::vector<double> normalize (std::vector<double> mu) { u = normalize(mu); vector<double> q = quantile_norm(100, 1); vector<double> p(100, 0.01); - asian_option A(0.05, 1.0, 50, 0.1, d, 45); + asian_option A(0.05, 1.0, 50, 0.1, 45, true); exponential_tilt<asian_option> G(mu, A); typedef compose_t<exponential_tilt<asian_option>, multi_gaussian_truncated> tilted_option; std::vector<tilted_option> X; @@ -90,16 +90,13 @@ std::vector<double> normalize (std::vector<double> mu) { for(int i=1; i<100; i++) { X.push_back(compose(G, multi_gaussian_truncated(q[i-1],q[i], u))); } - for(int i=0; i<100; i=i+10){ - std::cout<<X[i]()<<endl; - } stratified_sampling<tilted_option> S(p, X); - S.draw(1000); + S.draw(1000000); cout<<"l'estimateur de la moyenne est :"<<S.estimator().first<<endl; } void exemple2_rqmc(int d) { - asian_option A(0.05, 1.0, 50.0, 0.1, 16, 45); + asian_option A(0.05, 1.0, 50.0, 0.1, 45,true); int N= 10000; @@ -116,7 +113,7 @@ void exemple2_rqmc(int d) { } }; -int make_table(vector< vector<double> > data1, vector< vector<double> > data2) { +int make_table1(vector< vector<double> > data1, vector< vector<double> > data2) { std::fstream fs("doc/table.tex", std::fstream::out);; fs<<R"(\begin{tabular}{|r|rr|rr|c|})"<<std::endl; fs<<R"(\hline)"<<endl; @@ -139,7 +136,7 @@ int main() vector< vector<double> > data1 = exemple1_stratified(); cout<<"Randomised quasi Monte-Carlo sur l'exemple 1 de la normale"<<endl; vector< vector<double> > data2 = exemple1_rqmc(); - make_table(data1, data2); + make_table1(data1, data2); exemple2_stratified(16); return 0; |
