aboutsummaryrefslogtreecommitdiffstats
path: root/stratified_sampling.cpp
blob: b9597760dd12cf75df9b28e94f7b7580b788f709 (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
#include "stratified_sampling.hpp"
#include <algorithm>
#include <iostream>

std::pair<double, double> mean_var( std::vector<double> r){
    std::pair<double, double> p;
    for(auto &x: r){
        p.first += x;
        p.second += x*x;
    }
    p.first /= r.size();
    p.second /= r.size();
    p.second -= p.first * p.first;
    return p;
}

//actualisation du nombre de tirages à faire par strates
template <typename Gen>
void stratified_sampling<Gen>::update(int Nk) {
    int I = p.size();
    //reinitialistation du vecteur M du nombre de tirages par strates
    if (M.empty()) {
        M.resize(I,1);
    }
    else {
        for(int i=0; i<I; i++){
            M[i]=1;
        }
    }

    std::vector<double> m(I, 0); //le vecteur des m_i idéals

    if (sigma.empty()) {
        for (int i=0; i<I; i++) {
            m[i] = (Nk-I)*p[i];
        }
    }
    else {
        double scal = std::inner_product(p.begin(), p.end(), sigma.begin(), (double) 0);
        for (int i=0; i < I; i++) {
            m[i] = (Nk-I)*p[i]*sigma[i]/scal;
            std::cout<<m[i]<<std::endl;
        }
    }
    M[0]+=floor(m[0]);
    double current = m[0];
    for (int i=1; i<I; i++){
        M[i] += floor(current+m[i]) - floor(current);
        current += m[i];
    }
}