Table Of Contents

Previous topic

Parameter Learning

Next topic

Miscellaneous Packages

Inference Methods

The package inference contains modules that implement different inference methods as well as helper modules:

engine module

The inference engine handles inference.

  • Unrolling the Ground Bayesian Network (‘propositionalize’ the the first-order representation)
  • Run an inference algorithm (e.g. Gibbs sampling)

There are different algorithms that will need to perform inference:

  • Simple query P(event|evidence)
  • Expectation Maximization (EM) for missing data / latent variables
  • Structure learning
  • Reference uncertainty
inference.engine.GBN

Ground Bayesian Network used for inference of type GBNGraph.

inference.engine.GBNS

At this point, only one ground Bayesian network exists, GBN. If posterior samples are collected from multiple chains, e.g. in the case of MCMC inference, it would be ideal to run these chains in paralell. However this would require multiple instantiations of the ground Bayesian network. Not just the values of the vertices are required, since the network stucture changes with reference uncertainty, the whole network needs to be duplicated.

We could envision a dict() of ground Bayesian networks. Note that copy.deepcopy() doesn’t work as it would also copy all other istances, e.g. Attribute objects.

For now, changes are run sequentially and the convergence diagnostics are run on the collected samples of the posterior.

inference.engine.addChildren(attr, gbnVertices)[source]

Loads the set of children attribute objects for all dependencies for the given GBN vertices of the same attribute class. The BGN is updated and potential new latent variables are added to the queue.

Parameters:
inference.engine.addExistParents(refGbnV)

An uncertain relationship of type n:k has a n-side and a k-side. A ReferenceVertex is instantiated for an attribute object of the n-side and it is allowd to connect to k k-side attribute objects (which were added to the GBN by initReferenceUncertainty()).

Parameters:refGbnVReferenceVertex instance
inference.engine.addParents(attr, gbnVertices)[source]

Loads the set of parent attribute objects for all dependencies for the given GBN vertices of the same attribute class. The BGN is updated and potential new latent variables are added to the queue.

Parameters:
inference.engine.gbnQ

GBNqueue data structure used to unroll the GBN

inference.engine.inferenceAlgo

The algorithm used for the approximate inference (e.g. gibbs,:mod:.mh )

inference.engine.initReferenceUncertainty(dep)[source]

When a uncertain relationship is first encountered, all the attribute objects that could be associated with the object that initiated the reference have to be loaded. Additionally, all the parent attributes of the exist attributes need to be loaded as well.

Parameters:depUncertainDependency instance
inference.engine.query

Current Query instance of type Query

inference.engine.reset()[source]

Resets the GBNGraph and GBNqueue instances.

inference.engine.unrollGBN()[source]

The unrolled Ground Bayesian Network is a subgraph of the full graph that d-seperated the event variables Y from the query from the rest of the graph given the evidence variables.

Note

Add proper description of algorithm

inference.engine.validateGBN()[source]

Validating the GBN is necessary as there might be missing data (either because there is no datapoint or the datapoint is missing from the dataset). The standard approach would be to use an EM algorithm to find the MLE for the missing data. In order for the inference method to work we need to have valid probability distributions. In case there is a GBN vertex that has no parent vertex but a probability distribution conditional on that parent attribute, one way to avoid invalid GBN structures is to add a sampling vertex. This is only possible (and reasonable) in case this sampling vertex does not depend on parent values it

query module

When making inference in a PRM, specifying a query is not as straight forward as in Bayesian Networks. A Query instance consists of event and evidence variables, the inference goal being to find a posterior distribution over the event variables given the evidence variables.

Two auxiliary data structures are used to specify event and evidence variables,

class inference.query.ObjsVariable(constraint, pkValues, complete=True)[source]

Data structure that is used to define a set of attribute objects that are associated with a specific Qvariable instance.

pkValues is a list of primary keys of the attribute class that the Qvariable instance is associated with. The ObjsVariable.constraint allows to specify a subset of all attribute objects in an expressive manner

  • inclusive ‘incl’ : only these attribute objects
  • exclusive ‘excl’ : all but these attribute objects

As an example, to create a query for an Entity:

objsStudent = ObjsVariable('incl', [(1,),(4,),(11,)])

Or in case of a query for a Relationship:

objsAdvisor = ObjsVariable('incl', [(1,3),(4,3),(11,3)])    
constraint

Constraint is either ‘excl’ or ‘incl’

pkValues

List of sets of primary keys. Even in case of an Entity with only one primary key, the list needs to consist of sets, e.g.

  • [ (pk,),(pk,),(pk,),(pk,),...] in case of an Entity
  • [(pk1,pk2),(pk1,pk2),(pk1,pk2),(pk1,pk2),....] in case of a Relationship
