Skip to content

Instantly share code, notes, and snippets.

@kiwamizamurai
Created March 19, 2020 07:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kiwamizamurai/7b59f2b051b8740431c1b95c3fe46f8f to your computer and use it in GitHub Desktop.
DoublyRobust-AIPTW.ipynb
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kiwamizamurai
Copy link
Author

基本的にAIPTWはIPTWよりも精度は良いはずですがなぜでしょうか、、、

@tohtsky
Copy link

tohtsky commented Sep 11, 2020

丁寧な紹介記事ありがとうございます!

model_linear にはXだけではなく、Zも説明変数に入れる必要があるのではないでしょうか?

X_and_z = np.hstack([X, z[:, None]])
model_linear = LinearRegression().fit(X_and_z, y)

その上で、m_1とm_0は別個に

df_ps_m['m_1'] = model_linear.predict(np.hstack([X, np.repeat([1], X.shape[0])[:, None]])).flatten()
df_ps_m['m_0'] = model_linear.predict(np.hstack([X, np.repeat([0], X.shape[0])[:, None]])).flatten()

と計算し、

delta_dr_first_iptw = np.sum(df_ps_m["outcome"] * w1)
delta_dr_first_augmentation = np.sum(w1*df_ps_m["m_1"])  - np.sum(df_ps_m["m_1"])

delta_dr_first = (delta_dr_first_iptw - delta_dr_first_augmentation) / size


delta_dr_second_iptw = np.sum(df_ps_m["outcome"] * w0)
delta_dr_second_augmentation = np.sum(w0*df_ps_m["m_0"]) - np.sum(df_ps_m["m_0"])

delta_dr_second = (delta_dr_second_iptw - delta_dr_second_augmentation) / size

delta_dr = delta_dr_first - delta_dr_second

print('caucal effect ', delta_dr)

とすると、 delta_dr ~ 0.203となりました。

@kiwamizamurai
Copy link
Author

@tohtsky さん

いえいえ、こちらこそ非常に汚いコードで申し訳ないです。ありがとうございます。

昔の記事なのでうる覚えで見直しましたが線形モデルには確かにzは入ってますね。

なので@tohtsky さんが正しいです。コメント本当にありがとうございます。

delta_dr ~ 0.203

よかったです!。モヤモヤ解消ですありがとうございます!

@tohtsky
Copy link

tohtsky commented Sep 11, 2020

PythonでAIPWやっている例はあまりない(Rが多い)ので助かりました。
この辺、z を説明変数に入れたり、z毎に異なる線形モデルを立てたりいろいろ流儀があるみたいでややこしいですよね。。

@kiwamizamurai
Copy link
Author

kiwamizamurai commented Sep 11, 2020

@tohtsky ですね!ここらへんは少しややこしいですね。僕がやってたときもrがほとんどでしたね、そもそも因果推論も全然少なかった気がします。いまはやっとCFMLが少しずつ人気が出ていますね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment