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

added warning message and test for issue #837 #919

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions econml/_ortho_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,15 @@ def _fit_nuisances(Y, T, X, W, Z, sample_weight, groups):
elif not np.array_equal(fitted_inds, new_inds):
raise AttributeError("Different indices were fit by different folds, so they cannot be aggregated")

if nuisances[1].sum() == 0:
raise ValueError(
"""
In fitting nuisances, the estimates for E[T|Z,X,W] are identical to E[T|X,W],
resulting in a situation where the rows will all be weighted to zero. Please
examine your instrument variable `Z`.
"""
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the right place to address this issue: it would apply to all OrthoLearner subclasses, including ones like LinearDRLearner that don't exhibit the bug, and the problem is not that the nuisances sum to 0, it's that all values of the treatment residual are 0, which then makes them unsuitable to use as weights here. However, that logic is not specific to NonParamDMLIV, so if you made the check there it wouldn't make sense to refer to E[T|Z,X,W]. Therefore it would probably be better to make the change here where the T residual is generated.

if self.mc_iters is not None:
if self.mc_agg == 'mean':
nuisances = tuple(np.mean(nuisance_mc_variants, axis=0)
Expand Down
19 changes: 19 additions & 0 deletions econml/tests/test_dmliv.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,22 @@ def test_groups(self):
est.fit(y, T, Z=Z, X=X, W=W, groups=groups)
est.score(y, T, Z=Z, X=X, W=W)
est.const_marginal_effect(X)

def test_row_zero_weight_failure_mode(self):
np.random.seed(784)

n = 100
d_x = 3

Y = np.random.normal(size=(n,))
T = np.random.normal(size=(n,))
X = np.random.normal(size=(n, d_x))
Z = np.random.normal(size=(n,))

est = NonParamDMLIV(
discrete_instrument=False,
discrete_treatment=False,
model_final=LinearRegression()
)
with pytest.raises(ValueError, match=r" examine your instrument variable "):
est.fit(Y, T, Z=Z, X=X)
Loading