class_docstr: Base class for all inference algorithms.
  
  Converts BayesianNetwork and MarkovNetwork to a uniform representation so that inference
  algorithms can be applied. Also, it checks if all the associated CPDs / Factors are
  consistent with the model.
  
  Initialize inference for a model.
  
  Parameters
  ----------
  model: pgmpy.models.BayesianNetwork or pgmpy.models.MarkovNetwork or pgmpy.models.NoisyOrModel
      model for which to initialize the inference object.
  
  Examples
  --------
  >>> from pgmpy.inference import Inference
  >>> from pgmpy.models import BayesianNetwork
  >>> from pgmpy.factors.discrete import TabularCPD
  >>> student = BayesianNetwork([('diff', 'grade'), ('intel', 'grade')])
  >>> diff_cpd = TabularCPD('diff', 2, [[0.2], [0.8]])
  >>> intel_cpd = TabularCPD('intel', 2, [[0.3], [0.7]])
  >>> grade_cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1],
  ...                                     [0.1, 0.1, 0.1, 0.1],
  ...                                     [0.8, 0.8, 0.8, 0.8]],
  ...                        evidence=['diff', 'intel'], evidence_card=[2, 2])
  >>> student.add_cpds(diff_cpd, intel_cpd, grade_cpd)
  >>> model = Inference(student)
  
  >>> from pgmpy.models import MarkovNetwork
  >>> from pgmpy.factors.discrete import DiscreteFactor
  >>> import numpy as np
  >>> student = MarkovNetwork([('Alice', 'Bob'), ('Bob', 'Charles'),
  ...                        ('Charles', 'Debbie'), ('Debbie', 'Alice')])
  >>> factor_a_b = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
  ...                             values=np.random.rand(4))
  >>> factor_b_c = DiscreteFactor(['Bob', 'Charles'], cardinality=[2, 2],
  ...                             values=np.random.rand(4))
  >>> factor_c_d = DiscreteFactor(['Charles', 'Debbie'], cardinality=[2, 2],
  ...                             values=np.random.rand(4))
  >>> factor_d_a = DiscreteFactor(['Debbie', 'Alice'], cardinality=[2, 2],
  ...                             values=np.random.rand(4))
  >>> student.add_factors(factor_a_b, factor_b_c, factor_c_d, factor_d_a)
  >>> model = Inference(student)
class_docstr_tok: ["Base","class","for","all","inference","algorithms",".","Converts","BayesianNetwork","and","MarkovNetwork","to","a","uniform","representation","so","that","inference","algorithms","can","be","applied",".","Also",",","it","checks","if","all","the","associated","CPDs","\/","Factors","are","consistent","with","the","model",".","Initialize","inference","for","a","model",".","Parameters","--","--","--","--","--","model",":","pgmpy.models.BayesianNetwork","or","pgmpy.models.MarkovNetwork","or","pgmpy.models.NoisyOrModel","model","for","which","to","initialize","the","inference","object",".","Examples","--","--","--","--",">",">",">","from","pgmpy.inference","import","Inference",">",">",">","from","pgmpy.models","import","BayesianNetwork",">",">",">","from","pgmpy.factors.discrete","import","TabularCPD",">",">",">","student","=","BayesianNetwork","(","[","(","'diff","'",",","'grade","'",")",",","(","'intel","'",",","'grade","'",")","]",")",">",">",">","diff_cpd","=","TabularCPD","(","'diff","'",",","2",",","[","[","0.2","]",",","[","0.8","]","]",")",">",">",">","intel_cpd","=","TabularCPD","(","'intel","'",",","2",",","[","[","0.3","]",",","[","0.7","]","]",")",">",">",">","grade_cpd","=","TabularCPD","(","'grade","'",",","3",",","[","[","0.1",",","0.1",",","0.1",",","0.1","]",",","...","[","0.1",",","0.1",",","0.1",",","0.1","]",",","...","[","0.8",",","0.8",",","0.8",",","0.8","]","]",",","...","evidence=","[","'diff","'",",","'intel","'","]",",","evidence_card=","[","2",",","2","]",")",">",">",">","student.add_cpds","(","diff_cpd",",","intel_cpd",",","grade_cpd",")",">",">",">","model","=","Inference","(","student",")",">",">",">","from","pgmpy.models","import","MarkovNetwork",">",">",">","from","pgmpy.factors.discrete","import","DiscreteFactor",">",">",">","import","numpy","as","np",">",">",">","student","=","MarkovNetwork","(","[","(","'Alice","'",",","'Bob","'",")",",","(","'Bob","'",",","'Charles","'",")",",","...","(","'Charles","'",",","'Debbie","'",")",",","(","'Debbie","'",",","'Alice","'",")","]",")",">",">",">","factor_a_b","=","DiscreteFactor","(","[","'Alice","'",",","'Bob","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","factor_b_c","=","DiscreteFactor","(","[","'Bob","'",",","'Charles","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","factor_c_d","=","DiscreteFactor","(","[","'Charles","'",",","'Debbie","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","factor_d_a","=","DiscreteFactor","(","[","'Debbie","'",",","'Alice","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","student.add_factors","(","factor_a_b",",","factor_b_c",",","factor_c_d",",","factor_d_a",")",">",">",">","model","=","Inference","(","student",")"]
docstr: Prunes unnecessary nodes from the model to optimize the computation.
  
  Parameters
  ----------
  variables: list
      The variables on which the query is done i.e. the variables whose
      values we are interested in.
  
  evidence: dict (default: None)
      The variables whose values we know. The values can be specified as
      {variable: state}.
  
  Returns
  -------
  Pruned model: pgmpy.models.BayesianNetwork
      The pruned model.
  
  Examples
  --------
  >>>
  >>>
  
  References
  ----------
  [1] Baker, M., & Boult, T. E. (2013). Pruning Bayesian networks for efficient computation. arXiv preprint arXiv:1304.1112.
