aboutsummaryrefslogtreecommitdiffstats
path: root/statistics
diff options
context:
space:
mode:
Diffstat (limited to 'statistics')
-rw-r--r--statistics/chisq.hpp163
-rw-r--r--statistics/cstat.hpp66
2 files changed, 229 insertions, 0 deletions
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 <core/fitter.hpp>
+#include <iostream>
+#include <vector>
+#include <cmath>
+using std::cerr;using std::endl;
+
+namespace opt_utilities
+{
+ template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr>
+ class chisq
+ :public statistic<Ty,Tx,Tp,Ts,Tstr>
+ {
+ private:
+ bool verb;
+ int n;
+
+
+ statistic<Ty,Tx,Tp,Ts,Tstr>* do_clone()const
+ {
+ // return const_cast<statistic<Ty,Tx,Tp>*>(this);
+ return new chisq<Ty,Tx,Tp,Ts,Tstr>(*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<<result<<"\t";
+ for(size_t i=0;i<get_size(p);++i)
+ {
+ cerr<<get_element(p,i)<<",";
+ }
+ cerr<<endl;
+ }
+
+ }
+
+ return result;
+ }
+ };
+
+#if 1
+
+ template<>
+ class chisq<double,double,std::vector<double>,double,std::string>
+ :public statistic<double,double,std::vector<double> ,double,std::string>
+ {
+ public:
+ typedef double Ty;
+ typedef double Tx;
+ typedef std::vector<double> Tp;
+ typedef double Ts;
+ typedef std::string Tstr;
+ private:
+ bool verb;
+ int n;
+
+ statistic<Ty,Tx,Tp,Ts,Tstr>* do_clone()const
+ {
+ // return const_cast<statistic<Ty,Tx,Tp>*>(this);
+ return new chisq<Ty,Tx,Tp,Ts,Tstr>(*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<<chi<<"\n";
+ result+=chi*chi;
+ //std::cerr<<chi<<std::endl;
+ //cerr<<eval_model(this->datas()[i].x,p)<<endl;
+ //cerr<<this->datas()[i].y_upper_err<<endl;
+ // cerr<<this->datas()[i].x<<"\t"<<this->datas()[i].y<<"\t"<<eval_model(this->datas()[i].x,p)<<endl;
+ }
+ if(verb)
+ {
+ n++;
+ if(n%10==0)
+ {
+ cerr<<result<<"\t";
+ for(int i=0;i<(int)get_size(p);++i)
+ {
+ cerr<<get_element(p,i)<<",";
+ }
+ cerr<<endl;
+ }
+
+ }
+
+ return result;
+ }
+ };
+#endif
+}
+
+#endif
+//EOF
+
+
diff --git a/statistics/cstat.hpp b/statistics/cstat.hpp
new file mode 100644
index 0000000..b6b401d
--- /dev/null
+++ b/statistics/cstat.hpp
@@ -0,0 +1,66 @@
+#ifndef CSTAT_HPP
+#define CSTAT_HPP
+#include <core/fitter.hpp>
+#include <iostream>
+
+using std::cout;using std::endl;
+namespace opt_utilities
+{
+ template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr>
+ class cstat_poisson
+ :public statistic<Ty,Tx,Tp,Ts,Tstr>
+ {
+ private:
+ bool verb;
+ int n;
+
+ Ty lnfrac(Ty y)const
+ {
+ return y*log(y)-y;
+ }
+
+ public:
+ void verbose(bool v)
+ {
+ verb=v;
+ }
+ public:
+
+ statistic<Ty,Tx,Tp,Ts,Tstr>* do_clone()const
+ {
+ // return const_cast<statistic<Ty,Tx,Tp>*>(this);
+ return new cstat_poisson<Ty,Tx,Tp,Ts,Tstr>(*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<<result/this->p_fitter->get_dof()<<"\t";
+ for(int i=0;i<get_size(p);++i)
+ {
+ cout<<get_element(p,i)<<",";
+ }
+ cout<<endl;
+ }
+
+ }
+ return result;
+ }
+ };
+}
+
+#endif
+//EOF
+
+