github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/stat/distuv/gumbel_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  	"github.com/jingcheng-WU/gonum/floats/scalar"
    14  )
    15  
    16  func TestGumbelRightProbCDF(t *testing.T) {
    17  	t.Parallel()
    18  	for _, test := range []struct {
    19  		x, mu, beta, wantProb, wantCDF float64
    20  	}{
    21  		// Values calculated with scipy.stats.gumbel_r .
    22  		{-2, 0, 1, 0.0045662814201279153, 0.00061797898933109343},
    23  		{0.01, 0, 1, 0.36786110881643569, 0.37155817442380817},
    24  		{6, 0, 1, 0.0024726155730149077, 0.99752431739275249},
    25  
    26  		// Values calculated with Wolfram Alpha's ExtremeValueDistribution.
    27  		{0.1, 2, 5, 0.06776411497087929, 0.231706315790068},
    28  		{0.1, -2, 5, 0.06811997894673336, 0.5183799456323944},
    29  		{-2.1, -2, 0.1, 1.793740787340169, 0.06598803584531238},
    30  	} {
    31  		g := GumbelRight{Mu: test.mu, Beta: test.beta}
    32  		pdf := g.Prob(test.x)
    33  		if !scalar.EqualWithinAbsOrRel(pdf, test.wantProb, 1e-12, 1e-12) {
    34  			t.Errorf("Prob mismatch, x = %v, mu = %v, beta = %v. Got %v, want %v", test.x, test.mu, test.beta, pdf, test.wantProb)
    35  		}
    36  		cdf := g.CDF(test.x)
    37  		if !scalar.EqualWithinAbsOrRel(cdf, test.wantCDF, 1e-12, 1e-12) {
    38  			t.Errorf("CDF mismatch, x = %v, mu = %v, beta = %v. Got %v, want %v", test.x, test.mu, test.beta, cdf, test.wantCDF)
    39  		}
    40  	}
    41  }
    42  
    43  func TestGumbelRight(t *testing.T) {
    44  	t.Parallel()
    45  	src := rand.New(rand.NewSource(1))
    46  	for i, b := range []GumbelRight{
    47  		{0, 1, src},
    48  		{-5, 6, src},
    49  		{3, 0.1, src},
    50  	} {
    51  		testGumbelRight(t, b, i)
    52  	}
    53  }
    54  
    55  func testGumbelRight(t *testing.T, g GumbelRight, i int) {
    56  	const (
    57  		tol  = 1e-2
    58  		n    = 5e5
    59  		bins = 50
    60  	)
    61  	x := make([]float64, n)
    62  	generateSamples(x, g)
    63  	sort.Float64s(x)
    64  
    65  	min := math.Inf(-1)
    66  	testRandLogProbContinuous(t, i, min, x, g, tol, bins)
    67  	checkProbContinuous(t, i, x, math.Inf(-1), math.Inf(1), g, 1e-10)
    68  	checkEntropy(t, i, x, g, tol)
    69  	checkMean(t, i, x, g, tol)
    70  	checkMedian(t, i, x, g, tol)
    71  	checkVarAndStd(t, i, x, g, tol)
    72  	checkExKurtosis(t, i, x, g, 1e-1)
    73  	checkSkewness(t, i, x, g, 5e-2)
    74  	checkQuantileCDFSurvival(t, i, x, g, 5e-3)
    75  	if g.Mu != g.Mode() {
    76  		t.Errorf("Mismatch in mode value: got %v, want %g", g.Mode(), g.Mu)
    77  	}
    78  	if g.NumParameters() != 2 {
    79  		t.Errorf("Mismatch in NumParameters: got %v, want 2", g.NumParameters())
    80  	}
    81  }