gonum.org/v1/gonum@v0.14.0/stat/distmv/normalbench_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 distmv
     6  
     7  import (
     8  	"log"
     9  	"testing"
    10  
    11  	"golang.org/x/exp/rand"
    12  
    13  	"gonum.org/v1/gonum/mat"
    14  )
    15  
    16  func BenchmarkMarginalNormal10(b *testing.B) {
    17  	sz := 10
    18  	rnd := rand.New(rand.NewSource(1))
    19  	normal := randomNormal(sz, rnd)
    20  	normal.CovarianceMatrix(&mat.SymDense{}) // pre-compute sigma
    21  	b.ResetTimer()
    22  	for i := 0; i < b.N; i++ {
    23  		marg, ok := normal.MarginalNormal([]int{1}, nil)
    24  		if !ok {
    25  			b.Error("bad test")
    26  		}
    27  		_ = marg
    28  	}
    29  }
    30  
    31  func BenchmarkMarginalNormalReset10(b *testing.B) {
    32  	sz := 10
    33  	rnd := rand.New(rand.NewSource(1))
    34  	normal := randomNormal(sz, rnd)
    35  	b.ResetTimer()
    36  	for i := 0; i < b.N; i++ {
    37  		marg, ok := normal.MarginalNormal([]int{1}, nil)
    38  		if !ok {
    39  			b.Error("bad test")
    40  		}
    41  		_ = marg
    42  	}
    43  }
    44  
    45  func BenchmarkMarginalNormalSingle10(b *testing.B) {
    46  	sz := 10
    47  	rnd := rand.New(rand.NewSource(1))
    48  	normal := randomNormal(sz, rnd)
    49  	b.ResetTimer()
    50  	for i := 0; i < b.N; i++ {
    51  		marg := normal.MarginalNormalSingle(1, nil)
    52  		_ = marg
    53  	}
    54  }
    55  
    56  func randomNormal(sz int, rnd *rand.Rand) *Normal {
    57  	mu := make([]float64, sz)
    58  	for i := range mu {
    59  		mu[i] = rnd.Float64()
    60  	}
    61  	data := make([]float64, sz*sz)
    62  	for i := range data {
    63  		data[i] = rnd.Float64()
    64  	}
    65  	dM := mat.NewDense(sz, sz, data)
    66  	var sigma mat.SymDense
    67  	sigma.SymOuterK(1, dM)
    68  
    69  	normal, ok := NewNormal(mu, &sigma, nil)
    70  	if !ok {
    71  		log.Fatal("bad test, not pos def")
    72  	}
    73  	return normal
    74  }