Skip to content

bolderflight/statistics

Repository files navigation

License: MIT

Bolder Flight Systems Logo     Arduino Logo

Statistics

This library contains statistics functions and classes including streaming and moving window estimates of mean, variance, and standard deviation. This library is compatible with Arduino ARM and with CMake build systems. It would also be easy to include with other projects, since it is a header only library consisting of a single file.

Installation

Arduino

Use the Arduino Library Manager to install this library or clone to your Arduino/libraries folder. Additionally, the Bolder Flight Systems Circular Buffer library must be installed. This library is added as:

#include "statistics.h"

An example Arduino executable is located at examples/arduino/stats_example/stats_example.ino. Teensy 3.x, 4.x, and LC devices are used for testing under Arduino and this library should be compatible with other ARM devices. This library is not expected to work on AVR devices.

CMake

CMake is used to build this library, which is exported as a library target called statistics. The header is added as:

#include "statistics.h"

The library can be also be compiled stand-alone using the CMake idiom of creating a build directory and then, from within that directory issuing:

cmake ..
make

This will build the library, an example executable called stats_example, and an executable for testing using the Google Test framework, called stats_test. The example executable source file is located at examples/cmake/stats_example.cc.

Namespace

This library is within the namespace bfs.

RunningStats

This class implements Welford's method for streaming estimation of mean, variance, and standard deviation. The class is templated to support floating point types.

Methods

RunningStats Creates a RunningStats object operating on type T and initializes the estimator states.

bfs::RunningStats<float> stats;

void Update(T x) Accumulates data into the estimator.

for (unsigned int i = 0; i < 10; i++) {
  stats.Update((float) i);
}

T mean() Returns the current estimate of the mean of the accumulated data.

std::cout << stats.mean() << std::endl; // 4.5

T var() Returns the current estimate of the variance of the accumulated data.

std::cout << stats.var() << std::endl; // 9.1667

T std() Returns the current estimate of the standard deviation of the accumulated data.

std::cout << stats.std() << std::endl; // 3.0277

void Clear() Resets the estimator states.

stats.Clear();

MovingWindowStats

This class implements a modification of Welford's method for a moving window estimation of mean, variance, and standard deviation. The class is templated to support floating point types and window sizes. Estimates are always performed looking back from the current value.

Methods

MovingWindowStats<typename T, std::size_t N> Creates a MovingWindowStats object operating on type T with window length N and initializes the estimator states.

/* A moving window estimator operating on floats with a window size of 250 values */
bfs::MovingWindowStats<float, 250> stats;

void Update(T x) Accumulates data into the estimator, slides the window after the window is filled.

stats.Update(val);

T mean() Returns the current estimate of the mean of the windowed data.

std::cout << stats.mean() << std::endl;

T var() Returns the current estimate of the variance of the windowed data.

std::cout << stats.var() << std::endl;

T std() Returns the current estimate of the standard deviation of the windowed data.

std::cout << stats.std() << std::endl;

void Clear() Resets the estimator states.

stats.Clear();