aboutsummaryrefslogtreecommitdiffstats
path: root/src/projet.cpp
blob: efab70318c568841bdf6c0cbdd5f6ff79d176e31 (plain)
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
#include <iostream>
#include <fstream>
#include <vector>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_math.h>
#include "stratified_sampling.hpp"
#include <cmath>
#include <algorithm>
#include "opti.hpp"
#include "option.hpp"
#include "rqmc.hpp"

using namespace std;

struct first:public std::unary_function<std::vector<double>, double>
{
    double operator()(std::vector<double> X){
        return X[0];
    }
};

vector< vector<double> > exemple1_stratified() {
    int I = 10; //le nombre de strates
    vector<double> q = quantile_norm(10, 1);
    vector<double> p(I, 1/(double)I);
    vector<gaussian_truncated> rvar;
    rvar.push_back(gaussian_truncated(GSL_NEGINF, q[0]));
    for (int i=1; i<I; i++){
        rvar.push_back(gaussian_truncated(q[i-1], q[i]));
    };
    vector<int> N = {300, 1000, 10000, 20000}; //notre tableau du nombre successif de tirages, qui correspondent aux 300, 1300, 11300 et 31300
                                               //de l'article de Etoré et Jourdain
    vector< vector<double> > data (4);
    stratified_sampling<gaussian_truncated> S(p,rvar);
    cout<<"N"<<"\t"<<"moyenne"<<"\t\t"<<"sigma"<<"\t"<<"théorique"<<endl;
    vector<double> r(4,0);
    for (size_t i=0; i<N.size(); i++){
        S.draw(N[i]);
        r[0]= r[0] + N[i];
        r[1] = S.estimator().first;
        r[2] = S.estimator().second;
        r[3] = 0.1559335;
        cout<<r[0]<<"\t"<<r[1]<<"\t"<<r[2]<<"\t"<<r[3]<<endl;
        data[i] = r;
    };
    return data;
};

vector< vector<double> > exemple1_rqmc(){
    int I = 100;
    vector<int> N = {3, 13, 113, 313}; //les N choisis pour que les NI soient égaux aux N de l'exemple 1 stratified_sampling
    first f; //comme quasi_gaussian retourne un vecteur, on doit composer avec f pour avoir le double QG()[0]
    vector< vector<double> > data (4);
    for(size_t i =0; i<N.size(); i++){
        data[i] = monte_carlo (I, quasi_mean<struct first, sobol> (N[i], 1, f));
    }
    cout<<"moyenne"<<"\t\t"<<"sigma"<<"\t\t"<<"taille IC"<<endl;
    for(int i =0; i<3; i++){
        cout<<data[i][0]<<"\t"<<data[i][1]<<"\t"<<data[i][2]<<endl;
    }
    return data;
};


std::vector<double> normalize (std::vector<double> mu) {
    double norm_mu = accumulate(mu.begin(), mu.end(), 0.,
                                [](double x, double y){ return x + y*y;});
    vector<double> u(mu);
    transform(u.begin(), u.end(), u.begin(), [norm_mu](double x){ return x/sqrt(norm_mu); });
    return u;
}


vector <vector<double> > exemple2_stratified (int d, bool call = true){
    vector<double> K = {45, 50, 55};
    vector<int> N = {100000, 400000, 500000};
    int I = 100; //le nombre de strates
    vector< vector<double> > data(3);
    vector<double> q = quantile_norm(I, 1);
    vector<double> p(I, 1/(double)I);
    double r = 0.05;
    double T = 1.0;
    double S0 = 50;
    double V = 0.1;
    typedef compose_t<exponential_tilt<asian_option>, multi_gaussian_truncated> tilted_option;
    for (size_t i=0; i < K.size(); i++){
        vector<double> mu = argmax(r, T, S0, V, K[i], d, call);
        std::vector<double> u = normalize(mu);
        asian_option A(r, T, S0, V, K[i], call);
        exponential_tilt<asian_option> G(mu, A);
        std::vector<tilted_option> X;
        X.push_back(compose(G, multi_gaussian_truncated(GSL_NEGINF, q[0], u)));
        for(int j=1; j < I; j++) {
            X.push_back(compose(G, multi_gaussian_truncated(q[j-1], q[j], u)));
        }
        stratified_sampling<tilted_option> S(p, X);
        vector<double> r(3, 0);
        for (size_t j=0; j < N.size(); j++){
            S.draw(N[j]);
        }
        r[0] = K[i];
        r[1] = S.estimator().first;
        r[2] = S.estimator().second;
        data[i] = r;
    }
    return data;
}

