npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
InterpolatedDistribution1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_INTERPOLATEDDISTRIBUTION1D_HH_
2 #define NPSTAT_INTERPOLATEDDISTRIBUTION1D_HH_
3 
4 /*!
5 // \file InterpolatedDistribution1D.hh
6 //
7 // \brief Interpolation of 1-d distributions via linear interpolation
8 // of quantile functions
9 //
10 // Author: I. Volobouev
11 //
12 // March 2010
13 */
14 
15 #include <vector>
16 
18 #include "npstat/stat/WeightedDistro1DPtr.hh"
19 
20 namespace npstat {
21  /**
22  // 1-d continuous statistical distribution which interpolates between
23  // other distributions by performing linear interpolation of the
24  // quantile function.
25  //
26  // Note that the interpolated distributions must still exist while this
27  // object is in use (this class neither owns nor copies them).
28  */
30  {
31  public:
32  /**
33  // It is expected that the object will be constructed
34  // incrementally: first, the constructor will be called
35  // using the expected number of distributions to use
36  // and then function "add" will be used to put the
37  // distributions and weights in. Use the default constructor
38  // if the number of distributions is not known in advance
39  // (the class will still operate as expected but the
40  // performance will take a hit).
41  */
42  explicit InterpolatedDistribution1D(unsigned expectedNDistros);
43 
45 
46  inline virtual ~InterpolatedDistribution1D() {}
47 
48  inline virtual InterpolatedDistribution1D* clone() const
49  {return new InterpolatedDistribution1D(*this);}
50 
51  /**
52  // Add a new distribution. Note that the distribution
53  // will not be copied internally. Make sure that all
54  // added distributions still exist when you call
55  // methods "density", etc.
56  */
57  void add(const AbsDistribution1D& d, double weight);
58 
59  /** Replace an existing distribution */
60  void replace(unsigned i, const AbsDistribution1D& d, double weight);
61 
62  /** Modify the weight for an existing dostribution */
63  void setWeight(unsigned i, double weight);
64 
65  /** Clear all distributions */
66  void clear();
67 
68  /**
69  // The following function should be called to disable
70  // (and later enable) automatic weight normalization
71  // if you want to use the "setWeight" or "replace" methods
72  // many times and, especially, if at some point in this process
73  // the sum of the weights becomes zero. The "density" method
74  // can not be called if normalization is not enabled.
75  */
76  void normalizeAutomatically(bool allow);
77 
78  /** The number of distributions participating in the interpolation */
79  inline unsigned size() const {return count_;}
80 
81  inline unsigned expectedSize() const {return nExpected_;}
82 
83  // Methods inherited from AbsDistribution1D
84  double density(double x) const;
85  double cdf(double x) const;
86  double exceedance(double x) const;
87  double quantile(double x) const;
88 
89  /**
90  // Return both density and cumulative distribution function.
91  // More efficient than obtaining these two results separately.
92  */
93  void densityAndCdf(double x, double* pdensity, double* pcdf) const;
94 
95  // Methods needed for I/O
96  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
97 
98  static inline const char* classname()
99  {return "npstat::InterpolatedDistribution1D";}
100  static inline unsigned version() {return 1;}
101 
102  protected:
103  virtual bool isEqual(const AbsDistribution1D&) const;
104 
105  private:
106  void normalize();
107 
108  std::vector<Private::WeightedDistro1DPtr> wdmem_;
109  unsigned nExpected_;
110  unsigned count_;
111  double wsum_;
112  double xmin_;
113  double xmax_;
114  bool autoNormalize_;
115  };
116 }
117 
118 #endif // NPSTAT_INTERPOLATEDDISTRIBUTION1D_HH_
Interface for interpolating 1-d distributions.
Definition: AbsInterpolatedDistribution1D.hh:18
Definition: InterpolatedDistribution1D.hh:30
virtual bool isEqual(const AbsDistribution1D &) const
virtual gs::ClassId classId() const
Definition: InterpolatedDistribution1D.hh:96
double cdf(double x) const
double density(double x) const
double exceedance(double x) const
unsigned size() const
Definition: InterpolatedDistribution1D.hh:79
void densityAndCdf(double x, double *pdensity, double *pcdf) const
void setWeight(unsigned i, double weight)
virtual InterpolatedDistribution1D * clone() const
Definition: InterpolatedDistribution1D.hh:48
void replace(unsigned i, const AbsDistribution1D &d, double weight)
double quantile(double x) const
void normalizeAutomatically(bool allow)
void add(const AbsDistribution1D &d, double weight)
unsigned expectedSize() const
Definition: InterpolatedDistribution1D.hh:81
InterpolatedDistribution1D(unsigned expectedNDistros)
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31