RandomSamplerFromCDF.h
Go to the documentation of this file.
1 #ifndef _utl_RandomSamplerFromCDF_h_
2 #define _utl_RandomSamplerFromCDF_h_
3 
4 #include <vector>
5 #include <map>
6 #include <algorithm>
7 #include <string>
8 
9 #include <utl/VRandomSampler.h>
10 
11 
12 namespace utl {
13 
14  class RandomSamplerFromPDF;
15  class TabulatedFunction;
16 
17 
29 
30  public:
32  RandomSamplerFromCDF(const std::vector<double>& x, const std::vector<double>& y)
33  : fX(x), fY(y) { CheckAndNormalize(); }
34 
37 
39  RandomSamplerFromCDF(const std::vector<double>& cdf);
40 
41  RandomSamplerFromCDF(const std::map<double, double>& cdf);
42 
44  RandomSamplerFromCDF(const std::string& function,
45  const std::string& independentVariableName,
46  const double min, const double max, const int bins)
47  { SetFunction(function, independentVariableName, min, max, bins); }
48 
49  RandomSamplerFromCDF(const std::string& function,
50  const double min, const double max, const int bins)
51  { SetFunction(function, "x", min, max, bins); }
52 
53  virtual ~RandomSamplerFromCDF() { }
54 
55  double GetInverseCDF(const double y) const;
56 
57  double GetMinX() const { return fX.front(); }
58 
59  double GetMaxX() const { return fX.back(); }
60 
61  protected:
62  virtual double MapRandom(const double rand) const
63  { return GetInverseCDF(rand); }
64 
65  const std::vector<double>& GetCDFX() const { return fX; }
66 
67  const std::vector<double>& GetCDFY() const { return fY; }
68 
69  private:
70  void SetFunction(const std::string& function,
71  const std::string& independentVariableName,
72  const double min, const double max, const int bins);
73 
74  int GetIndex(const double y) const
75  { return std::distance(fY.begin(), std::lower_bound(fY.begin(), fY.end(), y)); }
76 
77  void CheckAndNormalize();
78 
79  void Dump() const;
80 
81  std::vector<double> fX;
82  std::vector<double> fY;
83 
84  friend class RandomSamplerFromPDF;
85  };
86 
87 }
88 
89 
90 #endif
const std::vector< double > & GetCDFY() const
Class to hold collection (x,y) points and provide interpolation between them.
int GetIndex(const double y) const
#define max(a, b)
std::vector< double > fY
RandomSamplerFromCDF(const std::string &function, const std::string &independentVariableName, const double min, const double max, const int bins)
Construct the CDF from a function defined by a string.
double GetInverseCDF(const double y) const
std::vector< double > fX
const std::vector< double > & GetCDFX() const
RandomSamplerFromCDF(const std::string &function, const double min, const double max, const int bins)
void SetFunction(const std::string &function, const std::string &independentVariableName, const double min, const double max, const int bins)
RandomSamplerFromCDF(const std::vector< double > &x, const std::vector< double > &y)
Construct using a CDF defined by X and Y vectors.
virtual double MapRandom(const double rand) const
Class to shoot random numbers given by a user-defined distribution function.

, generated on Tue Sep 26 2023.