public void PearsonRDistribution() { Random rng = new Random(1); // pick some underlying distributions for the sample variables, which must be normal but can have any parameters NormalDistribution xDistribution = new NormalDistribution(1, 2); NormalDistribution yDistribution = new NormalDistribution(3, 4); // try this for several sample sizes, all low so that we see the difference from the normal distribution // n = 3 maxima at ends; n = 4 uniform; n = 5 semi-circular "mound"; n = 6 parabolic "mound" foreach (int n in new int[] { 3, 4, 5, 6, 8 }) { Console.WriteLine("n={0}", n); // find r values Sample rSample = new Sample(); for (int i = 0; i < 100; i++) { // to get each r value, construct a bivariate sample of the given size with no cross-correlation BivariateSample xySample = new BivariateSample(); for (int j = 0; j < n; j++) { xySample.Add(xDistribution.GetRandomValue(rng), yDistribution.GetRandomValue(rng)); } double r = xySample.PearsonRTest().Statistic; rSample.Add(r); } // check whether r is distributed as expected TestResult result = rSample.KolmogorovSmirnovTest(new PearsonRDistribution(n)); Console.WriteLine("P={0}", result.LeftProbability); Assert.IsTrue(result.LeftProbability < 0.95); } }
public void BivariateNullAssociation() { Random rng = new Random(314159265); // Create sample sets for our three test statisics Sample PS = new Sample(); Sample SS = new Sample(); Sample KS = new Sample(); // variables to hold the claimed distribution of teach test statistic ContinuousDistribution PD = null; ContinuousDistribution SD = null; ContinuousDistribution KD = null; // generate a large number of bivariate samples and conduct our three tests on each for (int j = 0; j < 100; j++) { BivariateSample S = new BivariateSample(); // sample size should be large so that asymptotic assumptions are justified for (int i = 0; i < 100; i++) { double x = rng.NextDouble(); double y = rng.NextDouble(); S.Add(x, y); } TestResult PR = S.PearsonRTest(); PS.Add(PR.Statistic); PD = PR.Distribution; TestResult SR = S.SpearmanRhoTest(); SS.Add(SR.Statistic); SD = SR.Distribution; TestResult KR = S.KendallTauTest(); KS.Add(KR.Statistic); KD = KR.Distribution; } // do KS to test whether the samples follow the claimed distributions //Console.WriteLine(PS.KolmogorovSmirnovTest(PD).LeftProbability); //Console.WriteLine(SS.KolmogorovSmirnovTest(SD).LeftProbability); //Console.WriteLine(KS.KolmogorovSmirnovTest(KD).LeftProbability); Assert.IsTrue(PS.KolmogorovSmirnovTest(PD).LeftProbability < 0.95); Assert.IsTrue(SS.KolmogorovSmirnovTest(SD).LeftProbability < 0.95); Assert.IsTrue(KS.KolmogorovSmirnovTest(KD).LeftProbability < 0.95); }
public void PearsonRNullDistribution() { Random rng = new Random(1111111); // Pick some underlying distributions for the sample variables, // which must be normal but can have any parameters. NormalDistribution xDistribution = new NormalDistribution(1, 2); NormalDistribution yDistribution = new NormalDistribution(3, 4); // Try this for several sample sizes, all low so that we see the difference from the normal distribution // n = 3 maxima at ends; n = 4 uniform; n = 5 semi-circular "mound"; n = 6 parabolic "mound". foreach (int n in new int[] { 3, 4, 5, 6, 8 }) { // find r values Sample rSample = new Sample(); ContinuousDistribution rDistribution = null; for (int i = 0; i < 128; i++) { // to get each r value, construct a bivariate sample of the given size with no cross-correlation BivariateSample xySample = new BivariateSample(); for (int j = 0; j < n; j++) { xySample.Add( xDistribution.GetRandomValue(rng), yDistribution.GetRandomValue(rng) ); } TestResult rTest = xySample.PearsonRTest(); rSample.Add(rTest.Statistic); rDistribution = rTest.Distribution; } // Check whether r is distributed as expected TestResult result = rSample.KuiperTest(new PearsonRDistribution(n)); Assert.IsTrue(result.Probability > 0.01); Assert.IsTrue(rSample.PopulationMean.ConfidenceInterval(0.95).ClosedContains(rDistribution.Mean)); Assert.IsTrue(rSample.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(rDistribution.StandardDeviation)); } }
public static TestResult RunPairedTest(List <double> x, List <double> y, PairedTest pt) { BivariateSample s = new BivariateSample(); s.Add(x, y); switch (pt) { case PairedTest.KendallTau: return(s.KendallTauTest()); case PairedTest.PearsonR: return(s.PearsonRTest()); case PairedTest.SpearmanRho: return(s.SpearmanRhoTest()); case PairedTest.StudentT: return(s.PairedStudentTTest()); default: return(null); } }