public void TwoSampleKS2() { int n = 2 * 3 * 3; int m = 2 * 2 * 3; Random rng = new Random(0); NormalDistribution d = new NormalDistribution(); Histogram h = new Histogram((int) AdvancedIntegerMath.LCM(n, m) + 1); //int[] h = new int[AdvancedIntegerMath.LCM(n, m) + 1]; int count = 1000; for (int i = 0; i < count; i++) { Sample A = new Sample(); for (int j = 0; j < n; j++) A.Add(d.GetRandomValue(rng)); Sample B = new Sample(); for (int j = 0; j < m; j++) B.Add(d.GetRandomValue(rng)); TestResult r = Sample.KolmogorovSmirnovTest(A, B); int k = (int) Math.Round(r.Statistic * AdvancedIntegerMath.LCM(n, m)); //Console.WriteLine("{0} {1}", r.Statistic, k); h[k].Increment(); //h[k] = h[k] + 1; } KolmogorovTwoSampleExactDistribution ks = new KolmogorovTwoSampleExactDistribution(n, m); double chi2 = 0.0; int dof = 0; for (int i = 0; i < h.Count; i++) { double ne = ks.ProbabilityMass(i) * count; Console.WriteLine("{0} {1} {2}", i, h[i].Counts, ne); if (ne > 4) { chi2 += MoreMath.Sqr(h[i].Counts - ne) / ne; dof++; } } Console.WriteLine("chi^2={0} dof={1}", chi2, dof); TestResult r2 = h.ChiSquaredTest(ks); ChiSquaredDistribution rd = (ChiSquaredDistribution) r2.Distribution; Console.WriteLine("chi^2={0} dof={1} P={2}", r2.Statistic, rd.DegreesOfFreedom, r2.RightProbability); }
public void TwoSampleKS() { int n = 2 * 3 * 3 * 2; int m = 2 * 2 * 3; int lcm = (int) AdvancedIntegerMath.LCM(n, m); int gcf = (int) AdvancedIntegerMath.GCF(n, m); Console.WriteLine("{0} {1} {2} {3}", n, m, lcm, gcf); KolmogorovTwoSampleExactDistribution d0 = new KolmogorovTwoSampleExactDistribution(n, m); for (int k = d0.Minimum; k <= d0.Maximum; k++) { Console.WriteLine("{0} {1} {2}", k, d0.LatticePathSum(k * gcf), d0.LatticePathSum(k * gcf - 1)); } //for (int c = 1; c <= n; c++) { // Console.WriteLine("{0} {1}", c, EqualKS(n, c)); //} }