npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
InterpolatedDistro1DNP.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_INTERPOLATEDDISTRO1DNP_HH_
2 #define NPSTAT_INTERPOLATEDDISTRO1DNP_HH_
3 
4 /*!
5 // \file InterpolatedDistro1DNP.hh
6 //
7 // \brief Interpolation of 1-d distributions as a function of multiple
8 // parameters
9 //
10 // Author: I. Volobouev
11 //
12 // July 2015
13 */
14 
15 #include <stdexcept>
16 
17 #include "npstat/nm/GridAxis.hh"
18 #include "npstat/nm/ArrayND.hh"
19 
21 
22 namespace npstat {
24  {
25  public:
26  explicit InterpolatedDistro1DNP(const std::vector<GridAxis>& axes,
27  bool interpolateVertically = false);
28 
29  virtual ~InterpolatedDistro1DNP();
30 
33 
34  /** "Virtual copy constructor" */
35  inline virtual InterpolatedDistro1DNP* clone() const
36  {return new InterpolatedDistro1DNP(*this);}
37 
38  void interpolateVertically(bool b);
39  inline bool interpolatingVertically() const {return vertical_;}
40 
41  /**
42  // Set the distribution corresponding to the given parameter grid
43  // point. This class will assume the ownership of the "distro" pointer
44  // if "assumeDistroOwnership" parameter is "true" and will copy the
45  // distribution if it is "false".
46  */
47  void setGridDistro(const unsigned* cell, unsigned lenCell,
48  AbsDistribution1D* distro,
49  bool assumeDistroOwnership=true);
50 
51  /** Same as "setGridDistro" but using a linear index for the cell */
52  void setLinearDistro(unsigned long idx, AbsDistribution1D* distro,
53  bool assumeDistroOwnership=true);
54 
55  //@{
56  /**
57  // Lookup the distribution at the given grid cell. This method
58  // returns null pointer in case the distribution has not been
59  // set for that cell.
60  */
62  const unsigned* cell, unsigned lenCell) const;
63  const AbsDistribution1D* getLinearDistro(unsigned long idx) const;
64  //@}
65 
66  /**
67  // This method must be called before calculating the density
68  // (it will perform various preparations related to distribution
69  // interpolation). The length of the "coords" array must be
70  // equal to the number of grid axes.
71  */
72  void setGridCoords(const double* coords, unsigned lenCoords);
73 
74  /** Number of gris axes */
75  inline unsigned nAxes() const {return nAxes_;}
76 
77  /** Number of gris points (and associated distributions) */
78  inline unsigned long nDistros() const {return interpolated_.length();}
79 
80  /** The shape of the rectangular grid of parameters */
81  inline ArrayShape gridShape() const {return interpolated_.shape();}
82 
83  /** The grid axis for the given coordinate */
84  inline const GridAxis& getAxis(const unsigned i) const
85  {return axes_.at(i);}
86 
87  /** Probability density */
88  inline double density(const double x) const
89  {checkReady(); return interpolator_->density(x);}
90 
91  /** Cumulative distribution function */
92  inline double cdf(const double x) const
93  {checkReady(); return interpolator_->cdf(x);}
94 
95  /** 1 - cdf, avoiding subtractive cancellation */
96  inline double exceedance(const double x) const
97  {checkReady(); return interpolator_->exceedance(x);}
98 
99  /** The quantile function */
100  inline double quantile(const double x) const
101  {checkReady(); return interpolator_->quantile(x);}
102 
103  //@{
104  /** Prototype needed for I/O */
105  virtual inline gs::ClassId classId() const {return gs::ClassId(*this);}
106  virtual bool write(std::ostream&) const;
107  //@}
108 
109  static inline const char* classname()
110  {return "npstat::InterpolatedDistro1DNP";}
111  static inline unsigned version() {return 1;}
112  static InterpolatedDistro1DNP* read(const gs::ClassId& id,
113  std::istream& in);
114  protected:
115  virtual bool isEqual(const AbsDistribution1D&) const;
116 
117  private:
119 
120  void releaseDistros();
121  void cloneDistros();
122  void checkReady() const;
123 
124  std::vector<GridAxis> axes_;
125  std::vector<unsigned> cellBuf_;
126  ArrayND<AbsDistribution1D*> interpolated_;
127  AbsInterpolatedDistribution1D* interpolator_;
128  unsigned nAxes_;
129  bool vertical_;
130  bool pointSet_;
131 
132 #ifdef SWIG
133  public:
134  inline void setGridDistro_2(const unsigned* cell, unsigned lenCell,
135  AbsDistribution1D* distro)
136  {
137  this->setGridDistro(cell, lenCell, distro, false);
138  }
139 
140  inline void setLinearDistro_2(unsigned long idx, AbsDistribution1D* d)
141  {
142  this->setLinearDistro(idx, d, false);
143  }
144 #endif // SWIG
145  };
146 }
147 
148 #endif // NPSTAT_INTERPOLATEDDISTRO1DNP_HH_
Interface for interpolating 1-d distributions.
Arbitrary-dimensional array template.
Non-uniformly spaced coordinate sets for use in constructing rectangular grids.
Definition: AbsInterpolatedDistribution1D.hh:18
Definition: ArrayND.hh:93
Definition: GridAxis.hh:31
Definition: InterpolatedDistro1DNP.hh:24
void setGridDistro(const unsigned *cell, unsigned lenCell, AbsDistribution1D *distro, bool assumeDistroOwnership=true)
virtual bool isEqual(const AbsDistribution1D &) const
double cdf(const double x) const
Definition: InterpolatedDistro1DNP.hh:92
unsigned long nDistros() const
Definition: InterpolatedDistro1DNP.hh:78
unsigned nAxes() const
Definition: InterpolatedDistro1DNP.hh:75
double exceedance(const double x) const
Definition: InterpolatedDistro1DNP.hh:96
ArrayShape gridShape() const
Definition: InterpolatedDistro1DNP.hh:81
double quantile(const double x) const
Definition: InterpolatedDistro1DNP.hh:100
virtual gs::ClassId classId() const
Definition: InterpolatedDistro1DNP.hh:105
void setGridCoords(const double *coords, unsigned lenCoords)
virtual InterpolatedDistro1DNP * clone() const
Definition: InterpolatedDistro1DNP.hh:35
const AbsDistribution1D * getGridDistro(const unsigned *cell, unsigned lenCell) const
double density(const double x) const
Definition: InterpolatedDistro1DNP.hh:88
void setLinearDistro(unsigned long idx, AbsDistribution1D *distro, bool assumeDistroOwnership=true)
const GridAxis & getAxis(const unsigned i) const
Definition: InterpolatedDistro1DNP.hh:84
Definition: AbsArrayProjector.hh:14
std::vector< unsigned > ArrayShape
Definition: ArrayShape.hh:21
Definition: AbsDistribution1D.hh:31
virtual double density(double x) const =0
virtual double exceedance(double x) const =0
virtual double quantile(double x) const =0
virtual double cdf(double x) const =0