github.com/gopherd/gonum@v0.0.4/stat/samplemv/halton_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 samplemv
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  
    11  	"math/rand"
    12  
    13  	"github.com/gopherd/gonum/mat"
    14  	"github.com/gopherd/gonum/stat/distmv"
    15  )
    16  
    17  func TestHalton(t *testing.T) {
    18  	for cas, test := range []struct {
    19  		n int
    20  		d int
    21  	}{
    22  		{10, 1},
    23  		{100, 2},
    24  		{1000, 3},
    25  	} {
    26  		src := rand.New(rand.NewSource(1))
    27  		// Generate the samples.
    28  		batch := mat.NewDense(test.n, test.d, nil)
    29  		Halton{Kind: Owen, Q: distmv.NewUnitUniform(test.d, nil), Src: src}.Sample(batch)
    30  
    31  		// In each dimension, the samples should be stratefied according to the
    32  		// prime for that dimension. There should be at most 1 sample per
    33  		// 1/b^k block, where k is log(n)/log(b).
    34  		for d := 0; d < test.d; d++ {
    35  			b := float64(nthPrime(d))
    36  			fk := math.Log(float64(test.n)) / math.Log(b)
    37  			k := math.Ceil(fk)
    38  			den := math.Pow(b, k)
    39  			m := make(map[int]int)
    40  			for i := 0; i < test.n; i++ {
    41  				bucket := int(batch.At(i, d) * den)
    42  				m[bucket]++
    43  			}
    44  			for bucket, n := range m {
    45  				if n > 1 {
    46  					t.Errorf("case %d: bucket %v has %v samples", cas, bucket, n)
    47  				}
    48  			}
    49  		}
    50  	}
    51  }