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 pgmpy.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_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 pgmpy.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)[start]) 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_independencies(self, latex=False): """ Compute independencies in Bayesian Network. Parameters ---------- latex: boolean If latex=True then latex string of the independence assertion would be created. Examples -------- >>> from pgmpy.models import BayesianModel >>> student = BayesianModel() >>> student.add_nodes_from(['diff', 'intel', 'grades', 'letter', 'sat']) >>> student.add_edges_from([('diff', 'grades'), ('intel', 'grades'), ('grade', 'letter'), ... ('intel', 'sat')]) >>> student.get_independencies() """ independencies = Independencies() for start in (self.nodes()): for r in (1, len(self.nodes())): for observed in itertools.combinations(self.nodes(), r): independent_variables = self.active_trail_nodes(start, observed=observed) independent_variables = set(independent_variables) - {start} if independent_variables: independencies.add_assertions([start, independent_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 pgmpy.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() """ from pgmpy.exceptions import RequiredError 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 RequiredError: pass local_independencies.reduce() if latex: return local_independencies.latex_string() else: return local_independencies
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 pgmpy.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() """ from pgmpy.exceptions import RequiredError 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 RequiredError: pass local_independencies.reduce() if latex: return local_independencies.latex_string() else: return local_independencies
def get_independencies(self, latex=False): """ Compute independencies in Bayesian Network. Parameters ---------- latex: boolean If latex=True then latex string of the independence assertion would be created. Examples -------- >>> from pgmpy.models import BayesianModel >>> student = BayesianModel() >>> student.add_nodes_from(['diff', 'intel', 'grades', 'letter', 'sat']) >>> student.add_edges_from([('diff', 'grades'), ('intel', 'grades'), ('grade', 'letter'), ... ('intel', 'sat')]) >>> student.get_independencies() """ independencies = Independencies() for start in (self.nodes()): for r in (1, len(self.nodes())): for observed in itertools.combinations(self.nodes(), r): independent_variables = self.active_trail_nodes( start, observed=observed) independent_variables = set(independent_variables) - { start } if independent_variables: independencies.add_assertions( [start, independent_variables, observed]) independencies.reduce() if not latex: return independencies else: return independencies.latex_string()