github.com/gopherd/gonum@v0.0.4/stat/distuv/uniform_test.go (about) 1 // Copyright ©2017 The Gonum 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 distuv 6 7 import ( 8 "math" 9 "sort" 10 "testing" 11 12 "math/rand" 13 14 "github.com/gopherd/gonum/floats/scalar" 15 ) 16 17 func TestUniformProb(t *testing.T) { 18 t.Parallel() 19 for _, test := range []struct { 20 min, max, x, want float64 21 }{ 22 {0, 1, 1, 1}, 23 {2, 4, 0, 0}, 24 {2, 4, 5, 0}, 25 {2, 4, 3, 0.5}, 26 {0, 100, 1, 0.01}, 27 {-1, 1, -1.5, 0}, 28 {-1, 1, 1.5, 0}, 29 } { 30 u := Uniform{test.min, test.max, nil} 31 pdf := u.Prob(test.x) 32 if !scalar.EqualWithinAbsOrRel(pdf, test.want, 1e-15, 1e-15) { 33 t.Errorf("PDF mismatch, x = %v, min = %v, max = %v. Got %v, want %v", test.x, test.min, test.max, pdf, test.want) 34 } 35 logWant := math.Log(test.want) 36 logPdf := u.LogProb(test.x) 37 if !scalar.EqualWithinAbsOrRel(logPdf, logWant, 1e-15, 1e-15) { 38 t.Errorf("Log PDF mismatch, x = %v, min = %v, max = %v. Got %v, want %v", test.x, test.min, test.max, logPdf, logWant) 39 } 40 } 41 } 42 43 func TestUniformCDFSurvival(t *testing.T) { 44 t.Parallel() 45 for _, test := range []struct { 46 min, max, x, want float64 47 }{ 48 {0, 1, 1, 1}, 49 {0, 100, 100, 1}, 50 {0, 100, 0, 0}, 51 {0, 100, 50, 0.5}, 52 {0, 50, 10, 0.2}, 53 {-1, 1, -1.5, 0}, 54 {-1, 1, 1.5, 1}, 55 } { 56 u := Uniform{test.min, test.max, nil} 57 cdf := u.CDF(test.x) 58 if !scalar.EqualWithinAbsOrRel(cdf, test.want, 1e-15, 1e-15) { 59 t.Errorf("CDF mismatch, x = %v, min = %v, max = %v. Got %v, want %v", test.x, test.min, test.max, cdf, test.want) 60 } 61 survival := u.Survival(test.x) 62 if !scalar.EqualWithinAbsOrRel(survival, 1-test.want, 1e-15, 1e-15) { 63 t.Errorf("CDF mismatch, x = %v, min = %v, max = %v. Got %v, want %v", test.x, test.min, test.max, survival, 1-test.want) 64 } 65 } 66 } 67 68 func TestUniform(t *testing.T) { 69 t.Parallel() 70 src := rand.New(rand.NewSource(1)) 71 for i, b := range []Uniform{ 72 {1, 2, src}, 73 {0, 100, src}, 74 {50, 60, src}, 75 } { 76 testUniform(t, b, i) 77 } 78 } 79 80 func testUniform(t *testing.T, u Uniform, i int) { 81 const ( 82 tol = 1e-2 83 n = 1e5 84 bins = 50 85 ) 86 x := make([]float64, n) 87 generateSamples(x, u) 88 sort.Float64s(x) 89 90 testRandLogProbContinuous(t, i, 0, x, u, tol, bins) 91 checkMean(t, i, x, u, tol) 92 checkVarAndStd(t, i, x, u, tol) 93 checkExKurtosis(t, i, x, u, 7e-2) 94 checkProbContinuous(t, i, x, u.Min, u.Max, u, 1e-10) 95 checkQuantileCDFSurvival(t, i, x, u, 1e-2) 96 checkEntropy(t, i, x, u, tol) 97 checkSkewness(t, i, x, u, tol) 98 checkMedian(t, i, x, u, tol) 99 testDerivParam(t, &u) 100 } 101 102 func TestUniformScore(t *testing.T) { 103 t.Parallel() 104 u := Uniform{0, 1, nil} 105 for _, test := range []struct { 106 x, wantMin, wantMax float64 107 }{ 108 {-0.001, math.NaN(), math.NaN()}, 109 {0, math.NaN(), -1}, 110 {1, 1, math.NaN()}, 111 {1.001, math.NaN(), math.NaN()}, 112 } { 113 score := u.Score(nil, test.x) 114 if !scalar.Same(score[0], test.wantMin) { 115 t.Errorf("Score[0] mismatch for at %g: got %v, want %g", test.x, score[0], test.wantMin) 116 } 117 if !scalar.Same(score[1], test.wantMax) { 118 t.Errorf("Score[1] mismatch for at %g: got %v, want %g", test.x, score[1], test.wantMax) 119 } 120 } 121 } 122 123 func TestUniformScoreInput(t *testing.T) { 124 t.Parallel() 125 u := Uniform{0, 1, nil} 126 scoreInput := u.ScoreInput(0.5) 127 if scoreInput != 0 { 128 t.Errorf("Mismatch in input score for U(0, 1) at x == 0.5: got %v, want 0", scoreInput) 129 } 130 xs := []float64{-0.0001, 0, 1, 1.0001} 131 for _, x := range xs { 132 scoreInput = u.ScoreInput(x) 133 if !math.IsNaN(scoreInput) { 134 t.Errorf("Expected NaN score input for U(0, 1) at x == %g, got %v", x, scoreInput) 135 } 136 } 137 }