github.com/jgbaldwinbrown/perf@v0.1.1/pkg/stats/ttest_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 TestTTest(t *testing.T) { 10 s1 := Sample{Xs: []float64{2, 1, 3, 4}} 11 s2 := Sample{Xs: []float64{6, 5, 7, 9}} 12 13 check := func(want, got *TTestResult) { 14 if want.N1 != got.N1 || want.N2 != got.N2 || 15 !aeq(want.T, got.T) || !aeq(want.DoF, got.DoF) || 16 want.AltHypothesis != got.AltHypothesis || 17 !aeq(want.P, got.P) { 18 t.Errorf("want %+v, got %+v", want, got) 19 } 20 } 21 check3 := func(test func(alt LocationHypothesis) (*TTestResult, error), n1, n2 int, t, dof float64, pless, pdiff, pgreater float64) { 22 want := &TTestResult{N1: n1, N2: n2, T: t, DoF: dof} 23 24 want.AltHypothesis = LocationLess 25 want.P = pless 26 got, _ := test(want.AltHypothesis) 27 check(want, got) 28 29 want.AltHypothesis = LocationDiffers 30 want.P = pdiff 31 got, _ = test(want.AltHypothesis) 32 check(want, got) 33 34 want.AltHypothesis = LocationGreater 35 want.P = pgreater 36 got, _ = test(want.AltHypothesis) 37 check(want, got) 38 } 39 40 check3(func(alt LocationHypothesis) (*TTestResult, error) { 41 return TwoSampleTTest(s1, s1, alt) 42 }, 4, 4, 0, 6, 43 0.5, 1, 0.5) 44 check3(func(alt LocationHypothesis) (*TTestResult, error) { 45 return TwoSampleWelchTTest(s1, s1, alt) 46 }, 4, 4, 0, 6, 47 0.5, 1, 0.5) 48 49 check3(func(alt LocationHypothesis) (*TTestResult, error) { 50 return TwoSampleTTest(s1, s2, alt) 51 }, 4, 4, -3.9703446152237674, 6, 52 0.0036820296121056195, 0.0073640592242113214, 0.9963179703878944) 53 check3(func(alt LocationHypothesis) (*TTestResult, error) { 54 return TwoSampleWelchTTest(s1, s2, alt) 55 }, 4, 4, -3.9703446152237674, 5.584615384615385, 56 0.004256431565689112, 0.0085128631313781695, 0.9957435684343109) 57 58 check3(func(alt LocationHypothesis) (*TTestResult, error) { 59 return PairedTTest(s1.Xs, s2.Xs, 0, alt) 60 }, 4, 4, -17, 3, 61 0.0002216717691559955, 0.00044334353831207749, 0.999778328230844) 62 63 check3(func(alt LocationHypothesis) (*TTestResult, error) { 64 return OneSampleTTest(s1, 0, alt) 65 }, 4, 0, 3.872983346207417, 3, 66 0.9847668541689145, 0.030466291662170977, 0.015233145831085482) 67 check3(func(alt LocationHypothesis) (*TTestResult, error) { 68 return OneSampleTTest(s1, 2.5, alt) 69 }, 4, 0, 0, 3, 70 0.5, 1, 0.5) 71 }