Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Parameters
----------
u,v : nodes
Nodes can be any hashable Python object.
Examples
--------
>>> from pgmpy.models import MarkovModel
>>> G = MarkovModel()
>>> G.add_nodes_from(['Alice', 'Bob', 'Charles'])
>>> G.add_edge('Alice', 'Bob')
"""
# check that there is no self loop.
if u != v:
super(MarkovModel, self).add_edge(u, v, **kwargs)
else:
raise ValueError("Self loops are not allowed")
order.append(node_to_delete)
graph_copy = nx.Graph(self.edges())
for node in order:
for edge in itertools.combinations(graph_copy.neighbors(node), 2):
graph_copy.add_edge(edge[0], edge[1])
edge_set.add(edge)
graph_copy.remove_node(node)
if inplace:
for edge in edge_set:
self.add_edge(edge[0], edge[1])
return self
else:
graph_copy = MarkovModel(self.edges())
for edge in edge_set:
graph_copy.add_edge(edge[0], edge[1])
return graph_copy
Examples
--------
>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2)
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_edges_from([('a', phi1), ('b', phi1),
... ('b', phi2), ('c', phi2)])
>>> mm = G.to_markov_model()
"""
mm = MarkovModel()
variable_nodes = self.get_variable_nodes()
if len(set(self.nodes()) - set(variable_nodes)) != len(self.factors):
raise ValueError("Factors not associated with all the factor nodes.")
mm.add_nodes_from(variable_nodes)
for factor in self.factors:
scope = factor.scope()
mm.add_edges_from(itertools.combinations(scope, 2))
mm.add_factors(factor)
return mm
be the moral graph of the bayesian model.
Examples
--------
>>> from pgmpy.models import BayesianModel
>>> G = BayesianModel([('diff', 'grade'), ('intel', 'grade'),
... ('intel', 'SAT'), ('grade', 'letter')])
>>> mm = G.to_markov_model()
>>> mm.nodes()
['diff', 'grade', 'intel', 'SAT', 'letter']
>>> mm.edges()
[('diff', 'intel'), ('diff', 'grade'), ('intel', 'grade'),
('intel', 'SAT'), ('grade', 'letter')]
"""
moral_graph = self.moralize()
mm = MarkovModel(moral_graph.edges())
mm.add_factors(*[cpd.to_factor() for cpd in self.cpds])
return mm
>>> G.add_nodes_from([('a', 'b'), ('b', 'c')])
>>> G.add_edge(('a', 'b'), ('b', 'c'))
>>> G_copy = G.copy()
>>> G_copy.edges()
[(('a', 'b'), ('b', 'c'))]
>>> G_copy.nodes()
[('a', 'b'), ('b', 'c')]
>>> factor = DiscreteFactor([('a', 'b')], cardinality=[3],
... values=np.random.rand(3))
>>> G.add_factors(factor)
>>> G.get_factors()
[]
>>> G_copy.get_factors()
[]
"""
clone_graph = MarkovModel(self.edges())
clone_graph.add_nodes_from(self.nodes())
if self.factors:
factors_copy = [factor.copy() for factor in self.factors]
clone_graph.add_factors(*factors_copy)
return clone_graph