15
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DoWhyによる統計的因果推論

15
Last updated at Posted at 2020-05-09

DoWhyによる因果推論

DoWhyの方向性とやり方のメモ(備忘録)

目次

◆はじめに
◆必要性
◆導入環境
◆導入法
◆動かしてみた
--準備
--データ生成
--Model(仮定)
--Identification(変数の識別)
--Estimation(数式から推定)
--Check(Sensitivity Analysis)
◆参照

はじめに

DoWhyはPythonの因果推論ライブラリで、DAGによる仮定の明示、効果の識別、推定、感度分析を順に実行できる。本記事では、生成時の効果が分かっているデータで、この手順を試す。

DAGは分析者が変数間の関係を検討して与える。推定後のチェックは、仮定を変えたときの結果の変化を調べるものであり、DAGが正しいことを自動で保証するものではない。

必要性

予測モデルは、観測データのInputとOutcomeを結び付けるパターンを明らかにする。しかしながら、介入を実施してその効果を見たい場合には、データが存在しない現在の値から、Inputを変更した場合の影響を推定する必要がある。これは以下のような反事実の推定を行うことである。

・Will it work?
システムの変更は結果を改善しうるものなのか?

・Why did it work?
結果に変化をもたらしたものは何か?

・What should we do?
システムにどのような変更を加えたら、結果が改善されるか?

・What are the overall effects?
システムはどのように他の要因と作用し、その要因に影響を及ぼすか?

これらの疑問に答えるには因果推論が必要で、多くの方法があるが仮定と結果のロバスト性(assumptions and robustness of results)を比較することは困難である。

そこでDoWhyでは、与えられた問いを因果グラフとしてモデル化する原則的な方法を用いて、すべての仮定が明確になるようにする。次に、モデルと潜在的な結果のフレームワークを組み合わせて、一般的な因果推論に帰着させる。そして可能であれば、仮定の妥当性を自動的にテストしてミスに対する推定値のロバスト性を評価する。

導入環境

OS: Mojave (version; 10.14.6)
Python: 3.7.6
JupyterLab: 1.2.6
Graphviz: 2.40.1

導入法

以下のようにしてインストールした。

$ pip install dowhy

それとPyGraphvizを入れてなかったので、それも入れた。

$ sudo pip install --install-option="--include-path=/usr/local/include/" --install-option="--library-path=/usr/local/lib/" pygraphviz

動かしてみた

DoWhyの主要な流れは以下。

image.png

基本的には図に沿って実行していく。

準備

import numpy as np
import pandas as pd
import dowhy 
from dowhy import CausalModel
import dowhy.datasets
import pygraphviz

データ生成

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"])
# 結果が以下
    Z0        Z1        W0        W1        W2        W3        W4    v0  \
0  0.0  0.697077  0.145565 -1.297894  2.226496  1.645846 -0.385215  True   
1  0.0  0.427846  0.356681  1.331070  0.740072  0.265514 -1.838137  True   
2  1.0  0.525590  0.383658  0.535433  0.795982  1.743974 -1.554143  True   
3  0.0  0.135632 -0.002793 -0.607062  1.111112  0.558599 -1.201494  True   
4  1.0  0.966809 -0.613649  2.351279  0.746445 -1.000265 -0.700003  True   

           y  
0  11.272817  
1  13.219180  
2  12.116330  
3   7.008058  
4  18.573009  
digraph { U[label="Unobserved Confounders"]; U->y;v0->y; U->v0;W0-> v0; W1-> v0; W2-> v0; W3-> v0; W4-> v0;Z0-> v0; Z1-> v0;W0-> y; W1-> y; W2-> y; W3-> y; W4-> y;}

処置(原因)は"v0"で、結果が"y"となっている。
未観測共通原因は"U"として、"W"(0-4)は観測されている共通原因、そして"Z"(0-1)は"v0"の観測された変数。

上記のことが既知として、効果を推定する(真の因果効果10にどれだけ近づくか見ていく)。

Model(仮定)

model = CausalModel(
        data=df,
        treatment=data["treatment_name"],
        outcome=data["outcome_name"],
        graph=data["gml_graph"])
# 結果
INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']

DAGを描く

model.view_model()
from IPython.display import Image, display
display(Image(filename="causal_model.png"))

Identification(変数の識別)

先ほど描いたDAGから、推定に用いる変数を見積もる。

identified_estimand = model.identify_effect()
print(identified_estimand)
# 結果
INFO:dowhy.causal_identifier:Common causes of treatment and outcome:['W4', 'W2', 'W1', 'W3', 'W0', 'U']

WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.

WARN: Do you want to continue by ignoring any unobserved confounders? (use proceed_when_unidentifiable=True to disable this prompt) [y/n]  y

INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]

Estimand type: nonparametric-ate
### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d                                 
─────(Expectation(y|W4,W2,W1,W3,W0))
d[v₀]                               
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W4,W2,W1,W3,W0,U) = P(y|v0,W4,W2,W1,W3,W0)

### Estimand : 2
Estimand name: iv
No such variable found!

先ほどのDAGが正しく、Conditional Exchangeabilityが成立する場合(すなわち仮定が正しい場合)の見積もり結果となっている。

Backdoor基準から、"v0"を変化させた時の"W"で条件付けられた"y"の変化量が求めたいATEとなっている。

