This repository has been archived by the owner on Dec 22, 2018. It is now read-only.
/
sample_test.go
98 lines (86 loc) · 2.43 KB
/
sample_test.go
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 sample
import (
"math"
"sort"
"testing"
"github.com/gonum/stat"
"github.com/gonum/stat/dist"
)
type lhDist interface {
Quantile(float64) float64
CDF(float64) float64
}
func TestLatinHypercube(t *testing.T) {
for _, nSamples := range []int{1, 2, 5, 10, 20} {
samples := make([]float64, nSamples)
for _, dist := range []lhDist{
dist.Uniform{Min: 0, Max: 1},
dist.Uniform{Min: 0, Max: 10},
dist.Normal{Mu: 5, Sigma: 3},
} {
LatinHypercube(samples, dist, nil)
sort.Float64s(samples)
for i, v := range samples {
p := dist.CDF(v)
if p < float64(i)/float64(nSamples) || p > float64(i+1)/float64(nSamples) {
t.Errorf("probability out of bounds")
}
}
}
}
}
func TestImportance(t *testing.T) {
// Test by finding the expected value of a Normal
trueMean := 3.0
target := dist.Normal{Mu: trueMean, Sigma: 2}
proposal := dist.Normal{Mu: 0, Sigma: 5}
nSamples := 100000
x := make([]float64, nSamples)
weights := make([]float64, nSamples)
Importance(x, weights, target, proposal)
ev := stat.Mean(x, weights)
if math.Abs(ev-trueMean) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", trueMean, ev)
}
}
func TestRejection(t *testing.T) {
// Test by finding the expected value of a Normal
trueMean := 3.0
target := dist.Normal{Mu: trueMean, Sigma: 2}
proposal := dist.Normal{Mu: 0, Sigma: 5}
nSamples := 100000
x := make([]float64, nSamples)
Rejection(x, target, proposal, 100, nil)
ev := stat.Mean(x, nil)
if math.Abs(ev-trueMean) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", trueMean, ev)
}
}
type condNorm struct {
Sigma float64
}
func (c condNorm) ConditionalRand(y float64) float64 {
return dist.Normal{Mu: y, Sigma: c.Sigma}.Rand()
}
func (c condNorm) ConditionalLogProb(x, y float64) float64 {
return dist.Normal{Mu: y, Sigma: c.Sigma}.LogProb(x)
}
func TestMetropolisHastings(t *testing.T) {
// Test by finding the expected value of a Normal
trueMean := 3.0
target := dist.Normal{Mu: trueMean, Sigma: 2}
proposal := condNorm{Sigma: 5}
burnin := 500
nSamples := 100000 + burnin
x := make([]float64, nSamples)
MetropolisHastings(x, 100, target, proposal, nil)
// Remove burnin
x = x[burnin:]
ev := stat.Mean(x, nil)
if math.Abs(ev-trueMean) > 1e-2 {
t.Errorf("Mean mismatch: Want %v, got %v", trueMean, ev)
}
}