1
1
from typing import Any , List , Optional , Union
2
2
3
3
import pandas as pd
4
+ import numpy as np
4
5
from sklearn .neighbors import NearestNeighbors
5
6
6
7
from dowhy .causal_estimator import CausalEstimate , CausalEstimator
@@ -130,12 +131,20 @@ def estimate_effect(
130
131
131
132
att = 0
132
133
numtreatedunits = treated .shape [0 ]
133
- for i in range (numtreatedunits ):
134
- treated_outcome = treated .iloc [i ][self ._target_estimand .outcome_variable [0 ]].item ()
135
- control_outcome = control .iloc [indices [i ]][self ._target_estimand .outcome_variable [0 ]].item ()
136
- att += treated_outcome - control_outcome
137
134
138
- att /= numtreatedunits
135
+ outcome_variable = self ._target_estimand .outcome_variable [0 ]
136
+ treated_outcomes = treated [outcome_variable ]
137
+ control_outcomes = [control .iloc [i ][outcome_variable ].item () for i in indices ]
138
+
139
+ att = np .mean (treated_outcomes - control_outcomes )
140
+
141
+ # for i in range(numtreatedunits):
142
+
143
+ # treated_outcome = treated.iloc[i][self._target_estimand.outcome_variable[0]].item()
144
+ # control_outcome = control.iloc[indices[i]][self._target_estimand.outcome_variable[0]].item()
145
+ # att += treated_outcome - control_outcome
146
+
147
+ # att /= numtreatedunits
139
148
140
149
# Now computing ATC
141
150
treated_neighbors = NearestNeighbors (n_neighbors = 1 , algorithm = "ball_tree" ).fit (
@@ -144,12 +153,19 @@ def estimate_effect(
144
153
distances , indices = treated_neighbors .kneighbors (control [self .propensity_score_column ].values .reshape (- 1 , 1 ))
145
154
atc = 0
146
155
numcontrolunits = control .shape [0 ]
147
- for i in range (numcontrolunits ):
148
- control_outcome = control .iloc [i ][self ._target_estimand .outcome_variable [0 ]].item ()
149
- treated_outcome = treated .iloc [indices [i ]][self ._target_estimand .outcome_variable [0 ]].item ()
150
- atc += treated_outcome - control_outcome
151
156
152
- atc /= numcontrolunits
157
+ outcome_variable = self ._target_estimand .outcome_variable [0 ].item ()
158
+ control_outcome = control [outcome_variable ]
159
+ treated_outcome = [treated .iloc [i ][outcome_variable ].item () for i in indices ]
160
+
161
+ atc = np .mean (treated_outcome - control_outcome )
162
+
163
+ # for i in range(numcontrolunits):
164
+ # control_outcome = control.iloc[i][self._target_estimand.outcome_variable[0]].item()
165
+ # treated_outcome = treated.iloc[indices[i]][self._target_estimand.outcome_variable[0]].item()
166
+ # atc += treated_outcome - control_outcome
167
+
168
+ # atc /= numcontrolunits
153
169
154
170
if target_units == "att" :
155
171
est = att
0 commit comments