CARMA C++
Sampler.h
Go to the documentation of this file.
1 // $Id: Sampler.h,v 1.1 2010/12/13 21:06:32 eml Exp $
2 
3 #ifndef SZA_UTIL_SAMPLER_H
4 #define SZA_UTIL_SAMPLER_H
5 
16 #define SAMPLER_FN(fn) double (fn)(double x)
17 
18 #include <vector>
19 
20 namespace sza {
21  namespace util {
22 
23  class Sampler {
24  public:
25 
29  Sampler();
30 
34  virtual ~Sampler();
35 
36  //------------------------------------------------------------
37  // Methods for specifying the sampling function
38  //------------------------------------------------------------
39 
40  // Methods for specifying the sampling function, as two arrays
41  // specifying dy/dx
42 
43  void setdYdX(unsigned n, double* x, double* y);
44  void setdYdX(std::vector<double>& x, std::vector<double>& y);
45 
46  // Method for specifying the sampling dy/dx as a function
47 
48  void setdYdX(SAMPLER_FN(fn), double xMin, double xMax, double dx);
49 
50  // Method for specifying the integral of dy/dx (Y(x' > x))
51  // directly
52 
53  void setYX(SAMPLER_FN(fn));
54 
55  //------------------------------------------------------------
56  // Generate samples according to the specified sampling function
57  //------------------------------------------------------------
58 
59  std::vector<double> generateSamples(unsigned nSamp);
60 
61  //------------------------------------------------------------
62  // Generate poisson samples
63  //------------------------------------------------------------
64 
65  static std::vector<unsigned>
66  generatePoissonSamples(double mean, unsigned nSamp);
67 
68  //------------------------------------------------------------
69  // Generate Gaussian samples
70  //------------------------------------------------------------
71 
72  static std::vector<double>
73  generateGaussianSamples(double sigma, unsigned nSamp);
74 
75  //------------------------------------------------------------
76  // Seed the random number generator
77  //------------------------------------------------------------
78 
79  static void seed(unsigned int s);
80  static void seedRandom();
81 
82  //------------------------------------------------------------
83  // Utility functions
84  //------------------------------------------------------------
85 
86  // Return the natural log of the gamma function (series
87  // approximation)
88 
89  static double lnGamma(double x);
90 
91  // Return the natural log of the factorial of n. Exact for
92  // small n, usese series approximation to the Gamma functions
93  // for large n
94 
95  static double lnFactrl(unsigned n);
96 
97  // Return the value of the Poisson pdf, for k events, given a
98  // mean of lambda
99 
100  static double poissPdf(unsigned k, double lambda);
101 
102  private:
103 
104  // Binary search for samples
105 
106  double binSearchForSample();
107 
108  // True if we have a function to integrate
109 
110  bool haveFn_;
111 
112  // True if the integrated function is specified numerically, or
113  // as a functional form
114 
115  bool isFn_;
116 
117  // The number of points in our integrated function
118 
119  unsigned nPt_;
120 
121  // Integral version of the above
122 
123  std::vector<double> yInt_;
124  std::vector<double> xInt_;
125 
126  }; // End class Sampler
127 
128  } // End namespace util
129 } // End namespace sza
130 
131 
132 
133 #endif // End #ifndef SZA_UTIL_SAMPLER_H