Skip to content

Instantly share code, notes, and snippets.

@hide1202
Last active June 25, 2016 07:52
Show Gist options
  • Save hide1202/2bb0dc5046c30b6a518269ffcb4fd932 to your computer and use it in GitHub Desktop.
Save hide1202/2bb0dc5046c30b6a518269ffcb4fd932 to your computer and use it in GitHub Desktop.
Welford's method(variance and standard deviation)
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