From 22358d075a89d44dbe8ef901a0a42d2c3f8e285a Mon Sep 17 00:00:00 2001 From: Bertrand Date: Sat, 6 Feb 2016 19:32:06 +0000 Subject: encapsulage du stratified sampling --- Makefile | 4 +- main.cpp | 35 +++++++++++++ stratified_sampling.cpp | 130 +++++++++++++----------------------------------- stratified_sampling.hpp | 51 +++++++++++++++++++ 4 files changed, 123 insertions(+), 97 deletions(-) create mode 100644 main.cpp create mode 100644 stratified_sampling.hpp diff --git a/Makefile b/Makefile index c412ee3..62e9c4e 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,11 @@ RM = rm CXXFLAGS=-std=c++11 -O3 GSL_FLAGS:=$(shell pkg-config --libs gsl) -stratified_sampling: rtnorm.o stratified_sampling.o +main: main.o rtnorm.o stratified_sampling.o $(CXX) $^ -o $@ $(GSL_FLAGS) test: test.o mt19937.o $(CXX) $^ -o $@ clean: - -$(RM) -f *.o test stratified_sampling + -$(RM) -f *.o test stratified_sampling main diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..c1eb3ec --- /dev/null +++ b/main.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +//#include "rtnorm.hpp" +#include +#include +#include "stratified_sampling.hpp" +#include +#include + +//--génération quantiles-- +std::vector quantile_norm(int n, double sigma){ + std::vector q(n); + for (int i=0; i q; + q = quantile_norm(10, 1); + vector p(10, 0.1); + vector gen; + gen.push_back(gaussian_truncated(GSL_NEGINF,q[0])); + for (int i=1; i<10; i++){ + gen.push_back(gaussian_truncated(q[i-1],q[i])); + } + stratified_sampling S(p, gen); + //S.update(1000); + return 0; +} diff --git a/stratified_sampling.cpp b/stratified_sampling.cpp index 8f408cc..c03d447 100644 --- a/stratified_sampling.cpp +++ b/stratified_sampling.cpp @@ -1,31 +1,6 @@ -#include -#include -#include -#include "rtnorm.hpp" -#include -#include - -#include +#include "stratified_sampling.hpp" #include - -//--génération quantiles-- -std::vector quantile_norm(int n, double sigma){ - std::vector q(n); - for (int i=0; i q; - q = quantile_norm(n, sigma); - std::pair p; - p = rtnorm (gen, q[i], q[i+1], mu, sigma); - return p.first; - } +#include std::pair mean_var( std::vector r){ std::pair p; @@ -39,75 +14,40 @@ std::pair mean_var( std::vector r){ return p; } //actualisation du nombre de tirages à faire par strates -std::vector update_sampling (std::vector p, - std::vector sigma, int Nk) { - int I = p.size(); - std::vector M(I, 1); // notre vecteur final à retourner - std::vector m(I, 0); //le vecteur des m_i idéals - - if (sigma.empty()) { - for (int i=0; i q; - q = quantile_norm(10, 1); - - - std::pair p; - std::pair mv; - //number of classes - int I = 10; - //number of samples - int N = 10000; - std::vector r(N); - double a; - for (int i=0; i +void stratified_sampling::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 k; - std::vector z = {(double)1/3,(double)1/3,(double)1/3}; - std::vector sigma = {0.1, 0.4, 0.3}; - k = update_sampling(z, sigma, 10000); - for (int j=0; j m(I, 0); //le vecteur des m_i idéals - + if (sigma.empty()) { + for (int i=0; i +#include +#include "rtnorm.hpp" + +using namespace std; + +template +struct var_alea { + typedef T result_type; + var_alea() : value(0) {}; + var_alea(T value) : value(value) {}; + virtual ~var_alea() {}; + virtual T operator()() = 0; + T current() const { return value; }; + protected: + T value; +}; + +typedef var_alea var_alea_real; + +struct gaussian_truncated : public var_alea_real +{ + gaussian_truncated(double a, double b, double mean=0, double sigma=1) + :a(a), b(b), mean(mean), sigma(sigma) { + const gsl_rng_type* type = gsl_rng_default; + gen = gsl_rng_alloc(type); + }; + double operator()() { + pair p = rtnorm(gen, a, b, mean, sigma); + return value = p.first; + }; + ~gaussian_truncated() { gsl_rng_free(gen); } +private: + double mean, sigma, a, b; + gsl_rng *gen; +}; + +template +struct stratified_sampling { + stratified_sampling(vector p, vector gen) + :p(p), gen(gen) {}; + void update(int N); + //vector get_mean(); + //double estimator(); +private: + vector p; + vector M; + vector sigma; + vector gen; + +}; -- cgit v1.2.3-70-g09d2