Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Algorithms.Bandits {
|
---|
9 | class OnlineMeanAndVarianceEstimator {
|
---|
10 | public int N { get; private set; }
|
---|
11 | public double Sum { get; private set; }
|
---|
12 | public double Avg { get; private set; }
|
---|
13 | public double Variance { get { return sampleM2 / N; } }
|
---|
14 | private double sampleM2;
|
---|
15 |
|
---|
16 | public void UpdateReward(double reward) {
|
---|
17 | N++;
|
---|
18 | Sum += reward;
|
---|
19 |
|
---|
20 | var delta = reward - Avg;
|
---|
21 | Avg += delta / N;
|
---|
22 | sampleM2 += delta * (reward - Avg);
|
---|
23 | }
|
---|
24 |
|
---|
25 | public void Reset() {
|
---|
26 | Sum = 0.0;
|
---|
27 | Avg = 0.0;
|
---|
28 | N = 0;
|
---|
29 | sampleM2 = 0.0;
|
---|
30 | }
|
---|
31 | }
|
---|
32 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.