Package gPy :: Module Samplers :: Class BNSampler
[hide private]
[frames] | no frames]

Class BNSampler

source code


For sampling from Bayesian networks

Instance Methods [hide private]
 
__init__(self, bnm)
Construct a sampler from a dependency network
source code
List
forward_sample(self)
Forward sample from an (unconditional) BN
source code
Tuple
importance_sample(self)
Importance sample from a BN
source code
List
rejection_sample(self)
Rejection sample from a BN
source code

Inherited from DNSampler: condition, gibbs_sample, uncondition, variable_index, variables

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Instance Variables [hide private]
List _condition_index
Instantiated variables have their values, others have None
Tuple _cpts
For each variable, its CPT (in topological order).
Tuple _dists
For each variable, a dictionary mapping parent instantiations to a MultinomialSampler object
Tuple _indextuple
The tuple (0,1, ...n) where n is the number of variables
Tuple _indices
For each variable, a tuple of the indices corresponding to its parents
List _instskel
A list of Nones of the right length to hold a full joint instantiation.
Dictionary _var2index
Maps a variable to its index in _variables
Tuple _variables
An ordering of the DN's variables used for example, for Gibbs sampling
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, bnm)
(Constructor)

source code 

Construct a sampler from a dependency network

Parameters:
  • dnm - Dependency network
  • variables - An order in which to sample values for the variable in eg Gibbs sampling. If not supplied an arbitrary ordering is used.
Overrides: object.__init__
(inherited documentation)

forward_sample(self)

source code 

Forward sample from an (unconditional) BN

Returns: List
A joint instantiation
Raises:
  • KeyError - If BN has evidence

importance_sample(self)

source code 

Importance sample from a BN

Returns: Tuple
A joint instantiation and a weight

rejection_sample(self)

source code 

Rejection sample from a BN

Returns: List
A joint instantiation. If a rejection has occurred then the joint instantiation will contain None values

Instance Variable Details [hide private]

_cpts

For each variable, its CPT (in topological order). CPTs are not Parameters.CPT objects. self._cpts[i][parent_inst][cond] is the probability of the ith variable taking value cond when its parents have instantiation parent_inst, where parent_inst is a tuple of parent values.
Type:
Tuple

_var2index

Maps a variable to its index in _variables
Type:
Dictionary