class inference.query.Query(event, evidence=None)[source]

When performing inference on the PRM, we are given a set \mathbb{Y} (event variables) and a set \mathbb{E} (evidence variables), the inference goal being to find

P(\mathbb{Y} \mid \mathbb{E})

computeObjEvidenceLookup()[source]

Computes the objEvidenceLookup dictionary

event

List of Qvariable instances representing event variables

evidence

List of Qvariable instances representing evidence variables

gbnVertexInEvidence(gbnVertex)[source]

Calls objInEvidence() with parameters gbnVertex.attr and gbnVertex.ID

Parameters:gbnVertexGBNvertex instance
objEvidenceLookup

Dictionary used to check whether attribute objects are part of the evidence. Format:

{ key = Attribute instance : value = ( ObjsVariable.constraint , [ List of GBNvertex.ID ] ) }

When unrolling a GBN we are creating a d-separated BN for the query P(\mathbb{Y} \mid \mathbb{E}). We need an efficient way to look up wheter a certain GBN node is in the evidence because this influences the structure of the induced graph, e.g.

  • If a loaded child is in \mathbb{E} -> common cause -> load parents of node and don’t load children’
  • If a loaded child is not in \mathbb{E} -> load children of node

The dictionary is computed by computeObjEvidenceLookup()

objInEvidence(attr, gbnID)[source]

Returns True if attribute object passed as argument is part of the evidence. One would think that the a dictionary wouldn’t be necessary and that a simple list containing all gbnvertexIDs would suffice. But this is not the case since a Qvariable (e.g. evidence for one attibute) can either be ‘inclusive’ or exclusive, so the dictionary is needed to check the ObjsVariable.constraint.

Parameters:
  • attrAttribute instance
  • gbnID – Attribute object ID
Returns:

True if attribute object is in evidence

class inference.query.Qvariable(attr, objsVar, values=None)[source]

A Qvariable instance induces a set of attribute objects (GBNvertex instances) that are used for specifying event and evidence variables when making inference.

attr

Attribute instance

erClass

ObjsVariable instance.

objs

ObjsVariable instance.

inference.query.createQvar(attrName, objsVar=None, objsConstraint=None, objsPkValues=None)[source]

Creats a Qvariable from the string name of an Attribute instance, e.g. Professor.fame

Parameters:
Returns:

Qvariable instance

mcmc module

gibbs module

An implementation of a standard Gibbs sampler. The random walk samples the full conditional distributions of the sampling attribute in the GBN. The full conditionals are given by:

../_images/full_cond3.png

See References for more details

inference.mcmc.gibbs.BLOCKGIBBS

If True the sampler will collect samples for all event variables of the same attribute class in one block.

inference.mcmc.gibbs.BURNIN

Number of burn in samples

inference.mcmc.gibbs.CHAINS

Number of chains to be run

inference.mcmc.gibbs.ITER

Number of samples to collect

inference.mcmc.gibbs.collectSamples(nSample)[source]

Stores a sampled state (e.g. one value for each event variable) in the posterior.currentchain

Parameters:nSample – Int Count of the collected sample.
inference.mcmc.gibbs.configure()[source]

Configuring an inference algorithm allow the algorithm to precompute as much information as possible before making inference.

In the case of Gibbs sampling, all the conditional likelihood functions, of type Likelihood, for the probabilistic attributes can be precomputed.

inference.mcmc.gibbs.init(chainID='standardChain')[source]

Inintializes a MCMC run given a Ground Bayesian Network and a set of event variables. The initial state of the markov chain is sampled using initializeVertices().

Parameters:
  • currentGBNGBNgraph instance
  • chainID – Sting identification of the current run. Optional (default=’standardChain’). Running more than one chain requires different chain ids
inference.mcmc.gibbs.initializeVertices()[source]

Creates an initial state for the markov chain by assigning a value to all sampling vertices

inference.mcmc.gibbs.likelihoods

Dictionary of likelihood functions that are precomputed when the sampler is configured using inference.mcmc.configure()

{ key = Attribute instance : value = Likelihood of attribute }
inference.mcmc.gibbs.run()[source]

Sequentally runs CHAINS MCMC runs, the posterior samples are stored in posterior.samples.

mh module

An implementation of a Metropolis Hastings within Gibbs algorithm.

The random walk samples the full conditional distributions for normal GBNvertex instances (i.e. Gibbs sampling) and uses a Metropolis within Gibbs step to sample ReferenceVertex instances.

See References for more details
inference.mcmc.mh.BURNIN

