Belle II Software development
CalcMeanCov< N, RealType > Class Template Reference

Class to calculate mean and and covariance between a number of parameters on running data without storing the actual values, also for weighted entries. More...

#include <CalcMeanCov.h>

Public Types

typedef RealType value_type
 type of float variable to use for calculations and storage
 

Public Member Functions

 CalcMeanCov ()
 default constructor.
 
void clear ()
 Clear all values.
 
template<class... T>
void addWeighted (value_type weight, T... values)
 Update mean and covariance by adding a new, weighted entry.
 
template<class... T>
void add (T... values)
 Update mean and covariance by adding a new entry.
 
void add (const CalcMeanCov< N, RealType > &other)
 Merge the data set in 'other' into this one.
 
void addWeightedArray (value_type weight, value_type *values)
 Update mean and covarianced by adding a new weighted entry.
 
void addArray (value_type *values)
 Update mean and covariance by adding a new entry.
 
Getters

No range check if performed on indicies i and j

value_type getEntries () const
 Return the number of entries.
 
value_type getMean (int i) const
 Return the mean for parameter i.
 
value_type getCovariance (int i, int j) const
 Return the covariance between parameters i and j.
 
value_type getCorrelation (int i, int j) const
 Return the correlation coefficient between parameters i and j.
 
value_type getVariance (int i) const
 Return the variance for paramter i.
 
value_type getStddev (int i) const
 Return the standard deviation for parameter i.
 
value_type getSum (int i) const
 Return the weighted sum values for parameter i.
 
Templated getters

These getters are templated and provide compile time range checking for the parameter indices

template<int i = 0>
value_type getMean () const
 Return the mean for parameter i.
 
template<int i, int j>
value_type getCovariance () const
 Return the covariance between parameters i and j.
 
template<int i, int j>
value_type getCorrelation () const
 Return the correlation coefficient between parameters i and j.
 
template<int i = 0>
value_type getVariance () const
 Return the variance for paramter i.
 
template<int i = 0>
value_type getStddev () const
 Return the standard deviation for parameter i.
 
template<int i = 0>
value_type getSum () const
 Return the weighted sum values for parameter i.
 

Private Member Functions

template<int i, class... T>
void addValue (value_type weight, value_type x, T... values)
 Add a single value for parameter i and update mean and covariance.
 
template<int i>
void addValue (value_type)
 Break recursion of addValue when no parameters are left.
 
template<int i, int j, class... T>
void updateCov (value_type weight, value_type delta, value_type x, T... values)
 Update covariance between parameters i and j.
 
template<int i, int j>
void updateCov (value_type, value_type)
 Break recursion of updateCov once all parameters are consumed.
 
void addArrayValues (value_type weight, value_type *x)
 Add a new set of values and update mean and covariance.
 
constexpr int getIndex (unsigned int i, unsigned int j) const
 Access element in triangular matrix including diagonal elements.
 

Private Attributes

value_type m_entries
 Store the sum of weights.
 
value_type m_mean [N]
 Store the mean values for all parameters.
 
value_type m_covariance [N *(N+1)/2]
 Store the triangular covariance matrix for all parameters in continous memory.
 

Detailed Description

template<int N = 1, class RealType = double>
class Belle2::CalcMeanCov< N, RealType >

Class to calculate mean and and covariance between a number of parameters on running data without storing the actual values, also for weighted entries.

It can store more than one value per entry, e.g. x,y,z values but will not calculate correlations, only mean and standard deviation for all values. Also works with weighted entries.

See Philippe Pébay, Formulas for Robust, One-Pass Parallel Computation of Covariances and Arbitrary-Order Statistical Moments, SANDIA REPORT SAND2008-6212

Template Parameters
Nnumber of parameters
RealTypefloating point type to be used

Definition at line 35 of file CalcMeanCov.h.

Member Typedef Documentation

◆ value_type

typedef RealType value_type

type of float variable to use for calculations and storage

