

def check_independence(self, event1, event2, event3=None, condition_random_variable=False):
    "\n        Check if the Joint Probability Distribution satisfies the given independence condition.\n\n        Parameters\n        ----------\n        event1: list\n            random variable whose independence is to be checked.\n        event2: list\n            random variable from which event1 is independent.\n        values: 2D array or list like or 1D array or list like\n            A 2D list of tuples of the form (variable_name, variable_state).\n            A 1D list or array-like to condition over randome variables (condition_random_variable must be True)\n            The values on which to condition the Joint Probability Distribution.\n        condition_random_variable: Boolean (Default false)\n            If true and event3 is not None than will check independence condition over random variable.\n\n        For random variables say X, Y, Z to check if X is independent of Y given Z.\n        event1 should be either X or Y.\n        event2 should be either Y or X.\n        event3 should Z.\n\n        Examples\n        --------\n        >>> from pgmpy.factors import JointProbabilityDistribution as JPD\n        >>> prob = JPD(['I','D','G'],[2,2,3],\n                       [0.126,0.168,0.126,0.009,0.045,0.126,0.252,0.0224,0.0056,0.06,0.036,0.024])\n        >>> prob.check_independence(['I'], ['D'])\n        True\n        >>> prob.check_independence(['I'], ['D'], [('G', 1)])  # Conditioning over G_1\n        False\n        >>> # Conditioning over random variable G\n        >>> prob.check_independence(['I'], ['D'], ('G',), condition_random_variable=True)\n        False\n        "
    JPD = self.copy()
    if isinstance(event1, six.string_types):
        raise TypeError('Event 1 should be a list or array-like structure')
    if isinstance(event2, six.string_types):
        raise TypeError('Event 2 should be a list or array-like structure')
    if event3:
        if isinstance(event3, six.string_types):
            raise TypeError('Event 3 cannot of type string')
        elif condition_random_variable:
            if (not all((isinstance(var, six.string_types) for var in event3))):
                raise TypeError('Event3 should be a 1d list of strings')
            event3 = list(event3)
            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

dataset/ETHPy150Open pgmpy/pgmpy/pgmpy/factors/JointProbabilityDistribution.py JointProbabilityDistribution.check_independence/original