sampler

package
v0.0.0-...-6b453b2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const NeighborVarMax = 12

NeighborVarMax is the max size of the neighborhood allowed for a variable that we will collapse. Note that it includes the variable itself, so the total size of input space is 2^(M-1) where M is NeighborVarMax.

Variables

This section is empty.

Functions

func ChainConvergence

func ChainConvergence(chains []*Chain, distFunc Measure, mergedVars []*model.Variable) ([]float64, error)

ChainConvergence returns an array of floats that corresponds to the array of variables in the chains. Each float is a measure of the current convergence of the specified variable, where values close to 1.0 are better. Currently we return 1.0 for any collapsed variable. Note that as an optimization, this function will accept variables that are pre-merged. If an empty array is passed, variables will be merged automatically

func MergeChains

func MergeChains(chains []*Chain) ([]*model.Variable, error)

MergeChains returns a single variable array from multiple chains suitable for marginal dist calculations.

Types

type AdaptiveSampler

type AdaptiveSampler interface {
	Adapt(chains []*Chain, newChainCount int) ([]*Chain, error)
}

An AdaptiveSampler accepts a list of current chains and returns a new list ready to advance. The simplest AdaptiveSampler just returns the chains passed and is equivalent to whatever base sampler is currently in use.

type Chain

type Chain struct {
	Target            *model.Model
	Sampler           FullSampler
	ConvergenceWindow int
	ChainHistory      []*buffer.CircularInt
	TotalSampleCount  int64
	LastSample        []int
}

Chain provides functionality around a Gibbs sampler.

func NewChain

func NewChain(mod *model.Model, samp FullSampler, cw int, burnIn int64) (*Chain, error)

NewChain returns a chain ready to go. It even performs burnin.

func (*Chain) AdvanceChain

func (c *Chain) AdvanceChain(wg *sync.WaitGroup) error

AdvanceChain asynchonously generates samples until all variables have been sampled at least ConvergeWindow times. Variables that are Fixed or Collapsed are not checked for ConvergeWindow times

func (*Chain) ChainDist

func (c *Chain) ChainDist(distFunc Measure, varIdx int, mergedVar *model.Variable) (float64, float64, error)

ChainDist returns the within-chain and between-chain error based for the given Measure function against the specified variable. varIdx is the index of the variable under consideration mergedVar is a variable represented the merged chain estimate of the marginal The returned tuple is (within-chain, between-chain)

type ConvergenceSampler

type ConvergenceSampler struct {
	BaseModel *model.Model
	DistFunc  Measure
	Gen       *rand.Generator
	MaxChains int
}

ConvergenceSampler creates new collapsed chains based on convergence metrics.

func NewConvergenceSampler

func NewConvergenceSampler(gen *rand.Generator, m *model.Model, d Measure) (*ConvergenceSampler, error)

NewConvergenceSampler create a new IdentitySampler.

func (*ConvergenceSampler) Adapt

func (c *ConvergenceSampler) Adapt(chains []*Chain, newChainCount int) ([]*Chain, error)

Adapt for a ConvergenceSampler creates new chains with collapsed variables. The variable to collapse is selected from a probability distribution weighted by a convergence metric.

type FullSampler

type FullSampler interface {
	Sample([]int) (int, error)
}

A FullSampler populates the given array with values from the model (e.e. Gibbs sampling). The array MUST be the same size as the variables being sampled. Note that this call pattern (mutable param) parallels the samplers in the gonum stats subpackags. Samplers update the int slice and then return the index of the selected variable updated.

type GibbsCollapsed

type GibbsCollapsed struct {
	// contains filtered or unexported fields
}

GibbsCollapsed supports collapsing specified variables It is a smart wrapper around our gibbs-simple sampler.

func NewGibbsCollapsed

func NewGibbsCollapsed(gen *rand.Generator, m *model.Model) (*GibbsCollapsed, error)

NewGibbsCollapsed creates a new sampler

func (*GibbsCollapsed) BlanketSize

