# Online Variance

Let $n$ be the number of values, ${v}_{n}$ be the biased sample variance of the first $n$ values, ${v}_{n-1}$ be the biased sample variance for the first $n-1$ values, ${x}_{n}$ be the $n$ -th value, ${\overline{x}}_{n}$ be the sample mean of the first $n$ values, and ${\overline{x}}_{n-1}$ be the sample mean of the first $n-1$ values. Then, the recurrence equation for the biased sample variance (a.k.a. online variance) is:

${v}_{n}={v}_{n-1}-\frac{{v}_{n-1}-\left({x}_{n}-{\overline{x}}_{n}\right)\left({x}_{n}-{\overline{x}}_{n-1}\right)}{n}$

Proof:

The definition of the biased sample variance of the first $n$ values is defined as:

${v}_{n}=\frac{\sum _{i=1}^{n}{\left({x}_{i}-{\overline{x}}_{n}\right)}^{2}}{n}$

If we expand this definition, we have:

$\begin{array}{c}{v}_{n}=\frac{\sum _{i=1}^{n}\left({x}_{i}^{2}-2{x}_{i}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}\right)}{n}\\ {v}_{n}=\frac{\sum _{i=1}^{n-1}\left({x}_{i}^{2}-2{x}_{i}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\end{array}$

Since the recurrence equation for the sample mean is:

${\overline{x}}_{n}={\overline{x}}_{n-1}-\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}$ ,

then we also have:

$\begin{array}{c}{\overline{x}}_{n}^{2}={\left({\overline{x}}_{n-1}-\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)}^{2}\\ {\overline{x}}_{n}^{2}={\overline{x}}_{n-1}^{2}-2{\overline{x}}_{n-1}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+{\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)}^{2}\end{array}$

With these, we have:

$\begin{array}{c}{v}_{n}=\frac{\sum _{i=1}^{n-1}\left({x}_{i}^{2}-2{x}_{i}\left({\overline{x}}_{n-1}-\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+\left({\overline{x}}_{n-1}^{2}-2{\overline{x}}_{n-1}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+{\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)}^{2}\right)\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\sum _{i=1}^{n-1}\left({x}_{i}^{2}-2{x}_{i}{\overline{x}}_{n-1}+2{x}_{i}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+{\overline{x}}_{n-1}^{2}-2{\overline{x}}_{n-1}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+{\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)}^{2}\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\sum _{i=1}^{n-1}\left({x}_{i}^{2}-2{x}_{i}{\overline{x}}_{n-1}+{\overline{x}}_{n-1}^{2}+2{x}_{i}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)-2{\overline{x}}_{n-1}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+{\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)}^{2}\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\sum _{i=1}^{n-1}\left({\left({x}_{i}-{\overline{x}}_{n-1}\right)}^{2}+2{x}_{i}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)-2{\overline{x}}_{n-1}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+{\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)}^{2}\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\sum _{i=1}^{n-1}{\left({x}_{i}-{\overline{x}}_{n-1}\right)}^{2}+\sum _{i=1}^{n-1}\left(2{x}_{i}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)-2{\overline{x}}_{n-1}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+{\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)}^{2}\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\end{array}$

Since the biased sample variance for the first $n-1$ values is:

${v}_{n-1}=\frac{\sum _{i=1}^{n-1}{\left({x}_{i}-{\overline{x}}_{n-1}\right)}^{2}}{n-1}$ ,

then we also have:

$\sum _{i=1}^{n-1}{\left({x}_{i}-{\overline{x}}_{n-1}\right)}^{2}=\left(n-1\right){v}_{n-1}$ .

With this, we have:

