github.com/gopherd/gonum@v0.0.4/stat/distuv/chisquared_test.go (about)

     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  
     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 TestChiSquaredProb(t *testing.T) {
    18  	t.Parallel()
    19  	for _, test := range []struct {
    20  		x, k, want float64
    21  	}{
    22  		{10, 3, 0.0085003666025203432},
    23  		{2.3, 3, 0.19157345407042367},
    24  		{0.8, 0.2, 0.080363259903912673},
    25  	} {
    26  		pdf := ChiSquared{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 TestChiSquaredCDF(t *testing.T) {
    34  	t.Parallel()
    35  	for _, test := range []struct {
    36  		x, k, want float64
    37  	}{
    38  		// Values calculated with scipy.stats.chi2.cdf
    39  		{0, 1, 0},
    40  		{0.01, 5, 5.3002700426865167e-07},
    41  		{0.05, 3, 0.002929332764619924},
    42  		{0.5, 2, 0.22119921692859512},
    43  		{0.95, 3, 0.1866520918701263},
    44  		{0.99, 5, 0.036631697220869196},
    45  		{1, 1, 0.68268949213708596},
    46  		{1.5, 4, 0.17335853270322427},
    47  		{10, 10, 0.55950671493478743},
    48  		{25, 15, 0.95005656637357172},
    49  	} {
    50  		cdf := ChiSquared{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 TestChiSquared(t *testing.T) {
    58  	t.Parallel()
    59  	src := rand.New(rand.NewSource(1))
    60  	for i, b := range []ChiSquared{
    61  		{3, src},
    62  		{1.5, src},
    63  		{0.9, src},
    64  	} {
    65  		testChiSquared(t, b, i)
    66  	}
    67  }
    68  
    69  func testChiSquared(t *testing.T, c ChiSquared, 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  	checkVarAndStd(t, i, x, c, tol)
    82  	checkExKurtosis(t, i, x, c, 7e-2)
    83  	checkProbContinuous(t, i, x, 0, math.Inf(1), c, 1e-5)
    84  	checkQuantileCDFSurvival(t, i, x, c, 1e-2)
    85  
    86  	expectedMode := math.Max(c.K-2, 0)
    87  	if c.Mode() != expectedMode {
    88  		t.Errorf("Mode is not equal to max(k - 2, 0). Got %v, want %v", c.Mode(), expectedMode)
    89  	}
    90  	if c.NumParameters() != 1 {
    91  		t.Errorf("NumParameters is not 1. Got %v", c.NumParameters())
    92  	}
    93  	survival := c.Survival(-0.00001)
    94  	if survival != 1 {
    95  		t.Errorf("Survival is not 1 for negative argument. Got %v", survival)
    96  	}
    97  }