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: Modifies the model to incorporate virtual evidence. For each virtual evidence
  variable a binary variable is added as the child of the evidence variable to
  the model. The state 0 probabilities of the child is the evidence.
  
  Parameters
  ----------
  virtual_evidence: dict
      A dict of TabularCPD instances specifying the virtual evidence for each
      of the evidence variables.
  
  Returns
  -------
  None
  
  References
  ----------
  [1] Mrad, Ali Ben, et al. "Uncertain evidence in Bayesian networks: Presentation and comparison on a simple example." International Conference on Information Processing and Management of Uncertainty in Knowledge-Based Systems. Springer, Berlin, Heidelberg, 2012.
docstr_tok: ["Modifies","the","model","to","incorporate","virtual","evidence",".","For","each","virtual","evidence","variable","a","binary","variable","is","added","as","the","child","of","the","evidence","variable","to","the","model",".","The","state","0","probabilities","of","the","child","is","the","evidence",".","Parameters","--","--","--","--","--","virtual_evidence",":","dict","A","dict","of","TabularCPD","instances","specifying","the","virtual","evidence","for","each","of","the","evidence","variables",".","Returns","--","--","--","-","None","References","--","--","--","--","--","[","1","]","Mrad",",","Ali","Ben",",","et","al",".","``","Uncertain","evidence","in","Bayesian","networks",":","Presentation","and","comparison","on","a","simple","example",".","''","International","Conference","on","Information","Processing","and","Management","of","Uncertainty","in","Knowledge-Based","Systems",".","Springer",",","Berlin",",","Heidelberg",",","2012","."]
code: def _virtual_evidence(self, virtual_evidence):
      """
      Modifies the model to incorporate virtual evidence. For each virtual evidence
      variable a binary variable is added as the child of the evidence variable to
      the model. The state 0 probabilities of the child is the evidence.
  
      Parameters
      ----------
      virtual_evidence: dict
          A dict of TabularCPD instances specifying the virtual evidence for each
          of the evidence variables.
  
      Returns
      -------
      None
  
      References
      ----------
      [1] Mrad, Ali Ben, et al. "Uncertain evidence in Bayesian networks: Presentation and comparison on a simple example." International Conference on Information Processing and Management of Uncertainty in Knowledge-Based Systems. Springer, Berlin, Heidelberg, 2012.
      """
      for cpd in virtual_evidence:
          var = cpd.variables[0]
          if var not in self.model.nodes():
              raise ValueError(
                  "Evidence provided for variable which is not in the model"
              )
          elif len(cpd.variables) > 1:
              raise ValueError(
                  "Virtual evidence should be defined on individual variables. Maybe you are looking for soft evidence."
              )
  
          elif (
              self.model.get_cardinality(var)
              != cpd.get_cardinality([var])[var]
          ):
              raise ValueError(
                  "The number of states/cardinality for the evidence should be same as the number of states/cardinality of the variable in the model"
              )
  
      bn = self.model.copy()
      for cpd in virtual_evidence:
          var = cpd.variables[0]
          new_var = "__" + var
          bn.add_edge(var, new_var)
          values = compat_fns.get_compute_backend().vstack(
              (cpd.values, 1 - cpd.values)
          )
          new_cpd = TabularCPD(
              variable=new_var,
              variable_card=2,
              values=values,
              evidence=[var],
              evidence_card=[self.model.get_cardinality(var)],
              state_names={new_var: [0, 1], var: cpd.state_names[var]},
          )
          bn.add_cpds(new_cpd)
  
      self.__init__(bn)
  
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 4 for calling the Inference obj's underlying member method code with example usage: obj._virtual_evidence(virtual_evidence) without return types