File: general_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 (98 lines) | stat: -rw-r--r-- 2,580 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright ©2015 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"

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

type prober interface {
	Prob(x []float64) float64
	LogProb(x []float64) float64
}

type probCase struct {
	dist    prober
	loc     []float64
	logProb float64
}

func testProbability(t *testing.T, cases []probCase) {
	for _, test := range cases {
		logProb := test.dist.LogProb(test.loc)
		if math.Abs(logProb-test.logProb) > 1e-14 {
			t.Errorf("LogProb mismatch: want: %v, got: %v", test.logProb, logProb)
		}
		prob := test.dist.Prob(test.loc)
		if math.Abs(prob-math.Exp(test.logProb)) > 1e-14 {
			t.Errorf("Prob mismatch: want: %v, got: %v", math.Exp(test.logProb), prob)
		}
	}
}

func generateSamples(x *mat.Dense, r Rander) {
	n, _ := x.Dims()
	for i := 0; i < n; i++ {
		r.Rand(x.RawRowView(i))
	}
}

type Meaner interface {
	Mean([]float64) []float64
}

func checkMean(t *testing.T, cas int, x *mat.Dense, m Meaner, tol float64) {
	mean := m.Mean(nil)

	// Check that the answer is identical when using nil or non-nil.
	mean2 := make([]float64, len(mean))
	m.Mean(mean2)
	if !floats.Equal(mean, mean2) {
		t.Errorf("Mean mismatch when providing nil and slice. Case %v", cas)
	}

	// Check that the mean matches the samples.
	r, _ := x.Dims()
	col := make([]float64, r)
	meanEst := make([]float64, len(mean))
	for i := range meanEst {
		meanEst[i] = stat.Mean(mat.Col(col, i, x), nil)
	}
	if !floats.EqualApprox(mean, meanEst, tol) {
		t.Errorf("Returned mean and sample mean mismatch. Case %v. Empirical %v, returned %v", cas, meanEst, mean)
	}
}

type Cover interface {
	CovarianceMatrix(*mat.SymDense)
}

func checkCov(t *testing.T, cas int, x *mat.Dense, c Cover, tol float64) {
	var cov mat.SymDense
	c.CovarianceMatrix(&cov)
	n := cov.SymmetricDim()
	cov2 := mat.NewSymDense(n, nil)
	c.CovarianceMatrix(cov2)
	if !mat.Equal(&cov, cov2) {
		t.Errorf("Cov mismatch when providing nil and matrix. Case %v", cas)
	}
	var cov3 mat.SymDense
	c.CovarianceMatrix(&cov3)
	if !mat.Equal(&cov, &cov3) {
		t.Errorf("Cov mismatch when providing zero matrix. Case %v", cas)
	}

	// Check that the covariance matrix matches the samples
	var covEst mat.SymDense
	stat.CovarianceMatrix(&covEst, x, nil)
	if !mat.EqualApprox(&covEst, &cov, tol) {
		t.Errorf("Return cov and sample cov mismatch. Cas %v.\nGot:\n%0.4v\nWant:\n%0.4v", cas, mat.Formatted(&cov), mat.Formatted(&covEst))
	}
}