$\begin{array}{c}{v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\sum _{i=1}^{n-1}\left(2{x}_{i}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)-2{\overline{x}}_{n-1}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)+{\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)}^{2}\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(\sum _{i=1}^{n-1}\left(2{x}_{i}-2{\overline{x}}_{n-1}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\right)\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(\sum _{i=1}^{n-1}\left(2{x}_{i}\right)+\sum _{i=1}^{n-1}\left(-2{\overline{x}}_{n-1}\right)+\sum _{i=1}^{n-1}\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(2\sum _{i=1}^{n-1}\left({x}_{i}\right)-2{\overline{x}}_{n-1}\sum _{i=1}^{n-1}\left(1\right)+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\sum _{i=1}^{n-1}\left(1\right)\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(2\sum _{i=1}^{n-1}\left({x}_{i}\right)-2{\overline{x}}_{n-1}\left(n-1\right)+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(n-1\right)\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\end{array}$

Since the definition of the sample mean for the first $n-1$ values is:

${\overline{x}}_{n-1}=\frac{\sum _{i=1}^{n-1}{x}_{i}}{n-1}$ ,

then we also have:

$\sum _{i=1}^{n-1}{x}_{i}=\left(n-1\right){\overline{x}}_{n-1}$.

With this, we have:

$\begin{array}{c}{v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(2\left(n-1\right){\overline{x}}_{n-1}-2{\overline{x}}_{n-1}\left(n-1\right)+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(n-1\right)\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(\overline{)2\left(n-1\right){\overline{x}}_{n-1}}\overline{)-2\left(n-1\right){\overline{x}}_{n-1}}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(n-1\right)\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}\right)\left(n-1\right)+{x}_{n}^{2}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\left(n-1\right)\left(\frac{{\overline{x}}_{n-1}^{2}-2{x}_{n}{\overline{x}}_{n-1}+{x}_{n}^{2}}{{n}^{2}}\right)-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{{n}^{2}}-\frac{2\left(n-1\right){x}_{n}{\overline{x}}_{n-1}}{{n}^{2}}+\frac{\left(n-1\right){x}_{n}^{2}}{{n}^{2}}-2{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}^{2}}{n}\end{array}$

Since the recurrence equation for the sample mean is:

${\overline{x}}_{n}={\overline{x}}_{n-1}-\frac{{\overline{x}}_{n-1}-{x}_{n}}{n}$,

then we also have:

$\begin{array}{c}{\overline{x}}_{n}=\frac{n{\overline{x}}_{n-1}-{\overline{x}}_{n-1}+{x}_{n}}{n}\\ {\overline{x}}_{n}=\frac{\left(n-1\right){\overline{x}}_{n-1}+{x}_{n}}{n}\end{array}$

Moreover, we have:

$\begin{array}{c}{\overline{x}}_{n}^{2}={\left(\frac{\left(n-1\right){\overline{x}}_{n-1}+{x}_{n}}{n}\right)}^{2}\\ {\overline{x}}_{n}^{2}=\frac{{\left(\left(n-1\right){\overline{x}}_{n-1}+{x}_{n}\right)}^{2}}{{n}^{2}}\\ {\overline{x}}_{n}^{2}=\frac{{\left(n-1\right)}^{2}{\overline{x}}_{n-1}^{2}+2\left(n-1\right){x}_{n}{\overline{x}}_{n-1}+{x}_{n}^{2}}{{n}^{2}}\\ {\overline{x}}_{n}^{2}=\frac{{\left(n-1\right)}^{2}{\overline{x}}_{n-1}^{2}}{{n}^{2}}+\frac{2\left(n-1\right){x}_{n}{\overline{x}}_{n-1}}{{n}^{2}}+\frac{{x}_{n}^{2}}{{n}^{2}}\end{array}$

With this, we have:

$\begin{array}{c}{v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{{n}^{2}}-\frac{2\left(n-1\right){x}_{n}{\overline{x}}_{n-1}}{{n}^{2}}+\frac{\left(n-1\right){x}_{n}^{2}}{{n}^{2}}-2{x}_{n}{\overline{x}}_{n}+\left(\frac{{\left(n-1\right)}^{2}{\overline{x}}_{n-1}^{2}}{{n}^{2}}+\frac{2\left(n-1\right){x}_{n}{\overline{x}}_{n-1}}{{n}^{2}}+\frac{{x}_{n}^{2}}{{n}^{2}}\right)}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{{n}^{2}}\overline{)-\frac{2\left(n-1\right){x}_{n}{\overline{x}}_{n-1}}{{n}^{2}}}+\frac{\left(n-1\right){x}_{n}^{2}}{{n}^{2}}-2{x}_{n}{\overline{x}}_{n}+\frac{{\left(n-1\right)}^{2}{\overline{x}}_{n-1}^{2}}{{n}^{2}}\overline{)+\frac{2\left(n-1\right){x}_{n}{\overline{x}}_{n-1}}{{n}^{2}}}+\frac{{x}_{n}^{2}}{{n}^{2}}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\frac{{\left(n-1\right)}^{2}{\overline{x}}_{n-1}^{2}}{{n}^{2}}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{{n}^{2}}+\frac{\left(n-1\right){x}_{n}^{2}}{{n}^{2}}+\frac{{x}_{n}^{2}}{{n}^{2}}-2{x}_{n}{\overline{x}}_{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\left({\left(n-1\right)}^{2}+\left(n-1\right)\right)\left(\frac{{\overline{x}}_{n-1}^{2}}{{n}^{2}}\right)+\left(\left(n-1\right)+1\right)\left(\frac{{x}_{n}^{2}}{{n}^{2}}\right)-2{x}_{n}{\overline{x}}_{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\left(\left(n-1\right)\left(\left(n-1\right)+1\right)\right)\left(\frac{{\overline{x}}_{n-1}^{2}}{{n}^{2}}\right)+\left(n\right)\left(\frac{{x}_{n}^{2}}{{n}^{2}}\right)-2{x}_{n}{\overline{x}}_{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\left(\left(n-1\right)\left(n\right)\right)\left(\frac{{\overline{x}}_{n-1}^{2}}{{n}^{2}}\right)+\left(n\right)\left(\frac{{x}_{n}^{2}}{{n}^{2}}\right)-2{x}_{n}{\overline{x}}_{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}+\left(n-1\right)\left(\frac{{\overline{x}}_{n-1}^{2}}{n}\right)+\frac{{x}_{n}^{2}}{n}-2{x}_{n}{\overline{x}}_{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{n}+\frac{{x}_{n}^{2}}{n}-{x}_{n}{\overline{x}}_{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{n}-\frac{n{x}_{n}{\overline{x}}_{n}}{n}+\frac{{x}_{n}^{2}}{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{n}-\frac{n{x}_{n}{\overline{x}}_{n}-{x}_{n}^{2}}{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{n}-\frac{\left(n{\overline{x}}_{n}-{x}_{n}\right){x}_{n}}{n}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{n}-\left(\frac{n-1}{n-1}\right)\left(\frac{\left(n{\overline{x}}_{n}-{x}_{n}\right){x}_{n}}{n}\right)}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{n}-\left(\frac{\left(n-1\right){x}_{n}}{n}\right)\left(\frac{n{\overline{x}}_{n}-{x}_{n}}{n-1}\right)}{n}\end{array}$

As previously noted, the recurrence equation for the sample mean can be rewritten as:

${\overline{x}}_{n}=\frac{\left(n-1\right){\overline{x}}_{n-1}+{x}_{n}}{n}$,

then we have:

$\begin{array}{c}\frac{\left(n-1\right){\overline{x}}_{n-1}+{x}_{n}}{n}={\overline{x}}_{n}\\ \left(n-1\right){\overline{x}}_{n-1}+{x}_{n}=n{\overline{x}}_{n}\\ \left(n-1\right){\overline{x}}_{n-1}=n{\overline{x}}_{n}-{x}_{n}\\ {\overline{x}}_{n-1}=\frac{n{\overline{x}}_{n}-{x}_{n}}{n-1}\end{array}$

With this, we have:

$\begin{array}{c}{v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\frac{\left(n-1\right){\overline{x}}_{n-1}^{2}}{n}-\left(\frac{\left(n-1\right){x}_{n}}{n}\right)\left({\overline{x}}_{n-1}\right)}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\left(\frac{\left(n-1\right){\overline{x}}_{n-1}}{n}-\left(\frac{n{x}_{n}-{x}_{n}}{n}\right)\right)\left({\overline{x}}_{n-1}\right)}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\left(\frac{\left(n-1\right){\overline{x}}_{n-1}}{n}-\frac{\overline{)n}{x}_{n}}{\overline{)n}}+\frac{{x}_{n}}{n}\right)\left({\overline{x}}_{n-1}\right)}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\left(\left(\frac{\left(n-1\right){\overline{x}}_{n-1}+{x}_{n}}{n}\right)-{x}_{n}\right)\left({\overline{x}}_{n-1}\right)}{n}\end{array}$

