npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
GridInterpolatedDistribution.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_GRIDINTERPOLATEDDISTRIBUTION_HH_
2 #define NPSTAT_GRIDINTERPOLATEDDISTRIBUTION_HH_
3 
4 /*!
5 // \file GridInterpolatedDistribution.hh
6 //
7 // \brief Multivariate distribution obtained by nonparametric interpolation of
8 // probability densities
9 //
10 // Author: I. Volobouev
11 //
12 // July 2010
13 */
14 
15 #include "npstat/nm/GridAxis.hh"
16 #include "npstat/nm/ArrayND.hh"
17 
20 
21 namespace npstat {
22  /**
23  // This class interpolates between probability distributions placed at
24  // the points of a rectangular (not necessarily equidistant) grid. The
25  // weights in the interpolations are determined in the multilinear
26  // fashion. Multivariate quantile or copula interpolation method can be
27  // used, depending on the types of interpolated densities.
28  */
30  {
31  public:
32  /**
33  // Constructor arguments "axes" and "distributionDimension" are,
34  // respectively, the collection of parameter grid axes and the
35  // dimensionality of the associated distributions.
36  //
37  // If "interpolateCopulas" is true, copula interpolation method
38  // will be used (as implemented by class CopulaInterpolationND),
39  // otherwise unit map interpolation will be used.
40  //
41  // If "useNearestGridCellOnly" is true, interpolation will be
42  // disabled and only the distribution corresponding to the nearest
43  // grid cell will be used.
44  */
45  GridInterpolatedDistribution(const std::vector<GridAxis>& axes,
46  unsigned distributionDimension,
47  bool interpolateCopulas,
48  bool useNearestGridCellOnly = false);
49 
51 
53 
56 
57  inline virtual GridInterpolatedDistribution* clone() const
58  {return new GridInterpolatedDistribution(*this);}
59 
60  /**
61  // Associate a distribution with the given parameter grid cell.
62  //
63  // This object will assume the ownership of the "distro" pointer
64  // if "assumeOwnership" is true and will make a copy of it if
65  // "assumeOwnership" is false. All distributions must be set before
66  // density can be calculated on all points inside and outside the grid.
67  */
68  void setGridDistro(const unsigned* cell, unsigned lenCell,
69  AbsDistributionND* distro, bool assumeOwnership=true);
70 
71  /**
72  // Associate a distribution with the given parameter grid cell
73  // using a linear index for the cell
74  */
75  void setLinearDistro(unsigned long idx, AbsDistributionND* distro,
76  bool assumeOwnership=true);
77 
78  /**
79  // Get the distribution associated with the given parameter grid cell.
80  // Note that null pointer will be returned in case the cell has not
81  // been set yet.
82  */
84  const unsigned* cell, unsigned lenCell) const;
85 
86  /**
87  // Get the distribution associated with the given parameter grid cell
88  // using a linear index for the cell. Note that null pointer will be
89  // returned in case the cell has not been set yet.
90  */
91  const AbsDistributionND* getLinearDistro(unsigned long idx) const;
92 
93  /**
94  // The following function must be called before calculating
95  // the density. The length of the coordinates array must be
96  // equal to the number of grid axes.
97  */
98  void setGridCoords(const double* coords, unsigned lenCoords);
99 
100  /** Switch between unit map interpolation and copula interpolation */
101  void interpolateCopulas(bool b);
102 
103  /** Switch between normal interpolation and using just one cell */
104  void useSingleCell(bool b);
105 
106  //@{
107  /** Inspect object properties */
108  inline unsigned nAxes() const {return nAxes_;}
109  inline const GridAxis& getAxis(const unsigned i) const
110  {return axes_.at(i);}
111  inline unsigned long nDistros() const {return interpolated_.length();}
112  inline ArrayShape gridShape() const {return interpolated_.shape();}
113  inline bool interpolatingCopulas() const {return interpCopulas_;}
114  inline bool usingSingleCell() const {return useNearestOnly_;}
115  //@}
116 
117  //@{
118  /** Method inherited from AbsDistributionND */
119  double density(const double* x, unsigned dim) const;
120  void unitMap(const double* rnd, unsigned len, double* x) const;
121  bool mappedByQuantiles() const;
122  //@}
123 
124  //@{
125  /**
126  * This method works for copula interpolation only. Naturally,
127  * all interpolated distributions in this case must be of type
128  * (or must inherit from) CompositeDistributionND.
129  */
130  double marginalDensity(unsigned idim, double x) const;
131  double marginalCdf(unsigned idim, double x) const;
132  double marginalExceedance(unsigned idim, double x) const;
133  double marginalQuantile(unsigned idim, double x) const;
134  double copulaDensity(const double* x, unsigned dim) const;
135  double productOfTheMarginals(const double* x, unsigned dim) const;
136  //@}
137 
138  //@{
139  // Method needed for "geners" I/O
140  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
141  virtual bool write(std::ostream& of) const;
142  //@}
143 
144  // Class name for the I/O
145  static inline const char* classname()
146  {return "npstat::GridInterpolatedDistribution";}
147 
148  // Version number for the I/O
149  static inline unsigned version() {return 3;}
150 
151  // Read method for the I/O
152  static GridInterpolatedDistribution* read(
153  const gs::ClassId& id, std::istream& in);
154 
155  protected:
156  virtual bool isEqual(const AbsDistributionND& r) const;
157 
158  private:
160 
161  void releaseDistros();
162  void cloneDistros();
163  void rebuildInterpolator();
164  void setGridCoordsSingle(const double* coords);
165  void setGridCoordsInterpolating(const double* coords);
166  void checkReady() const;
167 
168  // We will own the interpolated distributions
169  std::vector<GridAxis> axes_;
170  std::vector<unsigned> cellBuf_;
171  ArrayND<AbsDistributionND*> interpolated_;
172  AbsInterpolationAlgoND* interpolator_;
173  AbsDistributionND* single_;
174  unsigned nAxes_;
175  bool pointSet_;
176  bool interpCopulas_;
177  bool useNearestOnly_;
178 
179 #ifdef SWIG
180  public:
181  void setGridDistro_2(const unsigned* cell, unsigned lenCell,
182  AbsDistributionND* distro)
183  {
184  this->setGridDistro(cell, lenCell, distro, false);
185  }
186  void setLinearDistro_2(unsigned long idx, AbsDistributionND* distro)
187  {
188  this->setLinearDistro(idx, distro, false);
189  }
190 #endif // SWIG
191  };
192 }
193 
194 #endif // NPSTAT_GRIDINTERPOLATEDDISTRIBUTION_HH_
Interface definition for multivariate continuous statistical distributions.
Interface for multidimensional interpolation between densities.
Arbitrary-dimensional array template.
Non-uniformly spaced coordinate sets for use in constructing rectangular grids.
Definition: AbsDistributionND.hh:26
unsigned dim() const
Definition: AbsDistributionND.hh:51
Definition: GridAxis.hh:31
Definition: GridInterpolatedDistribution.hh:30
const AbsDistributionND * getGridDistro(const unsigned *cell, unsigned lenCell) const
GridInterpolatedDistribution(const std::vector< GridAxis > &axes, unsigned distributionDimension, bool interpolateCopulas, bool useNearestGridCellOnly=false)
virtual gs::ClassId classId() const
Definition: GridInterpolatedDistribution.hh:140
double marginalDensity(unsigned idim, double x) const
unsigned nAxes() const
Definition: GridInterpolatedDistribution.hh:108
double density(const double *x, unsigned dim) const
void unitMap(const double *rnd, unsigned len, double *x) const
void setGridDistro(const unsigned *cell, unsigned lenCell, AbsDistributionND *distro, bool assumeOwnership=true)
void setLinearDistro(unsigned long idx, AbsDistributionND *distro, bool assumeOwnership=true)
void setGridCoords(const double *coords, unsigned lenCoords)
virtual GridInterpolatedDistribution * clone() const
Definition: GridInterpolatedDistribution.hh:57
const AbsDistributionND * getLinearDistro(unsigned long idx) const
Definition: AbsArrayProjector.hh:14
std::vector< unsigned > ArrayShape
Definition: ArrayShape.hh:21