vector< vector<double> > exemple2_rqmc(int d, bool call = true) {
    int N= 10000;
    double r = 0.05;
    double T = 1.0;
    double S0 = 50;
    double V = 0.1;
    vector< vector<double> > data(3);
    vector<double> K = {45, 50, 55};
    int I=100;//la taille du vrai Monte-Carlo
    for(int i =0; i<3; i++) {
        asian_option A(r, T, S0, V, K[i], call);
        data[i] = monte_carlo(I, quasi_mean<asian_option, sobol> (N, d, A));
    }
    return data;
};

int make_table1(vector< vector<double> > const &data1, vector< vector<double> > const &data2) {
    std::fstream fs("doc/table1.tex", std::fstream::out);
    fs<<R"(\begin{tabular}{|r|rr|rr|c|})"<<std::endl;
    fs<<R"(\hline)"<<endl;
    fs<<"N"<<" & "<<R"($\mu_{strat}$)"<<" & "<<R"($\mu_{rqmc}$)"<<" & "<<R"($\textrm{IC}_{strat}$)"<<" & "<<R"($\textrm{IC}_{rqmc}$)"<<" & "<< R"($\textrm{IC}_{strat}/\textrm{IC}_{rqmc}$)"<<R"(\\ \hline)"<<std::endl;
    fs.precision(2);
    for (int i=0; i< 4; i++) {
        double ic_strat = 1.95996*sqrt(data1[i][2]/(double) data1[i][0]);
        fs<<(int)data1[i][0]<<"&"<<scientific<<data1[i][1]<<"&"<<data2[i][0]<<"&"<<ic_strat<<"&"<<data2[i][2]/2<<"&"<<fixed<<ic_strat/(data2[i][2]/2)<<R"(\\ \hline)"<<std::endl;
    }
    fs<<R"(\end{tabular})"<<std::endl;
    fs.close();
    return 0;
}

int make_table2(vector< vector<double> > data1, vector< vector<double> > data2, vector< vector<double> > data3, vector< vector<double> > data4, string table_name) {
    std::fstream fs("doc/" + table_name, std::fstream::out);
    fs<<R"(\begin{tabular}{|r|r|rr|rr|c|})"<<std::endl;
    fs<<R"(\hline)"<<endl;
    fs<<"d"<<" & "<<"K"<<" & "<<R"($\mu_{strat}$)"<<" & "<<R"($\mu_{rqmc}$)"<<" & "<<R"($\textrm{IC}_{strat}$)"<<" & "<<R"($\textrm{IC}_{rqmc}$)"<<" & "<< R"($\textrm{IC}_{strat}/\textrm{IC}_{rqmc}$)"<<R"(\\ \hline)"<<std::endl;
    fs.precision(3);
    int N = 1000000;
    for (int i=0; i< 3; i++) {
        double ic_strat = 1.95996*sqrt(data1[i][2]/(double) N);
        fs<<"16"<<" & "<< (int) (data1[i][0]) <<"&"<< fixed <<data1[i][1] <<" & "<<data2[i][0]<<" & "<< scientific <<ic_strat<<" & "<<data2[i][2]/2<<" & "<<fixed<<ic_strat/(data2[i][2]/2)<<R"(\\)"<<std::endl;
    }
    fs<<R"(\hline\hline)"<<endl;
    for (int i=0; i< 3; i++) {
        double ic_strat = 1.95996*sqrt(data3[i][2]/(double) N);
        fs<<"64"<<" & "<< (int) (data3[i][0]) <<"&"<< fixed << data3[i][1] <<" & "<<data4[i][0]<<" & "<< scientific <<ic_strat<<" & "<<data4[i][2]/2<<" & "<<fixed<<ic_strat/(data4[i][2]/2)<<R"(\\)"<<std::endl;
    }
    fs<<R"(\hline)"<<endl;
    fs<<R"(\end{tabular})"<<std::endl;
    fs.close();
    return 0;
}

int main()
{
    init_alea(0);
    cout<<"comparaison stratified sampling/RQMC sur l'example 1\n"<<endl;
    make_table1(exemple1_stratified(), exemple1_rqmc());
    vector< vector<double> > data1 = exemple2_stratified(16);
    vector< vector<double> > data2 = exemple2_rqmc(16);
    vector< vector<double> > data3 = exemple2_stratified(64);
    vector< vector<double> > data4 = exemple2_rqmc(64);
    make_table2(data1, data2, data3, data4, "table2.tex");
    vector< vector<double> > data5 = exemple2_stratified(16, false);
    vector< vector<double> > data6 = exemple2_rqmc(16, false);
    vector< vector<double> > data7 = exemple2_stratified(64, false);
    vector< vector<double> > data8 = exemple2_rqmc(64,false);
    make_table2(data5, data6, data7, data8, "table3.tex");
    return 0;

}