Factor Graphs

API Reference

class pomegranate.FactorGraph.FactorGraph

A Factor Graph model.

A bipartite graph where conditional probability tables are on one side, and marginals for each of the variables involved are on the other side.

Parameters:

name : str, optional

The name of the model. Default is None.

bake()

Finalize the topology of the model.

Assign a numerical index to every state and create the underlying arrays corresponding to the states and edges between the states. This method must be called before any of the probability-calculating methods. This is the same as the HMM bake, except that at the end it sets current state information.

Parameters:None
Returns:None
from_json()

Read in a serialized FactorGraph and return the appropriate instance.

Parameters:

s: str

A JSON formatted string containing the file.

Returns:

model: object

A properly instantiated and baked model.

marginal()

Return the marginal probabilities of each variable in the graph.

This is equivalent to a pass of belief propogation on a graph where no data has been given. This will calculate the probability of each variable being in each possible emission when nothing is known.

Parameters:

None

Returns:

marginals : array-like, shape (n_nodes)

An array of univariate distribution objects showing the marginal probabilities of that variable.

plot()

Draw this model’s graph using NetworkX and matplotlib.

Note that this relies on networkx’s built-in graphing capabilities (and not Graphviz) and thus can’t draw self-loops.

See networkx.draw_networkx() for the keywords you can pass in.

Parameters:

**kwargs : any

The arguments to pass into networkx.draw_networkx()

Returns:

None

predict_proba()

Returns the probabilities of each variable in the graph given evidence.

This calculates the marginal probability distributions for each state given the evidence provided through loopy belief propogation. Loopy belief propogation is an approximate algorithm which is exact for certain graph structures.

Parameters:

data : dict or array-like, shape <= n_nodes, optional

The evidence supplied to the graph. This can either be a dictionary with keys being state names and values being the observed values (either the emissions or a distribution over the emissions) or an array with the values being ordered according to the nodes incorporation in the graph (the order fed into .add_states/add_nodes) and None for variables which are unknown. If nothing is fed in then calculate the marginal of the graph.

max_iterations : int, optional

The number of iterations with which to do loopy belief propogation. Usually requires only 1.

check_input : bool, optional

Check to make sure that the observed symbol is a valid symbol for that distribution to produce.

Returns:

probabilities : array-like, shape (n_nodes)

An array of univariate distribution objects showing the probabilities of each variable.

to_json()

Serialize the model to JSON

Parameters:

separators: tuple, optional

The two separators to pass to the json.dumps function for formatting.

indent: int, optional

The indentation to use at each level. Passed to json.dumps for formatting.