npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
CrossCovarianceAccumulator.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_CROSSCOVARIANCEACCUMULATOR_HH_
2 #define NPSTAT_CROSSCOVARIANCEACCUMULATOR_HH_
3 
4 /*!
5 // \file CrossCovarianceAccumulator.hh
6 //
7 // \brief Accumulator of covariances between two arrays of random quantities
8 //
9 // Author: I. Volobouev
10 //
11 // July 2011
12 */
13 
14 #include <vector>
15 
16 #include "geners/ClassId.hh"
17 #include "npstat/nm/Matrix.hh"
18 
19 namespace npstat {
20  /**
21  // Simple single-pass (which means imprecise) accumulator of covariances
22  // between two arrays. Correlations are between all possible pairs
23  // in which one element is taken from the first array and the other from
24  // the second array.
25  */
27  {
28  public:
29  /** n1 and n2 are the expected array sizes */
30  CrossCovarianceAccumulator(unsigned n1, unsigned n2);
31 
32  //@{
33  /** Basic inspector of accumulated quantities */
34  inline unsigned dim1() const {return dim1_;}
35  inline unsigned dim2() const {return dim2_;}
36  inline unsigned long count() const {return count_;}
37  inline const std::vector<long double>& sum1() const {return sum1_;}
38  inline const std::vector<long double>& sum2() const {return sum2_;}
39  inline const std::vector<long double>& sumsq1() const {return sum1_;}
40  inline const std::vector<long double>& sumsq2() const {return sum2_;}
41  long double sumsq(unsigned i, unsigned j) const;
42  //@}
43 
44  //@{
45  /** Inspect mean, standard deviation, etc */
46  double mean1(unsigned i) const;
47  double mean2(unsigned i) const;
48  double stdev1(unsigned i) const;
49  double stdev2(unsigned i) const;
50  double meanUncertainty1(unsigned i) const;
51  double meanUncertainty2(unsigned i) const;
52  //@}
53 
54  /** Covariance between the given variables */
55  double cov(unsigned i, unsigned j) const;
56 
57  /** Correlation coefficient between the given variables */
58  double corr(unsigned i, unsigned j) const;
59 
60  /**
61  // Covariance matrix (of dimensions n1 x n2,
62  // where n1 and n2 are given in the constructor)
63  */
65 
66  /**
67  // Correlation matrix (of dimensions n1 x n2,
68  // where n1 and n2 are given in the constructor)
69  */
71 
72  /**
73  // Accumulate the statistical summaries.
74  //
75  // The length of array data1 should be n1 (as in the constructor).
76  // The length of array data2 should be n2 (as in the constructor).
77  */
78  template<typename T1, typename T2>
79  void accumulate(const T1* data1, unsigned len1,
80  const T2* data2, unsigned len2);
81 
82  /** Accumulate summaries from a pair of vectors or arrays */
83  template<typename ArrayPair>
84  inline void accumulate(const ArrayPair& pair)
85  {
86  accumulate(&pair.first[0], pair.first.size(),
87  &pair.second[0], pair.second.size());
88  }
89 
90  /** Add the statistical summaries from another accumulator */
92 
93  /** Accumulate summaries from a pair of vectors or arrays */
94  template<typename ArrayPair>
95  inline CrossCovarianceAccumulator& operator+=(const ArrayPair& pair)
96  {accumulate(pair); return *this;}
97 
98  /** Add the statistical summaries from another accumulator */
101  {accumulate(r); return *this;}
102 
103  /** Reset all accumulators and counters */
104  void reset();
105 
106  /** Comparison for equality */
108 
109  /** Logical negation of operator== */
110  inline bool operator!=(const CrossCovarianceAccumulator& r) const
111  {return !(*this == r);}
112 
113  //@{
114  /** Method related to "geners" I/O */
115  inline gs::ClassId classId() const {return gs::ClassId(*this);}
116  bool write(std::ostream& of) const;
117  //@}
118 
119  static inline const char* classname()
120  {return "npstat::CrossCovarianceAccumulator";}
121  static inline unsigned version() {return 1;}
122  static CrossCovarianceAccumulator* read(const gs::ClassId&, std::istream&);
123 
124  private:
126 
127  unsigned dim1_;
128  unsigned dim2_;
129  unsigned long count_;
130  std::vector<long double> sum1_;
131  std::vector<long double> sum2_;
132  std::vector<long double> sumsq1_;
133  std::vector<long double> sumsq2_;
134  std::vector<long double> sumsq_;
135  };
136 }
137 
138 #include "npstat/stat/CrossCovarianceAccumulator.icc"
139 
140 #endif // NPSTAT_CROSSCOVARIANCEACCUMULATOR_HH_
Template matrix class.
Definition: CrossCovarianceAccumulator.hh:27
gs::ClassId classId() const
Definition: CrossCovarianceAccumulator.hh:115
void accumulate(const CrossCovarianceAccumulator &)
Matrix< double > crossCovMat() const
CrossCovarianceAccumulator(unsigned n1, unsigned n2)
double corr(unsigned i, unsigned j) const
unsigned dim1() const
Definition: CrossCovarianceAccumulator.hh:34
double mean1(unsigned i) const
CrossCovarianceAccumulator & operator+=(const CrossCovarianceAccumulator &r)
Definition: CrossCovarianceAccumulator.hh:99
void accumulate(const T1 *data1, unsigned len1, const T2 *data2, unsigned len2)
double cov(unsigned i, unsigned j) const
Matrix< double > crossCorrMat() const
CrossCovarianceAccumulator & operator+=(const ArrayPair &pair)
Definition: CrossCovarianceAccumulator.hh:95
bool operator==(const CrossCovarianceAccumulator &r) const
bool operator!=(const CrossCovarianceAccumulator &r) const
Definition: CrossCovarianceAccumulator.hh:110
void accumulate(const ArrayPair &pair)
Definition: CrossCovarianceAccumulator.hh:84
Definition: AbsArrayProjector.hh:14