pages tagged probability http://meng6net.localhost/tag/probability/ <p><small>Copyright © 2005-2020 by <code>Meng Lu &lt;lumeng3@gmail.com&gt;</code></small></p> Meng Lu's home page ikiwiki Fri, 09 Oct 2015 23:34:58 +0000 Compute sample variance of data stream http://meng6net.localhost/blog/compute_sample_variance_of_data_stream/ http://meng6net.localhost/blog/compute_sample_variance_of_data_stream/ alogrithm data stream mathematics note probability statistics study Tue, 06 Oct 2015 19:39:59 +0000 2015-10-09T23:34:58Z <p>Sample variance of a data stream $\left \{ X_1, X_2, \cdots, X_N, X_{N+1}, \cdots \right \}$ can be computed without saving the data points individually, but only sample mean and sample variance values for the current sample size $N$:</p> <p>$$ \begin{align} s<sup>2</sup> <em>N &amp;= \frac{1}{N} \sum</em> {i=1}^N \left( X <em>i - \bar{X}</em> N \right)<sup>2</sup> \\ &amp;= \frac{1}{N} \sum <em>{i=1}^N \left( X</em> i<sup>2</sup> - \bar{X} _N<sup>2</sup> \right)~, \end{align} $$</p> <p>where</p> <p>$$ \begin{equation} {\bar{X}} <em>N = \frac{1}{N} \sum</em> {i=1}^N X _i~. \end{equation} $$</p> <p>Store $N$, $\bar{X} <em>N$, and $s</em> N<sup>2</sup>$. When datum $X _{N+1}$ is obtained, the values can be updated as the following:</p> <p>$$ \begin{equation} \bar{X} <em>{N+1} = \frac{1}{N+1} \left( N \bar{X}</em> N + X _{N+1} \right)~, \end{equation} $$</p> <p>$$ \begin{align} s<sup>2</sup> <em>{N+1} &amp;= \frac{1}{N+1} \sum</em> {i=1}^{N+1} \left( X <em>i<sup>2</sup> - \bar{X}</em> {N+1}^2\right) \\ &amp;= \frac{1}{N+1} \sum <em>{i=1}^{N+1}X</em> i<sup>2</sup> - \bar{X} <em>{N+1}^2 \\ &amp;= \frac{1}{N+1} \left(N s</em> N<sup>2</sup> + \bar{X} <em>N<sup>2</sup> + X</em> {N+1}^2 \right)- \bar{X} _{N+1}^2~. \end{align} $$</p> /blog/compute_sample_variance_of_data_stream/#comments