github.com/jgbaldwinbrown/perf@v0.1.1/pkg/stats/utest_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package stats
     6  
     7  import "testing"
     8  
     9  func TestMannWhitneyUTest(t *testing.T) {
    10  	check := func(want, got *MannWhitneyUTestResult) {
    11  		if want.N1 != got.N1 || want.N2 != got.N2 ||
    12  			!aeq(want.U, got.U) ||
    13  			want.AltHypothesis != got.AltHypothesis ||
    14  			!aeq(want.P, got.P) {
    15  			t.Errorf("want %+v, got %+v", want, got)
    16  		}
    17  	}
    18  	check3 := func(x1, x2 []float64, U float64, pless, pdiff, pgreater float64) {
    19  		want := &MannWhitneyUTestResult{N1: len(x1), N2: len(x2), U: U}
    20  
    21  		want.AltHypothesis = LocationLess
    22  		want.P = pless
    23  		got, _ := MannWhitneyUTest(x1, x2, want.AltHypothesis)
    24  		check(want, got)
    25  
    26  		want.AltHypothesis = LocationDiffers
    27  		want.P = pdiff
    28  		got, _ = MannWhitneyUTest(x1, x2, want.AltHypothesis)
    29  		check(want, got)
    30  
    31  		want.AltHypothesis = LocationGreater
    32  		want.P = pgreater
    33  		got, _ = MannWhitneyUTest(x1, x2, want.AltHypothesis)
    34  		check(want, got)
    35  	}
    36  
    37  	s1 := []float64{2, 1, 3, 5}
    38  	s2 := []float64{12, 11, 13, 15}
    39  	s3 := []float64{0, 4, 6, 7} // Interleaved with s1, but no ties
    40  	s4 := []float64{2, 2, 2, 2}
    41  	s5 := []float64{1, 1, 1, 1, 1}
    42  
    43  	// Small sample, no ties
    44  	check3(s1, s2, 0, 0.014285714285714289, 0.028571428571428577, 1)
    45  	check3(s2, s1, 16, 1, 0.028571428571428577, 0.014285714285714289)
    46  	check3(s1, s3, 5, 0.24285714285714288, 0.485714285714285770, 0.8285714285714285)
    47  
    48  	// Small sample, ties
    49  	// TODO: Check these against some other implementation.
    50  	check3(s1, s1, 8, 0.6285714285714286, 1, 0.6285714285714286)
    51  	check3(s1, s4, 10, 0.8571428571428571, 0.7142857142857143, 0.3571428571428571)
    52  	check3(s1, s5, 17.5, 1, 0, 0.04761904761904767)
    53  
    54  	r, err := MannWhitneyUTest(s4, s4, LocationDiffers)
    55  	if err != ErrSamplesEqual {
    56  		t.Errorf("want ErrSamplesEqual, got %+v, %+v", r, err)
    57  	}
    58  
    59  	// Large samples.
    60  	l1 := make([]float64, 500)
    61  	for i := range l1 {
    62  		l1[i] = float64(i * 2)
    63  	}
    64  	l2 := make([]float64, 600)
    65  	for i := range l2 {
    66  		l2[i] = float64(i*2 - 41)
    67  	}
    68  	l3 := append([]float64{}, l2...)
    69  	for i := 0; i < 30; i++ {
    70  		l3[i] = l1[i]
    71  	}
    72  	// For comparing with R's wilcox.test:
    73  	// l1 <- seq(0, 499)*2
    74  	// l2 <- seq(0,599)*2-41
    75  	// l3 <- l2; for (i in 1:30) { l3[i] = l1[i] }
    76  
    77  	check3(l1, l2, 135250, 0.0024667680407086112, 0.0049335360814172224, 0.9975346930458906)
    78  	check3(l1, l1, 125000, 0.5000436801680628, 1, 0.5000436801680628)
    79  	check3(l1, l3, 134845, 0.0019351907119808942, 0.0038703814239617884, 0.9980659818257166)
    80  }