Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Common/OnlineMeanAndVarianceEstimator.cs @ 13475

Last change on this file since 13475 was 12290, checked in by gkronber, 10 years ago

#2283 created a new branch to separate development from aballeit

File size: 878 bytes
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace HeuristicLab.Algorithms.Bandits {
9  public 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      // double alpha = 0.01;
23      double alpha = 1.0 / N;
24      Avg = Avg + alpha * (delta);
25      sampleM2 += delta * (reward - Avg);
26    }
27
28    public void Reset() {
29      Sum = 0.0;
30      Avg = 0.0;
31      N = 0;
32      sampleM2 = 0.0;
33    }
34  }
35}
Note: See TracBrowser for help on using the repository browser.