BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlsta_moment

Detailed Description

Outline

Purpose

Online algorithm for mean, variance, skew, and kurtosis.

Classes

Description

This component provides a mechanism, bdlsta::Moment, that provides online calculation of basic statistics: mean, variance, skew, and kurtosis while maintaining accuracy. Online algorithms process the data in one pass, while keeping good accuracy. The online algorithms used are Welford for variance, and the stable skew and kurtosis algorithms taken from: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Higher-order_statistics

The implementation uses template specialization so the user can choose the statistics necessary, and not calculate or allocate memory for those statistics that are not needed.

The template parameter is a value from the provided enum and having the following interpretation:

M1 - mean
M2 - variance+mean
M3 - skew+variance+mean
M4 - kurtosis+skew+variance+mean

Usage

This section illustrates intended use of this component.

Example 1: Calculating skew, variance, and mean

This example shows how to accumulate a set of values, and calculate the skew, variance, and kurtosis.

First, we create example input and instantiate the appropriate mechanism:

double input[] = { 1.0, 2.0, 4.0, 5.0 };
Definition bdlsta_moment.h:222

Then, we invoke the add routine to accumulate the data:

for(int i = 0; i < 4; ++i) {
m3.add(input[i]);
}
void add(double value)
Add the specified value to the data set.

Finally, we assert that the mean, variance, and skew are what we expect:

ASSERT(4 == m3.count());
ASSERT(3.0 == m3.mean());
ASSERT(1e-5 > fabs(3.33333 - m3.variance()));
ASSERT(1e-5 > fabs(0.0 - m3.skew()));
double variance() const
Definition bdlsta_moment.h:475
double skew() const
Definition bdlsta_moment.h:454
int count() const
Returns the number of elements in the data set.
Definition bdlsta_moment.h:403
double mean() const
Definition bdlsta_moment.h:434