npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
discretizationErrorND.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_DISCRETIZATIONERRORND_HH_
2 #define NPSTAT_DISCRETIZATIONERRORND_HH_
3 
4 /*!
5 // \file discretizationErrorND.hh
6 //
7 // \brief Calculate the ISE due to binning for multivariate densities
8 //
9 // Author: I. Volobouev
10 //
11 // June 2015
12 */
13 
15 #include "npstat/stat/HistoND.hh"
16 
17 namespace npstat {
18  template<class Axis>
19  double discretizationErrorND(
20  const AbsDistributionND& fcn, const std::vector<Axis>& axes,
21  const unsigned pointsPerBinWhenDiscretizing,
22  const unsigned nIntegrationPoints, const bool normalize=true)
23  {
24  // First, discretize the density
25  ArrayND<double> densityScan(Private::makeHistoShape(axes));
26  densityScan.functorFill(makeDensityAveScanND(
27  fcn, axes, pointsPerBinWhenDiscretizing));
28 
29  // Now, go over all bins and accumulate the ISE.
30  // We also need to accumulate the density integral
31  // in order to have proper normalization.
32  const unsigned dim = fcn.dim();
33  const unsigned long len = densityScan.length();
34  long double integ = 0.0L;
35  long double ise = 0.0L;
36  double center[CHAR_BIT*sizeof(unsigned long)];
37  double size[CHAR_BIT*sizeof(unsigned long)];
38  unsigned index[CHAR_BIT*sizeof(unsigned long)];
39  for (unsigned long ipt=0; ipt<len; ++ipt)
40  {
41  densityScan.convertLinearIndex(ipt, index, dim);
42  double volume = 1.0;
43  for (unsigned i=0; i<dim; ++i)
44  {
45  center[i] = axes[i].binCenter(index[i]);
46  const double width = axes[i].binWidth(index[i]);
47  volume *= width;
48  size[i] = width;
49  }
50  const double c = densityScan.linearValue(ipt);
51  integ += c*volume;
52  DensityDiscretizationErrorND err(fcn, 1.0, c);
54  err, center, size, dim, nIntegrationPoints);
55  }
56  if (normalize)
57  {
58  if (integ > 0.0L)
59  return ise/integ/integ;
60  else
61  return 0.0;
62  }
63  else
64  return ise;
65  }
66 }
67 
68 #endif // NPSTAT_DISCRETIZATIONERRORND_HH_
Fill multivariate arrays with multivariate density values by averaging inside the corresponding bins.
Arbitrary-dimensional histogram template.
Definition: AbsArrayProjector.hh:14
double rectangleIntegralCenterAndSize(const AbsMultivariateFunctor &f, const double *rectangleCenter, const double *rectangleSize, unsigned dim, unsigned integrationPoints)