lmoffatt / macro_dr

GNU General Public License v3.0
1 stars 1 forks source link

implement a class for continuous actualization of moments #204

Closed lmoffatt closed 4 months ago

lmoffatt commented 4 months ago

we implement a class for continuous calculation of means and variances with minimal truncation errors

Formula for mean single sample

{ \bar x}_{ n + 1 } ={\bar x_n} +\frac{x-\bar{x_n}}{n+1}

formula for aggregating two samples

{ \bar x} _ { n + m } = \bar x_n+{\frac { m  }{n+m}} \cdot {(\bar x_m - \bar x_n)}

formula for aggregating n samples

{ \bar x} _ { \sum n  } = \sum \frac {n_i}{\sum_i n_i} \cdot  \bar x_{n_i}

formula for actulizing the variance

var _ { n + 1 } =  var _ n +  ( { \bar x _ n - {\bar x} _ { n + 1 } } ) ^ 2
 + \frac{ ( x - {\bar x} _ { n + 1 } ) ^ 2 - var_n} {n}

variance of two samples: (there is an error in this formula)

var_{n+m} = var_n +
{\frac {n}{n+m-1} }\cdot \left ( {\bar x}_n-{\bar x}_{n+m} \right)^2 
+
{\frac {m}{n+m-1} }\cdot \left ( {\bar x}_m-{\bar x}_{n+m} \right)^2 
+
{\frac {m-1}{n+m-1} }\cdot \left (var_m -var_n \right)

variance of n samples:

var_{\sum n_i} =
\sum_i {\frac {n_i}{ \sum n_i -1} }\cdot \left ( {\bar x}_{n_i} - {\bar x}_{\sum n_i} \right)^2 
+
\sum_i {\frac {n_i-1}{\sum n_i-1} }\cdot var_{n_i}