aboutsummaryrefslogtreecommitdiffstats
path: root/misc/bootstrap.hpp
blob: bd1f4a3b4a101cb252e2dbb0a6e3599c896d1340 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
   \file bootstrap.hpp
*/


#ifndef BOOT_STRIP
#define BOOT_STRIP
#define OPT_HEADER
#include <core/fitter.hpp>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <algorithm>
#include <data_sets/default_data_set.hpp>
using std::cout;
namespace opt_utilities
{

  /**
     \brief using bootstrap method to estimate confidence interval
     \tparam Ty the return type of a model
     \tparam Tx the type of self-var
     \tparam Tp the type of model parameters
     \tparam Ts the type of statistic
     \tparam Tstr the type of string used
   */
  template <typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr=std::string>
  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,Ts,Tstr>* p_fitter;
    Tp origin_param;
  public:

    /**
       default construct
     */
    bootstrap()
      :p_fitter(NULL)
    {}

    /**
       destructure
     */
    ~bootstrap()
    {
      reset();
    }


    /**
       set a fitter
       \param pf the fitter, the confidence interval of which will be estimated
     */
    void set_fitter(fitter<Ty,Tx,Tp,Ts,Tstr>& pf)
    {
      param_pool.clear();
      p_fitter=&pf;
      origin_data_set=dynamic_cast<const default_data_set<Ty,Tx>&>(pf.get_data_set());
      origin_param=pf.get_all_params();
    }
    
    /**
       reset the estimation
       reload the data
       restore the parameters
     */
    void reset()
    {
      if(p_fitter!=NULL)
	{
	  p_fitter->load_data(origin_data_set);
	  p_fitter->set_param_value(origin_param);
	}
    }


    /**
       resample n times
       \param n the times to sample
     */
    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");
	}
    }

    /**
       get the param of i-th sampling
       \param i the order of parameter
       \return the parameter
     */
    const Tp& get_param(int i)const
    {
      return param_pool.at(i);
    }
    
  private:
    Tp sample()
    {
      if(p_fitter==NULL)
	{
	  throw fitter_unset();
	}
      current_data_set=default_data_set<Ty,Tx>();
      for(size_t 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.add_data(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;
      return param_pool.back();
    }
    
  public:

    /**
       Calculate the confdence interval for one parameter under given level
       \param param_name the name of the parameter to be estimated
       \param level the confidence level
       \return a std::pair containing the lower and upper boundaries of the confidence interval
     */
    std::pair<typename element_type_trait<Tp>::element_type,typename element_type_trait<Tp>::element_type>
    interval(const Tstr& 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