npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
MultivariateWeightedSumsqAccumulator.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_MULTIVARIATEWEIGHTEDSUMSQACCUMULATOR_HH_
2 #define NPSTAT_MULTIVARIATEWEIGHTEDSUMSQACCUMULATOR_HH_
3 
4 /*!
5 // \file MultivariateWeightedSumsqAccumulator.hh
6 //
7 // \brief Accumulator of weighted sums of squares for covariance calculations
8 //
9 // Author: I. Volobouev
10 //
11 // April 2011
12 */
13 
14 #include <vector>
15 
16 #include "npstat/nm/Matrix.hh"
18 
19 namespace npstat {
20  /**
21  * Class for accumulating multivariate weighted sums of squares
22  * and calculating corresponding covariances. Intended for use
23  * together with MultivariateWeightedSumAccumulator. For example,
24  * to determine covariance matrix of all ntuple columns, do
25  * something along the following lines:
26  *
27  * @code
28  * const AbsNtuple<T>& nt = ... // reference to your ntuple
29  * const WeightCalculator& wcalc = ... // reference to the weight functor
30  * MultivariateWeightedSumAccumulator sums;
31  * nt.weightedCycleOverRows(sums, wcalc);
32  * MultivariateWeightedSumsqAccumulator sumsqs(sums);
33  * nt.weightedCycleOverRows(sumsqs, wcalc);
34  * const Matrix<long double>& m = sumsqs.covMat(); // covariance matrix
35  * @endcode
36  */
37  template<typename Precise = long double>
39  {
40  public:
41  typedef Precise precise_type;
42 
43  /**
44  // To calculate covariances for select columns only, use this
45  // constructor with a non-trivial column map. Statistics will be
46  // accumulated only for the columns included in the "columns"
47  // argument. This can be useful for speeding up the code, as the
48  // computational complexity of covariance matrix calculations
49  // increases as the number of columns squared.
50  */
51  template<typename Precise2>
53  const unsigned long* columns, unsigned nColumns,
55 
56  /** Constructor with a trivial column map */
57  template<typename Precise2>
60 
61  //@{
62  /** Inspect object properties */
63  inline unsigned dim() const {return mapLen_;}
64  inline unsigned long ncalls() const {return ncalls_;}
65  inline unsigned long expectedNumCols() const {return nCols_;}
66  inline Precise weightSum() const {return weightSum_;}
67  inline const std::vector<unsigned long>& indexMap() const
68  {return indexMap_;}
69  //@}
70 
71  //@{
72  /** Mean values from the sum accumulator provided in the constructor */
73  inline const std::vector<Precise>& meanVector() const {return mean_;}
74  inline Precise mean(const unsigned i) const {return mean_.at(i);}
75  //@}
76 
77  /**
78  // This method returns the effective number of counts
79  // which is (squared sum of weights)/(sum of squared weights)
80  */
81  Precise count() const;
82 
83  /** Accumulate statistics for an array (ntuple row) */
84  template<typename T>
85  void accumulate(const T* data, unsigned long len, double w);
86 
87  /** Reset all accumulators and counters */
88  void reset();
89 
90  /** Accumulated sums of squares or cross terms */
91  const Precise& sumsq(unsigned i, unsigned j) const;
92 
93  /**
94  // Covariance between the variables corresponding to the
95  // given indices (as mapped to the original indices)
96  */
97  Precise cov(unsigned i, unsigned j) const;
98 
99  /** Correlation coefficient between the given variables */
100  Precise corr(unsigned i, unsigned j) const;
101 
102  /** Standard deviation for the given variable */
103  Precise stdev(unsigned i) const;
104 
105  /** Retrieve the covariance matrix */
107 
108  /** Retrieve the correlation matrix */
110 
111  private:
112  std::vector<Precise> sumsq_;
113  std::vector<Precise> mean_;
114  std::vector<unsigned long> indexMap_;
115  Precise weightSum_;
116  Precise weightSumSq_;
117  unsigned long nCols_;
118  unsigned long ncalls_;
119  unsigned mapLen_;
120  };
121 }
122 
123 #include "npstat/stat/MultivariateWeightedSumsqAccumulator.icc"
124 
125 #endif // NPSTAT_MULTIVARIATEWEIGHTEDSUMSQACCUMULATOR_HH_
Template matrix class.
Accumulator of weighted array sums for use with AbsNtuple method weightedCycleOverRows and similar.
Definition: Matrix.hh:49
Definition: MultivariateWeightedSumAccumulator.hh:29
Definition: MultivariateWeightedSumsqAccumulator.hh:39
const Precise & sumsq(unsigned i, unsigned j) const
const std::vector< Precise > & meanVector() const
Definition: MultivariateWeightedSumsqAccumulator.hh:73
void accumulate(const T *data, unsigned long len, double w)
MultivariateWeightedSumsqAccumulator(const unsigned long *columns, unsigned nColumns, const MultivariateWeightedSumAccumulator< Precise2 > &sums)
unsigned dim() const
Definition: MultivariateWeightedSumsqAccumulator.hh:63
MultivariateWeightedSumsqAccumulator(const MultivariateWeightedSumAccumulator< Precise2 > &sums)
Precise corr(unsigned i, unsigned j) const
Precise cov(unsigned i, unsigned j) const
Definition: AbsArrayProjector.hh:14