File: dirichlet_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (68 lines) | stat: -rw-r--r-- 1,294 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright ©2016 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package distmv

import (
	"math"
	"testing"

	"golang.org/x/exp/rand"

	"gonum.org/v1/gonum/mat"
)

func TestDirichlet(t *testing.T) {
	// Data from Scipy.
	for cas, test := range []struct {
		Dir  *Dirichlet
		x    []float64
		prob float64
	}{
		{
			NewDirichlet([]float64{1, 1, 1}, nil),
			[]float64{0.2, 0.3, 0.5},
			2.0,
		},
		{
			NewDirichlet([]float64{0.6, 10, 8.7}, nil),
			[]float64{0.2, 0.3, 0.5},
			0.24079612737071665,
		},
	} {
		p := test.Dir.Prob(test.x)
		if math.Abs(p-test.prob) > 1e-14 {
			t.Errorf("Probablility mismatch. Case %v. Got %v, want %v", cas, p, test.prob)
		}
	}

	rnd := rand.New(rand.NewSource(1))
	for cas, test := range []struct {
		Dir *Dirichlet
	}{
		{
			NewDirichlet([]float64{1, 1, 1}, rnd),
		},
		{
			NewDirichlet([]float64{2, 3}, rnd),
		},
		{
			NewDirichlet([]float64{0.2, 0.3}, rnd),
		},
		{
			NewDirichlet([]float64{0.2, 4}, rnd),
		},
		{
			NewDirichlet([]float64{0.1, 4, 20}, rnd),
		},
	} {
		const n = 1e5
		d := test.Dir
		dim := d.Dim()
		x := mat.NewDense(n, dim, nil)
		generateSamples(x, d)
		checkMean(t, cas, x, d, 1e-2)
		checkCov(t, cas, x, d, 1e-2)
	}
}