func (g *GibbsCollapsed) BlanketSize(v *model.Variable) int

BlanketSize return the variable's neighborhood size

func (*GibbsCollapsed) Collapse

func (g *GibbsCollapsed) Collapse(varIdx int) (*model.Variable, error)

Collapse integrates out the variable given by index. If the index is < 0, a variable is randomly chosen. The collapsed variable is returned for inspection.

func (*GibbsCollapsed) FunctionCount

func (g *GibbsCollapsed) FunctionCount(v *model.Variable) int

FunctionCount returns the variable's factor count

func (*GibbsCollapsed) FunctionsChanged

func (g *GibbsCollapsed) FunctionsChanged() error

FunctionsChanged is called when the models Function array has changed. That means we need to update some of our bookkeeping.

func (*GibbsCollapsed) Sample

func (g *GibbsCollapsed) Sample(s []int) (int, error)

Sample returns a single sample - implements FullSampler

type GibbsSimple

type GibbsSimple struct {
	// contains filtered or unexported fields
}

GibbsSimple is our baseline, simple to code Gibbs sampler

func NewGibbsSimple

func NewGibbsSimple(gen *rand.Generator, m *model.Model) (*GibbsSimple, error)

NewGibbsSimple creates a new sampler

func (*GibbsSimple) FunctionsChanged

func (g *GibbsSimple) FunctionsChanged() error

FunctionsChanged is called when the models Function array has changed. That means we need to update some of our bookkeeping.

func (*GibbsSimple) Sample

func (g *GibbsSimple) Sample(s []int) (int, error)

Sample returns a single sample - implements FullSampler

func (*GibbsSimple) SampleVar

func (g *GibbsSimple) SampleVar(varIdx int, s []int) (int, error)

SampleVar samples from the pre-selected varIdx variable.

type IdentitySampler

type IdentitySampler struct{}

IdentitySampler is just a non-adaptive strategy.

func NewIdentitySampler

func NewIdentitySampler() (*IdentitySampler, error)

NewIdentitySampler create a new IdentitySampler

func (*IdentitySampler) Adapt

func (i *IdentitySampler) Adapt(chains []*Chain, newChainCount int) ([]*Chain, error)

Adapt for an IdentitySampler is just an identity operation (thus the clever name :)

type Measure

type Measure func(v1 *model.Variable, v2 *model.Variable) float64

Measure is an error metric used by ChainConverge. One example is our model.HellingerDiff

type UniformSampler

type UniformSampler struct {
	// contains filtered or unexported fields
}

UniformSampler provides uniform sampling for our interfaces

func NewUniformSampler

func NewUniformSampler(gen *rand.Generator, maxVars int) (*UniformSampler, error)

NewUniformSampler creates a new uniform sampler

func (*UniformSampler) UniSample

func (s *UniformSampler) UniSample(card int) (int, error)

UniSample samples uniformly from [0, card).

func (*UniformSampler) VarSample

func (s *UniformSampler) VarSample(vs []*model.Variable, excludeCollapsed bool) (int, error)

VarSample implements VarSample interface. If excludeCollapsed is true, no collapsed variable will be selected. Variable with a Fixed Val will never be selected.

func (*UniformSampler) WeightedSample

func (s *UniformSampler) WeightedSample(card int, weights []float64) (int, error)

WeightedSample samples from [0, card) based on the card-sized array of weights. Mainly for sampling directly from a variable's marginal.

type VarSampler

type VarSampler interface {
	VarSample(vs []*model.Variable, excludeCollapsed bool) (int, error)
}

A VarSampler selects from an array of variables with some probability. Currently used yo select the next variable to sample in a chain in our Gibbs sampler. The selection routine should exclude variables with a Fixed Value and optionally exclude Variable with Collapsed==true.

type WeightedSampler

type WeightedSampler interface {
	WeightedSample(card int, weights []float64) (int, error)
}

WeightedSampler provides a way to select from a cardinality of values given an array of weights.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL