-
Notifications
You must be signed in to change notification settings - Fork 0
/
onlinemean.c
32 lines (27 loc) · 634 Bytes
/
onlinemean.c
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
26
27
28
29
30
31
32
/*
* Welford's Online algorithm to estimate population mean and variance.
*
* Created on: Feb 6, 2019
* Author: Danylo Ulianych
*/
#include <math.h>
#include "onlinemean.h"
void OnlineMean_Init(OnlineMean *oMean) {
OnlineMean_Reset(oMean);
}
void OnlineMean_Update(OnlineMean *oMean, float newValue) {
oMean->count++;
if (oMean->count > 1) {
float delta = newValue - oMean->mean;
oMean->mean += delta / oMean->count;
} else {
oMean->mean = newValue;
}
}
float OnlineMean_GetMean(OnlineMean *oMean) {
return oMean->mean;
}
void OnlineMean_Reset(OnlineMean *oMean) {
oMean->count = 0;
oMean->mean = 0.f;
}