Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the test_accuracy function by modifying the assertion logic #867

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 46 additions & 28 deletions econml/tests/test_discrete_outcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def test_accuracy(self):
discrete_outcome = True
discrete_treatment = True
true_ate = 0.3
W = np.random.uniform(-1, 1, size=(n, 1))
D = np.random.binomial(1, .5 + .1 * W[:, 0], size=(n,))
Y = np.random.binomial(1, .5 + true_ate * D + .1 * W[:, 0], size=(n,))
num_iterations = 10

ests = [
LinearDML(discrete_outcome=discrete_outcome, discrete_treatment=discrete_treatment),
Expand All @@ -39,34 +37,42 @@ def test_accuracy(self):

for est in ests:

if isinstance(est, CausalForestDML):
est.fit(Y, D, X=W)
ate = est.ate(X=W)
ate_lb, ate_ub = est.ate_interval(X=W)
count_within_interval = 0

else:
est.fit(Y, D, W=W)
ate = est.ate()
ate_lb, ate_ub = est.ate_interval()
for _ in range(num_iterations):

if isinstance(est, LinearDRLearner):
est.summary(T=1)
else:
est.summary()
W = np.random.uniform(-1, 1, size=(n, 1))
D = np.random.binomial(1, .5 + .1 * W[:, 0], size=(n,))
Y = np.random.binomial(1, .5 + true_ate * D + .1 * W[:, 0], size=(n,))

if isinstance(est, CausalForestDML):
est.fit(Y, D, X=W)
ate_lb, ate_ub = est.ate_interval(X=W)

else:
est.fit(Y, D, W=W)
ate_lb, ate_ub = est.ate_interval()

if isinstance(est, LinearDRLearner):
est.summary(T=1)
else:
est.summary()

if ate_lb <= true_ate <= ate_ub:
count_within_interval += 1

proportion_in_interval = ((ate_lb < true_ate) & (true_ate < ate_ub)).mean()
np.testing.assert_array_less(0.50, proportion_in_interval)
assert count_within_interval >= 7, (
f"{est.__class__.__name__}: True ATE falls within the interval bounds "
f"only {count_within_interval} times out of {num_iterations}"
)

# accuracy test, DML
def test_accuracy_iv(self):
n = 10000
n = 1000
discrete_outcome = True
discrete_treatment = True
true_ate = 0.3
W = np.random.uniform(-1, 1, size=(n, 1))
Z = np.random.uniform(-1, 1, size=(n, 1))
D = np.random.binomial(1, .5 + .1 * W[:, 0] + .1 * Z[:, 0], size=(n,))
Y = np.random.binomial(1, .5 + true_ate * D + .1 * W[:, 0], size=(n,))
num_iterations = 10

ests = [
OrthoIV(discrete_outcome=discrete_outcome, discrete_treatment=discrete_treatment),
Expand All @@ -75,14 +81,26 @@ def test_accuracy_iv(self):

for est in ests:

est.fit(Y, D, W=W, Z=Z)
ate = est.ate()
ate_lb, ate_ub = est.ate_interval()
count_within_interval = 0

for _ in range(num_iterations):

W = np.random.uniform(-1, 1, size=(n, 1))
Z = np.random.uniform(-1, 1, size=(n, 1))
D = np.random.binomial(1, .5 + .1 * W[:, 0] + .1 * Z[:, 0], size=(n,))
Y = np.random.binomial(1, .5 + true_ate * D + .1 * W[:, 0], size=(n,))

est.fit(Y, D, W=W, Z=Z)
ate_lb, ate_ub = est.ate_interval()
est.summary()

est.summary()
if ate_lb <= true_ate <= ate_ub:
count_within_interval += 1

proportion_in_interval = ((ate_lb < true_ate) & (true_ate < ate_ub)).mean()
np.testing.assert_array_less(0.50, proportion_in_interval)
assert count_within_interval >= 7, (
f"{est.__class__.__name__}: True ATE falls within the interval bounds "
f"only {count_within_interval} times out of {num_iterations}"
)

def test_string_outcome(self):
n = 100
Expand Down
Loading