Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlsta_moment
[Package bdlsta]

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

Namespaces

namespace  bdlsta

Detailed Description

Outline
Purpose:
Online algorithm for mean, variance, skew, and kurtosis.
Classes:
bdlsta::Moment online calculation of mean, variance, skew, and kurtosis
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 };

  bdlsta::Moment<bdlsta::MomentLevel::e_M3> m3;
Then, we invoke the add routine to accumulate the data:
  for(int i = 0; i < 4; ++i) {
      m3.add(input[i]);
  }
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()));