Documentation ¶
Index ¶
- Constants
- func ChainConvergence(chains []*Chain, distFunc Measure, mergedVars []*model.Variable) ([]float64, error)
- func MergeChains(chains []*Chain) ([]*model.Variable, error)
- type AdaptiveSampler
- type Chain
- type ConvergenceSampler
- type FullSampler
- type GibbsCollapsed
- type GibbsSimple
- type IdentitySampler
- type Measure
- type UniformSampler
- type VarSampler
- type WeightedSampler
Constants ¶
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
Types ¶
type AdaptiveSampler ¶
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 (*Chain) AdvanceChain ¶
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 ¶
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 ¶
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.
type GibbsSimple ¶
type GibbsSimple struct {
// contains filtered or unexported fields
}
GibbsSimple is our baseline, simple to code Gibbs sampler
func NewGibbsSimple ¶
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.
type IdentitySampler ¶
type IdentitySampler struct{}
IdentitySampler is just a non-adaptive strategy.
func NewIdentitySampler ¶
func NewIdentitySampler() (*IdentitySampler, error)
NewIdentitySampler create a new IdentitySampler
type Measure ¶
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 ¶
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 ¶
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.