aboutsummaryrefslogtreecommitdiffstats
path: root/statistics/cstat.hpp
blob: 7193a937209c0b041e9131afc59acaf2d043441e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
   \file cstat.hpp
 */

#ifndef CSTAT_HPP
#define CSTAT_HPP
#define OPT_HEADER
#include <core/fitter.hpp>
#include <math/vector_operation.hpp>
#include <iostream>
#include <cassert>


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<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr>
  class cstat
    :public statistic<Ty,Tx,Tp,Ts,Tstr>
  {
  private:
    bool verb;
    int n;
  public:
    cstat()
      :verb(true)
    {}

    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<Ty,Tx,Tp,Ts,Tstr>(*this);
    }

    Ts do_eval(const Tp& p)
    {
      Ts result(0);
      for(int i=(this->get_data_set()).size()-1;i>=0;--i)
	{
	  Ty model_y=eval_model(this->get_data_set().get_data(i).get_x(),p);
	  result-=contract(this->get_data_set().get_data(i).get_y(),std::log(model_y),result);
	}

      if(verb)
	{
	  n++;
	  if(n%10==0)
	    {
	      cout<<"a:"<<result<<"\t";
	      for(size_t i=0;i<get_size(p);++i)
		{
		  cout<<get_element(p,i)<<",";
		}
	      cout<<endl;
	    }
	  
	}
      return result;
    }
  };
}

#endif
//EOF