Estimation(数式から推定)

ここでは数式を用いて先ほどの結果を実際に当てはめて計算する。
DoWhyでは以下が利用可能。

  • 回帰
  • 層別
  • マッチング
  • IPW
  • IV
  • RDD

また、CATE(conditional ATE)の推定に利用されるPythonライブラリであるEconML(microsoft)と併用することで、CATEも利用可能。
(まだ読みきれていない。EconML利用できるということはMeta-LeanerやORFとかを観察データから推定に利用できることなのか?)

今回は層別を実行した。

causal_estimate = model.estimate_effect(identified_estimand,
                                       method_name="backdoor.propensity_score_stratification") # backdoor.xxxの末尾で変更する
print(causal_estimate)
print("Causal Estimate is" + str(causal_estimate.value))
# 結果
INFO:dowhy.causal_estimator:INFO: Using Propensity Score Stratification Estimator

INFO:dowhy.causal_estimator:b: y~v0+W4+W2+W1+W3+W0

*** Causal Estimate ***

## Target estimand
Estimand type: nonparametric-ate
### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d                                 
─────(Expectation(y|W4,W2,W1,W3,W0))
d[v₀]                               
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W4,W2,W1,W3,W0,U) = P(y|v0,W4,W2,W1,W3,W0)

### Estimand : 2
Estimand name: iv
Estimand expression:
Expectation(Derivative(y, [Z1, Z0])*Derivative([v0], [Z1, Z0])**(-1))
Estimand assumption 1, As-if-random: If U→→y then ¬(U →→{Z1,Z0})
Estimand assumption 2, Exclusion: If we remove {Z1,Z0}→{v0}, then ¬({Z1,Z0}→y)

## Realized estimand
b: y~v0+W4+W2+W1+W3+W0
## Estimate
Value: 10.074645926227818

Causal Estimate is10.074645926227818

最後のEstimateが推定した値である(ちなみにデータ生成の段階では因果効果を10に設定している)。

Check(Sensitivity Analysis)

ここでは、ランダムな共通原因の追加、未観測共通原因の追加、処置のランダム置換、データの部分抽出という4つの操作を試す。追加変数や未測定交絡の仮定に対する感度、効果がない設定での出力、標本を変えたときの安定性を、それぞれ別に確認する。

今回の推定は、設定したDAGとBackdoor基準に依存する。これらのチェックを通過しても仮定の正しさは証明されないため、推定値と一緒に、どの操作を試したかを記録する。

  • ランダムな共通原因変数を追加する
res_random=model.refute_estimate(identified_estimand, estimate, method_name="random_common_cause")
print(res_random)
# 結果
INFO:dowhy.causal_estimator:INFO: Using Propensity Score Stratification Estimator

INFO:dowhy.causal_estimator:b: y~v0+W4+W2+W1+W3+W0+w_random

Refute: Add a Random Common Cause
Estimated effect:(10.074645926227818,)
New effect:(9.950638858187622,)
  • 未観測共通原因を追加
res_unobserved=model.refute_estimate(identified_estimand, estimate, method_name="add_unobserved_common_cause",
                                     confounders_effect_on_treatment="binary_flip", confounders_effect_on_outcome="linear",
                                    effect_strength_on_treatment=0.01, effect_strength_on_outcome=0.02)
print(res_unobserved)
# 結果
INFO:dowhy.causal_estimator:INFO: Using Propensity Score Stratification Estimator

INFO:dowhy.causal_estimator:b: y~v0+W4+W2+W1+W3+W0

Refute: Add an Unobserved Common Cause
Estimated effect:(10.074645926227818,)
New effect:(9.189776632922118,)
  • 原因(v0)をランダムな変数に置換
res_placebo=model.refute_estimate(identified_estimand, estimate,
                                 method_name="placebo_treatment_refuter",
                                 placebo_type="permute")
print(res_placebo)
# 結果
INFO:dowhy.causal_estimator:INFO: Using Propensity Score Stratification Estimator

INFO:dowhy.causal_estimator:b: y~placebo+W4+W2+W1+W3+W0

Refute: Use a Placebo Treatment
Estimated effect:(10.074645926227818,)
New effect:(0.07317739755853733,)
  • 基データのサブセットで推定
res_subset=model.refute_estimate(identified_estimand, estimate,
                                 method_name="data_subset_refuter",
                                 subset_fraction=0.9) # 基データから抽出するデータの割合
print(res_subset)
# 結果
INFO:dowhy.causal_estimator:INFO: Using Propensity Score Stratification Estimator

INFO:dowhy.causal_estimator:b: y~v0+W4+W2+W1+W3+W0

Refute: Use a subset of data
Estimated effect:(10.074645926227818,)
New effect:(10.125793210348078,)

また、再現性のためにrandom.seedを設定することも可能。

res_subset=model.refute_estimate(identified_estimand, estimate,
                                 method_name="data_subset_refuter",
                                 subset_fraction=0.9, random_seed=1) # サブセットの割合とシード値の設定
print(res_subset)
# 結果
INFO:dowhy.causal_estimator:INFO: Using Propensity Score Stratification Estimator

INFO:dowhy.causal_estimator:b: y~v0+W4+W2+W1+W3+W0

Refute: Use a subset of data
Estimated effect:(10.074645926227818,)
New effect:(10.068276205100556,)

参照

15
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?