pyGM: Graphical Models

class pyGM.GraphModel(factorList=None, copy=True, isLog=False)[source]

A basic graphical model class; represents a collection of factors.

Example:

>>> flist = readUai('myfile.uai')  # read a list of factors from a UAI format file
>>> model = GraphModel(flist)      # makes a copy of the factors for manipulation

The model may be stored in an exponential, product of factors form: f(X) = prod_r f_r(X_r), or in a log-probability, sum of factors form: heta(X) = sum_r heta_r(X_r).

Various accessor functions enable finding factors that depend on one or more variables, variables that share one or more factors (their Markov blanket), manipulations to the graph (such as eliminating one or more variables), and visualization (through networkx).

addFactors(flist, copy=True)[source]

Add a list of factors to the model; factors are copied locally unless copy = False

condition(evidence)[source]

Condition (clamp) the graphical model on a partial configuration (dict) {Xi:xi, Xj:xj, …}

condition2(vs, xs)[source]

Condition (clamp) the graphical model on the partial configuration vs=xs (may be lists or tuples)

connectedComponents()[source]

Find the connected components of the model’s Markov graph. Returns a list of sets of variables.

copy()[source]

Return a (deep) copy of the graphical model

drawBayesNet(**kwargs)[source]

Draw a Bayesian Network (directed acyclic graph) using networkx function calls

Args:
**kwargs: remaining keyword arguments passed to networkx.draw()

Example: >>> model.drawBayesNet( labels={0:‘0’, … } ) # keyword args passed to networkx.draw()

drawFactorGraph(var_color='w', factor_color=(0.2, 0.2, 0.8), **kwargs)[source]

Draw a factorgraph using networkx function calls

Args:
var_color (str, tuple): networkx color descriptor for drawing variable nodes factor_color (str, tuple): networkx color for drawing factor nodes var_labels (dict): variable id to label string for variable nodes factor_labels (dict): factor id to label string for factor nodes **kwargs: remaining keyword arguments passed to networkx.draw()

Example: >>> model.drawFactorGraph( var_labels={0:‘0’, … } ) # keyword args passed to networkx.draw()

drawLimid(C, D, U, **kwargs)[source]

Draw a limited-memory influence diagram (limid) using networkx

Args:
**kwargs: remaining keyword arguments passed to networkx.draw()

Example: >>> model.drawLimid(C,D,U, var_labels={0:‘0’, … } ) # keyword args passed to networkx.draw()

drawMarkovGraph(**kwargs)[source]

Draw a Markov random field using networkx function calls

Args:
**kwargs: remaining keyword arguments passed to networkx.draw()

Example: >>> model.drawMarkovGraph( labels={0:‘0’, … } ) # keyword args passed to networkx.draw()

eliminate(elimVars, elimOp)[source]

Eliminate (remove) a set of variables from the model

Args:
elimVars (iterable): list of variables to eliminate (in order of elimination) elimOp (str or lambda-fn): function to eliminate variable v from factor F; ‘max’, ‘min’, ‘sum’, ‘lse’, or a user-defined custom function, e.g., ‘lambda F,v: …’
factorsWith(v, copy=True)[source]

Get the list of all factors that include variable v

factorsWithAll(vs)[source]

Get the list of all factors that include all variables in the list vs

factorsWithAny(vs)[source]

Get the list of all factors that include any variables in the list vs

isBN(tol=1e-06)[source]

Check whether the graphical model is a valid Bayes net (one CPT per variable)

isBinary()[source]

Check whether the graphical model is binary (all variables <= 2 states)

isCSP()[source]

Check whether the graphical model is a valid CSP (all zeros or ones)

isPairwise()[source]

Check whether the graphical model is pairwise (has maximum scope size 2)

joint()[source]

Compute brute-force joint function F(x) = prod_r f_r(x_r) as a (large) factor

logValue(x, subset=None)[source]

Evaluate log F(x) = sum_r log f_r(x_r) for some (full) configuration x. If optional subset != None, uses only the factors in the Markov blanket of subset.

makeCanonical()[source]

Add/merge factors to make a canonical factor graph: singleton factors plus maximal cliques

makeMinimal()[source]

Merge factors to make a minimal factor graph: retain only factors over maximal cliques

markovBlanket(v)[source]

Get the Markov blanket of variable v (all variables involved in a factor with v)

property nfactors

The number of factors in the model

property nvar

The number of variables ( = largest variable id) in the model

nxMarkovGraph(all_vars=False)[source]

Get networkx Graph object of the Markov graph of the model

Example: >>> G = nxMarkovGraph(model) >>> nx.draw(G)

removeFactors(flist)[source]

Remove a list of factors from the model

>>> model.removeFactors(model.factorsWith([0]))    # remove all factors involving X0
toExp()[source]

Convert internal factors to exp form (product of probabilities) if not. May use ‘isLog’ to check.

toLog()[source]

Convert internal factors to log form (if not already). May use ‘isLog’ to check.

value(x, subset=None)[source]

Evaluate F(x) = prod_r f_r(x_r) for some (full) configuration x. If optional subset != None, uses only the factors in the Markov blanket of subset.

var(i)[source]

Return a variable object (with # states) for id ‘i’; equals model.X[i]

property vars

List of variables in the graphical model; equals model.X