Definition at line 42 of file CalcMeanCov.h.

Constructor & Destructor Documentation

◆ CalcMeanCov()

CalcMeanCov ( )
inline

default constructor.

Definition at line 39 of file CalcMeanCov.h.

39{ clear(); }
void clear()
Clear all values.
Definition: CalcMeanCov.h:45

Member Function Documentation

◆ add() [1/2]

void add ( const CalcMeanCov< N, RealType > &  other)
inline

Merge the data set in 'other' into this one.

Definition at line 77 of file CalcMeanCov.h.

78 {
79 const value_type n = m_entries + other.m_entries;
80 if (n == 0)
81 return;
82 for (int i = 0; i < N; ++i) {
83 const value_type delta = (other.m_mean[i] - m_mean[i]);
84 //Update covariance matrix
85 for (int j = i; j < N; ++j) {
86 const value_type delta2 = (other.m_mean[j] - m_mean[j]);
87 m_covariance[getIndex(i, j)] += other.m_covariance[getIndex(i, j)] + m_entries * other.m_entries
88 / n * delta * delta2;
89 }
90 //Update mean value
91 m_mean[i] += other.m_entries * delta / n;
92 }
93 m_entries = n;
94 }
value_type m_entries
Store the sum of weights.
Definition: CalcMeanCov.h:267
constexpr int getIndex(unsigned int i, unsigned int j) const
Access element in triangular matrix including diagonal elements.
Definition: CalcMeanCov.h:260
RealType value_type
type of float variable to use for calculations and storage
Definition: CalcMeanCov.h:42
value_type m_mean[N]
Store the mean values for all parameters.
Definition: CalcMeanCov.h:269
value_type m_covariance[N *(N+1)/2]
Store the triangular covariance matrix for all parameters in continous memory.
Definition: CalcMeanCov.h:272

◆ add() [2/2]

void add ( T...  values)
inline

Update mean and covariance by adding a new entry.

Parameters
valuesvalues for all parameters. The number of Parameters must be equal to N

Definition at line 71 of file CalcMeanCov.h.

72 {
73 addWeighted(1.0, values...);
74 }
void addWeighted(value_type weight, T... values)
Update mean and covariance by adding a new, weighted entry.
Definition: CalcMeanCov.h:59

◆ addArray()

void addArray ( value_type values)
inline

Update mean and covariance by adding a new entry.

Parameters
valuespointer to the first value

Definition at line 109 of file CalcMeanCov.h.

110 {
111 addWeightedArray(1.0, values);
112 }
void addWeightedArray(value_type weight, value_type *values)
Update mean and covarianced by adding a new weighted entry.
Definition: CalcMeanCov.h:100

◆ addArrayValues()

void addArrayValues ( value_type  weight,
value_type x 
)
inlineprivate

Add a new set of values and update mean and covariance.

This function does the same as addValue and updateCov but in a non-templated way.

Parameters
weightweight of the entry
xpointer to the actual values

Definition at line 240 of file CalcMeanCov.h.

241 {
242 for (int i = 0; i < N; ++i) {
243 const value_type delta = (x[i] - m_mean[i]);
244 //Update covariance matrix
245 for (int j = i; j < N; ++j) {
246 const value_type delta2 = (x[j] - m_mean[j]);
247 m_covariance[getIndex(i, j)] += (m_entries - weight) * weight
248 / m_entries * delta * delta2;
249 }
250 //Update mean value
251 m_mean[i] += weight * delta / m_entries;
252 }
253 }

◆ addValue() [1/2]

void addValue ( value_type  weight,
value_type  x,
T...  values 
)
inlineprivate

Add a single value for parameter i and update mean and covariance.

See also
addArrayValues
Template Parameters
iindex of the parameter
Parameters
weightweight of the entry
xactual value
valuesremaining values to be added recursively

Definition at line 199 of file CalcMeanCov.h.

