Class MovingAverageStatistics

Class Documentation

class MovingAverageStatistics

A class for calculating moving average statistics. This operates in constant memory and constant time. Note: reset() must be called manually in order to start a new measurement window.

The statistics calculated are average, maximum, minimum, and standard deviation (population). All are calculated online without storing the observation data. Specifically, the average is a running sum and the variance is obtained by Welford’s online algorithm (reference: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford%27s_online_algorithm) for standard deviation.

When statistics are not available, e.g. no observations have been made, NaNs are returned.

Public Functions

MovingAverageStatistics() = default
~MovingAverageStatistics() = default
double Average () const RCPPUTILS_TSA_REQUIRES(mutex_)

Returns the arithmetic mean of all data recorded. If no observations have been made, returns NaN.

Returns

The arithmetic mean of all data recorded, or NaN if the sample count is 0.

double Max () const RCPPUTILS_TSA_REQUIRES(mutex_)

Returns the maximum value recorded. If size of list is zero, returns NaN.

Returns

The maximum value recorded, or NaN if size of data is zero.

double Min () const RCPPUTILS_TSA_REQUIRES(mutex_)

Returns the minimum value recorded. If size of list is zero, returns NaN.

Returns

The minimum value recorded, or NaN if size of data is zero.

double StandardDeviation () const RCPPUTILS_TSA_REQUIRES(mutex_)

Returns the standard deviation (population) of all data recorded. If size of list is zero, returns NaN.

Variance is obtained by Welford’s online algorithm, see https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford%27s_online_algorithm

Returns

The standard deviation (population) of all data recorded, or NaN if size of data is zero.

StatisticData GetStatistics() const

Return a StatisticData object, containing average, minimum, maximum, standard deviation (population), and sample count. For the case of no observations, the average, min, max, and standard deviation are NaN.

Returns

StatisticData object, containing average, minimum, maximum, standard deviation (population), and sample count.

void Reset()

Reset all calculated values. Equivalent to a new window for a moving average.

virtual void AddMeasurement(const double item)

Observe a sample for the given window. The input item is used to calculate statistics. Note: any input values of NaN will be discarded and not added as a measurement.

Parameters

item – The item that was observed

uint64_t GetCount() const

Return the number of samples observed

Returns

the number of samples observed