github.com/gopherd/gonum@v0.0.4/mathext/erf_test.go (about)

     1  // Copyright ©2017 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 mathext
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/gopherd/gonum/floats"
    11  	"github.com/gopherd/gonum/floats/scalar"
    12  )
    13  
    14  func TestNormalQuantile(t *testing.T) {
    15  	t.Parallel()
    16  	// Values from https://www.johndcook.com/blog/normal_cdf_inverse/
    17  	p := []float64{
    18  		0.0000001,
    19  		0.00001,
    20  		0.001,
    21  		0.05,
    22  		0.15,
    23  		0.25,
    24  		0.35,
    25  		0.45,
    26  		0.55,
    27  		0.65,
    28  		0.75,
    29  		0.85,
    30  		0.95,
    31  		0.999,
    32  		0.99999,
    33  		0.9999999,
    34  	}
    35  	ans := []float64{
    36  		-5.199337582187471,
    37  		-4.264890793922602,
    38  		-3.090232306167813,
    39  		-1.6448536269514729,
    40  		-1.0364333894937896,
    41  		-0.6744897501960817,
    42  		-0.38532046640756773,
    43  		-0.12566134685507402,
    44  		0.12566134685507402,
    45  		0.38532046640756773,
    46  		0.6744897501960817,
    47  		1.0364333894937896,
    48  		1.6448536269514729,
    49  		3.090232306167813,
    50  		4.264890793922602,
    51  		5.199337582187471,
    52  	}
    53  	for i, v := range p {
    54  		got := NormalQuantile(v)
    55  		if !scalar.EqualWithinAbsOrRel(got, ans[i], 1e-10, 1e-10) {
    56  			t.Errorf("Quantile mismatch. Case %d, want: %v, got: %v", i, ans[i], got)
    57  		}
    58  	}
    59  }
    60  
    61  var nqtmp float64
    62  
    63  func BenchmarkNormalQuantile(b *testing.B) {
    64  	ps := make([]float64, 1000) // ensure there are small values
    65  	floats.Span(ps, 0, 1)
    66  	for i := 0; i < b.N; i++ {
    67  		for _, v := range ps {
    68  			nqtmp = NormalQuantile(v)
    69  		}
    70  	}
    71  }