Skip to content

Commit

Permalink
"Fixes linear DRIV shape inconsistency" (#920)
Browse files Browse the repository at this point in the history
Signed-off-by: ssemov <ssemov@gmail.com>
  • Loading branch information
ssemov authored Oct 16, 2024
1 parent fae5f72 commit e0271fd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
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

0 comments on commit e0271fd

Please sign in to comment.