Skip to content

Commit 804bf95

Browse files
add vector operations
1 parent 7aa5b2b commit 804bf95

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

dowhy/causal_estimators/propensity_score_matching_estimator.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Any, List, Optional, Union
22

33
import pandas as pd
4+
import numpy as np
45
from sklearn.neighbors import NearestNeighbors
56

67
from dowhy.causal_estimator import CausalEstimate, CausalEstimator
@@ -130,12 +131,20 @@ def estimate_effect(
130131

131132
att = 0
132133
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
137134

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
139148

140149
# Now computing ATC
141150
treated_neighbors = NearestNeighbors(n_neighbors=1, algorithm="ball_tree").fit(
@@ -144,12 +153,19 @@ def estimate_effect(
144153
distances, indices = treated_neighbors.kneighbors(control[self.propensity_score_column].values.reshape(-1, 1))
145154
atc = 0
146155
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
151156

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
153169

154170
if target_units == "att":
155171
est = att

0 commit comments

Comments
 (0)