docstr_tok: ["Prunes","unnecessary","nodes","from","the","model","to","optimize","the","computation",".","Parameters","--","--","--","--","--","variables",":","list","The","variables","on","which","the","query","is","done","i.e",".","the","variables","whose","values","we","are","interested","in",".","evidence",":","dict","(","default",":","None",")","The","variables","whose","values","we","know",".","The","values","can","be","specified","as","{","variable",":","state","}",".","Returns","--","--","--","-","Pruned","model",":","pgmpy.models.BayesianNetwork","The","pruned","model",".","Examples","--","--","--","--",">",">",">",">",">",">","References","--","--","--","--","--","[","1","]","Baker",",","M.",",","&","Boult",",","T.","E.","(","2013",")",".","Pruning","Bayesian","networks","for","efficient","computation",".","arXiv","preprint","arXiv:1304.1112","."]
code: def _prune_bayesian_model(self, variables, evidence):
      """
      Prunes unnecessary nodes from the model to optimize the computation.
  
      Parameters
      ----------
      variables: list
          The variables on which the query is done i.e. the variables whose
          values we are interested in.
  
      evidence: dict (default: None)
          The variables whose values we know. The values can be specified as
          {variable: state}.
  
      Returns
      -------
      Pruned model: pgmpy.models.BayesianNetwork
          The pruned model.
  
      Examples
      --------
      >>>
      >>>
  
      References
      ----------
      [1] Baker, M., & Boult, T. E. (2013). Pruning Bayesian networks for efficient computation. arXiv preprint arXiv:1304.1112.
      """
      evidence = {} if evidence is None else evidence
      variables = (
          list(self.model.nodes())
          if len(variables) == 0
          else list(variables)
      )
  
      # Step 1: Remove all the variables that are d-separated from `variables` when conditioned
      #         on `evidence`
      d_connected = self.model.active_trail_nodes(
          variables=variables,
          observed=list(evidence.keys()),
          include_latents=True,
      )
      d_connected = set.union(*d_connected.values()).union(
          evidence.keys()
      )
      bn = self.model.subgraph(d_connected)
      evidence = {
          var: state
          for var, state in evidence.items()
          if var in d_connected
      }
  
      # Step 2: Reduce the model to ancestral graph of [`variables` + `evidence`]
      bn = bn.get_ancestral_graph(
          list(variables) + list(evidence.keys())
      )
  
      # Step 3: Since all the CPDs are lost, add them back. Also marginalize them if some
      #         of the variables in scope aren't in the network anymore.
      cpds = []
      for var in bn.nodes():
          cpd = self.model.get_cpds(var)
          scope_diff = set(cpd.scope()) - set(bn.nodes())
          if len(scope_diff) == 0:
              cpds.append(cpd)
          else:
              cpds.append(cpd.marginalize(scope_diff, inplace=False))
  
      bn.cpds = cpds
  
      return bn, evidence
  
file_path: pgmpy/pgmpy/inference/base.py
imports: from collections import defaultdict
  from itertools import chain
  import numpy
  from pgmpy.factors.discrete import TabularCPD
  from pgmpy.models import BayesianNetwork, DynamicBayesianNetwork, FactorGraph, JunctionTree, MarkovNetwork
  from pgmpy.utils import compat_fns
label_desc: Use image node_id 3 for calling the Inference obj's underlying member method code with example usage: obj._prune_bayesian_model(variables, evidence) and returns: bn,  evidence