Beispiel #1
0
def CDist(scm_a, scm_b, nb_samples_x, nb_samples_k=5, discrete=False):
    nodes_a = scm_a.nodes()
    nodes_b = scm_b.nodes()
    assert nodes_a == nodes_b, \
        "The two causal graphs should have the same nodes"
    n = len(nodes_a)

    x_distrib = IndependentComponentsDistribution(
        [UniformDistribution(0, 1) for _ in range(n)])
    scm_a.precompute_log_probabilities()
    scm_b.precompute_log_probabilities()

    results = IDist(scm_a, scm_b, nb_samples_k, discrete=discrete)

    for x in x_distrib.sample(nb_samples_x):
        # print('x: ' + str(x))
        cf_a = en.NUTSSampler(n, scm_a.log_probability)
        cf_b = en.NUTSSampler(n, scm_b.log_probability)

        counterfactual_scm_a = copy.deepcopy(scm_a)
        counterfactual_scm_b = copy.deepcopy(scm_b)

        # print("Creating noise")
        a_samples = cf_a.sample(x, M=nb_samples_k, Madapt=200)
        b_samples = cf_b.sample(x, M=nb_samples_k, Madapt=200)

        counterfactual_scm_a.noises_distribution = CounterfactualNoise(
            a_samples, x, nb_samples_k)
        counterfactual_scm_b.noises_distribution = CounterfactualNoise(
            b_samples, x, nb_samples_k)
        # print('End noise')

        # print("Run IDist")
        results += IDist(counterfactual_scm_a,
                         counterfactual_scm_b,
                         nb_samples_k,
                         discrete=discrete)
        # print("E nd IDist")

    return results / float(nb_samples_x + 1)
Beispiel #2
0
    def __init__(self, funcs):
        """
        A multidimensional freeform probability distribution object for
        independent dimensions

        Parameters
        ----------
        funcs: list, chippr.gauss or chippr.discrete or chippr.gmix objects
            1D functions, one per dimension
        """
        self.funcs = funcs
        self.dims = len(funcs)
        #for d in range(self.dims):
        #    print('multi dist '+str((d, type(self.funcs[d]))))
        self.funcs = [func.dist for func in self.funcs]
        self.dist = ICD(self.funcs)
def non_gaussian_noise(n):
    IndependentComponentsDistribution([
        GammaDistribution(alpha=np.random.rand(), beta=np.random.rand())
        for _ in range(n)
    ])
def default_gaussian_noise(n):
    return IndependentComponentsDistribution(
        [NormalDistribution(0, np.random.rand()) for _ in range(n)])
Beispiel #5
0
def default_noise(n):
    return IndependentComponentsDistribution(
        [NormalDistribution(0, 0.1) for _ in range(n)])
Beispiel #6
0
class multi_dist(object):
    def __init__(self, funcs):
        """
        A multidimensional freeform probability distribution object for
        independent dimensions

        Parameters
        ----------
        funcs: list, chippr.gauss or chippr.discrete or chippr.gmix objects
            1D functions, one per dimension
        """
        self.funcs = funcs
        self.dims = len(funcs)
        #for d in range(self.dims):
        #    print('multi dist '+str((d, type(self.funcs[d]))))
        self.funcs = [func.dist for func in self.funcs]
        self.dist = ICD(self.funcs)

    def pdf(self, points):
        return self.evaluate(points)

    def evaluate_one(self, point):
        """
        Function to evaluate the probability at a point in multidimensional
        space

        Parameters
        ----------
        point: numpy.ndarray, float
            coordinate at which to evaluate the probability

        Returns
        -------
        prob: float
            probability associated with point
        """
        # prob = 1.
        # for d in range(self.dims):
        #     prob *= self.funcs[d].evaluate_one(point[d])
        prob = self.dist.probability(point)

        return prob

    def evaluate(self, points):
        """
        Function to evaluate the probability at a point in multidimensional
        space

        Parameters
        ----------
        points: numpy.ndarray, float
            coordinates at which to evaluate the probability

        Returns
        -------
        probs: float
            probabilities associated with points
        """
        # if len(np.shape(points.T)) == 1:
        #     return self.evaluate_one(points)
        # probs = np.ones(len(points))
        # points = points.T
        # for d in range(self.dims):
        #     probs *= self.funcs[d].evaluate(points[d])
        probs = self.dist.probability(points)
        return probs

    def sample_one(self):
        """
        Function to take one sample from independent distributions

        Returns
        -------
        point: numpy.ndarray, float
            single sample from independent distributions
        """
        # point = np.zeros(self.dims)
        # for d in range(self.dims):
        #     point[d] = self.funcs[d].sample_one()
        point = self.dist.sample(1)
        return point

    def sample(self, n_samps):
        """
        Function to sample from multivariate probability distribution with independent components

        Parameters
        ----------
        n_samps: positive int
            number of samples to take

        Returns
        -------
        points: ndarray, float
            array of n_samps samples from independent distributions
        """
        # print('multi_dist trying to sample '+str(n_samps)+' from '+str(self.dist))
        # points = np.array([self.sample_one() for n in range(n_samps)])
        points = np.array([self.dist.sample() for n in range(n_samps)])
        # print('multi_dist sampled '+str(n_samps)+' from '+str(self.dist))
        return points