blob: 1cf7fcbacf6f0273ef70599def572034b4c00473 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include "stats.h"
void stats_running_reset(struct stats_running *s)
{
if( s ) {
s->n = 0;
s->S = NAN;
s->m = NAN;
}
}
void stats_running_push(struct stats_running *s, double value)
{
if( s && isfinite(value) ) {
double const m_prev = 0 < s->n ? s->m : 0.;
double const S_prev = 1 < s->n ? s->S : 0.;
unsigned const n = (s->n += 1);
s->m = m_prev + (value - m_prev) / n;
if( 1 < n ) {
/* variance is defined only for n > 1 */
s->S = S_prev + (value - s->m) * (value - m_prev);
}
}
}
|