class_docstr: Base class for Joint Probability Distribution
class_docstr_tok: ["Base","class","for","Joint","Probability","Distribution"]
docstr: Check if the Joint Probability Distribution satisfies the given independence condition.
  
  Parameters
  ----------
  event1: list
      random variable whose independence is to be checked.
  event2: list
      random variable from which event1 is independent.
  values: 2D array or list like or 1D array or list like
      A 2D list of tuples of the form (variable_name, variable_state).
      A 1D list or array-like to condition over randome variables (condition_random_variable must be True)
      The values on which to condition the Joint Probability Distribution.
  condition_random_variable: Boolean (Default false)
      If true and event3 is not None than will check independence condition over random variable.
  
  For random variables say X, Y, Z to check if X is independent of Y given Z.
  event1 should be either X or Y.
  event2 should be either Y or X.
  event3 should Z.
  
  Examples
  --------
  >>> from pgmpy.factors.discrete import JointProbabilityDistribution as JPD
  >>> prob = JPD(['I','D','G'],[2,2,3],
                 [0.126,0.168,0.126,0.009,0.045,0.126,0.252,0.0224,0.0056,0.06,0.036,0.024])
  >>> prob.check_independence(['I'], ['D'])
  True
  >>> prob.check_independence(['I'], ['D'], [('G', 1)])  # Conditioning over G_1
  False
  >>> # Conditioning over random variable G
  >>> prob.check_independence(['I'], ['D'], ('G',), condition_random_variable=True)
  False
docstr_tok: ["Check","if","the","Joint","Probability","Distribution","satisfies","the","given","independence","condition",".","Parameters","--","--","--","--","--","event1",":","list","random","variable","whose","independence","is","to","be","checked",".","event2",":","list","random","variable","from","which","event1","is","independent",".","values",":","2D","array","or","list","like","or","1D","array","or","list","like","A","2D","list","of","tuples","of","the","form","(","variable_name",",","variable_state",")",".","A","1D","list","or","array-like","to","condition","over","randome","variables","(","condition_random_variable","must","be","True",")","The","values","on","which","to","condition","the","Joint","Probability","Distribution",".","condition_random_variable",":","Boolean","(","Default","false",")","If","true","and","event3","is","not","None","than","will","check","independence","condition","over","random","variable",".","For","random","variables","say","X",",","Y",",","Z","to","check","if","X","is","independent","of","Y","given","Z.","event1","should","be","either","X","or","Y.","event2","should","be","either","Y","or","X.","event3","should","Z",".","Examples","--","--","--","--",">",">",">","from","pgmpy.factors.discrete","import","JointProbabilityDistribution","as","JPD",">",">",">","prob","=","JPD","(","[","'","I","'",",","'D","'",",","'","G","'","]",",","[","2,2,3","]",",","[","0.126,0.168,0.126,0.009,0.045,0.126,0.252,0.0224,0.0056,0.06,0.036,0.024","]",")",">",">",">","prob.check_independence","(","[","'","I","'","]",",","[","'D","'","]",")","True",">",">",">","prob.check_independence","(","[","'","I","'","]",",","[","'D","'","]",",","[","(","'","G","'",",","1",")","]",")","#","Conditioning","over","G_1","False",">",">",">","#","Conditioning","over","random","variable","G",">",">",">","prob.check_independence","(","[","'","I","'","]",",","[","'D","'","]",",","(","'","G","'",",",")",",","condition_random_variable=True",")","False"]
code: def check_independence(
      self, event1, event2, event3=None, condition_random_variable=False
  ):
      """
      Check if the Joint Probability Distribution satisfies the given independence condition.
  
      Parameters
      ----------
      event1: list
          random variable whose independence is to be checked.
      event2: list
          random variable from which event1 is independent.
      values: 2D array or list like or 1D array or list like
          A 2D list of tuples of the form (variable_name, variable_state).
          A 1D list or array-like to condition over randome variables (condition_random_variable must be True)
          The values on which to condition the Joint Probability Distribution.
      condition_random_variable: Boolean (Default false)
          If true and event3 is not None than will check independence condition over random variable.
  
      For random variables say X, Y, Z to check if X is independent of Y given Z.
      event1 should be either X or Y.
      event2 should be either Y or X.
      event3 should Z.
  
      Examples
      --------
      >>> from pgmpy.factors.discrete import JointProbabilityDistribution as JPD
      >>> prob = JPD(['I','D','G'],[2,2,3],
                     [0.126,0.168,0.126,0.009,0.045,0.126,0.252,0.0224,0.0056,0.06,0.036,0.024])
      >>> prob.check_independence(['I'], ['D'])
      True
      >>> prob.check_independence(['I'], ['D'], [('G', 1)])  # Conditioning over G_1
      False
      >>> # Conditioning over random variable G
      >>> prob.check_independence(['I'], ['D'], ('G',), condition_random_variable=True)
      False
      """
      JPD = self.copy()
      if isinstance(event1, str):
          raise TypeError(
              "Event 1 should be a list or array-like structure"
          )
  
      if isinstance(event2, str):
          raise TypeError(
              "Event 2 should be a list or array-like structure"
          )
  
      if event3:
          if isinstance(event3, str):
              raise TypeError("Event 3 cannot of type string")
  
          elif condition_random_variable:
              if not all(isinstance(var, str) for var in event3):
                  raise TypeError(
                      "event3 should be a 1d list of strings"
                  )
              event3 = list(event3)
              # Using the definition of conditional independence
              # If P(X,Y|Z) = P(X|Z)*P(Y|Z)
              # This can be expanded to P(X,Y,Z)*P(Z) == P(X,Z)*P(Y,Z)
              phi_z = JPD.marginal_distribution(
                  event3, inplace=False
              ).to_factor()
              for variable_pair in itertools.product(event1, event2):
                  phi_xyz = JPD.marginal_distribution(
                      event3 + list(variable_pair), inplace=False
                  ).to_factor()
                  phi_xz = JPD.marginal_distribution(
                      event3 + [variable_pair[0]], inplace=False
                  ).to_factor()
                  phi_yz = JPD.marginal_distribution(
                      event3 + [variable_pair[1]], inplace=False
                  ).to_factor()
                  if phi_xyz * phi_z != phi_xz * phi_yz:
                      return False
              return True
          else:
              JPD.conditional_distribution(event3)
  
      for variable_pair in itertools.product(event1, event2):
          if JPD.marginal_distribution(
              variable_pair, inplace=False
          ) != JPD.marginal_distribution(
              variable_pair[0], inplace=False
          ) * JPD.marginal_distribution(
              variable_pair[1], inplace=False
          ):
              return False
      return True
  
file_path: pgmpy/pgmpy/factors/discrete/JointProbabilityDistribution.py
imports: import itertools
  from functools import reduce
  from operator import mul
  import numpy
  from pgmpy.factors.discrete import DiscreteFactor
  from pgmpy.independencies import Independencies
label_desc: Use image node_id 5 for calling the JointProbabilityDistribution obj's underlying member method code with example usage: obj.check_independence(event1, event2, event3, condition_random_variable) and returns: True, False, True, False