npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
StatAccumulatorPair.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_STATACCUMULATORPAIR_HH_
2 #define NPSTAT_STATACCUMULATORPAIR_HH_
3 
4 /*!
5 // \file StatAccumulatorPair.hh
6 //
7 // \brief Single-pass accumulator of statistical summary for a set of
8 // two-dimensional observations
9 //
10 // Author: I. Volobouev
11 //
12 // July 2011
13 */
14 
15 #include <stdexcept>
16 #include <utility>
17 
19 
20 namespace npstat {
21  /**
22  // This class functions like a pair of StatAccumulator
23  // objects but it also accumulates the cross term so that the
24  // correlation coefficient between two variables can be evaluated.
25  */
27  {
28  public:
29  inline StatAccumulatorPair() : crossSumsq_(0.0L) {}
30 
31  /** Statistical summary for the first variable */
32  inline const StatAccumulator& first() const {return first_;}
33 
34  /** Statistical summary for the second variable */
35  inline const StatAccumulator& second() const {return second_;}
36 
37  /** The sum of cross terms */
38  inline long double crossSumsq() const {return crossSumsq_;}
39 
40  /** Number of observations processed so far */
41  inline unsigned long count() const {return first_.count();}
42 
43  /** Covariance between two variables */
44  double cov() const;
45 
46  /** Correlation coefficient between two variables */
47  double corr() const;
48 
49  //@{
50  /** Accumulate the data */
51  void accumulate(double x, double y);
52  void accumulate(const std::pair<double,double>& point);
53  //@}
54 
55  /** Accumulate the data from another accumulator */
57 
58  /** Clear all accumulators */
59  void reset();
60 
61  /** Accumulate the data */
63  const std::pair<double,double>& point)
64  {accumulate(point); return *this;}
65 
66  /** Add the summary from another accumulator */
68  {accumulate(r); return *this;}
69 
70  /**
71  // The effect of "operator*=" is the same as if all values
72  // were multiplied by "r" for each "accumulate" call
73  */
74  template<typename ConvertibleToLDouble>
75  inline StatAccumulatorPair& operator*=(const ConvertibleToLDouble& r)
76  {
77  const long double factor(static_cast<long double>(r));
78  first_ *= factor;
79  second_ *= factor;
80  crossSumsq_ *= factor*factor;
81  return *this;
82  }
83 
84  /**
85  // The effect of "operator/=" is the same as if all values
86  // were divided by "r" for each "accumulate" call
87  */
88  template<typename ConvertibleToLDouble>
89  inline StatAccumulatorPair& operator/=(const ConvertibleToLDouble& r)
90  {
91  const long double denom = static_cast<long double>(r);
92  if (denom == 0.0L) throw std::domain_error(
93  "In npstat::StatAccumulatorPair::operator/=: "
94  "division by zero");
95  return operator*=(1.0L/denom);
96  }
97 
98  /** Binary multiplication by a double */
99  template<typename ConvToLDouble>
100  inline StatAccumulatorPair operator*(const ConvToLDouble& r) const
101  {
102  StatAccumulatorPair acc(*this);
103  acc *= r;
104  return acc;
105  }
106 
107  /** Binary division by a double */
108  template<typename ConvToLDouble>
109  inline StatAccumulatorPair operator/(const ConvToLDouble& r) const
110  {
111  StatAccumulatorPair acc(*this);
112  acc /= r;
113  return acc;
114  }
115 
116  /** Comparison for equality */
117  bool operator==(const StatAccumulatorPair& r) const;
118 
119  /** Logical negation of operator== */
120  inline bool operator!=(const StatAccumulatorPair& r) const
121  {return !(*this == r);}
122 
123  //@{
124  /** Method related to "geners" I/O */
125  inline gs::ClassId classId() const {return gs::ClassId(*this);}
126  bool write(std::ostream& of) const;
127  //@}
128 
129  static inline const char* classname()
130  {return "npstat::StatAccumulatorPair";}
131  static inline unsigned version() {return 1;}
132  static void restore(const gs::ClassId& id, std::istream& in,
133  StatAccumulatorPair* acc);
134  private:
135  StatAccumulator first_;
136  StatAccumulator second_;
137  long double crossSumsq_;
138 
139 #ifdef SWIG
140  public:
141  inline StatAccumulatorPair mul2(const double r) const
142  {return operator*(r);}
143 
144  inline StatAccumulatorPair div2(const double r) const
145  {return operator/(r);}
146 
147  inline StatAccumulatorPair& imul2(const double r)
148  {*this *= r; return *this;}
149 
150  inline StatAccumulatorPair& idiv2(const double r)
151  {*this /= r; return *this;}
152 #endif // SWIG
153  };
154 }
155 
156 #endif // NPSTAT_STATACCUMULATORPAIR_HH_
Single-pass accumulator of statistical summary for a set of numbers.
Definition: StatAccumulatorPair.hh:27
long double crossSumsq() const
Definition: StatAccumulatorPair.hh:38
const StatAccumulator & first() const
Definition: StatAccumulatorPair.hh:32
bool operator!=(const StatAccumulatorPair &r) const
Definition: StatAccumulatorPair.hh:120
StatAccumulatorPair & operator+=(const StatAccumulatorPair &r)
Definition: StatAccumulatorPair.hh:67
StatAccumulatorPair & operator*=(const ConvertibleToLDouble &r)
Definition: StatAccumulatorPair.hh:75
StatAccumulatorPair operator/(const ConvToLDouble &r) const
Definition: StatAccumulatorPair.hh:109
void accumulate(const StatAccumulatorPair &)
gs::ClassId classId() const
Definition: StatAccumulatorPair.hh:125
StatAccumulatorPair & operator+=(const std::pair< double, double > &point)
Definition: StatAccumulatorPair.hh:62
StatAccumulatorPair operator*(const ConvToLDouble &r) const
Definition: StatAccumulatorPair.hh:100
void accumulate(double x, double y)
bool operator==(const StatAccumulatorPair &r) const
unsigned long count() const
Definition: StatAccumulatorPair.hh:41
const StatAccumulator & second() const
Definition: StatAccumulatorPair.hh:35
StatAccumulatorPair & operator/=(const ConvertibleToLDouble &r)
Definition: StatAccumulatorPair.hh:89
Definition: StatAccumulator.hh:33
unsigned long count() const
Definition: StatAccumulator.hh:38
Definition: AbsArrayProjector.hh:14