200 {
201 const value_type delta = (x - m_mean[i]);
202 //Update covariance elements
203 updateCov<i, i>(weight, delta, x, values...);
204 //Update mean value
205 m_mean[i] += weight * delta / m_entries;
206 //recursively call again for remaining values
207 addValue < i + 1 > (weight, values...);
208 }

◆ addValue() [2/2]

void addValue ( value_type  )
inlineprivate

Break recursion of addValue when no parameters are left.

Definition at line 210 of file CalcMeanCov.h.

210{}

◆ addWeighted()

void addWeighted ( value_type  weight,
T...  values 
)
inline

Update mean and covariance by adding a new, weighted entry.

Parameters
weightweight of the entry
valuesvalues for all parameters. The number of parameters must be equal to N

Definition at line 59 of file CalcMeanCov.h.

60 {
61 static_assert(sizeof...(values) == N,
62 "Number of arguments must be equal N");
63 m_entries += weight;
64 addValue<0>(weight, values...);
65 }

◆ addWeightedArray()

void addWeightedArray ( value_type  weight,
value_type values 
)
inline

Update mean and covarianced by adding a new weighted entry.

Parameters
weightweight of entry
valuespointer to the first value

Definition at line 100 of file CalcMeanCov.h.

101 {
102 m_entries += weight;
103 addArrayValues(weight, values);
104 }
void addArrayValues(value_type weight, value_type *x)
Add a new set of values and update mean and covariance.
Definition: CalcMeanCov.h:240

◆ clear()

void clear ( )
inline

Clear all values.

Definition at line 45 of file CalcMeanCov.h.

46 {
47 m_entries = 0;
48 for (int i = 0; i < N; i++)
49 m_mean[i] = 0;
50 for (int i = 0; i < N * (N + 1) / 2; i++)
51 m_covariance[i] = 0;
52 }

◆ getCorrelation() [1/2]

value_type getCorrelation ( ) const
inline

Return the correlation coefficient between parameters i and j.

Definition at line 166 of file CalcMeanCov.h.

167 {
168 if (i == j) return 1.0;
169 if (m_entries == 0.0) return 0.0;
170 return getCovariance<i, j>() / (getStddev<i>() * getStddev<j>());
171 }

◆ getCorrelation() [2/2]

value_type getCorrelation ( int  i,
int  j 
) const
inline

Return the correlation coefficient between parameters i and j.

Definition at line 130 of file CalcMeanCov.h.

131 {
132 if (i == j) return 1.0;
133 if (m_entries == 0.0) return 0.0;
134 return getCovariance(i, j) / (getStddev(i) * getStddev(j));
135 }
value_type getStddev() const
Return the standard deviation for parameter i.
Definition: CalcMeanCov.h:178
value_type getCovariance() const
Return the covariance between parameters i and j.
Definition: CalcMeanCov.h:158

◆ getCovariance() [1/2]

value_type getCovariance ( ) const
inline

Return the covariance between parameters i and j.

Definition at line 158 of file CalcMeanCov.h.

159 {
160 static_assert(i >= 0 && i < N, "index i out of range");
161 static_assert(j >= 0 && j < N, "index j out of range");
162 if (m_entries == 0.0) return 0.0;
163 return m_covariance[getIndex(i, j)] / m_entries;
164 }

◆ getCovariance() [2/2]

value_type getCovariance ( int  i,
int  j 
) const
inline

Return the covariance between parameters i and j.

Definition at line 124 of file CalcMeanCov.h.

125 {
126 if (m_entries == 0.0) return 0.0;
127 return m_covariance[getIndex(i, j)] / m_entries;
128 }

◆ getEntries()

value_type getEntries ( ) const
inline

Return the number of entries.

Definition at line 120 of file CalcMeanCov.h.

120{ return m_entries; }

◆ getIndex()

constexpr int getIndex ( unsigned int  i,
unsigned int  j 
) const
inlineconstexprprivate

