def get_independencies(self, latex=False): """ Computes independencies in the Bayesian Network, by checking d-seperation. Parameters ---------- latex: boolean If latex=True then latex string of the independence assertion would be created. Examples -------- >>> from pgm.models import BayesianModel >>> chain = BayesianModel([('X', 'Y'), ('Y', 'Z')]) >>> chain.get_independencies() (X _|_ Z | Y) (Z _|_ X | Y) """ independencies = Independencies() for start in (self.nodes()): rest = set(self.nodes()) - {start} for r in range(len(rest)): for observed in itertools.combinations(rest, r): d_seperated_variables = rest - set(observed) - set( self.active_trail_nodes(start, observed=observed)) if d_seperated_variables: independencies.add_assertions( [start, d_seperated_variables, observed]) independencies.reduce() if not latex: return independencies else: return independencies.latex_string()
def get_local_independencies(self, latex=False): """ Returns all the local independencies present in the markov model. Local independencies are the independence assertion in the form of .. math:: {X \perp W - {X} - MB(X) | MB(X)} where MB is the markov blanket of all the random variables in X Parameters ---------- latex: boolean If latex=True then latex string of the indepedence assertion would be created Examples -------- >>> from pgm.models import MarkovModel >>> mm = MarkovModel() >>> mm.add_nodes_from(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7']) >>> mm.add_edges_from([('x1', 'x3'), ('x1', 'x4'), ('x2', 'x4'), ... ('x2', 'x5'), ('x3', 'x6'), ('x4', 'x6'), ... ('x4', 'x7'), ('x5', 'x7')]) >>> mm.get_local_independecies() """ local_independencies = Independencies() all_vars = set(self.nodes()) for node in self.nodes(): markov_blanket = set(self.markov_blanket(node)) rest = all_vars - set([node]) - markov_blanket try: local_independencies.add_assertions( [node, list(rest), list(markov_blanket)]) except ValueError: pass local_independencies.reduce() if latex: return local_independencies.latex_string() else: return local_independencies