From 20c287b91345e348cc00b8a0da53ff967be0cac9 Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Sat, 14 May 2011 16:24:05 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@197 ed2142bd-67ad-457f-ba7c-d818d4011675 --- statistics/kmm_stat.hpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 statistics/kmm_stat.hpp (limited to 'statistics') diff --git a/statistics/kmm_stat.hpp b/statistics/kmm_stat.hpp new file mode 100644 index 0000000..0a205bd --- /dev/null +++ b/statistics/kmm_stat.hpp @@ -0,0 +1,101 @@ +/** + \file kmm_stat.hpp + \brief maximum-liklihood statistic + \author Junhua Gu + */ + +#ifndef KMM_STAT_HPP +#define KMM_STAT_HPP +#define OPT_HEADER +#include +#include +#include +#include +#include + + +using std::cout;using std::endl; +namespace opt_utilities +{ + + /** + \brief c-statistic, max-likelihood method + \tparam Ty the return type of model + \tparam Tx the type of the self-var + \tparam Tp the type of model parameter + \tparam Ts the type of the statistic + \tparam Tstr the type of the string used + */ + template + class kmm_stat + :public statistic,optvec,optvec,Ts,Tstr> + { + private: + bool verb; + int n; + public: + kmm_stat() + :verb(true) + {} + + void verbose(bool v) + { + verb=v; + } + + const char* do_get_type_name()const + { + return "kmm likelihood"; + } + + public: + + statistic,optvec,optvec,Ts,Tstr>* do_clone()const + { + // return const_cast*>(this); + return new kmm_stat(*this); + } + + Ts do_eval(const optvec& p) + { + Ts result(0); + + kmm_component* kmm= + dynamic_cast* >(&const_cast,optvec,optvec,std::string>&>(this->get_fitter().get_model())); + + if(kmm==0) + { + throw opt_exception("model is not a kmm component"); + } + + for(int i=(this->get_data_set()).size()-1;i>=0;--i) + { + optvec unsummed_possibility(kmm->eval_unsumed(this->get_data_set().get_data(i).get_x(),p)); + T maxp=*max_element(unsummed_possibility.begin(),unsummed_possibility.end()); + T logp=std::log(maxp); + result-=this->get_data_set().get_data(i).get_y()[0]*logp; + } + + if(verb) + { + n++; + if(n%10==0) + { + cout<<"a:"<