dowhy学习1

特点:

与前人相比,DoWhy对因果推理模型的实现做出了三个关键贡献。

  • 提出了一种将给定问题建模为因果图的原则方法,使所有假设都清晰可见。
  • 为许多流行的因果推理方法提供统一的接口,结合了图形模型和潜在结果的两个主要框架。
  • 如果可能的话,自动测试假设的有效性,并评估对违规的估计的稳健性能。

安装:

pycharm里一键添加

或者使用pip(参考官网)

原理与使用:

dowhy支持gml(首选)和dot两种因果图格式,加载数据后,主要可以实现以下四种操作:建模、识别、估计反驳

# 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["gml_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")

四步因果推理

1,对因果问题建模。可以提供一个局部图,以表示有关某些变量的先验知识。DoWhy会自动将其余变量视为潜在的混杂因素。

2,确定目标估计。基于因果图,DoWhy根据图形模型找到识别所需因果效应的所有可能方法。

3,根据确定的估计值来计算因果效应。DoWhy支持基于后门准则和工具变量的方法。

4,反驳所得的估计。可以使用多种反驳方法来验证因果推断是使用DoWhy的主要好处。

一个例子

估计治疗对疾病痊愈的因果效应

1,导入数据集

扫描二维码关注公众号,回复: 11539126 查看本文章
import dowhy.datasets
data = dowhy.datasets.linear_dataset(beta=10,
        num_common_causes=5,
        num_instruments = 2,
        num_samples=10000,
        treatment_is_binary=True)
df = data["df"]
print(df.head())
print(data["dot_graph"])
print("\n")
print(data["gml_graph"])

2,以GML图格式输入因果图

# With graph
model=CausalModel(
        data = df,
        treatment=data["treatment_name"],
        outcome=data["outcome_name"],
        graph=data["gml_graph"]
        )

3,可以不用数据,仅通过因果图实现识别

identified_estimand = model.identify_effect()
print(identified_estimand)

4,预测

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

猜你喜欢

转载自blog.csdn.net/zhuiyunzhugang/article/details/107776168