File: example_burnin_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 (40 lines) | stat: -rw-r--r-- 1,345 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
// 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 sampleuv_test

import (
	"gonum.org/v1/gonum/stat/distuv"
	"gonum.org/v1/gonum/stat/sampleuv"
)

type ProposalDist struct {
	Sigma float64
}

func (p ProposalDist) ConditionalRand(y float64) float64 {
	return distuv.Normal{Mu: y, Sigma: p.Sigma}.Rand()
}

func (p ProposalDist) ConditionalLogProb(x, y float64) float64 {
	return distuv.Normal{Mu: y, Sigma: p.Sigma}.LogProb(x)
}

func ExampleMetropolisHastings_burnin() {
	n := 1000    // The number of samples to generate.
	burnin := 50 // Number of samples to ignore at the start.
	var initial float64
	// target is the distribution from which we would like to sample.
	target := distuv.Weibull{K: 5, Lambda: 0.5}
	// proposal is the proposal distribution. Here, we are choosing
	// a tight Gaussian distribution around the current location. In
	// typical problems, if Sigma is too small, it takes a lot of samples
	// to move around the distribution. If Sigma is too large, it can be hard
	// to find acceptable samples.
	proposal := ProposalDist{Sigma: 0.2}

	samples := make([]float64, n)
	mh := sampleuv.MetropolisHastings{Initial: initial, Target: target, Proposal: proposal, BurnIn: burnin}
	mh.Sample(samples)
}