Since the recurrence equation of the sample mean can be rewritten as:

${\overline{x}}_{n}=\frac{\left(n-1\right){\overline{x}}_{n-1}+{x}_{n}}{n}$,

then we have:

$\begin{array}{c}{v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+\left({\overline{x}}_{n}-{x}_{n}\right)\left({\overline{x}}_{n-1}\right)}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}+{\overline{x}}_{n}{\overline{x}}_{n-1}-{x}_{n}{\overline{x}}_{n-1}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+{x}_{n}^{2}-{x}_{n}{\overline{x}}_{n}-{x}_{n}{\overline{x}}_{n-1}+{\overline{x}}_{n}{\overline{x}}_{n-1}}{n}\\ {v}_{n}=\frac{\left(n-1\right){v}_{n-1}+\left({x}_{n}-{\overline{x}}_{n}\right)\left({x}_{n}-{\overline{x}}_{n-1}\right)}{n}\\ {v}_{n}=\frac{n{v}_{n-1}-{v}_{n-1}+\left({x}_{n}-{\overline{x}}_{n}\right)\left({x}_{n}-{\overline{x}}_{n-1}\right)}{n}\\ {v}_{n}=\frac{\overline{)n}{v}_{n-1}}{\overline{)n}}+\frac{-{v}_{n-1}+\left({x}_{n}-{\overline{x}}_{n}\right)\left({x}_{n}-{\overline{x}}_{n-1}\right)}{n}\\ {v}_{n}={v}_{n-1}-\frac{{v}_{n-1}-\left({x}_{n}-{\overline{x}}_{n}\right)\left({x}_{n}-{\overline{x}}_{n-1}\right)}{n}\end{array}$

Therefore, the recurrence equation for the biased sample variance (a.k.a. online variance) is:

${v}_{n}={v}_{n-1}-\frac{{v}_{n-1}-\left({x}_{n}-{\overline{x}}_{n}\right)\left({x}_{n}-{\overline{x}}_{n-1}\right)}{n}$

Example C++ code that computes the online variance:

// Filename: main.cpp
#include <iostream>
#include <iomanip>

int main() {

double x;
double n = 0;
double mean = 0;
double variance = 0;
double prev_mean; // previous mean
double prev_variance; // previous variance

if ( std::cin >> x ) {
++n;
mean = x;
variance = 0;
while ( std::cin >> x ) {
prev_mean = mean;
prev_variance = variance;
++n;
mean = prev_mean - ( prev_mean - x ) / n;
variance = prev_variance - ( prev_variance - ( x - mean ) * ( x - prev_mean ) ) / n;
}
}

std::cout << "n:        " << n << '\n';
std::cout << "mean:     " << std::setprecision( 17 ) << mean << '\n';
std::cout << "variance: " << std::setprecision( 17 ) << variance << '\n';

}

Example of data.txt:

-281.189
974.663
25.8526
.
.
.

Command Line:

g++ -o main.exe main.cpp -std=c++11 -march=native -O3 -Wall -Wextra -Werror -static
./main.exe < data.txt

Note: Mathematica’s Variance[] function computes the unbiased sample variance, not the biased sample variance; therefore, the biased sample variance is computed in Mathematica as:

( ( Length[ list ] – 1 ) / Length[ list ] ) * Variance[ list ]

Tagged on:

### One thought on “Online Variance”

1. Joshua Burkholder Post author

This is also derived from the following:

This site uses Akismet to reduce spam. Learn how your comment data is processed.