コード例 #1
0
ファイル: base.py プロジェクト: while519/dowhy
    def null_refutation_test(self,
                             data=None,
                             dataset="linear",
                             beta=10,
                             num_common_causes=1,
                             num_instruments=1,
                             num_samples=100000,
                             treatment_is_binary=True):
        # Supports user-provided dataset object
        if data is None:
            data = dowhy.datasets.linear_dataset(
                beta=beta,
                num_common_causes=num_common_causes,
                num_instruments=num_instruments,
                num_samples=num_samples,
                treatment_is_binary=treatment_is_binary)

        model = CausalModel(data=data['df'],
                            treatment=data["treatment_name"],
                            outcome=data["outcome_name"],
                            graph=data["gml_graph"],
                            proceed_when_unidentifiable=True,
                            test_significance=None)
        target_estimand = model.identify_effect()
        ate_estimate = model.estimate_effect(
            identified_estimand=target_estimand,
            method_name=self.estimator_method,
            test_significance=None)
        true_ate = data["ate"]

        # To test if there are any exceptions
        ref = model.refute_estimate(
            target_estimand,
            ate_estimate,
            method_name=self.refuter_method,
            confounders_effect_on_treatment=self.confounders_effect_on_t,
            confounders_effect_on_outcome=self.confounders_effect_on_y,
            effect_strength_on_treatment=self.effect_strength_on_t,
            effect_strength_on_outcome=self.effect_strength_on_y)
        # To test if the estimate is identical if refutation parameters are zero
        refute = model.refute_estimate(
            target_estimand,
            ate_estimate,
            method_name=self.refuter_method,
            confounders_effect_on_treatment=self.confounders_effect_on_t,
            confounders_effect_on_outcome=self.confounders_effect_on_y,
            effect_strength_on_treatment=0,
            effect_strength_on_outcome=0)
        error = abs(refute.new_effect - ate_estimate.value)
        print(
            "Error in refuted estimate = {0} with tolerance {1}%. Estimated={2},After Refutation={3}"
            .format(error, self._error_tolerance * 100, ate_estimate.value,
                    refute.new_effect))
        res = True if (
            error < abs(ate_estimate.value) * self._error_tolerance) else False
        assert res
コード例 #2
0
    model = CausalModel(
        data=data["df"],
        treatment=data["treatment_name"],
        outcome=data["outcome_name"],
        graph=data["dot_graph"])

    identified_estimand = model.identify_effect()

    estimate = model.estimate_effect(
        identified_estimand,
        method_name="backdoor.linear_regression")
    print("Causal Estimate is " + str(estimate.value))

    # Adding a random common cause variable
    res_random = model.refute_estimate(
        identified_estimand, estimate,
        method_name="random_common_cause")
    print(res_random)

    # Replacing treatment with a random (placebo) variable
    res_placebo = model.refute_estimate(
        identified_estimand,
        estimate,
        method_name="placebo_treatment_refuter", placebo_type="permute")
    print(res_placebo)

    # Removing a random subset of the data
    res_subset = model.refute_estimate(
        identified_estimand,
        estimate,
        method_name="data_subset_refuter",
コード例 #3
0
ファイル: dowhy_001.py プロジェクト: yuzuohong/Test_Codes
from dowhy.do_why import CausalModel
import dowhy.datasets
import Pygraphviz

# Load some sample data
data = dowhy.datasets.linear_dataset(beta=10,
                                     num_common_causes=5,
                                     num_instruments=2,
                                     num_samples=10000,
                                     treatment_is_binary=True)

# Create a causal model from the data and given graph.
model = CausalModel(
    data=data["df"],
    treatment=data["treatment_name"],
    outcome=data["outcome_name"],
    graph=data["dot_graph"],
)

# Identify causal effect and return target estimands
identified_estimand = model.identify_effect()

# Estimate the target estimand using a statistical method.
estimate = model.estimate_effect(
    identified_estimand, method_name="backdoor.propensity_score_matching")

# Refute the obtained estimate using multiple robustness checks.
refute_results = model.refute_estimate(identified_estimand,
                                       estimate,
                                       method_name="random_common_cause")