npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
WeightedStatAccumulatorPair.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_WEIGHTEDSTATACCUMULATORPAIR_HH_
2 #define NPSTAT_WEIGHTEDSTATACCUMULATORPAIR_HH_
3 
4 /*!
5 // \file WeightedStatAccumulatorPair.hh
6 //
7 // \brief Single-pass accumulator of statistical summary for a set of
8 // weighted points in two dimensions
9 //
10 // Author: I. Volobouev
11 //
12 // June 2012
13 */
14 
15 #include <stdexcept>
16 #include <utility>
17 
19 
20 namespace npstat {
21  /**
22  // This class functions like a pair of WeightedStatAccumulator
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 WeightedStatAccumulatorPair() : crossSumsq_(0.0L) {}
30 
31  /** Statistical summary for the first variable */
32  inline const WeightedStatAccumulator& first() const {return first_;}
33 
34  /** Statistical summary for the second variable */
35  inline const WeightedStatAccumulator& second() const {return second_;}
36 
37  /** The sum of cross terms */
38  inline long double crossSumsq() const {return crossSumsq_;}
39 
40  /**
41  // This method returns the effective number of counts
42  // which is (squared sum of weights)/(sum of squared weights)
43  */
44  inline double count() const {return first_.count();}
45 
46  /**
47  // This method returns the total number of times
48  // the "accumulate" method was called since the last
49  // reset (or since the object was created)
50  */
51  inline unsigned long ncalls() const {return first_.ncalls();}
52 
53  /**
54  // This method returns the number of times
55  // the "accumulate" method was called with a positive weight
56  */
57  inline unsigned long nfills() const {return first_.nfills();}
58 
59  /** Covariance between two variables */
60  double cov() const;
61 
62  /** Correlation coefficient between two variables */
63  double corr() const;
64 
65  //@{
66  /** Accumulate the data */
67  void accumulate(double x, double y, double w);
68  void accumulate(const std::pair<double,double>& point, double w);
69  //@}
70 
71  //@{
72  /** Accumulate the data from another accumulator */
74  void accumulate(const WeightedStatAccumulatorPair&, double w);
75  //@}
76 
77  /** Clear all accumulators */
78  void reset();
79 
80  /** Accumulate the data */
81  template<typename Triple>
83  {accumulate(t.first, t.second, t.third); return *this;}
84 
85  /** Add the summary from another accumulator */
88  {accumulate(r); return *this;}
89 
90  /**
91  // The effect of "operator*=" is the same as if all values
92  // were multiplied by "r" for each "accumulate" call
93  */
94  template<typename ConvertibleToLDouble>
95  inline WeightedStatAccumulatorPair& operator*=(const ConvertibleToLDouble& r)
96  {
97  const long double factor(static_cast<long double>(r));
98  first_ *= factor;
99  second_ *= factor;
100  crossSumsq_ *= factor*factor;
101  return *this;
102  }
103 
104  /**
105  // Scaling of the weights. The effect is the same as if all
106  // weights were multiplied by "r" for each "accumulate" call.
107  // Note that r can not be negative for this method.
108  */
110  {
111  first_.scaleWeights(r);
112  second_.scaleWeights(r);
113  crossSumsq_ *= r;
114  return *this;
115  }
116 
117  /**
118  // The effect of "operator/=" is the same as if all values
119  // were divided by "r" for each "accumulate" call
120  */
121  template<typename ConvertibleToLDouble>
122  inline WeightedStatAccumulatorPair& operator/=(const ConvertibleToLDouble& r)
123  {
124  const long double denom = static_cast<long double>(r);
125  if (denom == 0.0L) throw std::domain_error(
126  "In npstat::WeightedStatAccumulatorPair::operator/=: "
127  "division by zero");
128  return operator*=(1.0L/denom);
129  }
130 
131  /** Binary multiplication by a double */
132  template<typename ConvToLDouble>
133  inline WeightedStatAccumulatorPair operator*(const ConvToLDouble& r) const
134  {
135  WeightedStatAccumulatorPair acc(*this);
136  acc *= r;
137  return acc;
138  }
139 
140  /** Binary division by a double */
141  template<typename ConvToLDouble>
142  inline WeightedStatAccumulatorPair operator/(const ConvToLDouble& r) const
143  {
144  WeightedStatAccumulatorPair acc(*this);
145  acc /= r;
146  return acc;
147  }
148 
149  /** Comparison for equality */
151 
152  /** Logical negation of operator== */
153  inline bool operator!=(const WeightedStatAccumulatorPair& r) const
154  {return !(*this == r);}
155 
156  //@{
157  /** Method related to "geners" I/O */
158  inline gs::ClassId classId() const {return gs::ClassId(*this);}
159  bool write(std::ostream& of) const;
160  //@}
161 
162  static inline const char* classname()
163  {return "npstat::WeightedStatAccumulatorPair";}
164  static inline unsigned version() {return 1;}
165  static void restore(const gs::ClassId& id, std::istream& in,
166  WeightedStatAccumulatorPair* acc);
167  private:
168  WeightedStatAccumulator first_;
169  WeightedStatAccumulator second_;
170  long double crossSumsq_;
171 
172 #ifdef SWIG
173  public:
174  inline WeightedStatAccumulatorPair mul2(const double r) const
175  {return operator*(r);}
176 
177  inline WeightedStatAccumulatorPair div2(const double r) const
178  {return operator/(r);}
179 
180  inline WeightedStatAccumulatorPair& imul2(const double r)
181  {*this *= r; return *this;}
182 
183  inline WeightedStatAccumulatorPair& idiv2(const double r)
184  {*this /= r; return *this;}
185 #endif // SWIG
186  };
187 }
188 
189 #endif // NPSTAT_WEIGHTEDSTATACCUMULATORPAIR_HH_
Single-pass accumulator of statistical summary for a set of weighted observations.
Definition: WeightedStatAccumulatorPair.hh:27
const WeightedStatAccumulator & first() const
Definition: WeightedStatAccumulatorPair.hh:32
WeightedStatAccumulatorPair operator/(const ConvToLDouble &r) const
Definition: WeightedStatAccumulatorPair.hh:142
WeightedStatAccumulatorPair & operator*=(const ConvertibleToLDouble &r)
Definition: WeightedStatAccumulatorPair.hh:95
bool operator==(const WeightedStatAccumulatorPair &r) const
unsigned long ncalls() const
Definition: WeightedStatAccumulatorPair.hh:51
WeightedStatAccumulatorPair & operator+=(const WeightedStatAccumulatorPair &r)
Definition: WeightedStatAccumulatorPair.hh:86
bool operator!=(const WeightedStatAccumulatorPair &r) const
Definition: WeightedStatAccumulatorPair.hh:153
const WeightedStatAccumulator & second() const
Definition: WeightedStatAccumulatorPair.hh:35
WeightedStatAccumulatorPair operator*(const ConvToLDouble &r) const
Definition: WeightedStatAccumulatorPair.hh:133
WeightedStatAccumulatorPair & operator+=(const Triple &t)
Definition: WeightedStatAccumulatorPair.hh:82
WeightedStatAccumulatorPair & scaleWeights(const double r)
Definition: WeightedStatAccumulatorPair.hh:109
WeightedStatAccumulatorPair & operator/=(const ConvertibleToLDouble &r)
Definition: WeightedStatAccumulatorPair.hh:122
double count() const
Definition: WeightedStatAccumulatorPair.hh:44
void accumulate(const WeightedStatAccumulatorPair &)
void accumulate(double x, double y, double w)
gs::ClassId classId() const
Definition: WeightedStatAccumulatorPair.hh:158
unsigned long nfills() const
Definition: WeightedStatAccumulatorPair.hh:57
long double crossSumsq() const
Definition: WeightedStatAccumulatorPair.hh:38
Definition: WeightedStatAccumulator.hh:35
unsigned long nfills() const
Definition: WeightedStatAccumulator.hh:103
unsigned long ncalls() const
Definition: WeightedStatAccumulator.hh:96
WeightedStatAccumulator & scaleWeights(double r)
Definition: AbsArrayProjector.hh:14
Definition: Triple.hh:18
Second second
Second element of the triple.
Definition: Triple.hh:84
Third third
Third element of the triple.
Definition: Triple.hh:85
First first
First element of the triple.
Definition: Triple.hh:83