Belle II Software  release-08-01-10
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. More...
 
template<class... T>
void add (T... values)
 Update mean and covariance by adding a new entry. More...
 
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. More...
 
void addArray (value_type *values)
 Update mean and covariance by adding a new entry. More...
 
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. More...
 
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. More...
 
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. More...
 
constexpr int getIndex (unsigned int i, unsigned int j) const
 Access element in triangular matrix including diagonal elements. More...
 

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. More...
 

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 Function Documentation

◆ add()

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.

◆ 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.

◆ addValue()

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.

◆ 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.

◆ 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.

◆ 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.

◆ updateCov()

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.

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.


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