npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
DensityScan1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_DENSITYSCAN1D_HH_
2 #define NPSTAT_DENSITYSCAN1D_HH_
3 
4 /*!
5 // \file DensityScan1D.hh
6 //
7 // \brief Utilities for discretizing 1-d densities
8 //
9 // Author: I. Volobouev
10 //
11 // March 2014
12 */
13 
15 
16 namespace npstat {
17  /** A functor for filling one-dimensional ArrayND with density values */
19  {
20  public:
21  /**
22  // Parameter "nIntegrationPoints" determines how many points per bin
23  // will be used to calculate the bin average. This number must be
24  // either 0 (use cdf difference at the bin edges), 1 (use density
25  // value at the center of the bin), or one of the numbers of points
26  // supported by the "GaussLegendreQuadrature" class.
27  */
28  DensityScan1D(const AbsDistribution1D& fcn, double normfactor,
29  unsigned long nbins, double xmin, double xmax,
30  unsigned nIntegrationPoints=1);
31 
32  double operator()(const unsigned* index, unsigned len) const;
33 
34  inline double averageDensity(const unsigned binNumber) const
35  {return (*this)(&binNumber, 1U);}
36 
37  private:
38  DensityScan1D();
39 
40  const AbsDistribution1D& fcn_;
41  double norm_;
42  double xmin_;
43  double bw_;
44  std::vector<long double> a_;
45  std::vector<long double> weights_;
46  unsigned nPoints_;
47  };
48 
49  /**
50  // A functor for filling one-dimensional ArrayND with density values.
51  //
52  // Similar to DensityScan1D but can be used with an arbitrary transform
53  // of the array index. The arbitrariness of the transform also prevents
54  // us from integrating over the bin size.
55  */
56  template<class Transform>
58  {
59  public:
60  /**
61  // The "transform" parameter will not be copied internally. The user
62  // is responsible for ensuring the proper lifetime of this object.
63  */
65  const Transform& transform,
66  const double normfactor) :
67  fcn_(fcn), mapping_(transform), norm_(normfactor) {}
68 
69  inline double operator()(const unsigned* index, const unsigned len) const
70  {
71  if (len != 1U) throw std::invalid_argument(
72  "In npstat::DensityScan1DTrans::operator(): "
73  "incompatible input point dimensionality");
74  assert(index);
75  const double x = mapping_(static_cast<double>(index[0]));
76  return norm_*fcn_.density(x);
77  }
78 
79  private:
80  DensityScan1DTrans();
81 
82  const AbsDistribution1D& fcn_;
83  const Transform& mapping_;
84  double norm_;
85  };
86 
87  /**
88  // A helper functor to be used for the determination of density
89  // discretization errors (when the density is represented by
90  // a collection of values on a grid)
91  */
92  class DensityDiscretizationError1D : public Functor1<double, double>
93  {
94  public:
95  /**
96  // "discreteValue" is the density value on a particular
97  // discretization interval. It is expected that the driver
98  // code will integrate the L2 error returned by this functor
99  // over all such intervals.
100  */
102  const double normfactor,
103  const double discreteValue)
104  : fcn_(fcn), norm_(normfactor), h_(discreteValue) {}
105 
106  inline virtual ~DensityDiscretizationError1D() {}
107 
108  inline virtual double operator()(const double& a) const
109  {
110  const double d = norm_*fcn_.density(a) - h_;
111  return d*d;
112  }
113 
114  private:
116  const AbsDistribution1D& fcn_;
117  const double norm_;
118  const double h_;
119  };
120 }
121 
122 #endif // NPSTAT_DENSITYSCAN1D_HH_
Interface definition for 1-d continuous statistical distributions.
Definition: DensityScan1D.hh:93
DensityDiscretizationError1D(const AbsDistribution1D &fcn, const double normfactor, const double discreteValue)
Definition: DensityScan1D.hh:101
Definition: DensityScan1D.hh:58
DensityScan1DTrans(const AbsDistribution1D &fcn, const Transform &transform, const double normfactor)
Definition: DensityScan1D.hh:64
Definition: DensityScan1D.hh:19
DensityScan1D(const AbsDistribution1D &fcn, double normfactor, unsigned long nbins, double xmin, double xmax, unsigned nIntegrationPoints=1)
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31
virtual double density(double x) const =0
Definition: SimpleFunctors.hh:58