Access element in triangular matrix including diagonal elements.

This function returns the storage index of an element (i,j) in a symmetric matrix including diagonal elements if the elements are stored in a continous array of size n(n+1)/2

Definition at line 260 of file CalcMeanCov.h.

261 {
262 //swap indices if i >= j
263 return (i < j) ? ((j + 1) * j / 2 + i) : ((i + 1) * i / 2 + j);
264 }

◆ getMean() [1/2]

value_type getMean ( ) const
inline

Return the mean for parameter i.

Definition at line 152 of file CalcMeanCov.h.

153 {
154 static_assert(i >= 0 && i < N, "index i out of range");
155 return m_mean[i];
156 }

◆ getMean() [2/2]

value_type getMean ( int  i) const
inline

Return the mean for parameter i.

Definition at line 122 of file CalcMeanCov.h.

122{ return m_mean[i]; }

◆ getStddev() [1/2]

value_type getStddev ( ) const
inline

Return the standard deviation for parameter i.

Definition at line 178 of file CalcMeanCov.h.

179 {
180 return std::sqrt(getVariance<i>());
181 }

◆ getStddev() [2/2]

value_type getStddev ( int  i) const
inline

Return the standard deviation for parameter i.

Definition at line 139 of file CalcMeanCov.h.

139{ return std::sqrt(getVariance(i)); }
value_type getVariance() const
Return the variance for paramter i.
Definition: CalcMeanCov.h:173

◆ getSum() [1/2]

value_type getSum ( ) const
inline

Return the weighted sum values for parameter i.

Definition at line 183 of file CalcMeanCov.h.

184 {
185 return getMean<i>() * m_entries;
186 }

◆ getSum() [2/2]

value_type getSum ( int  i) const
inline

Return the weighted sum values for parameter i.

Definition at line 141 of file CalcMeanCov.h.

141{ return getMean(i) * m_entries; }
value_type getMean() const
Return the mean for parameter i.
Definition: CalcMeanCov.h:152

◆ getVariance() [1/2]

value_type getVariance ( ) const
inline

Return the variance for paramter i.

Definition at line 173 of file CalcMeanCov.h.

174 {
175 return getCovariance<i, i>();
176 }

◆ getVariance() [2/2]

value_type getVariance ( int  i) const
inline

Return the variance for paramter i.

Definition at line 137 of file CalcMeanCov.h.

137{ return getCovariance(i, i); }

◆ updateCov() [1/2]

void updateCov ( value_type  weight,
value_type  delta,
value_type  x,
T...  values 
)
inlineprivate

Update covariance between parameters i and j.

See also
addArrayValues
Template Parameters
ifirst index
jsecond index
Parameters
weightweight of the entry
deltadifference between parameter i and mean of i
xvalue of parameter j
valuesremaining values with index >j

Definition at line 222 of file CalcMeanCov.h.

224 {
225 const value_type delta2 = (x - m_mean[j]);
226 m_covariance[getIndex(i, j)] += (m_entries - weight) * weight
227 / m_entries * delta * delta2;
228 updateCov < i, j + 1 > (weight, delta, values...);
229 }

◆ updateCov() [2/2]

void updateCov ( value_type  ,
value_type   
)
inlineprivate

Break recursion of updateCov once all parameters are consumed.

Definition at line 232 of file CalcMeanCov.h.

232{}

Member Data Documentation

◆ m_covariance

value_type m_covariance[N *(N+1)/2]
private

Store the triangular covariance matrix for all parameters in continous memory.

Actual covariance is m_covariance[getIndex(i,j)]/m_entries

Definition at line 272 of file CalcMeanCov.h.

◆ m_entries

value_type m_entries
private

Store the sum of weights.

Definition at line 267 of file CalcMeanCov.h.

◆ m_mean

value_type m_mean[N]
private

Store the mean values for all parameters.

Definition at line 269 of file CalcMeanCov.h.


The documentation for this class was generated from the following file: