mxfusion.models.factor_graph

Members

class mxfusion.models.factor_graph.FactorGraph(name, verbose=False)

Bases: object

A graph defining how Factor objects relate to one another.

The two primary functionalities of this class are:

  • compute_log_prob which computes the log probability of some variables in the graph and
  • draw_samples which draws samples for some target variables from the graph
components_graph

Return the Graph object of the component

Returns:dict of ModelComponents
Return type:{ UUID : ModelComponent }
components

Return all the ModelComponents held in the FactorGraph.

Returns:dict of ModelComponents
Return type:{ UUID : ModelComponent }
distributions

Return the distributions held in the FactorGraph.

Returns:dict of Distributions
Return type:{ UUID : Distribution }
functions

Return the functions held in the FactorGraph.

Returns:dict of Functions
Return type:{ UUID : Function }
modules

Return the modules held in the FactorGraph.

Returns:dict of Modules
Return type:{ UUID : Module }
variables

Return the variables held in the FactorGraph.

Returns:dict of Variables
Return type:{ UUID : Variable }
ordered_factors

Return a sorted list of Factors in the graph.

Return type:A topologically sorted list of Factors in the graph.
roots

Return all root notes in the graph.

leaves

Return all leaf nodes in the graph.

var_ties

Return a mapping of Variables in the FactorGraph that are tied together (i.e. the same / aliases of each other.)

Returns:dict of UUIDs of the variables to tie
Return type:{ uuid_of_var_to_tie : uuid_of_var_to_tie_to }
log_pdf(F, variables, targets=None)

Compute the logarithm of the probability/probability density of a set of random variables in the factor graph. The set of random variables are specified in the “target” argument and any necessary conditional variables are specified in the “conditionals” argument. Any relevant constants are specified in the “constants” argument.

Parameters:
  • F – the MXNet computation mode (mxnet.symbol or mxnet.ndarray).
  • variables ({UUID : MXNet NDArray or MXNet Symbol}) – The set of variables
  • targets ({uuid : mxnet NDArray or mxnet Symbol}) – Variables to compute the log probability of.
Returns:

the sum of the log probability of all the target variables.

Return type:

mxnet NDArray or mxnet Symbol

draw_samples(F, variables, num_samples=1, targets=None)

Draw samples from the target variables of the Factor Graph. If the targets argument is None, draw samples from all the variables that are not in the conditional variables. If the targets argument is given, this method returns a list of samples of variables in the order of the target argument, otherwise it returns a dict of samples where the keys are the UUIDs of variables and the values are the samples.

Parameters:
  • F – the MXNet computation mode (mxnet.symbol or mxnet.ndarray).
  • variables ({UUID : MXNet NDArray or MXNet Symbol}) – The set of variables
  • num_samples (int) – The number of samples to draw for the target variables.
  • targets ([UUID]) – a list of Variables to draw samples from.
Returns:

the samples of the target variables.

Return type:

(MXNet NDArray or MXNet Symbol,) or {str(UUID): MXNet NDArray or MXNet Symbol}

remove_component(component)

Removes the specified component from the factor graph.

Parameters:component (ModelComponent) – The component to remove.
get_markov_blanket(node)

Gets the Markov Blanket for a node, which is the node’s predecessors, the nodes successors, and those successors’ other predecessors.

get_descendants(node)

Recursively gets all successors in the graph for the given node. :rtype: set of all nodes in the graph descended from the node.

remove_subgraph(node)

Removes a node and its parent graph recursively.

replace_subgraph(target_variable, new_subgraph)

Replaces the target_variable with the new_subgraph.

TODO If the factor of target_variable or new_subgraph as multiple outputs, this will fail.

Parameters:
  • target_variable (Variable) – The variable that will be replaced by the new_subgraph.
  • new_subgraph (Variable) – We assume this is a Variable right now (aka the graph ends in a Variable).
extract_distribution_of(variable)

Extracts the distribution of the variable passed in, returning a replicated copy of the passed in variable with only its parent subgraph attached (also replicated).

Parameters:variable (Variable) – The variable to extract the distribution from.
Returns:a replicated copy of the passed in variable.
Return type:Variable
clone(leaves=None)

Clones a model, maintaining the same functionality and topology. Replicates all of its ModelComponents, while maintaining the same UUIDs.

Starts upward from the leaves and copies everything in the graph recursively.

Parameters:leaves – If None, use the leaves in this model, otherwise use the provided leaves.
Returns:the cloned model
get_parameters(excluded=None, include_inherited=True)

Get all the parameters not in the excluded list.

Parameters:
  • excluded (set(UUID) or [UUID]) – a list of variables to be excluded.
  • include_inherited (boolean) – whether inherited variables are included.
Returns:

the list of constant variables.

Return type:

[Variable]

get_constants()

Get all the constants in the factor graph.

Returns:the list of constant variables.
Return type:[Variable]
static reconcile_graphs(current_graphs, primary_previous_graph, secondary_previous_graphs=None, primary_current_graph=None)

Reconciles two sets of graphs, matching the model components in the previous graph to the current graph. This is primarily used when loading back a graph from a file and matching it to an existing in-memory graph in order to load the previous graph’s parameters correctly.

Parameters:current_graphs – A list of the graphs we are reconciling a loaded factor graph against. This must be a

fully built set of graphs generated through the model definition process. :param primary_previous_graph: A graph which may have been loaded in from a file and be partially specified, or could be a full graph built through model definition. :param secondary_previous_graphs: A list of secondary graphs (e.g. posteriors) that share components with the primary_previous_graph. :param primary_current_graph: Optional parameter to specify the primary_current_graph, otherwise it is taken to be the model in the current_graphs (which should be unique).

Return type:{previous ModelComponent : current ModelComponent}
load_from_json(json_graph)
static load_graphs(graphs_list, existing_graphs=None)

Method to load back in a graph. The graphs should have been saved down using the save method, and be a JSON representation of the graph generated by the [networkx](https://networkx.github.io) library.

Parameters:graphs_list (list of dicts loaded in using the ModelComponentDecoder class.) – A list of raw json dicts loaded in from memory representing the FactorGraphs to create.
as_json()

Returns the FactorGraph in a form suitable for JSON serialization. This is assuming a JSON serializer that knows how to handle ModelComponents such as the one defined in mxfusion.util.serialization.

static save(graph_file, json_graphs)

Method to save this graph down into a file. The graph file will be saved down as a JSON representation of the graph generated by the [networkx](https://networkx.github.io) library.

Parameters:graph_file (str of filename) – The file containing the primary model to load back for this inference algorithm.