diff options
| author | Bertrand <bertrand.horel@gmail.com> | 2016-02-03 21:00:07 +0000 |
|---|---|---|
| committer | Bertrand <bertrand.horel@gmail.com> | 2016-02-03 21:00:07 +0000 |
| commit | 3711294a9ca5b6651b6486eb1fe053ff697528ec (patch) | |
| tree | c117e571f566daa5bcea86a56466c2bba57ad5de /stratified_sampling.cpp | |
| parent | 419281176fc54599d277359bf84fa4f8fe1344b4 (diff) | |
| download | projet_C++-3711294a9ca5b6651b6486eb1fe053ff697528ec.tar.gz | |
fonction d'update du nombres de tirages par strates
Diffstat (limited to 'stratified_sampling.cpp')
| -rw-r--r-- | stratified_sampling.cpp | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/stratified_sampling.cpp b/stratified_sampling.cpp index f6c3ff1..4703aba 100644 --- a/stratified_sampling.cpp +++ b/stratified_sampling.cpp @@ -1,9 +1,16 @@ #include <iostream> #include <gsl/gsl_rng.h> - +#include <vector> #include "rtnorm.hpp" #include <gsl/gsl_cdf.h> #include <gsl/gsl_math.h> +#define LOW_DISCREPANCY_HPP +#include <cmath> +#include <climits> +#include <list> +#include <algorithm> +#include <numeric> + //--génération quantiles-- std::vector<double> quantile_norm(int n, double sigma){ @@ -35,6 +42,37 @@ std::pair<double, double> mean_var( std::vector<double> r){ p.second -= p.first * p.first; return p; } +//actualisation du nombre de tirages à faire par strates +std::vector<int> update_sampling (std::vector<double> p, +std::vector<double> sigma, int n) { + std::vector<int> r; // notre vecteur final à retourner + std::vector<double> m; //le vecteur des Mi idéals + int s = p.size(); + if (sigma.empty()) { + for (int i = 0; i<s ; i++) { + m.push_back(double(n)*p[i]); + } + } + else { + double scal = std::inner_product(p.begin(), p.end(), sigma.begin(), (double) 0); + for (int i=0; i<s; i++) { + double v = p[i]*sigma[i]/scal; + m.push_back(v*n); + std::cout<<m[i]<<std::endl; + } + } + r.push_back((int)(m[0])); + double v1 = 0; + for (int i=1; i<s-1; i++){ + for (int j=0; j<i; j++) { + v1=v1+m[j]; + } + r.push_back((int)v1 - (int)(v1-m[i])); + } + r.push_back(n - std::accumulate( r.begin(), r.end(), 0 )); + return r; +} + int main() { @@ -44,7 +82,7 @@ int main() gsl_rng *gen = gsl_rng_alloc (type); // Rand generator allocation std::vector<double> q; q = quantile_norm(10, 1); - std::cout<<q[5]<<std::endl; + std::pair<double, double> p; std::pair<double, double> mv; @@ -65,8 +103,18 @@ int main() r[j] = p.first; } mv = mean_var(r); - std::cout<<"mean :"<<mv.first<<" var :"<<mv.second<<std::endl; + //std::cout<<"mean :"<<mv.first<<" var :"<<mv.second<<std::endl; } + std::vector<int> k; + std::vector<double> z = {(double)1/3,(double)1/3,(double)1/3}; + std::vector<double> sigma = {0.1, 0.4, 0.3}; + k = update_sampling(z, sigma, 10000); + for (int j=0; j<k.size(); j++){ + std::cout<<k[j]<<std::endl; + } gsl_rng_free(gen); + + + return 0; } |
