npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
histoStats.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_HISTOSTATS_HH_
2 #define NPSTAT_HISTOSTATS_HH_
3 
4 /*!
5 // \file histoStats.hh
6 //
7 // \brief Calculate various descriptive statistics for histograms
8 //
9 // Helper functions for calculating simple stats of multivariate
10 // histograms. Histogram bin centers are used as coordinates for
11 // which the statistical info is calculated and bin contents are
12 // used as weights. Naturally, a numerically sound two-pass algorithm
13 // is used to calculate the covariance matrix.
14 //
15 // The code is not going to verify that every bin entry is non-negative
16 // (very often, this is already guaranteed by the manner in which the
17 // histogram is accumulated). Note, however, that in the presence of
18 // negative weights the results can not be easily interpreted.
19 //
20 // Author: I. Volobouev
21 //
22 // December 2011
23 */
24 
25 #include <vector>
26 
27 #include "geners/CPP11_auto_ptr.hh"
28 #include "npstat/nm/Matrix.hh"
31 
32 namespace npstat {
33  /**
34  // Calculate the mean of histogrammed data. The length of the "mean"
35  // array should be equal to or exceed the dimensionality of the histogram.
36  */
37  template<class Histo>
38  void histoMean(const Histo& histo, double* mean, unsigned lengthMean);
39 
40  /**
41  // Calculate quantiles for the given histogram axis assuming that
42  // the histogram contents are non-negative. All other variables are
43  // marginalized. Overflow bins are ignored.
44  */
45  template<class Histo>
46  void histoQuantiles(const Histo& histo, unsigned axisNumber,
47  const double* qvalues,
48  double* quantiles, unsigned nqvalues);
49 
50  /**
51  // Calculate covariance matrix for the histogrammed data.
52  // Upon exit, the size of the matrix will be N x N, where N
53  // is the histogram dimensionality.
54  */
55  template<class Histo, unsigned Len>
56  void histoCovariance(const Histo& histo, Matrix<double,Len>* covariance);
57 
58  /**
59  // Construct a "BinnedDensity1D" object out of a uniformly binned
60  // 1-d histogram. All histogram bins must be non-negative.
61  */
62  template<class Histo>
63  CPP11_auto_ptr<BinnedDensity1D> histoDensity1D(
64  const Histo& histo, unsigned interpolationDegree=0);
65 
66  /**
67  // Construct a "BinnedDensityND" object out of a uniformly binned
68  // multivariate histogram. All histogram bins must be non-negative.
69  */
70  template<class Histo>
71  CPP11_auto_ptr<BinnedDensityND> histoDensityND(
72  const Histo& histo, unsigned interpolationDegree=0);
73 
74 #ifdef SWIG
75  template<class Histo>
76  inline BinnedDensity1D* histoDensity1D2(
77  const Histo& histo, unsigned iDegree=0)
78  {
79  CPP11_auto_ptr<BinnedDensity1D> p(histoDensity1D(histo, iDegree));
80  return p.release();
81  }
82 
83  template<class Histo>
84  inline BinnedDensityND* histoDensityND2(
85  const Histo& histo, unsigned iDegree=0)
86  {
87  CPP11_auto_ptr<BinnedDensityND> p(histoDensityND(histo, iDegree));
88  return p.release();
89  }
90 
91  template<class Histo>
92  inline std::vector<double> histoMean2(const Histo& histo)
93  {
94  const unsigned dim = histo.dim();
95  std::vector<double> vec(dim, 0.0);
96  if (dim)
97  histoMean(histo, &vec[0], dim);
98  return vec;
99  }
100 
101  template<class Histo>
102  inline Matrix<double>* histoCovariance2(const Histo& histo)
103  {
104  const unsigned dim = histo.dim();
105  CPP11_auto_ptr<Matrix<double> > m(new Matrix<double>(dim, dim));
106  histoCovariance(histo, &*m);
107  return m.release();
108  }
109 
110  template<class Histo>
111  inline std::vector<double> histoQuantiles2(
112  const Histo& histo, const unsigned axisNumber,
113  const std::vector<double>& qvalues)
114  {
115  const unsigned long nq = qvalues.size();
116  std::vector<double> quantiles(nq);
117  if (nq)
118  histoQuantiles(histo, axisNumber, &qvalues[0], &quantiles[0], nq);
119  return quantiles;
120  }
121 #endif // SWIG
122 }
123 
124 #include "npstat/stat/histoStats.icc"
125 
126 #endif // NPSTAT_HISTOSTATS_HH_
A number of useful 1-d continuous statistical distributions.
A number of useful multivariate continuous statistical distributions.
Template matrix class.
Definition: Distributions1D.hh:1032
Definition: Matrix.hh:49
Definition: AbsArrayProjector.hh:14
CPP11_auto_ptr< BinnedDensity1D > histoDensity1D(const Histo &histo, unsigned interpolationDegree=0)
void histoCovariance(const Histo &histo, Matrix< double, Len > *covariance)
CPP11_auto_ptr< BinnedDensityND > histoDensityND(const Histo &histo, unsigned interpolationDegree=0)
void histoMean(const Histo &histo, double *mean, unsigned lengthMean)
void histoQuantiles(const Histo &histo, unsigned axisNumber, const double *qvalues, double *quantiles, unsigned nqvalues)