From 1f4a944064bc42284c33e6b755353d191cf288e8 Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Mon, 15 Dec 2008 07:26:12 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@1 ed2142bd-67ad-457f-ba7c-d818d4011675 --- statistics/chisq.hpp | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++ statistics/cstat.hpp | 66 +++++++++++++++++++++ 2 files changed, 229 insertions(+) create mode 100644 statistics/chisq.hpp create mode 100644 statistics/cstat.hpp (limited to 'statistics') diff --git a/statistics/chisq.hpp b/statistics/chisq.hpp new file mode 100644 index 0000000..15b3d33 --- /dev/null +++ b/statistics/chisq.hpp @@ -0,0 +1,163 @@ +#ifndef CHI_SQ_HPP +#define CHI_SQ_HPP +#include +#include +#include +#include +using std::cerr;using std::endl; + +namespace opt_utilities +{ + template + class chisq + :public statistic + { + private: + bool verb; + int n; + + + statistic* do_clone()const + { + // return const_cast*>(this); + return new chisq(*this); + } + public: + void verbose(bool v) + { + verb=v; + } + public: + chisq() + :verb(false) + {} + + + + Ts do_eval(const Tp& p) + { + Ts result(0); + for(int i=(this->datas()).size()-1;i>=0;--i) + { + Ty chi=(this->datas().get_data(i).get_y()-eval_model(this->datas().get_data(i).get_x(),p))/this->datas().get_data(i).get_y_upper_err(); + result+=chi*chi; + + } + if(verb) + { + n++; + if(n%10==0) + { + + cerr< + class chisq,double,std::string> + :public statistic ,double,std::string> + { + public: + typedef double Ty; + typedef double Tx; + typedef std::vector Tp; + typedef double Ts; + typedef std::string Tstr; + private: + bool verb; + int n; + + statistic* do_clone()const + { + // return const_cast*>(this); + return new chisq(*this); + } + + + public: + void verbose(bool v) + { + verb=v; + } + public: + chisq() + :verb(false) + {} + + + + Ty do_eval(const Tp& p) + { + Ty result(0); + for(int i=(this->datas()).size()-1;i>=0;--i) + { + +#ifdef HAVE_X_ERROR + Tx x1=this->datas().get_data(i).get_x()-this->datas().get_data(i).get_x_lower_err(); + Tx x2=this->datas().get_data(i).get_x()+this->datas().get_data(i).get_x_upper_err(); + Ty errx=(eval_model(x1,p)-eval_model(x2,p))/2; + //Ty errx=0; +#else + Ty errx=0; +#endif + + Ty y_model=eval_model(this->datas().get_data(i).get_x(),p); + Ty y_obs=this->datas().get_data(i).get_y(); + Ty y_err; + + if(y_model>y_obs) + { + y_err=this->datas().get_data(i).get_y_upper_err(); + } + else + { + y_err=this->datas().get_data(i).get_y_lower_err(); + } + + Ty chi=(y_obs-y_model)/std::sqrt(y_err*y_err+errx*errx); + + // Ty chi=(this->datas().get_data(i).get_y()-eval_model(this->datas().get_data(i).get_x(),p)); + // cerr<datas()[i].x,p)<datas()[i].y_upper_err<datas()[i].x<<"\t"<datas()[i].y<<"\t"<datas()[i].x,p)< +#include + +using std::cout;using std::endl; +namespace opt_utilities +{ + template + class cstat_poisson + :public statistic + { + private: + bool verb; + int n; + + Ty lnfrac(Ty y)const + { + return y*log(y)-y; + } + + public: + void verbose(bool v) + { + verb=v; + } + public: + + statistic* do_clone()const + { + // return const_cast*>(this); + return new cstat_poisson(*this); + } + + Ts do_eval(const Tp& p) + { + Ts result(0); + for(int i=(this->datas()).size()-1;i>=0;--i) + { + Ty model_y=eval_model(this->datas().get_data(i).get_x(),p); + result+=model_y-this->datas().get_data(i).get_y()*log(model_y)+lnfrac(this->datas().get_data(i).get_y()); + } + + if(verb) + { + n++; + if(n%10==0) + { + cout<p_fitter->get_dof()<<"\t"; + for(int i=0;i