diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2008-12-15 07:26:12 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2008-12-15 07:26:12 +0000 |
commit | 1f4a944064bc42284c33e6b755353d191cf288e8 (patch) | |
tree | c8cb2253dea5f395e0f867aa6976433bd3eb00de /misc | |
download | opt-utilities-1f4a944064bc42284c33e6b755353d191cf288e8.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@1 ed2142bd-67ad-457f-ba7c-d818d4011675
Diffstat (limited to 'misc')
-rw-r--r-- | misc/bootstrap.hpp | 132 | ||||
-rw-r--r-- | misc/data_loaders.hpp | 180 |
2 files changed, 312 insertions, 0 deletions
diff --git a/misc/bootstrap.hpp b/misc/bootstrap.hpp new file mode 100644 index 0000000..b46748d --- /dev/null +++ b/misc/bootstrap.hpp @@ -0,0 +1,132 @@ +#ifndef BOOT_STRIP +#define BOOT_STRIP +#include <core/fitter.hpp> +#include <vector> +#include <cstdlib> +#include <iostream> +#include <utility> +#include <algorithm> +#include <core/default_data_set.hpp> +using std::cout; +namespace opt_utilities +{ + template <typename Ty,typename Tx,typename Tp> + class bootstrap + { + private: + Ty rand_norm(Ty y0,Ty y_err)const + { + Ty y; + do + { + y=(rand()/(Ty)RAND_MAX-(Ty).5)*(10*y_err)+y0; + } + while(rand()/(Ty)RAND_MAX>exp(-(y-y0)*(y-y0)/(y_err*y_err))); + return y; + + } + public: + std::vector<Tp> param_pool; + default_data_set<Ty,Tx> current_data_set; + default_data_set<Ty,Tx> origin_data_set; + fitter<Ty,Tx,Tp>* p_fitter; + Tp origin_param; + public: + bootstrap() + :p_fitter(NULL) + {} + + void set_fitter(fitter<Ty,Tx,Tp>& pf) + { + param_pool.clear(); + p_fitter=&pf; + origin_data_set=dynamic_cast<const default_data_set<Ty,Tx>&>(pf.datas()); + origin_param=pf.get_all_params(); + } + + + + void sample(int n) + { + if(p_fitter!=NULL) + { + for(int i=0;i<n;++i) + { + sample(); + } + + p_fitter->load_data(origin_data_set); + p_fitter->set_param_value(origin_param); + } + else + { + throw opt_exception("Fitter unset"); + } + } + + const Tp& get_param(int i)const + { + return param_pool.at(i); + } + + private: + void sample() + { + current_data_set=default_data_set<Ty,Tx>(); + for(int i=0;i<origin_data_set.size();++i) + { + data<Ty,Tx> d; + d=origin_data_set.get_data(i); + d.set_y(rand_norm(d.get_y(),(d.get_y_upper_err()+d.get_y_lower_err())/2)); + current_data_set.push_back(d); + } + p_fitter->load_data(current_data_set); + p_fitter->set_param_value(origin_param); + param_pool.push_back(p_fitter->fit()); + for(size_t i=0;i<(param_pool.back()).size();++i) + { + cout<<param_pool.back()[i]<<","; + } + std::cout<<std::endl; + } + + public: + std::pair<typename element_type_trait<Tp>::element_type,typename element_type_trait<Tp>::element_type> + interval(std::string param_name,double level) + { + if(p_fitter==NULL) + { + throw opt_exception("Fitter unset"); + } + if(param_pool.empty()) + { + throw opt_exception("Bootstrap not done"); + } + //sample(); + std::vector<typename element_type_trait<Tp>::element_type> _tmp; + int order=p_fitter->get_param_order(param_name); + for(typename std::vector<Tp>::iterator i=param_pool.begin(); + i!=param_pool.end();++i) + { + _tmp.push_back((*i)[order]); + } + sort(_tmp.begin(),_tmp.end()); + std::pair<typename std::vector<typename element_type_trait<Tp>::element_type>::iterator, + typename std::vector<typename element_type_trait<Tp>::element_type>::iterator> + itv=equal_range(_tmp.begin(),_tmp.end(),origin_param[order]); + int current_param_position=itv.second-_tmp.begin(); + std::cout<<_tmp.size()<<std::endl; + return std::pair<typename element_type_trait<Tp>::element_type, + typename element_type_trait<Tp>::element_type>( + _tmp.at((int)((1-level)*current_param_position)), + _tmp.at((int)(current_param_position+level*(_tmp.size()-current_param_position))) + ); + + } + + }; + +}; + +#endif +//EOF diff --git a/misc/data_loaders.hpp b/misc/data_loaders.hpp new file mode 100644 index 0000000..6d2195d --- /dev/null +++ b/misc/data_loaders.hpp @@ -0,0 +1,180 @@ +#ifndef DATA_LOADERS_H +#define DATA_LOADERS_H +#include <core/fitter.hpp> +#include <core/default_data_set.hpp> +#include <iostream> +#include <fstream> +#include <cmath> + +namespace opt_utilities +{ + template <typename Ty,typename Tx> + class dl_x_y_ye; + + template <typename Ty,typename Tx> + std::istream& operator>>(std::istream& ifs,dl_x_y_ye<Ty,Tx>& dl); + + template <typename Ty,typename Tx> + class dl_x_xe_y_ye; + + template <typename Ty,typename Tx> + std::istream& operator>>(std::istream& ifs,dl_x_xe_y_ye<Ty,Tx>& dl); + + template <typename Ty,typename Tx> + class dl_x_xu_xl_y_yu_yl; + + template <typename Ty,typename Tx> + std::istream& operator>> (std::istream& ifs,dl_x_xu_xl_y_yu_yl<Ty,Tx>& dl); + + template <typename Ty,typename Tx> + class dl_x_y_ye + { + private: + default_data_set<Ty,Tx> ds; + public: + data_set<Ty,Tx>& get_data_set() + { + return ds; + } + + void load_from(std::istream& ifs) + { + for(;;) + { + Tx x; + Tx x_err; + Ty y; + Ty y_err(1); + + ifs>>x>>y>>y_err; + + if(!ifs.good()) + { + break; + } + data<Ty,Tx> d(x,y,y_err,y_err,x_err,x_err); + ds.push_back(d); + } + //return ifs; + } + + void load_from(const char* name) + { + std::ifstream ifs(name); + load_from(ifs); + + } + //friend std::istream& operator>> <>(std::istream& ifs,dl_x_y_ye<Ty,Tx>& dl); + }; + + template <typename Ty,typename Tx> + std::istream& operator>>(std::istream& ifs,dl_x_y_ye<Ty,Tx>& dl) + { + dl.load_from(ifs); + return ifs; + } + + + template <typename Ty,typename Tx> + class dl_x_xe_y_ye + { + private: + default_data_set<Ty,Tx> ds; + public: + data_set<Ty,Tx>& get_data_set() + { + return ds; + } + + void load_from(std::istream& ifs) + { + for(;;) + { + Tx x; + Tx x_err; + Ty y; + Ty y_err(1); + + ifs>>x>>x_err>>y>>y_err; + + if(!ifs.good()) + { + break; + } + data<Ty,Tx> d(x,y,y_err,y_err,x_err,x_err); + ds.push_back(d); + } + } + + void load_from(const char* name) + { + std::ifstream ifs(name); + load_from(ifs); + } + }; + + template <typename Ty,typename Tx> + std::istream& operator>>(std::istream& ifs,dl_x_xe_y_ye<Ty,Tx>& dl) + { + dl.load_from(ifs); + return ifs; + } + + template <typename Ty,typename Tx> + class dl_x_xu_xl_y_yu_yl + { + private: + + default_data_set<Ty,Tx> ds; + public: + data_set<Ty,Tx>& get_data_set() + { + return ds; + } + + void load_from(std::istream& ifs) + { + for(;;) + { + Tx x; + Tx xl,xu; + Ty y; + Ty yl(1),yu(1); + + ifs>>x>>xu>>xl>>y>>yu>>yl; + + xu=std::abs(xu); + xl=std::abs(xl); + yu=std::abs(yu); + yl=std::abs(yl); + + if(!ifs.good()) + { + break; + } + data<Ty,Tx> d(x,y,yl,yu,xl,xu); + ds.push_back(d); + } + } + + void load_from(const char* name) + { + std::ifstream ifs(name); + load_from(ifs); + } + }; + + template <typename Ty,typename Tx> + std::istream& operator>> (std::istream& ifs,dl_x_xu_xl_y_yu_yl<Ty,Tx>& dl) + { + dl.load_from(ifs); + return ifs; + } + + + +} + + +#endif +//EOF |