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

     1  // Copyright ©2015 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  
    15  func TestLognormal(t *testing.T) {
    16  	t.Parallel()
    17  	src := rand.New(rand.NewSource(1))
    18  	for i, dist := range []LogNormal{
    19  		{Mu: 0.1, Sigma: 0.3, Src: src},
    20  		{Mu: 0.01, Sigma: 0.01, Src: src},
    21  		{Mu: 2, Sigma: 0.01, Src: src},
    22  	} {
    23  		const (
    24  			tol = 1e-2
    25  			n   = 1e5
    26  		)
    27  		x := make([]float64, n)
    28  		generateSamples(x, dist)
    29  		sort.Float64s(x)
    30  
    31  		checkMean(t, i, x, dist, tol)
    32  		checkVarAndStd(t, i, x, dist, tol)
    33  		checkEntropy(t, i, x, dist, tol)
    34  		checkExKurtosis(t, i, x, dist, 2e-1)
    35  		checkSkewness(t, i, x, dist, 5e-2)
    36  		checkMedian(t, i, x, dist, tol)
    37  		checkQuantileCDFSurvival(t, i, x, dist, tol)
    38  		checkProbContinuous(t, i, x, 0, math.Inf(1), dist, 1e-10)
    39  		checkProbQuantContinuous(t, i, x, dist, tol)
    40  		checkMode(t, i, x, dist, 1e-2, 1e-2)
    41  
    42  		logProb := dist.LogProb(-0.0001)
    43  		if !math.IsInf(logProb, -1) {
    44  			t.Errorf("Expected LogProb == -Inf for x < 0, got %v", logProb)
    45  		}
    46  		if dist.NumParameters() != 2 {
    47  			t.Errorf("Mismatch in NumParameters: got %v, want 2", dist.NumParameters())
    48  		}
    49  	}
    50  }
    51  
    52  // See https://github.com/gonum/gonum/issues/577 for details.
    53  func TestLognormalIssue577(t *testing.T) {
    54  	t.Parallel()
    55  	x := 1.0e-16
    56  	max := 1.0e-295
    57  	cdf := LogNormal{Mu: 0, Sigma: 1}.CDF(x)
    58  	if cdf <= 0 {
    59  		t.Errorf("LogNormal{0,1}.CDF(%e) should be positive. got: %e", x, cdf)
    60  	}
    61  	if cdf > max {
    62  		t.Errorf("LogNormal{0,1}.CDF(%e) is greater than %e. got: %e", x, max, cdf)
    63  	}
    64  }