14 std::vector<double> &in_cdf,
27 size_t intvl_size = ceil(cdf_size/(
double)subdiv_factor);
29 if (intvl_size < final_res)
41 for (
int i=0; i<subdiv_factor; i++)
43 int beg = ibin + i*intvl_size;
44 int end = ibin + (i+1)*intvl_size-1;
46 if (i == (subdiv_factor-1))
75 if (subdiv_factor >= 1)
79 if (in_cdf.size() <= 10)
81 else if (in_cdf.size() <= 10000)
100 size_t cdf_size = in_cdf.size();
106 this->subdiv_factor_,
114 int beg = i*intvl_size;
115 int end = (i+1)*intvl_size-1;
117 if (i == (this->subdiv_factor_ - 1))
126 this->subdiv_factor_,
138 int cdf_size = ref_cdf_.size();
141 if (x <= ref_cdf_[0])
143 else if (x >= ref_cdf_[cdf_size - 1])
144 ret_val = cdf_size-1;
148 std::pair<int,int> range(0,cdf_size-1);
151 int num_sub_intvls = sub_intvls_.size();
152 for (
int s=0; s<num_sub_intvls; s++)
154 if (sub_intvls_[s]->Sample(x, range))
162 for (
int k=range.first; k<=range.second; k++)
172 else if ((x >= ref_cdf_[k - 1]) and (x < ref_cdf_[k]))
184 <<
"chi_math::CDFSampler::Sample. Error in CDF sampling routine. "
185 <<
"A bin was not found.";
195 std::pair<int, int> &range)
202 if (x < ref_cdf[cbin_i])
204 range.first = cbin_i;
205 range.second = cbin_f;
211 if ((x >= ref_cdf[cbin_i-1]) and (x < ref_cdf[cbin_f]))
213 range.first = cbin_i;
214 range.second = cbin_f;
224 int num_sub_intvls = sub_intvls.size();
225 for (
int s=0; s<num_sub_intvls; s++)
227 if (sub_intvls[s]->Sample(x,range))
274 size_t fine_limit = 5;
275 size_t cdf_size = cdf_bin.size();
278 size_t lookup_f = cdf_size-1;
282 size_t indB = std::ceil(cdf_size/2.0)-1;
283 size_t indC = cdf_size-1;
285 bool refine_limit_reached =
false;
287 if ((indB-indA) <= fine_limit)
288 refine_limit_reached =
true;
294 int refine_count = 0;
295 while (!refine_limit_reached)
298 if (x <= cdf_bin[indA])
299 refine_limit_reached =
true;
300 else if (x > cdf_bin[indC])
301 refine_limit_reached =
true;
302 else if ((x >= cdf_bin[indA]) and (x < cdf_bin[indB]))
304 intvl_size = indB-indA+1;
307 indB = indA + std::ceil(intvl_size/2.0)-1;
311 intvl_size = indC-indB+1;
314 indB = indA + std::ceil(intvl_size/2.0)-1;
324 if (intvl_size <= fine_limit)
326 refine_limit_reached =
true;
341 else if (x >= cdf_bin[cdf_size-1])
342 ret_val = cdf_size-1;
345 for (
int k=lookup_i; k<=lookup_f; k++)
355 else if ((x >= cdf_bin[k-1]) and (x < cdf_bin[k]))
368 <<
"chi_math::SampleCDF. Error in CDF sampling routine. "
369 <<
"A bin was not found."
static void Exit(int error_code)
CDFSampler(std::vector< double > &in_cdf, int subdiv_factor=AUTO_SUBDIV, int final_res=AUTO_FINERES)
std::vector< SubIntvl * > sub_intvls_
std::vector< double > & ref_cdf_
int SampleCDF(double x, std::vector< double > cdf_bin)
bool Sample(double x, std::pair< int, int > &range)
std::vector< double > & ref_cdf
std::vector< SubIntvl * > sub_intvls
SubIntvl(std::string offset, int ibin, int fbin, std::vector< double > &in_cdf, int subdiv_factor=10, int final_res=10, bool inhibit=false)