github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/stat/distuv/inversegamma_test.go (about) 1 // Copyright ©2018 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 "golang.org/x/exp/rand" 13 14 "github.com/jingcheng-WU/gonum/floats/scalar" 15 ) 16 17 func TestInverseGamma(t *testing.T) { 18 t.Parallel() 19 // Values extracted from a comparison with scipy 20 for _, test := range []struct { 21 x, alpha, want float64 22 }{ 23 {0.9, 4.5, 0.050521067785046482}, 24 {0.04, 45, 0.10550644842525572}, 25 {20, 0.4, 0.0064691988681571536}, 26 } { 27 pdf := InverseGamma{Alpha: test.alpha, Beta: 1}.Prob(test.x) 28 if !scalar.EqualWithinAbsOrRel(pdf, test.want, 1e-10, 1e-10) { 29 t.Errorf("Pdf mismatch. Got %v, want %v", pdf, test.want) 30 } 31 } 32 src := rand.NewSource(1) 33 for i, g := range []InverseGamma{ 34 {Alpha: 5.6, Beta: 0.5, Src: src}, 35 {Alpha: 30, Beta: 1.7, Src: src}, 36 {Alpha: 30.2, Beta: 1.7, Src: src}, 37 } { 38 testInverseGamma(t, g, i) 39 } 40 } 41 42 func testInverseGamma(t *testing.T, f InverseGamma, i int) { 43 const ( 44 tol = 1e-2 45 n = 2e6 46 bins = 50 47 ) 48 x := make([]float64, n) 49 generateSamples(x, f) 50 sort.Float64s(x) 51 52 testRandLogProbContinuous(t, i, 0, x, f, tol, bins) 53 checkMean(t, i, x, f, tol) 54 checkVarAndStd(t, i, x, f, 2e-2) 55 checkExKurtosis(t, i, x, f, 2e-1) 56 checkProbContinuous(t, i, x, 0, math.Inf(1), f, 1e-10) 57 checkQuantileCDFSurvival(t, i, x, f, 5e-2) 58 checkMode(t, i, x, f, 1e-2, 1e-2) 59 60 cdf0 := f.CDF(0) 61 if cdf0 != 0 { 62 t.Errorf("Expected zero CDF at 0, but got: %v", cdf0) 63 } 64 cdfNeg := f.CDF(-0.0001) 65 if cdfNeg != 0 { 66 t.Errorf("Expected zero CDF for a negative argument, but got: %v", cdfNeg) 67 } 68 surv0 := f.Survival(0) 69 if surv0 != 1 { 70 t.Errorf("Mismatch in Survival at 0. Got %v, want 1", surv0) 71 } 72 survNeg := f.Survival(-0.0001) 73 if survNeg != 1 { 74 t.Errorf("Mismatch in Survival for a negative argument. Got %v, want 1", survNeg) 75 } 76 if f.NumParameters() != 2 { 77 t.Errorf("Mismatch in NumParameters. Got %v, want 2", f.NumParameters()) 78 } 79 pdf0 := f.Prob(0) 80 if pdf0 != 0 { 81 t.Errorf("Expected zero PDF at 0, but got: %v", pdf0) 82 } 83 pdfNeg := f.Prob(-0.0001) 84 if pdfNeg != 0 { 85 t.Errorf("Expected zero PDF for a negative argument, but got: %v", pdfNeg) 86 } 87 } 88 89 func TestInverseGammaLowAlpha(t *testing.T) { 90 t.Parallel() 91 f := InverseGamma{Alpha: 1, Beta: 1} 92 mean := f.Mean() 93 if !math.IsInf(mean, 1) { 94 t.Errorf("Expected +Inf mean for alpha <= 1, got %v", mean) 95 } 96 f = InverseGamma{Alpha: 2, Beta: 1} 97 stdDev := f.StdDev() 98 if !math.IsInf(stdDev, 1) { 99 t.Errorf("Expected +Inf standard deviation for alpha <= 2, got %v", stdDev) 100 } 101 variance := f.Variance() 102 if !math.IsInf(variance, 1) { 103 t.Errorf("Expected +Inf variance for alpha <= 2, got %v", variance) 104 } 105 f = InverseGamma{Alpha: 4, Beta: 1} 106 exKurt := f.ExKurtosis() 107 if !math.IsInf(exKurt, 1) { 108 t.Errorf("Expected +Inf excess kurtosis for alpha <= 4, got %v", exKurt) 109 } 110 }