1// Copyright ©2016 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 5package distuv 6 7import ( 8 "sort" 9 "testing" 10 11 "golang.org/x/exp/rand" 12 13 "gonum.org/v1/gonum/floats" 14) 15 16func TestGamma(t *testing.T) { 17 // Values a comparison with scipy 18 for _, test := range []struct { 19 x, alpha, want float64 20 }{ 21 {0.9, 0.1, 0.046986817861555757}, 22 {0.9, 0.01, 0.0045384353289090401}, 23 {0.45, 0.01, 0.014137035997241795}, 24 } { 25 pdf := Gamma{Alpha: test.alpha, Beta: 1}.Prob(test.x) 26 if !floats.EqualWithinAbsOrRel(pdf, test.want, 1e-10, 1e-10) { 27 t.Errorf("Pdf mismatch. Got %v, want %v", pdf, test.want) 28 } 29 } 30 src := rand.New(rand.NewSource(1)) 31 for i, g := range []Gamma{ 32 33 {Alpha: 0.5, Beta: 0.8, Src: src}, 34 {Alpha: 0.9, Beta: 6, Src: src}, 35 {Alpha: 0.9, Beta: 500, Src: src}, 36 37 {Alpha: 1, Beta: 1, Src: src}, 38 39 {Alpha: 1.6, Beta: 0.4, Src: src}, 40 {Alpha: 2.6, Beta: 1.5, Src: src}, 41 {Alpha: 5.6, Beta: 0.5, Src: src}, 42 {Alpha: 30, Beta: 1.7, Src: src}, 43 {Alpha: 30.2, Beta: 1.7, Src: src}, 44 } { 45 testGamma(t, g, i) 46 } 47} 48 49func testGamma(t *testing.T, f Gamma, i int) { 50 // TODO(btracey): Replace this when Gamma implements FullDist. 51 const ( 52 tol = 1e-2 53 n = 1e5 54 bins = 50 55 ) 56 x := make([]float64, n) 57 generateSamples(x, f) 58 sort.Float64s(x) 59 60 testRandLogProbContinuous(t, i, 0, x, f, tol, bins) 61 checkMean(t, i, x, f, tol) 62 checkVarAndStd(t, i, x, f, 2e-2) 63 checkExKurtosis(t, i, x, f, 2e-1) 64 checkProbContinuous(t, i, x, f, 1e-3) 65 checkQuantileCDFSurvival(t, i, x, f, 5e-2) 66} 67