genomfart.stats package¶
Submodules¶
genomfart.stats.pgm module¶
-
class
genomfart.stats.pgm.bayes_net[source]¶ Bases:
networkx.classes.digraph.DiGraphClass used to represent a Bayesian network
Attributes
nameMethods
add_cycle(nodes, **attr)Add a cycle. add_edge(u, v[, attr_dict])Add an edge between u and v. add_edges_from(ebunch[, attr_dict])Add all the edges in ebunch. add_node(cpd)Adds a node to the Bayes net add_nodes_from(nodes, **attr)Add multiple nodes. add_path(nodes, **attr)Add a path. add_star(nodes, **attr)Add a star. add_weighted_edges_from(ebunch[, weight])Add all the edges in ebunch as weighted edges with specified weights. adjacency_iter()Return an iterator of (node, adjacency dict) tuples for all nodes. adjacency_list()Return an adjacency list representation of the graph. adjlist_dict_factoryalias of dictclear()Remove all nodes and edges from the graph. convert_to_discrete_factor_graph([nthreads])Converts the existing bayesian network to a factor graph copy()Return a copy of the graph. degree([nbunch, weight])Return the degree of a node or nodes. degree_iter([nbunch, weight])Return an iterator for (node, degree). edge_attr_dict_factoryalias of dictedges([nbunch, data, default])Return a list of edges. edges_iter([nbunch, data, default])Return an iterator over the edges. get_edge_data(u, v[, default])Return the attribute dictionary associated with edge (u,v). has_edge(u, v)Return True if the edge (u,v) is in the graph. has_node(n)Return True if the graph contains the node n. has_predecessor(u, v)Return True if node u has predecessor v. has_successor(u, v)Return True if node u has successor v. in_degree([nbunch, weight])Return the in-degree of a node or nodes. in_degree_iter([nbunch, weight])Return an iterator for (node, in-degree). in_edges([nbunch, data])Return a list of the incoming edges. in_edges_iter([nbunch, data])Return an iterator over the incoming edges. is_directed()Return True if graph is directed, False otherwise. is_multigraph()Return True if graph is a multigraph, False otherwise. nbunch_iter([nbunch])Return an iterator of nodes contained in nbunch that are also in the graph. neighbors(n)Return a list of successor nodes of n. neighbors_iter(n)Return an iterator over successor nodes of n. node_dict_factoryalias of dictnodes([data])Return a list of the nodes in the graph. nodes_iter([data])Return an iterator over the nodes. nodes_with_selfloops()Return a list of nodes with self loops. number_of_edges([u, v])Return the number of edges between two nodes. number_of_nodes()Return the number of nodes in the graph. number_of_selfloops()Return the number of selfloop edges. order()Return the number of nodes in the graph. out_degree([nbunch, weight])Return the out-degree of a node or nodes. out_degree_iter([nbunch, weight])Return an iterator for (node, out-degree). out_edges([nbunch, data, default])Return a list of edges. out_edges_iter([nbunch, data, default])Return an iterator over the edges. predecessors(n)Return a list of predecessor nodes of n. predecessors_iter(n)Return an iterator over predecessor nodes of n. remove_edge(u, v)Remove the edge between u and v. remove_edges_from(ebunch)Remove all edges specified in ebunch. remove_node(n)Remove node n. remove_nodes_from(nbunch)Remove multiple nodes. reverse([copy])Return the reverse of the graph. selfloop_edges([data, default])Return a list of selfloop edges. size([weight])Return the number of edges. subgraph(nbunch)Return the subgraph induced on nodes in nbunch. successors(n)Return a list of successor nodes of n. successors_iter(n)Return an iterator over successor nodes of n. to_directed()Return a directed copy of the graph. to_undirected([reciprocal])Return an undirected representation of the digraph. write_libpgm_file(filename)Writes to a file that can be read by libpgm -
add_node(cpd)[source]¶ Adds a node to the Bayes net
Parameters: cpd : cpd object
A conditional probability distribution for the node
-
-
class
genomfart.stats.pgm.cpd(node_name, parents)[source]¶ Bases:
objectBase class for a conditional probability distribution
Methods
get_name()Gets the name of the node get_parents()Gets the name of the node’s parents
-
class
genomfart.stats.pgm.discrete_cpd(node_name, parents, parent_value_dict, node_values, prob_dict)[source]¶ Bases:
genomfart.stats.pgm.cpdClass used to represent a conditional probability distribution with only discrete values
Methods
get_name()Gets the name of the node get_parents()Gets the name of the node’s parents get_prob([ordered_arg_vals, log, node_value])Gets the (log) probability of node values given the values -
__init__(node_name, parents, parent_value_dict, node_values, prob_dict)[source]¶ Instantiates the discrete CPD
Parameters: node_name : hashable
Name of the node
parents : tuple or list
Ordered names of the parents
parent_value_dict : dict
Dictionary of parent -> (Ordered values parent can take)
node_values : tuple
Ordered values this child node can take
prob_dict : dict
Dictionary of (parent1 value, parent2 value, ...) -> np.array([prob child value1, prob child value2, ...])
-
get_prob(ordered_arg_vals=None, log=False, node_value=None, **parent_vals)[source]¶ Gets the (log) probability of node values given the values of its parents
Parameters: ordered_arg_vals : tuple
Ordered argument values for parents
log : bool
Whether to return the log-probability rather than the actual probability
node_value : None or value
If not None, a specified node value at which the CPD should be evaluated
parent_vals :
keyword arguments of parent_name -> value
Returns: Conditional (log) probability. If node_value is None, will return the whole
probability vector. Otherwise, gives the probability for the given node_value
entry
-
-
class
genomfart.stats.pgm.discrete_factor_graph[source]¶ Bases:
networkx.classes.graph.GraphClass used to represent a factor graph for discrete variables, which is a bipartite graph with factor and variable nodes. Node attributes under bipartite are ‘f’ and ‘v’, respectively. Factor nodes have attribute ‘func’, containing their functions
Attributes
nameMethods
add_cycle(nodes, **attr)Add a cycle. add_edge(u, v[, attr_dict])Add an edge between u and v. add_edges_from(ebunch[, attr_dict])Add all the edges in ebunch. add_factor_node(factor_name, factor_vars, ...)Adds a factor node to the graph, along with its variable nodes add_node(n[, attr_dict])Add a single node n and update node attributes. add_nodes_from(nodes, **attr)Add multiple nodes. add_path(nodes, **attr)Add a path. add_star(nodes, **attr)Add a star. add_weighted_edges_from(ebunch[, weight])Add all the edges in ebunch as weighted edges with specified weights. adjacency_iter()Return an iterator of (node, adjacency dict) tuples for all nodes. adjacency_list()Return an adjacency list representation of the graph. adjlist_dict_factoryalias of dictclear()Remove all nodes and edges from the graph. copy()Return a copy of the graph. degree([nbunch, weight])Return the degree of a node or nodes. degree_iter([nbunch, weight])Return an iterator for (node, degree). edge_attr_dict_factoryalias of dictedges([nbunch, data, default])Return a list of edges. edges_iter([nbunch, data, default])Return an iterator over the edges. get_edge_data(u, v[, default])Return the attribute dictionary associated with edge (u,v). get_norm_beliefs(var)Gets the normalized beliefs for a variable node based on the current get_random_spanning_tree()Gets a random spanning tree of this graph has_edge(u, v)Return True if the edge (u,v) is in the graph. has_node(n)Return True if the graph contains the node n. is_directed()Return True if graph is directed, False otherwise. is_multigraph()Return True if graph is a multigraph, False otherwise. nbunch_iter([nbunch])Return an iterator of nodes contained in nbunch that are also in the graph. neighbors(n)Return a list of the nodes connected to the node n. neighbors_iter(n)Return an iterator over all neighbors of node n. node_dict_factoryalias of dictnodes([data])Return a list of the nodes in the graph. nodes_iter([data])Return an iterator over the nodes. nodes_with_selfloops()Return a list of nodes with self loops. number_of_edges([u, v])Return the number of edges between two nodes. number_of_nodes()Return the number of nodes in the graph. number_of_selfloops()Return the number of selfloop edges. order()Return the number of nodes in the graph. remove_edge(u, v)Remove the edge between u and v. remove_edges_from(ebunch)Remove all edges specified in ebunch. remove_node(n)Remove node n. remove_nodes_from(nodes)Remove multiple nodes. reset_nonobs_messages()Resets all messages on the graph that aren’t involving observed run_BP_tree()Run belief propagation for a tree. run_synchronous_LBP([maxit, verbose])Runs loopy belief propagation by synchronously updating run_synchronous_update([edge_yielder, ...])Updates all nodes simultaneously selfloop_edges([data, default])Return a list of selfloop edges. send_message_to_factor(var, factor_name[, ...])Sends a message from a variable to a factor send_message_to_var(factor_name, var[, update])Sends a message from a factor to a variable set_variable_state(var, val)Sets a variable’s state, given observed data set_variable_unobserved(var)Sets a variable’s state to unobserved size([weight])Return the number of edges. subgraph(nbunch)Return the subgraph induced on nodes in nbunch. to_directed()Return a directed representation of the graph. to_undirected()Return an undirected copy of the graph. -
add_factor_node(factor_name, factor_vars, factor_func, var_value_dict)[source]¶ Adds a factor node to the graph, along with its variable nodes
Parameters: factor_name : hashable
Name to give the factor node
factor_vars : tuple or list
Ordered list of variables connected to the factor node. This should be the same order as the arguments given to the factor_func
factor_func : function
Function taking ordered factor_vars values as arguments and returning some scalar value
var_value_dict : dict
Dictionary of variable -> (Ordered values variable can take)
-
get_norm_beliefs(var)[source]¶ Gets the normalized beliefs for a variable node based on the current messages running to it
Parameters: var : hashable
The variable to get the beliefs for
Returns: Vector of normalized beliefs (probabilities)
-
get_random_spanning_tree()[source]¶ Gets a random spanning tree of this graph
Returns: A random spanning tree of this factor graph
-
reset_nonobs_messages()[source]¶ Resets all messages on the graph that aren’t involving observed variables
-
run_BP_tree()[source]¶ Run belief propagation for a tree. Will throw an error if not a tree
Raises: ValueError
If not a tree
-
run_synchronous_LBP(maxit=100, verbose=False)[source]¶ Runs loopy belief propagation by synchronously updating messages while they change
Parameters: maxit : int
Maximum number of iterations to perform
-
run_synchronous_update(edge_yielder=None, return_changed=False)[source]¶ Updates all nodes simultaneously
Parameters: edge_yielder : iterable
Lists edges to update
return_changed : bool
Whether to return list of edges that were modified
-
send_message_to_factor(var, factor_name, update=True)[source]¶ Sends a message from a variable to a factor
Parameters: var : hashable
Variable name
factor_name : hashable
Factor name
-
send_message_to_var(factor_name, var, update=True)[source]¶ Sends a message from a factor to a variable
Parameters: var : hashable
Variable name
factor_name : hashable
Factor name
-