github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/stat/sampleuv/example_rate_test.go (about)

     1  // Copyright ©2015 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 sampleuv_test
     6  
     7  import (
     8  	"github.com/jingcheng-WU/gonum/stat/distuv"
     9  	"github.com/jingcheng-WU/gonum/stat/sampleuv"
    10  )
    11  
    12  func ExampleMetropolisHastings_samplingRate() {
    13  	// See Burnin example for a description of these quantities.
    14  	n := 1000
    15  	burnin := 300
    16  	var initial float64
    17  	target := distuv.Weibull{K: 5, Lambda: 0.5}
    18  	proposal := ProposalDist{Sigma: 0.2}
    19  
    20  	// Successive samples are correlated with one another through the
    21  	// Markov Chain defined by the proposal distribution. One may use
    22  	// a sampling rate to decrease the correlation in the samples for
    23  	// an increase in computation cost. The rate parameter specifies
    24  	// that for every accepted sample stored in `samples`, rate - 1 accepted
    25  	// samples are not stored in `samples`.
    26  	rate := 50
    27  
    28  	mh := sampleuv.MetropolisHastings{
    29  		Initial:  initial,
    30  		Target:   target,
    31  		Proposal: proposal,
    32  		BurnIn:   burnin,
    33  		Rate:     rate,
    34  	}
    35  
    36  	samples := make([]float64, n)
    37  	mh.Sample(samples)
    38  }