How to use the causallib.simulation.CausalSimulator3.CausalSimulator3._sample_from_row_stochastic_matrix function in causallib

To help you get started, we’ve selected a few causallib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github IBM / causallib / causallib / simulation / CausalSimulator3.py View on Github external
min_p = cur_pdfs.div(cur_pdfs.sum()).min()
            cur_propensity = (max_p - min_p) * (cur_pdfs - cur_pdfs.min()) / \
                             (cur_pdfs.max() - cur_pdfs.min()) + min_p  # type: pd.Series
            # assign the propensity to the assigned category:
            propensity.loc[cur_samples_mask, cur_category] = cur_propensity
            # assign the propensity to the other, not assigned, categories:
            left_over_ps = prob_category.drop(cur_category)  # type: pd.Series
            left_over_ps = left_over_ps.div(left_over_ps.sum())
            not_propensity = pd.DataFrame(data=np.tile(np.ones_like(cur_propensity) - cur_propensity,
                                                       (left_over_ps.size, 1)).transpose(),
                                          index=cur_propensity.index, columns=left_over_ps.index)
            not_propensity = not_propensity.mul(left_over_ps)
            propensity.loc[cur_samples_mask, left_over_ps.index] = not_propensity
        # propensity = propensity.astype(np.float)
        # treatment assignment is drawn according to marginal propensities:
        treatment = CausalSimulator3._sample_from_row_stochastic_matrix(propensity)
        return propensity, treatment
github IBM / causallib / causallib / simulation / CausalSimulator3.py View on Github external
index_names = x_continuous.index
        columns_names = prob_category.index
        propensity = pd.DataFrame(index=index_names, columns=columns_names)
        # start with filling up the odds ratio:
        for cur_category, p in prob_category.iteritems():
            t = x_continuous.quantile(p, interpolation="higher")
            cur_propensity = (1.0 / (1 + np.exp((x_continuous - np.repeat(t, x_continuous.size)))))  # type: pd.Series
            cur_propensity = cur_propensity.div(np.ones_like(cur_propensity) - cur_propensity)
            cur_propensity += np.abs(np.random.normal(loc=0.0, scale=1 - snr, size=cur_propensity.size))
            # cur_propensity += np.random.exponential(scale=np.sqrt(snr), size=cur_propensity.size)
            propensity.loc[:, cur_category] = cur_propensity

        # normalize into probabilities:
        propensity = propensity.div(propensity.sum(axis="columns"), axis="rows")
        # treatment assignment is drawn according to marginal propensities:
        treatment = CausalSimulator3._sample_from_row_stochastic_matrix(propensity)
        return propensity, treatment
github IBM / causallib / causallib / simulation / CausalSimulator3.py View on Github external
ValueError: If given more than to categories. This method supports dichotomous treatment only.
        """
        if prob_category.size != 2:  # this method suited for dichotomous outcome only
            raise ValueError("logistic method supports only binary treatment. Got the distribution vector "
                             "{p_vec} of length {n_cat}".format(n_cat=prob_category.size, p_vec=prob_category))
        index_names = x_continuous.index
        columns_names = prob_category.index
        propensity = pd.DataFrame(index=index_names, columns=columns_names)
        # compute propensities:
        t = x_continuous.quantile(prob_category.iloc[1], interpolation="higher")
        slope = params.get("slope", 1.0) if params is not None else 1.0
        cur_propensity = 1.0 / (1 + np.exp(slope * (x_continuous - np.repeat(t, x_continuous.size))))
        # assign the propensity values:
        propensity.loc[:, columns_names[1]] = cur_propensity
        propensity.loc[:, columns_names[0]] = np.ones(cur_propensity.size) - cur_propensity
        treatment = CausalSimulator3._sample_from_row_stochastic_matrix(propensity)
        return propensity, treatment

causallib

A Python package for flexible and modular causal inference modeling

Apache-2.0
Latest version published 5 months ago

Package Health Score

67 / 100
Full package analysis

Similar packages