using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HeuristicLab.Algorithms.Bandits { public class OnlineMeanAndVarianceEstimator { public int N { get; private set; } public double Sum { get; private set; } public double Avg { get; private set; } public double Variance { get { return sampleM2 / N; } } private double sampleM2; public void UpdateReward(double reward) { N++; Sum += reward; var delta = reward - Avg; //Avg += delta / N; // double alpha = 0.01; double alpha = 1.0 / N; Avg = Avg + alpha * (delta); sampleM2 += delta * (reward - Avg); } public void Reset() { Sum = 0.0; Avg = 0.0; N = 0; sampleM2 = 0.0; } } }