Chi-Tech
cdfsampler.h
Go to the documentation of this file.
1#ifndef _chi_math_cdfsampler_h
2#define _chi_math_cdfsampler_h
3
4
5
6//###################################################################
7/**Object for implementing an efficient cdf sampler.
8 *
9 * Normal linear sampling of bins of a Cumulative Distribution Function
10 * is O(N) for each sample which can lead to very expensive sampling
11 * of distributions with many samples. To this end this sampler
12 * is designed to subdivide the bins recursively until a suitable
13 * linear search can be performed.
14 *
15 * The speed-up for cdfs of >1000 bins is more than a factor 100.
16 *
17 * In order to use this sampler for repeated sampling calls make
18 * sure to initialize it outside the phases that will repeatedly sample
19 * it because it has some over-head to it that gets executed in the constructor.
20 *
21 \code
22 chi_math::CDFSampler sampler(in_cdf);
23 \endcode
24 *
25 * */
27{
28public:
29 struct SubIntvl;
30 static const int AUTO_SUBDIV = -1;
31 static const int AUTO_FINERES = -2;
32
33private:
36 std::vector<double>& ref_cdf_;
37 std::vector<SubIntvl*> sub_intvls_;
38
39public:
40 CDFSampler(std::vector<double>& in_cdf,
41 int subdiv_factor=AUTO_SUBDIV,
42 int final_res=AUTO_FINERES);
43
44 int Sample(double x);
45};
46
47//###################################################################
48/**Sub-structure for sub-intervals*/
50{
51 int cbin_i;
52 int cbin_f;
53 std::vector<double>& ref_cdf;
55
56 std::vector<SubIntvl*> sub_intvls;
57
58 std::string offset;
59
60 SubIntvl(std::string offset, int ibin, int fbin,
61 std::vector<double>& in_cdf,
62 int subdiv_factor=10,
63 int final_res=10,
64 bool inhibit=false);
65
66 bool Sample(double x, std::pair<int,int>& range);
67};
68
69
70#endif
CDFSampler(std::vector< double > &in_cdf, int subdiv_factor=AUTO_SUBDIV, int final_res=AUTO_FINERES)
Definition: cdfsampler.cc:69
std::vector< SubIntvl * > sub_intvls_
Definition: cdfsampler.h:37
static const int AUTO_SUBDIV
Definition: cdfsampler.h:30
std::vector< double > & ref_cdf_
Definition: cdfsampler.h:36
int Sample(double x)
Definition: cdfsampler.cc:135
static const int AUTO_FINERES
Definition: cdfsampler.h:31
bool Sample(double x, std::pair< int, int > &range)
Definition: cdfsampler.cc:194
std::vector< double > & ref_cdf
Definition: cdfsampler.h:53
std::vector< SubIntvl * > sub_intvls
Definition: cdfsampler.h:56
SubIntvl(std::string offset, int ibin, int fbin, std::vector< double > &in_cdf, int subdiv_factor=10, int final_res=10, bool inhibit=false)
Definition: cdfsampler.cc:12