aboutsummaryrefslogtreecommitdiffstats
path: root/src/projet.cpp
blob: 4a5889e4cfffb57544f537c79a1bed8e850762c5 (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
#include <iostream>
#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() {
    vector<double> q = quantile_norm(10, 1);
    vector<double> p(10, 0.1);
    vector<gaussian_truncated> rvar;
    rvar.push_back(gaussian_truncated(GSL_NEGINF, q[0]));
    for (int i=1; i<10; 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 (int i=0; i<4; 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(int i =0; i<4; 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;
};

 void exemple2_stratified (){
     int d= 16;
     std::vector<double> mu(d);
     mu = argmax(0.05, 1.0, 50, 0.1, 45, d);
     double norm_mu = 0;
     std::vector<double> u(d);
     for(int i=0; i<d; i++) {
         norm_mu += mu[i]*mu[i];
     }
     for(int i=0; i<d; i++) {
         u[i] = mu[i]/sqrt(norm_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);
     exponential_tilt<asian_option> G(mu, A);
     typedef compose_t<exponential_tilt<asian_option>, multi_gaussian_truncated> tilted_option;
     std::vector<tilted_option> X;
     X.push_back(compose(G, multi_gaussian_truncated(GSL_NEGINF,q[0], u)));
     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);
     cout<<"l'estimateur de la moyenne est :"<<S.estimator().first<<endl;
}

void exemple2_rqmc() {
    asian_option A(0.05, 1.0, 50.0, 0.1, 16, 45);
    int N= 10000;
    
    int d =16; 

    
    std::vector<double> result(3);
    result = monte_carlo(100, quasi_mean<asian_option, sobol> (N, d, A));
    for(int i =0; i<3; i++){
        std::cout<<result[i]<<std::endl;
    }
    
    std::vector<double> result2(3);
    result2 = monte_carlo(100, quasi_mean<asian_option, halton> (N, d, A));
    for(int i =0; i<3; i++){
        std::cout<<result2[i]<<std::endl;
    }
};


int main()
{   
    init_alea(1);
    cout<<"Stratified_sampling sur l'exemple 1 de la normale"<<endl;
    exemple1_stratified(); 
    cout<<"Randomised quasi Monte-Carlo sur l'exemple 1 de la normale"<<endl;
    exemple1_rqmc();
    return 0;
    
}