18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
WelfordsVariance.cs 1.42 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2023-12-31 09:48 . Switch to file-scoped namespaces (#431)
namespace Algorithms.Other;
/// <summary>Implementation of Welford's variance algorithm.
/// </summary>
public class WelfordsVariance
{
/// <summary>
/// Mean accumulates the mean of the entire dataset,
/// m2 aggregates the squared distance from the mean,
/// count aggregates the number of samples seen so far.
/// </summary>
private int count;
public double Count => count;
private double mean;
public double Mean => count > 1 ? mean : double.NaN;
private double m2;
public double Variance => count > 1 ? m2 / count : double.NaN;
public double SampleVariance => count > 1 ? m2 / (count - 1) : double.NaN;
public WelfordsVariance()
{
count = 0;
mean = 0;
}
public WelfordsVariance(double[] values)
{
count = 0;
mean = 0;
AddRange(values);
}
public void AddValue(double newValue)
{
count++;
AddValueToDataset(newValue);
}
public void AddRange(double[] values)
{
var length = values.Length;
for (var i = 1; i <= length; i++)
{
count++;
AddValueToDataset(values[i - 1]);
}
}
private void AddValueToDataset(double newValue)
{
var delta1 = newValue - mean;
var newMean = mean + delta1 / count;
var delta2 = newValue - newMean;
m2 += delta1 * delta2;
mean = newMean;
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助