npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
DiscreteDistributions1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_DISCRETEDISTRIBUTIONS1D_HH_
2 #define NPSTAT_DISCRETEDISTRIBUTIONS1D_HH_
3 
4 /*!
5 // \file DiscreteDistributions1D.hh
6 //
7 // \brief A number of useful 1-d discrete statistical distributions
8 //
9 // Author: I. Volobouev
10 //
11 // May 2013
12 */
13 
14 #include <vector>
15 
17 
18 namespace npstat {
19  /** Discrete distribution defined by a table of probabilities */
21  {
22  public:
23  //
24  // Before applying the shift, probs[0] is the probability at 0,
25  // probs[1] is the probability at 1, etc.
26  //
27  template <typename Real>
28  inline DiscreteTabulated1D(const long shift, const Real* probs,
29  const unsigned probLen)
31  table_(probs, probs+probLen) {initialize();}
32 
33  inline DiscreteTabulated1D(const long shift,
34  const std::vector<double>& probs)
36  table_(probs) {initialize();}
37 
38  inline virtual ~DiscreteTabulated1D() {}
39 
40  inline virtual DiscreteTabulated1D* clone() const
41  {return new DiscreteTabulated1D(*this);}
42 
43  inline const std::vector<double>& probabilities() const {return table_;}
44 
45  // Methods needed for I/O
46  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
47  virtual bool write(std::ostream& os) const;
48 
49  static inline const char* classname()
50  {return "npstat::DiscreteTabulated1D";}
51  static inline unsigned version() {return 1;}
52  static DiscreteTabulated1D* read(const gs::ClassId& id, std::istream&);
53 
54  protected:
55  virtual bool isEqual(const AbsDiscreteDistribution1D&) const;
56 
57  private:
59 
60  void initialize();
61 
62  double unshiftedProbability(long x) const;
63  double unshiftedCdf(double x) const;
64  double unshiftedExceedance(double x) const;
65  long unshiftedQuantile(double x) const;
66 
67  std::vector<double> table_;
68  std::vector<double> cdf_;
69  std::vector<double> exceedance_;
70  unsigned firstNonZero_;
71  unsigned lastNonZero_;
72  };
73 
74  /**
75  // Function which pools together two discrete tabulated distributions.
76  // Arguments "sampleSize1" and "sampleSize2" define the proportions with
77  // which the input distributions are combined. Arguments "first" and
78  // "oneAfterLast" define the support of the combined distribution. "First"
79  // is the first argument for which the combined density can be be positive
80  // (whether it will actually be positive also depends on the supports of
81  // the combined distribitions). "OneAfterLast" will be larger by one than
82  // the last value of the support.
83  */
85  double sampleSize1,
86  const DiscreteTabulated1D& d2,
87  double sampleSize2,
88  long first, long oneAfterLast);
89 
90  /**
91  // The Poisson distribution. The calculations of cdf and quantiles,
92  // together with generation of random numbers, are implemented by
93  // constructing the cdf lookup table. Once such a table is constructed,
94  // calculations of this kind become pretty fast. Nevertheless, since
95  // construction of the complete cdf table takes some time (especially
96  // for large Poisson lambdas), one can imagine situations in which
97  // it may be more efficient to set up a Poisson process instead.
98  // For example, if you need to generate a few random numbers for many
99  // different values of lambda, you might be able to save some CPU time
100  // by following the Poisson process route.
101  */
103  {
104  public:
105  explicit Poisson1D(double lambda);
106 
107  Poisson1D(const Poisson1D&);
108  Poisson1D& operator=(const Poisson1D&);
109 
110  inline virtual Poisson1D* clone() const {return new Poisson1D(*this);}
111 
112  inline virtual ~Poisson1D() {delete table_;}
113 
114  inline double mean() const {return lambda_;}
115 
116  double probability(long x) const;
117  double cdf(double x) const;
118  double exceedance(double x) const;
119  long quantile(double x) const;
120  unsigned random(AbsRandomGenerator& g, long* generatedRandom) const;
121 
122  // Methods needed for I/O
123  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
124  virtual bool write(std::ostream& os) const;
125 
126  static inline const char* classname() {return "npstat::Poisson1D";}
127  static inline unsigned version() {return 1;}
128  static Poisson1D* read(const gs::ClassId& id, std::istream& in);
129 
130  protected:
131  inline virtual bool isEqual(const AbsDiscreteDistribution1D& o) const
132  {
133  const Poisson1D& r = static_cast<const Poisson1D&>(o);
134  return lambda_ == r.lambda_;
135  }
136 
137  private:
138  Poisson1D();
139 
140  void buildTable();
141 
142  double lambda_;
143  DiscreteTabulated1D* table_;
144  long minUse_;
145  long double lnlambda_;
146  };
147 }
148 
149 #endif // NPSTAT_DISCRETEDISTRIBUTIONS1D_HH_
Interface definition for 1-d discrete statistical distributions.
Definition: DiscreteDistributions1D.hh:21
virtual bool isEqual(const AbsDiscreteDistribution1D &) const
virtual DiscreteTabulated1D * clone() const
Definition: DiscreteDistributions1D.hh:40
virtual gs::ClassId classId() const
Definition: DiscreteDistributions1D.hh:46
Definition: DiscreteDistributions1D.hh:103
virtual bool isEqual(const AbsDiscreteDistribution1D &o) const
Definition: DiscreteDistributions1D.hh:131
virtual Poisson1D * clone() const
Definition: DiscreteDistributions1D.hh:110
virtual gs::ClassId classId() const
Definition: DiscreteDistributions1D.hh:123
double cdf(double x) const
unsigned random(AbsRandomGenerator &g, long *generatedRandom) const
long quantile(double x) const
double exceedance(double x) const
double probability(long x) const
Definition: AbsDiscreteDistribution1D.hh:94
Definition: AbsArrayProjector.hh:14
DiscreteTabulated1D pooledDiscreteTabulated1D(const DiscreteTabulated1D &d1, double sampleSize1, const DiscreteTabulated1D &d2, double sampleSize2, long first, long oneAfterLast)
Definition: AbsDiscreteDistribution1D.hh:19
Definition: AbsRandomGenerator.hh:27