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

"Fixes linear DRIV shape inconsistency" #920

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions econml/inference/_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from ._bootstrap import BootstrapEstimator
from ..utilities import (Summary, _safe_norm_ppf, broadcast_unit_treatments,
cross_product, inverse_onehot, ndim,
parse_final_model_params, reshape_treatmentwise_effects, shape, filter_none_kwargs)
parse_final_model_params, reshape_treatmentwise_effects, shape, filter_none_kwargs,
reshape_outcomewise_effects)

"""Options for performing inference in estimators."""

Expand Down Expand Up @@ -281,8 +282,8 @@ def effect_inference(self, X, *, T0, T1):
elif self.featurizer is not None:
X = self.featurizer.transform(X)
XT = cross_product(X, T1 - T0)
e_pred = self._predict(XT)
e_stderr = self._prediction_stderr(XT)
e_pred = reshape_outcomewise_effects(self._predict(XT), self._d_y)
e_stderr = reshape_outcomewise_effects(self._prediction_stderr(XT), self._d_y)
d_y = self._d_y[0] if self._d_y else 1

mean_XT = XT.mean(axis=0, keepdims=True)
Expand Down
3 changes: 1 addition & 2 deletions econml/tests/test_treatment_featurization.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,7 @@ def test_featurization(self):
assert est.intercept__inference().summary_frame().shape == expected_intercept_inf_shape

# loose inference checks
# temporarily skip LinearDRIV and SparseLinearDRIV for weird effect shape reasons
if isinstance(est, (KernelDML, LinearDRIV, SparseLinearDRIV)):
if isinstance(est, (KernelDML)):
continue

if est._inference is None:
Expand Down
22 changes: 22 additions & 0 deletions econml/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,28 @@ def reshape_treatmentwise_effects(A, d_t, d_y):
else:
return A

def reshape_outcomewise_effects(A, d_y):
"""
Given an effects matrix, reshape second dimension to be consistent with d_y[0].

Parameters
----------
A : array
The effects array to be reshaped. It should have shape (m,) or (m, d_y).
d_y : tuple of int
Either () if Y was a vector, or a 1-tuple of the number of columns of Y if it was an array.

Returns
-------
A : array
The reshaped effects array with shape:
- (m, ) if d_y is () and Y is a vector,
- (m, d_y) if d_y is a 1-tuple and Y is an array.
"""
if np.shape(A)[1:] == d_y or d_y == ():
return A
else:
return A.reshape(-1, d_y[0])

def einsum_sparse(subscripts, *arrs):
"""
Expand Down
Loading