# マッチング
target_list = e[e.index.isin(D[D==1].index)]
ate = 0
k = 0
for i in D[D==0].index:
num = e[e.index.isin(D[D==0].index)][i]
idx = np.abs(np.asarray(target_list) - num).argmin()
if np.abs(target_list.iloc[idx] - num) > stdev(e)/100:
continue
ate += int(Y.loc[target_list.index[idx]]) - int(Y.loc[i])
k += 1
ate = ate / k
# 層別化
interval = np.arange(0,1.05,0.05)
ate = 0
for i in range(0,len(interval)-1):
temp0 = e[(e.index.isin(D[D==0].index)) &
(e>=interval[i]) &
(e<interval[i+1])].index
temp1 = e[(e.index.isin(D[D==1].index)) &
(e>=interval[i]) &
(e<interval[i+1])].index
if (len(temp0) > 0) & (len(temp1) > 0):
ate += (Y[temp1].mean() - Y[temp0].mean())*(len(temp1) + len(temp0))
ate = ate / len(e)
# カーネルマッチング
f = sm.Logit(Y[Y.index.isin(D[D==0].index)], e[e.index.isin(D[D==0].index)]).fit(method="newton")
Y_hat = f.predict(e[e.index.isin(D[D==1].index)])
ate = (Y[Y.index.isin(D[D==1].index)] - Y_hat).sum() / len(Y_hat)
# DR
model0 = sm.Logit(Y[Y.index.isin(D[D==0].index)], X[X.index.isin(D[D==0].index)]).fit(method="ncg")
Y_hat0 = model0.predict(X)
model1 = sm.Logit(Y[Y.index.isin(D[D==1].index)], X[X.index.isin(D[D==1].index)]).fit(method="ncg")
Y_hat1 = model1.predict(X)
ate = ((D * Y / e - (1 - (D / e) * Y_hat1)).sum() - ((1-D) * Y / (1-e) - (1 - ((1-D) / (1-e)) * Y_hat0)).sum()) / len(Y)