Number of burn in samples

inference.mcmc.mh.CHAINS

Number of chains to be run

inference.mcmc.mh.ITER

Number of samples to collect

inference.mcmc.mh.configure()[source]

Configuring an inference algorithm allow the algorithm to precompute as much information as possible before making inference.

In the case of Gibbs sampling, all the conditional likelihood functions, of type Likelihood, for the probabilistic attributes can be precomputed.

inference.mcmc.mh.init(chainID='standardChain')[source]

Inintializes a MCMC run given a Ground Bayesian Network and a set of event variables. The initial state of the markov chain is sampled using initializeVertices().

Parameters:
  • currentGBNGBNgraph instance
  • chainID – Sting identification of the current run. Optional (default=’standardChain’). Running more than one chain requires different chain ids
inference.mcmc.mh.initializeVertices()[source]

Creates an initial state for the markov chain by assigning a value to all sampling vertices

inference.mcmc.mh.likelihoods

Dictionary of likelihood functions that are precomputed when the sampler is configured using inference.mcmc.configure()

{ key = Attribute instance : value = Likelihood of attribute }
inference.mcmc.mh.mcmcStep()[source]

Performs a MCMC sampling step, either a Gibbs step or a Metropolis Hastings. In case of GBNvertex instances we use Gibbs sampling and for ReferenceVertex instances we use a Metropolis within Gibbs step.

Note that we can exploit the conditional independence by Lazy Aggregation. When sampling all event vertices of a certain attribute, we need to perform the (potential aggregation) on the parent vertices only once (see references).

inference.mcmc.mh.run()[source]

Sequentally runs CHAINS MCMC runs, the posterior samples are stored in posterior.samples.

posterior module

The posterior distribution results from running inference for a given query using MCMC. This module contains

  • Data structures to collect posterior samples
  • Convergence diagnostics

The convergence diagnostics have to be called by the ProbReM project script, e.g. from outside the framework.

inference.mcmc.posterior.autocorrelation(max_l=50, **kwargs)[source]

Plots the autocorrelation. Computed according to Probabilistic Graphical Models (p. 521).

Parameters:
  • max_l – The autocorellation will be calculated up to lag max_l (default=50)
  • chainID – Optional identification of chain to be analyzed
inference.mcmc.posterior.collectSamples(nSample)[source]

Extracting the value of a node and storing it in the appropriate numpy.array, currentChain.

Parameters:nSample – Int Count of the collected sample, i.e. the row number
inference.mcmc.posterior.cumulativeMean(chain)[source]

Returns the cumulative mean of chain.

Parameters:chainnumpy.array
inference.mcmc.posterior.currentChain

numpy.array that is currently being used by the sampler

inference.mcmc.posterior.currentIndex

Dictionary mapping each event variable ID to an index used to access currentChain

inference.mcmc.posterior.gelman_rubin()[source]

Plots the Gelman Rubin convergence diagnostic, according to Probabilistic Graphical Models (p. 523).

inference.mcmc.posterior.histogramm(**kwargs)[source]

Convergence diagnostics that plots the posterior density function (using the matplotlib histogram) mean of all the sampling variables in the currentChain. If a chainID is provided the histogram of the associated chain is plotted instead. If varIndex or gbnV is provided, only the histogram of this variable is plotted.

Parameters:
  • chainID – Optional identification of chain to be analyzed
  • varIndex – Optional index of event variable to be analyzed
  • gbnV – Optional GBNvertex event variable to be analyzed
  • fig – Optional matplotlib.figure.Figure to be used
inference.mcmc.posterior.initChain(chainID, ITER, onlyEvent=False)[source]

Initializes a new MCMC run. Note that onlyEvent=False, so the samples or all engine.GBN.samplingVertices are collected. But the posterior is a joint distribution over the event variables (thus the other sampling variables are already marginalized)

Parameters:
  • chainID – String identification for new chain
  • ITER – Number of samples to be collected
  • onlyEvent – Bolean, if True only the values of the event vertices are collected (i.e. not latent sampling variables)
inference.mcmc.posterior.mean(chainID=None, gbnV=None, sVarInd=None, combined=False)[source]

Returns the posterior mean of all the sampling variables in the currentChain. If a chainID is provided the mean of the associated chain is returned instead. If sVarInd or gbnV is provided, only the mean of this variable is returned.

The pylab.hist() method is used to compute the histogram

Parameters:
  • chainID – Optional identification of chain to be analyzed
  • sVarInd – Optional index of event variable to be analyzed
  • gbnV – Optional GBNvertex event variable to be analyzed
  • combined – Optional (default False), if True the mean of the mean of all event variables is returned (single value).
Returns:

Posterior mean as numpy.array. If sVarInd or gbnV are specified, this is a single value

inference.mcmc.posterior.plotCumulativeMean(**kwargs)[source]

Convergence diagnostics that plots the cumulative mean of all the sampling variables in the currentChain. If a chainID is provided the cumulative mean of the associated chain is plotted instead. If sVarInd is provided - e.g. the index of a sampling variable - only the cumulative mean of this variable is plotted.

Parameters:
  • chainID – Optional identification of chain to be analyzed
  • varIndex – Optional index of event variable to be analyzed
  • gbnV – Optional GBNvertex to be analyzed
  • fig – Optional matplotlib.figure.Figure to be used
inference.mcmc.posterior.plotCumulativeMeanAllChains(**kwargs)[source]

Plots the cumulative mean of all available chains using cumulativeMean(). If the plots are to be displayed on the same figure, use the figID keyword. If the plots are to display only a specific variable, use the gbnV or varIndex keyword.

Parameters:kwargs – Optional arguments for cumulativeMean()
inference.mcmc.posterior.posteriorVertices

Dictionary of vertices that we are collecting samples from (e.g. the event vertices or all sampling vertices including latent variables), set

inference.mcmc.posterior.samples

Dicitonary containing all the samples collected during inference. It is common to run more than one chain to montitor convergence, the key/value pairs are stored

{ key = ‘chainIdentification’ : value = numpy.array }

likelihood module

class inference.mcmc.likelihood.CLF(likAttr, condAttrs)[source]

Conditional Likelihood Functions are used to generate the full conditional sampling distributions for the Gibbs sampler implemented in inference.mcmc. A conditional likelihood function is an instance of the class CLF. Similar to a tabular CPD CPDTabular, CLF constists of a matrix, a method indexrow() that indexes a conditional variable assignment. The matrix is composed from values of the orginal CPD.

Lets say we have an attribute C with two parent attributes A, B. The CPD is given by P(C|A,B). In this case there will be two likelihood functions for attribute C:

self[A] = P(c|A,b) = L(A|c,b)
self[B] = P(c|a,B) = L(B|c,a)

We note that, as in the case of a normal CPD where the parents are ordered, the order of the conditional variabels are fixed in the likelihood function. The associated attribute is first, followed by the appropriate parents in the orignial parents. By appropriate parents we mean all parents except the parent attribute for that the likelihood function, e.g.

  • For self[A], the order of the conditional parents is (c,b)
  • For self[B], the order of the conditional parents is (c,a)

See the class Likelihood for more details.

computeLikelihood(condValues)[source]
Parameters:condValues – List of conditional values
Returns:List of likelihood values for conditional assignment
computeLogLik()[source]

Calculates the log probability distribution likLogMatrix

condAttrs

The conditional attributes (see CLF)

conditionalAssignment(parentAssignment)[source]

The parentAssignment is a full assignmnet to the parent attributes to the original attribute. We remove the entry that corresponds to the likelihood attribute. Note that we actually modify this list.

Parameters:parentAssignment – List of values. Assignments to the parent attributes
indexRow(condValues)[source]

Returns the index for the likMatrix row that corresponds to the assignment of the conditional attributes passed in condValues

Condvalues :List of conditional values
Returns:Index of likMatrix row
initCLF()[source]

Computes the number of possible conditional attribute assigments and the index multipliers needed to compute the row index of a given conditional assignment.

likAttr

The likelihood attribute of type Attribute

likLogMatrix

The log of likMatrix

likMatrix

Likelihood matrix of type numpy.array

likMatrixDim

Dimensions of the likelihood matrix

reverseIndexRow(index)[source]

Returns the list of values that correspond to the conditional assignment of a row of likMatrix

Parameters:index – Row of likMatrix
Returns:List of conditional values
class inference.mcmc.likelihood.Likelihood(cpd)[source]

Conditional Likelihood Functions are used to generate the full conditional sampling distributions for the Gibbs sampler implemented in mcmc.

There is an Likelihood instance for every probabilistic attribute in the PRM. It implements a dictionary which stores a conditional likelihood function of type CLF for every parent attribute of every attribute class key.

Lets say we have an attribute C with two parent attributes A, B. The CPD is given by P(C|A,B). In this case the likelihood functions of type CLF for attribute C are stored in double dictionary:

self[C][A] = P(c|A,b) = L(A|c,b)
self[C][B] = P(c|a,B) = L(B|c,a)

See CLF for more details.

cpd

The CPD instance of the attribute class that the likelihood functions are computed for