需要引用Nuget包 MathNet.Numerics
公式
Minitab 合并标准差公式说明
https://support.minitab.com/zh-cn/minitab/18/help-and-how-to/quality-and-process-improvement/control-charts/how-to/variables-charts-for-subgroups/i-mr-r-s-chart/methods-and-formulas/estimating-sigma-for-the-r-chart-or-the-s-chart/#pooled-standard-deviation-method
代码
using MathNet.Numerics.Statistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Spc.Core
{
/// <summary>
/// 合并标准差方法
/// </summary>
public class PooledEstimateSigma : IEstimateSigma
{
public double GetSigma(double[] vs, bool useUnbiasedConstant = false, int sampleSize = 0, int movingRangeLength = 2)
{
var sp = 0d;
var n = 0;
if (sampleSize <= 0)
{
n = 2;
}
else
{
n = sampleSize;
}
sp = vs.Average();
if (useUnbiasedConstant)
{
var d = vs.Sum(x => n - 1);
var c4 = SigmaConstants.C4AddOne(d);
sp = sp.Div(c4);
}
return sp;
}
public double GetSigma(double[][] vs, bool useUnbiasedConstant = false, int sampleSize = 0, int movingRangeLength = 2)
{
var sp = 0d;
var counts = vs.Select(m => m.Length).ToArray();
if (counts.Min() != counts.Max())
{
var sn = vs.Sum(x => Math.Pow(x.Sum(x1 => x1.Sub(x.Average())), 2));
var ni = vs.Sum(x => x.Length - 1);
sp = Math.Sqrt(sn.Div(ni));
}
else
{
sp = vs.Average(x => Statistics.StandardDeviation(x));
}
if (useUnbiasedConstant)
{
var d = vs.Sum(x => x.Length - 1);
var c4 = SigmaConstants.C4(d + 1);
sp = sp.Div(c4);
}
return sp;
}
}
}