在这种情况下,Kolmogorov-Smirnov 统计量可能会对您有所帮助。
下面是一个使用 Kolmogorov-Smirnov 统计的实现,该函数返回相似的概率。
#include <math.h>
#define EPS1 0.001
#define EPS2 1.0e-8
float kstest(float alam) {
int j;
float a2, fac = 2.0, sum = 0.0, term, termbf = 0.0;
a2 = -2.0 * alam * alam;
for (j = 1; j <= 100; j++) {
term = fac * exp(a2 * j * j);
sum += term;
if (fabs(term) <= EPS1 * termbf || fabs(term) <= EPS2 * sum)
return sum;
fac = -fac;
termbf = fabs(term);
}
return 1.0;
}
void checkSameDist(float data1[], unsigned long n1, float data2[],
unsigned long n2, float *d, float *prob) {
float kstest(float alam);
void sort(unsigned long n, float arr[]);
unsigned long j1 = 1,
j2 = 1;
float d1, d2, dt, en1, en2, en, fn1 = 0.0, fn2 = 0.0;
sort(n1, data1);
sort(n2, data2);
en1 = n1;
en2 = n2;
*d = 0.0;
while (j1 <= n1 && j2 <= n2) {
if ((d1 = data1[j1]) <= (d2 = data2[j2]))
fn1 = j1++ / en1;
if (d2 <= d1)
fn2 = j2++ / en2;
if ((dt = fabs(fn2 - fn1)) > *d)
*d = dt;
}
en = sqrt(en1 * en2 / (en1 + en2));
*prob = kstest((en + 0.12 + 0.11 / en) * (*d));
}
还要检查,以下函数检查特定分布是否正常,您可以对其进行一些修改(这将使您对统计数据有更多的直觉以及如何从头开始实现它
(https://walteis.wordpress.com/2012 /04/26/a-kolmogorov-smirnov-implementation/ )
public bool IsNormal
{
get
{
// This method uses the Kolmogorov-Smirnov test to determine a normal distribution.
// The level of significance (alpha) used is .05, and the critical values used are from Table 1 of:
// The Kolmogorov-Smirnov Test for Goodness of Fit
// Frank J. Massey, Jr.
// Journal of the American Statistical Association
// Vol. 46, No. 253 (Mar., 1951) (pp. 68-78)
if (DataSet.Count == 0)
return false;
List<double> vals = DataSet.Values.ToList();
Accumulator acc = new Accumulator(vals.ToArray());
double dmax = double.MinValue;
double cv = 0;
MathNet.Numerics.Distributions.NormalDistribution test = new MathNet.Numerics.Distributions.NormalDistribution(acc.Mean, acc.Sigma);
// the 0 entry is to force the list to be a base 1 index table.
List<double> cvTable = new List<double>() { 0, .975, .842, .708, .624, .565,
.521, .486, .457, .432, .410,
.391, .375, .361, .349, .338,
.328, .318, .309, .301, .294};
test.EstimateDistributionParameters(DataSet.Values.ToArray());
vals.Sort();
for (int i = 0; i < vals.Count; i++)
{
double dr = Math.Abs(((i + 1) / (double)vals.Count) - test.CumulativeDistribution(vals[i]));
double dl = Math.Abs(test.CumulativeDistribution(vals[i]) - (i / (double)vals.Count));
dmax = Math.Max(dmax, Math.Max(dl, dr));
}
// get critical value and compare to d(N)
if (vals.Count <= 10)
cv = cvTable[vals.Count];
else if (vals.Count > 10)
cv = 1.36 / Math.Sqrt(vals.Count);
return (dmax < cv);
}
}
祝你好运