Last active
June 25, 2016 07:52
-
-
Save hide1202/2bb0dc5046c30b6a518269ffcb4fd932 to your computer and use it in GitHub Desktop.
Welford's method(variance and standard deviation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace variance | |
{ | |
public class Variance | |
{ | |
private int _count = 1; | |
private double _mean = 0.0; | |
private double _variance = 0.0; | |
// Caculate Standard deviation | |
public double NewStdev(int newValue) | |
{ | |
double oldMean = _mean; | |
_mean = oldMean + ((newValue - oldMean) / _count); | |
_variance = _variance + ((newValue - _mean) * (newValue - oldMean)); | |
return Math.Sqrt(_variance / (_count++ - 1)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment