protected override void EndProcessing() { var hypo = TestingHelper.GetTwoSampleHypothesis(Alternate); MannWhitneyWilcoxonTest test; if (ParameterSetName == "Pipeline") { var samples = _data.GroupBy(CategoryName).ToDoubleJaggedArrayOf(ValueName); if (samples.Length != 2) { WriteError(new ErrorRecord(new RuntimeException("The number of categories is not two"), "", ErrorCategory.InvalidArgument, null)); return; } test = new MannWhitneyWilcoxonTest(samples[0], samples[1], hypo, (Exact ? true : (Nullable <bool>)null), !NoAdjustForTies); } else { test = new MannWhitneyWilcoxonTest(Sample1, Sample2, hypo, (Exact ? true : (Nullable <bool>)null), !NoAdjustForTies); } test.Size = Size; WriteObject(test); }
public void same_vectors() { // https://github.com/accord-net/framework/issues/857 /* * a <- c(250,200,450,400,250,250,350,0,200,400,300,600,200,200, * 550,100,300,250,350,200,550,200,450,400,200,400,450, * 200,400,400,500,450,300,250,200) * b <- c(250,200,450,400,250,250,350,0,200,400,300,600,200,200, * 550,100,300,250,350,200,550,200,450,400,200,400,450, * 200,400,400,500,450,300,250,200) * * wilcox.test(a, b) */ double[] a = new double[] { 250, 200, 450, 400, 250, 250, 350, 0, 200, 400, 300, 600, 200, 200, 550, 100, 300, 250, 350, 200, 550, 200, 450, 400, 200, 400, 450, 200, 400, 400, 500, 450, 300, 250, 200 }; double[] b = new double[] { 250, 200, 450, 400, 250, 250, 350, 0, 200, 400, 300, 600, 200, 200, 550, 100, 300, 250, 350, 200, 550, 200, 450, 400, 200, 400, 450, 200, 400, 400, 500, 450, 300, 250, 200 }; var target = new MannWhitneyWilcoxonTest(a, b, adjustForTies: true); Assert.AreEqual(1.0, target.PValue, 5e-3); }
public void same_as_above_inverted_order() { double[] sample2 = { 4.6, 4.7, 4.9, 5.1, 5.2, 5.5, 5.8, 6.1, 6.5, 6.5, 7.2 }; double[] sample1 = { 5.2, 5.3, 5.4, 5.6, 6.2, 6.3, 6.8, 7.7, 8.0, 8.1 }; var test = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond); Assert.AreEqual(096.5, test.RankSum2); Assert.AreEqual(134.5, test.RankSum1); Assert.AreEqual(79.5, test.Statistic1); Assert.AreEqual(30.5, test.Statistic2); //Assert.AreEqual(30.5, test.Statistic); Assert.AreEqual(DistributionTail.OneUpper, test.Tail); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsGreaterThanSecond, test.Hypothesis); Assert.IsTrue(test.Significant); Assert.AreEqual(0.043834132843420769, test.PValue, 1e-10); test = new MannWhitneyWilcoxonTest(sample1, sample2); Assert.AreEqual(TwoSampleHypothesis.ValuesAreDifferent, test.Hypothesis); double[] expectedRank2 = { 1, 2, 3, 4, 5.5, 9, 11, 12, 15.5, 15.5, 18 }; double[] expectedRank1 = { 5.5, 7, 8, 10, 13, 14, 17, 19, 20, 21 }; Assert.IsTrue(test.Rank1.IsEqual(expectedRank1)); Assert.IsTrue(test.Rank2.IsEqual(expectedRank2)); Assert.AreEqual(096.5, test.RankSum2); Assert.AreEqual(134.5, test.RankSum1); Assert.AreEqual(79.5, test.Statistic1); Assert.AreEqual(30.5, test.Statistic2); Assert.IsFalse(test.Significant); Assert.AreEqual(0.087668265686841537, test.PValue, 1e-10); // Create a new Mann-Whitney-Wilcoxon's test to compare the two samples test = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond); Assert.AreEqual(096.5, test.RankSum2); Assert.AreEqual(134.5, test.RankSum1); Assert.AreEqual(79.5, test.Statistic1); Assert.AreEqual(30.5, test.Statistic2); //Assert.AreEqual(30.5, test.Statistic); Assert.AreEqual(DistributionTail.OneLower, test.Tail); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsSmallerThanSecond, test.Hypothesis); Assert.IsFalse(test.Significant); Assert.AreEqual(0.959420610349403, test.PValue, 1e-10); }
public void non_exact_symmetry_same_size() { double[] sample2 = { 4.6, 4.7, 4.9, 5.1, 5.2, 5.5, 5.8, 6.1, 6.5, 6.5 }; double[] sample1 = { 5.2, 5.3, 5.4, 5.6, 6.2, 6.3, 6.8, 7.7, 8.0, 8.1 }; { // Those should be equal var e12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: true); Assert.AreEqual(e12.PValue, 0.97945122400837448, 1e-5); // ok var a12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.AreEqual(a12.PValue, 0.97945122400837448, 1e-5); // ok var e21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: true); Assert.AreEqual(e21.PValue, 0.97945122400837448, 1e-5); // ok var a21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(a21.PValue, 0.97945122400837448, 1e-5); // ok Assert.IsFalse(a12.IsExact); Assert.IsFalse(a21.IsExact); Assert.IsFalse(a12.IsExact); Assert.IsFalse(e12.IsExact); Assert.AreEqual(a12.PValue, e12.PValue, 1e-8); Assert.AreEqual(a21.PValue, e21.PValue, 1e-8); Assert.AreEqual(a12.PValue, e21.PValue, 1e-8); Assert.AreEqual(e12.PValue, e21.PValue, 1e-8); } { // Those should be equal var e12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: true); Assert.AreEqual(e12.PValue, 0.024597676755701903, 1e-5); // ok var a12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(a12.PValue, 0.024597676755701903, 1e-5); // ok var e21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: true); Assert.AreEqual(e21.PValue, 0.024597676755701903, 1e-5); // ok var a21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.AreEqual(a21.PValue, 0.024597676755701903, 1e-5); // ok Assert.IsFalse(a12.IsExact); Assert.IsFalse(a21.IsExact); Assert.IsFalse(a12.IsExact); Assert.IsFalse(e12.IsExact); Assert.AreEqual(a12.PValue, e12.PValue, 1e-8); Assert.AreEqual(a21.PValue, e21.PValue, 1e-8); Assert.AreEqual(a12.PValue, e21.PValue, 1e-8); Assert.AreEqual(e12.PValue, e21.PValue, 1e-8); } }
public void MannWhitneyWilcoxonTestConstructorTest() { // The following example comes from Richard Lowry's page at // http://vassarstats.net/textbook/ch11a.html. As stated by // Richard, this example deals with persons seeking treatment // by claustrophobia. Those persons are randomly divided into // two groups, and each group receive a different treatment // for the disorder. // The hypothesis would be that treatment A would more effective // than B. To check this hypothesis, we can use Mann-Whitney's Test // to compare the medians of both groups. // Claustrophobia test scores for people treated with treatment A double[] sample1 = { 4.6, 4.7, 4.9, 5.1, 5.2, 5.5, 5.8, 6.1, 6.5, 6.5, 7.2 }; // Claustrophobia test scores for people treated with treatment B double[] sample2 = { 5.2, 5.3, 5.4, 5.6, 6.2, 6.3, 6.8, 7.7, 8.0, 8.1 }; // Create a new Mann-Whitney-Wilcoxon's test to compare the two samples MannWhitneyWilcoxonTest test = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond); Assert.AreEqual(096.5, test.RankSum1); Assert.AreEqual(134.5, test.RankSum2); Assert.AreEqual(79.5, test.Statistic1); Assert.AreEqual(30.5, test.Statistic2); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsSmallerThanSecond, test.Hypothesis); // Directional should be significant Assert.IsTrue(test.Significant); Assert.AreEqual(0.043834132843420748, test.PValue); test = new MannWhitneyWilcoxonTest(sample1, sample2); Assert.AreEqual(TwoSampleHypothesis.ValuesAreDifferent, test.Hypothesis); double[] expectedRank1 = { 1, 2, 3, 4, 5.5, 9, 11, 12, 15.5, 15.5, 18 }; double[] expectedRank2 = { 5.5, 7, 8, 10, 13, 14, 17, 19, 20, 21 }; Assert.IsTrue(expectedRank1.IsEqual(expectedRank1)); Assert.IsTrue(expectedRank2.IsEqual(expectedRank2)); Assert.AreEqual(096.5, test.RankSum1); Assert.AreEqual(134.5, test.RankSum2); Assert.AreEqual(79.5, test.Statistic1); Assert.AreEqual(30.5, test.Statistic2); // Non-directional is non-significant Assert.IsFalse(test.Significant); }
public void non_exact_symmetry() { /* * a = c(4.6, 4.7, 4.9, 5.1, 5.2, 5.5, 5.8, 6.1, 6.5, 6.5, 7.2) * b = c(5.2, 5.3, 5.4, 5.6, 6.2, 6.3, 6.8, 7.7, 8.0, 8.1) * wilcox.test(a,b,'greater') # W = 30.5, p-value = 0.9609 * wilcox.test(a,b,'less') # W = 30.5, p-value = 0.04541 */ double[] sample2 = { 4.6, 4.7, 4.9, 5.1, 5.2, 5.5, 5.8, 6.1, 6.5, 6.5, 7.2 }; double[] sample1 = { 5.2, 5.3, 5.4, 5.6, 6.2, 6.3, 6.8, 7.7, 8.0, 8.1 }; { // Those should be equal var e12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: true); Assert.AreEqual(e12.PValue, 0.96093080726732927, 1e-5); // ok Assert.IsFalse(e12.IsExact); // the data contains ties var a12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.IsFalse(a12.IsExact); // the data contains ties Assert.AreEqual(a12.PValue, 0.96093080726732927, 1e-5); // ok var e21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: true); Assert.IsFalse(e21.IsExact); // the data contains ties Assert.AreEqual(e21.PValue, 0.96093080726732927, 1e-5); // ok var a21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(a21.PValue, 0.96093080726732927, 1e-5); // ok Assert.IsFalse(a21.IsExact); // the data contains ties Assert.AreEqual(a12.PValue, e12.PValue, 1e-8); Assert.AreEqual(a21.PValue, e21.PValue, 1e-8); Assert.AreEqual(a12.PValue, e21.PValue, 1e-8); } { // Those should be equal var e12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: true); Assert.AreEqual(e12.PValue, 0.0454062185864162, 1e-5); // ok var a12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(a12.PValue, 0.0454062185864162, 1e-5); // ok var e21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: true); Assert.AreEqual(e21.PValue, 0.0454062185864162, 1e-5); // ok var a21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.AreEqual(a21.PValue, 0.0454062185864162, 1e-5); // ok Assert.AreEqual(a12.PValue, e12.PValue, 1e-8); Assert.AreEqual(a21.PValue, e21.PValue, 1e-8); Assert.AreEqual(a12.PValue, e21.PValue, 1e-8); } }
public void symmetry_test() { // Example from https://www.r-bloggers.com/wilcoxon-mann-whitney-rank-sum-test-or-test-u/ double[] a = { 6, 8, 2, 4, 4, 5 }; double[] b = { 7, 10, 4, 3, 5, 6 }; var ab = new MannWhitneyWilcoxonTest(a, b); var ba = new MannWhitneyWilcoxonTest(b, a); Assert.AreEqual(0.5736, ab.PValue, 1e-4); Assert.AreEqual(ab.PValue, ba.PValue, 1e-10); }
public void MannWhitneyWilcoxonTestConstructorTest1() { // Example from Conover, "Practical nonparametric statistics", 1999 (pg. 218) double[] sample1 = { 14.8, 7.3, 5.6, 6.3, 9.0, 4.2, 10.6, 12.5, 12.9, 16.1, 11.4, 2.7 }; double[] sample2 = { 12.7, 14.2, 12.6, 2.1, 17.7, 11.8, 16.9, 7.9, 16.0, 10.6, 5.6, 5.6, 7.6, 11.3, 8.3, 6.7, 3.6, 1.0, 2.4, 6.4, 9.1, 6.7,18.6, 3.2, 6.2, 6.1, 15.3, 10.6, 1.8, 5.9, 9.9, 10.6, 14.8, 5.0, 2.6, 4.0 }; var target = new MannWhitneyWilcoxonTest(sample1, sample2); Assert.IsFalse(target.StatisticDistribution.Exact); Assert.AreEqual(216, target.StatisticDistribution.Mean); // mean of U (not W as in R) Assert.AreEqual(1762.4680851063831, target.StatisticDistribution.Variance, 1e-10); Assert.AreEqual(0.52789241803534459, target.PValue, 1e-10); target = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond); Assert.IsFalse(target.StatisticDistribution.Exact); Assert.AreEqual(DistributionTail.OneUpper, target.Tail); Assert.AreEqual(target.Tail, DistributionTail.OneUpper); Assert.AreEqual(0.2639462090176723, target.PValue, 1e-10); target = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond); Assert.IsFalse(target.StatisticDistribution.Exact); Assert.AreEqual(DistributionTail.OneLower, target.Tail); Assert.AreEqual(0.743781059309828, target.PValue, 1e-10); // Same but with inverted sample1 & sample2 target = new MannWhitneyWilcoxonTest(sample2, sample1); Assert.IsFalse(target.StatisticDistribution.Exact); Assert.AreEqual(216, target.StatisticDistribution.Mean); // mean of U (not W as in R) Assert.AreEqual(1762.4680851063831, target.StatisticDistribution.Variance, 1e-10); Assert.AreEqual(0.52789241803534459, target.PValue, 1e-10); target = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsGreaterThanSecond); Assert.IsFalse(target.StatisticDistribution.Exact); Assert.AreEqual(DistributionTail.OneUpper, target.Tail); Assert.AreEqual(0.743781059309828, target.PValue, 1e-10); target = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsSmallerThanSecond); Assert.IsFalse(target.StatisticDistribution.Exact); Assert.AreEqual(DistributionTail.OneLower, target.Tail); Assert.AreEqual(0.2639462090176723, target.PValue, 1e-10); }
private static void RealTest(AggregateMetrics first, AggregateMetrics second) { // Wilcoxon test to not assume normal distributions var mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest( first.Runs.Select(run => run.ElapsedNanos).ToArray(), second.Runs.Select(run => run.ElapsedNanos).ToArray(), TwoSampleHypothesis.FirstValueIsSmallerThanSecond); Trace.WriteLine( "MannWWT s1 < s2: " + mannWhitneyWilcoxonTest.PValue + " Stat1: " + mannWhitneyWilcoxonTest.Statistic1 + "; Stat2: " + mannWhitneyWilcoxonTest.Statistic2 + " - Significant: " + mannWhitneyWilcoxonTest.Significant + " - Hyp: " + mannWhitneyWilcoxonTest.Hypothesis); PerfAssertContext.AssertIsTrue(mannWhitneyWilcoxonTest.Significant); }
public void RCompatibilityTest() { // Test case from https://github.com/accord-net/framework/issues/389 /* * // Test against R: * a <- c(35, 15, 25, 10, 45, 20, 21, 22, 30, 17) * b <- c(20, 17, 23, 15, 49, 19, 24, 26, 33, 18) * * wilcox.test(a,b, paired=FALSE, alternative="greater") # W = 49.5, p-value = 0.5302 * wilcox.test(a,b, paired=FALSE, alternative="two.sided") # W = 49.5, p-value = 1 * wilcox.test(a,b, paired=FALSE, alternative="less") # W = 49.5, p-value = 0.5 */ var s1 = new double[] { 35, 15, 25, 10, 45, 20, 21, 22, 30, 17 }; var s2 = new double[] { 20, 17, 23, 15, 49, 19, 24, 26, 33, 18 }; var mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(0.5302, mannWhitneyWilcoxonTest.PValue, 1e-4); // Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic); Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic1); Assert.AreEqual(50.5, mannWhitneyWilcoxonTest.Statistic2); Assert.AreEqual(false, mannWhitneyWilcoxonTest.Significant); Assert.AreEqual(DistributionTail.OneUpper, mannWhitneyWilcoxonTest.Tail); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsGreaterThanSecond, mannWhitneyWilcoxonTest.Hypothesis); mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.ValuesAreDifferent, exact: false); Assert.AreEqual(1, mannWhitneyWilcoxonTest.PValue); // Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic); Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic1); Assert.AreEqual(50.5, mannWhitneyWilcoxonTest.Statistic2); Assert.AreEqual(false, mannWhitneyWilcoxonTest.Significant); Assert.AreEqual(TwoSampleHypothesis.ValuesAreDifferent, mannWhitneyWilcoxonTest.Hypothesis); mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.AreEqual(0.5, mannWhitneyWilcoxonTest.PValue); //Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic); Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic1); Assert.AreEqual(50.5, mannWhitneyWilcoxonTest.Statistic2); Assert.AreEqual(false, mannWhitneyWilcoxonTest.Significant); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsSmallerThanSecond, mannWhitneyWilcoxonTest.Hypothesis); }
public void MannWhitneyWilcoxonTestConstructorTest1() { // Example from Conover, "Practical nonparametric statistics", 1999 (pg. 218) double[] sample1 = { 14.8, 7.3, 5.6, 6.3, 9.0, 4.2, 10.6, 12.5, 12.9, 16.1, 11.4, 2.7 }; double[] sample2 = { 12.7, 14.2, 12.6, 2.1, 17.7, 11.8, 16.9, 7.9, 16.0, 10.6, 5.6, 5.6, 7.6, 11.3, 8.3, 6.7, 3.6, 1.0, 2.4, 6.4, 9.1, 6.7,18.6, 3.2, 6.2, 6.1, 15.3, 10.6, 1.8, 5.9, 9.9, 10.6, 14.8, 5.0, 2.6, 4.0 }; var target = new MannWhitneyWilcoxonTest(sample1, sample2); Assert.AreEqual(0.529, target.PValue, 1e-3); }
public void non_exact_symmetry() { // DO NOT CHANGE double[] sample2 = { 4.6, 4.7, 4.9, 5.1, 5.2, 5.5, 5.8, 6.1, 6.5, 6.5, 7.2 }; double[] sample1 = { 5.2, 5.3, 5.4, 5.6, 6.2, 6.3, 6.8, 7.7, 8.0, 8.1 }; { // Those should be equal var e12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: true); Assert.AreEqual(e12.PValue, 0.959420610349403, 1e-5); // ok var a12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.AreEqual(a12.PValue, 0.96093080726732927, 1e-5); // ok var e21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: true); Assert.AreEqual(e21.PValue, 0.959420610349403, 1e-5); // ok var a21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(a21.PValue, 0.96093080726732927, 1e-5); // ok Assert.AreEqual(a12.PValue, e12.PValue, 1e-2); Assert.AreEqual(a21.PValue, e21.PValue, 1e-2); Assert.AreEqual(a12.PValue, e21.PValue, 1e-2); } { // Those should be equal var e12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: true); Assert.AreEqual(e12.PValue, 0.043834132843420769, 1e-5); // ok var a12 = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(a12.PValue, 0.0454062185864162, 1e-5); // ok var e21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: true); Assert.AreEqual(e21.PValue, 0.043834132843420769, 1e-5); // ok var a21 = new MannWhitneyWilcoxonTest(sample2, sample1, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.AreEqual(a21.PValue, 0.0454062185864162, 1e-5); // ok Assert.AreEqual(a12.PValue, e12.PValue, 1e-2); Assert.AreEqual(a21.PValue, e21.PValue, 1e-2); Assert.AreEqual(a12.PValue, e21.PValue, 1e-2); } }
public void RCompatibilityTest2() { // Test case from https://github.com/accord-net/framework/issues/389 /* * // Test against R: * c <- c(45,45,15,50,30,15,30,35,25) * d <- c(45,55,20,55,20,25,35,45,20) * wilcox.test(c,d, paired=FALSE, alternative="two.sided", conf.level=0.95, exact=TRUE) # W=49.5, p-value=0.6557 * wilcox.test(c,d, paired=FALSE, alternative="less", conf.level=0.95, exact=TRUE) # W=49.5, p-value=0.3278 * wilcox.test(c,d, paired=FALSE, alternative="greater", conf.level=0.95, exact=TRUE) # W=35, p-value=0.7037 */ var s1 = new double[] { 45, 45, 15, 50, 30, 15, 30, 35, 25 }; var s2 = new double[] { 45, 55, 20, 55, 20, 25, 35, 45, 20 }; var mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(0.7037, mannWhitneyWilcoxonTest.PValue, 1e-4); // Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic); Assert.AreEqual(35, mannWhitneyWilcoxonTest.Statistic1); Assert.AreEqual(46, mannWhitneyWilcoxonTest.Statistic2); Assert.AreEqual(false, mannWhitneyWilcoxonTest.Significant); Assert.AreEqual(DistributionTail.OneUpper, mannWhitneyWilcoxonTest.Tail); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsGreaterThanSecond, mannWhitneyWilcoxonTest.Hypothesis); mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.ValuesAreDifferent, exact: false); Assert.AreEqual(0.6557, mannWhitneyWilcoxonTest.PValue, 1e-4); // Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic); Assert.AreEqual(35, mannWhitneyWilcoxonTest.Statistic1); Assert.AreEqual(46, mannWhitneyWilcoxonTest.Statistic2); Assert.AreEqual(false, mannWhitneyWilcoxonTest.Significant); Assert.AreEqual(TwoSampleHypothesis.ValuesAreDifferent, mannWhitneyWilcoxonTest.Hypothesis); mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest(s1, s2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.AreEqual(0.3278, mannWhitneyWilcoxonTest.PValue, 1e-4); //Assert.AreEqual(49.5, mannWhitneyWilcoxonTest.Statistic); Assert.AreEqual(35, mannWhitneyWilcoxonTest.Statistic1); Assert.AreEqual(46, mannWhitneyWilcoxonTest.Statistic2); Assert.AreEqual(false, mannWhitneyWilcoxonTest.Significant); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsSmallerThanSecond, mannWhitneyWilcoxonTest.Hypothesis); }
public static void FirstIsNotSlower(AggregateMetrics first, AggregateMetrics second) { double[] firstSamples = first.Runs.Select(run => run.ElapsedNanos).ToArray(); double[] secondSamples = second.Runs.Select(run => run.ElapsedNanos).ToArray(); // Wilcoxon test to not assume normal distributions var mannWhitneyWilcoxonTest = new MannWhitneyWilcoxonTest( firstSamples, secondSamples, TwoSampleHypothesis.FirstValueIsGreaterThanSecond); Trace.WriteLine($"Sample 1 mean: {firstSamples.Average()} Sample 2 mean: {secondSamples.Average()}"); Trace.WriteLine( "MannWWT s1 > s2: " + mannWhitneyWilcoxonTest.PValue + " Stat1: " + mannWhitneyWilcoxonTest.Statistic1 + "; Stat2: " + mannWhitneyWilcoxonTest.Statistic2 + " - Significant: " + mannWhitneyWilcoxonTest.Significant + " - Hyp: " + mannWhitneyWilcoxonTest.Hypothesis); // We want to assert that we CANNOT statistically say that first > second (it can be less than, equal, or it might be greater than but not // with statistical significance -- the only thing it CAN'T be, is KNOWN to be slower [>]) PerfAssertContext.AssertIsFalse(mannWhitneyWilcoxonTest.Significant); }
public void MannWhitneyWilcoxonTestConstructorTest1() { // Example from Conover, "Practical nonparametric statistics", 1999 (pg. 218) double[] sample1 = { 14.8, 7.3, 5.6, 6.3, 9.0, 4.2, 10.6, 12.5, 12.9, 16.1, 11.4, 2.7 }; double[] sample2 = { 12.7, 14.2, 12.6, 2.1, 17.7, 11.8, 16.9, 7.9, 16.0, 10.6, 5.6, 5.6, 7.6, 11.3, 8.3,6.7, 3.6, 1.0, 2.4, 6.4, 9.1, 6.7, 18.6, 3.2, 6.2, 6.1, 15.3, 10.6, 1.8, 5.9, 9.9, 10.6, 14.8, 5.0, 2.6, 4.0 }; var target = new MannWhitneyWilcoxonTest(sample1, sample2); Assert.AreEqual(0.529, target.PValue, 1e-3); }
static void Main(String[] args) { double[] all_electrodes = new double[] { 0.85334198572355613, 0.85918234912394553, 0.84620376378974693, 0.835820895522388, 0.84815055158987673, 0.82543802725502924, 0.83517196625567813, 0.85074626865671643, 0.85269305645684623, 0.83776768332251783, 0.84445165476963013 }; double[][] electrodes = new double[][] { //2 new double[] { 0.88384166125892283, 0.84944841012329653, 0.84750162232316673, 0.84944841012329653, 0.84295911745619723, 0.84685269305645683, 0.86242699545749513, 0.84685269305645683, 0.85204412719013622, 0.84815055158987673, 0.85295262816353024 }, //3 new double[] { 0.85918234912394553, 0.86242699545749513, 0.86437378325762493, 0.82933160285528873, 0.85334198572355613, 0.84425697598961713, 0.85139519792342633, 0.84879948085658663, 0.87865022712524332, 0.84815055158987673, 0.853990914990266 }, //4 new double[] { 0.86502271252433482, 0.86956521739130432, 0.87410772225827382, 0.84944841012329653, 0.86956521739130432, 0.853990914990266, 0.835820895522388, 0.85788449059052563, 0.82219338092147953, 0.85593770279039583, 0.85535366645035693 }, //5 new double[] { 0.85139519792342633, 0.84620376378974693, 0.872809863724854, 0.853990914990266, 0.83517196625567813, 0.83906554185593774, 0.86826735885788453, 0.87929915639195333, 0.85139519792342633, 0.85723556132381573, 0.85548345230369871 }, //6 new double[] { 0.85074626865671643, 0.87345879299156393, 0.863075924724205, 0.83062946138870863, 0.85139519792342633, 0.85334198572355613, 0.84944841012329653, 0.86567164179104472, 0.85723556132381573, 0.85334198572355613, 0.85483452303698915 }, //7 new double[] { 0.85204412719013622, 0.85269305645684623, 0.845554834523037, 0.863075924724205, 0.84295911745619723, 0.854639844256976, 0.85723556132381573, 0.86632057105775473, 0.85269305645684623, 0.85074626865671643, 0.85379623621025336 }, //8 new double[] { 0.81116158338741073, 0.84231018818948733, 0.84620376378974693, 0.86891628812459443, 0.845554834523037, 0.86761842959117452, 0.85528877352368593, 0.88384166125892283, 0.84944841012329653, 0.86826735885788453, 0.85386112913692414 }, //9 new double[] { 0.84815055158987673, 0.85983127839065543, 0.863075924724205, 0.845554834523037, 0.86177806619078523, 0.86502271252433482, 0.85204412719013622, 0.82608695652173914, 0.853990914990266, 0.85334198572355613, 0.85288773523685923 }, //10 new double[] { 0.85074626865671643, 0.84685269305645683, 0.84036340038935753, 0.85334198572355613, 0.87151200519143412, 0.85853341985723552, 0.854639844256976, 0.85788449059052563, 0.854639844256976, 0.853990914990266, 0.85425048669695 }, //11 new double[] { 0.85983127839065543, 0.85918234912394553, 0.84295911745619723, 0.845554834523037, 0.86048020765736533, 0.84879948085658663, 0.86242699545749513, 0.86826735885788453, 0.881245944192083, 0.82478909798831923, 0.85535366645035682 }, //12 new double[] { 0.86956521739130432, 0.863075924724205, 0.853990914990266, 0.83127839065541853, 0.83776768332251783, 0.85983127839065543, 0.85593770279039583, 0.87021414665801433, 0.85334198572355613, 0.82998053212199874, 0.85249837767683323 }, //13 new double[] { 0.845554834523037, 0.87605451005840362, 0.84815055158987673, 0.84101232965606754, 0.85788449059052563, 0.84295911745619723, 0.85983127839065543, 0.85139519792342633, 0.87021414665801433, 0.85983127839065543, 0.855288773523686 }, //14 new double[] { 0.85204412719013622, 0.85723556132381573, 0.86956521739130432, 0.84750162232316673, 0.854639844256976, 0.84231018818948733, 0.84360804672290723, 0.863724853990915, 0.85204412719013622, 0.84620376378974693, 0.85288773523685912 }, //15 new double[] { 0.84620376378974693, 0.863075924724205, 0.83906554185593774, 0.85658663205710572, 0.872809863724854, 0.84750162232316673, 0.863075924724205, 0.844905905256327, 0.84620376378974693, 0.85853341985723552, 0.85379623621025291 }, //16 new double[] { 0.85788449059052563, 0.85658663205710572, 0.83257624918883844, 0.85204412719013622, 0.85139519792342633, 0.84815055158987673, 0.872809863724854, 0.84944841012329653, 0.86112913692407522, 0.85723556132381573, 0.85392602206359491 }, //17 new double[] { 0.84750162232316673, 0.85204412719013622, 0.85139519792342633, 0.85334198572355613, 0.84036340038935753, 0.85139519792342633, 0.86761842959117452, 0.853990914990266, 0.85334198572355613, 0.863075924724205, 0.85340687865022713 }, //18 new double[] { 0.85269305645684623, 0.872160934458144, 0.85788449059052563, 0.85593770279039583, 0.853990914990266, 0.85334198572355613, 0.826735885788449, 0.86242699545749513, 0.85528877352368593, 0.85139519792342633, 0.85418559377027914 } }; Console.WriteLine("Evolving electrodes only:"); Console.WriteLine(""); Console.WriteLine(""); for (var i = 0; i < electrodes.Length; i++) { MannWhitneyWilcoxonTest mwTest = new MannWhitneyWilcoxonTest(all_electrodes, electrodes[i], TwoSampleHypothesis.FirstValueIsSmallerThanSecond); Console.WriteLine((i + 2) + " electrodes:"); Console.WriteLine("MWW Test: Statistic - " + mwTest.Statistic + " , PValue - " + mwTest.PValue + " , Significant - " + mwTest.Significant); Console.WriteLine(""); } double[][] powerbands = new double[][] { //2 new double[] { 0.83971447112264763, 0.85788449059052563, 0.85269305645684623, 0.86437378325762493, 0.845554834523037, 0.84231018818948733, 0.87345879299156393, 0.85528877352368593, 0.85009733939000653, 0.854639844256976, 0.85360155743024024 }, //3 new double[] { 0.853990914990266, 0.85983127839065543, 0.84879948085658663, 0.84750162232316673, 0.85334198572355613, 0.854639844256976, 0.85918234912394553, 0.84879948085658663, 0.85204412719013622, 0.86177806619078523, 0.85399091499026591 }, //4 new double[] { 0.863724853990915, 0.85658663205710572, 0.83711875405580793, 0.86567164179104472, 0.85139519792342633, 0.83257624918883844, 0.85918234912394553, 0.83906554185593774, 0.83776768332251783, 0.88578844905905252, 0.85288773523685912 }, //5 new double[] { 0.86567164179104472, 0.86826735885788453, 0.845554834523037, 0.84101232965606754, 0.86112913692407522, 0.85593770279039583, 0.85009733939000653, 0.86048020765736533, 0.83776768332251783, 0.85918234912394553, 0.854510058403634 }, //6 new double[] { 0.844905905256327, 0.85204412719013622, 0.85723556132381573, 0.86048020765736533, 0.84815055158987673, 0.84360804672290723, 0.85788449059052563, 0.86632057105775473, 0.86891628812459443, 0.85788449059052563, 0.85574302401038282 }, //7 new double[] { 0.863724853990915, 0.854639844256976, 0.854639844256976, 0.85918234912394553, 0.85983127839065543, 0.85658663205710572, 0.84166125892277743, 0.84879948085658663, 0.85009733939000653, 0.84815055158987673, 0.85373134328358213 }, //8 new double[] { 0.84879948085658663, 0.86112913692407522, 0.86502271252433482, 0.84231018818948733, 0.84815055158987673, 0.88319273199221282, 0.85204412719013622, 0.85074626865671643, 0.835820895522388, 0.85204412719013622, 0.85392602206359514 }, //9 new double[] { 0.854639844256976, 0.85139519792342633, 0.872809863724854, 0.82868267358857883, 0.87475665152498383, 0.83776768332251783, 0.854639844256976, 0.854639844256976, 0.84620376378974693, 0.86502271252433482, 0.854055807916937 } }; Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("Evolving electrodes AND powerband"); Console.WriteLine(""); for (var i = 0; i < powerbands.Length; i++) { MannWhitneyWilcoxonTest mwTest = new MannWhitneyWilcoxonTest(all_electrodes, powerbands[i], TwoSampleHypothesis.FirstValueIsSmallerThanSecond); Console.WriteLine((i + 2) + " electrodes:"); Console.WriteLine("MWW Test: Statistic - " + mwTest.Statistic + " , PValue - " + mwTest.PValue + " , Significant - " + mwTest.Significant); Console.WriteLine(""); } Console.ReadKey(); }
public void same_as_above_non_exact() { double[] sample2 = { 4.6, 4.7, 4.9, 5.1, 5.2, 5.5, 5.8, 6.1, 6.5, 6.5, 7.2 }; double[] sample1 = { 5.2, 5.3, 5.4, 5.6, 6.2, 6.3, 6.8, 7.7, 8.0, 8.1 }; // Create a new Mann-Whitney-Wilcoxon's test to compare the two samples var test = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond, exact: false); Assert.AreEqual(096.5, test.RankSum2); Assert.AreEqual(134.5, test.RankSum1); Assert.AreEqual(79.5, test.Statistic1); Assert.AreEqual(30.5, test.Statistic2); //Assert.AreEqual(30.5, test.Statistic); Assert.AreEqual(DistributionTail.OneUpper, test.Tail); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsGreaterThanSecond, test.Hypothesis); Assert.IsTrue(test.Significant); Assert.AreEqual(0.0454062185864162, test.PValue, 1e-10); // Create a new Mann-Whitney-Wilcoxon's test to compare the two samples test = new MannWhitneyWilcoxonTest(sample1, sample2, exact: false); Assert.AreEqual(TwoSampleHypothesis.ValuesAreDifferent, test.Hypothesis); double[] expectedRank2 = { 1, 2, 3, 4, 5.5, 9, 11, 12, 15.5, 15.5, 18 }; double[] expectedRank1 = { 5.5, 7, 8, 10, 13, 14, 17, 19, 20, 21 }; Assert.IsTrue(test.Rank1.IsEqual(expectedRank1)); Assert.IsTrue(test.Rank2.IsEqual(expectedRank2)); Assert.AreEqual(096.5, test.RankSum2); Assert.AreEqual(134.5, test.RankSum1); Assert.AreEqual(79.5, test.Statistic1); Assert.AreEqual(30.5, test.Statistic2); // Non-directional is non-significant Assert.IsFalse(test.Significant); Assert.AreEqual(0.0908124371728325, test.PValue, 1e-10); // Create a new Mann-Whitney-Wilcoxon's test to compare the two samples test = new MannWhitneyWilcoxonTest(sample1, sample2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond, exact: false); Assert.AreEqual(096.5, test.RankSum2); Assert.AreEqual(134.5, test.RankSum1); Assert.AreEqual(79.5, test.Statistic1); Assert.AreEqual(30.5, test.Statistic2); Assert.AreEqual(DistributionTail.OneLower, test.Tail); Assert.AreEqual(TwoSampleHypothesis.FirstValueIsSmallerThanSecond, test.Hypothesis); // Directional should be significant Assert.IsFalse(test.Significant); Assert.AreEqual(0.96093080726732927, test.PValue, 1e-10); }
public RankSumResult(MannWhitneyWilcoxonTest result, int n1, int n2) { this.result = result; N1 = n1; N2 = n2; }