gonum.org/v1/gonum@v0.14.0/stat/distuv/chi_test.go (about)

     1  // Copyright ©2021 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  	"gonum.org/v1/gonum/floats/scalar"
    15  )
    16  
    17  func TestChiProb(t *testing.T) {
    18  	t.Parallel()
    19  	for _, test := range []struct {
    20  		x, k, want float64
    21  	}{
    22  		{10, 3, 1.538919725341288e-20},
    23  		{2.3, 3, 0.2997000593061405},
    24  		{0.8, 0.2, 0.1702707693447167},
    25  	} {
    26  		pdf := Chi{test.k, nil}.Prob(test.x)
    27  		if !scalar.EqualWithinAbsOrRel(pdf, test.want, 1e-10, 1e-10) {
    28  			t.Errorf("Pdf mismatch, x = %v, K = %v. Got %v, want %v", test.x, test.k, pdf, test.want)
    29  		}
    30  	}
    31  }
    32  
    33  func TestChiCDF(t *testing.T) {
    34  	t.Parallel()
    35  	for _, test := range []struct {
    36  		x, k, want float64
    37  	}{
    38  		// Values calculated with scipy.stats.chi.cdf
    39  		{0, 1, 0},
    40  		{0.01, 5, 5.319040436531812e-12},
    41  		{0.05, 3, 3.3220267268523235e-05},
    42  		{0.5, 2, 0.1175030974154046},
    43  		{0.95, 3, 0.17517554009157732},
    44  		{0.99, 5, 0.035845177452671864},
    45  		{1, 1, 0.6826894921370859},
    46  		{1.5, 4, 0.3101135068635068},
    47  		{10, 10, 1},
    48  		{25, 15, 1},
    49  	} {
    50  		cdf := Chi{test.k, nil}.CDF(test.x)
    51  		if !scalar.EqualWithinAbsOrRel(cdf, test.want, 1e-10, 1e-10) {
    52  			t.Errorf("CDF mismatch, x = %v, K = %v. Got %v, want %v", test.x, test.k, cdf, test.want)
    53  		}
    54  	}
    55  }
    56  
    57  func TestChi(t *testing.T) {
    58  	t.Parallel()
    59  	src := rand.New(rand.NewSource(1))
    60  	for i, b := range []Chi{
    61  		{3, src},
    62  		{1.5, src},
    63  		{0.9, src},
    64  	} {
    65  		testChi(t, b, i)
    66  	}
    67  }
    68  
    69  func testChi(t *testing.T, c Chi, i int) {
    70  	const (
    71  		tol  = 1e-2
    72  		n    = 1e6
    73  		bins = 50
    74  	)
    75  	x := make([]float64, n)
    76  	generateSamples(x, c)
    77  	sort.Float64s(x)
    78  
    79  	testRandLogProbContinuous(t, i, 0, x, c, tol, bins)
    80  	checkMean(t, i, x, c, tol)
    81  	checkMedian(t, i, x, c, tol)
    82  	checkVarAndStd(t, i, x, c, tol)
    83  	checkEntropy(t, i, x, c, tol)
    84  	checkExKurtosis(t, i, x, c, 7e-2)
    85  	checkProbContinuous(t, i, x, 0, math.Inf(1), c, 1e-5)
    86  	checkQuantileCDFSurvival(t, i, x, c, 1e-2)
    87  
    88  	expectedMode := math.Sqrt(c.K - 1)
    89  	if !scalar.Same(c.Mode(), expectedMode) {
    90  		t.Errorf("Mode is not equal to sqrt(k - 1). Got %v, want %v", c.Mode(), expectedMode)
    91  	}
    92  	if c.NumParameters() != 1 {
    93  		t.Errorf("NumParameters is not 1. Got %v", c.NumParameters())
    94  	}
    95  	survival := c.Survival(-0.00001)
    96  	if survival != 1 {
    97  		t.Errorf("Survival is not 1 for negative argument. Got %v", survival)
    98  	}
    99  }