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

Add causal analysis solution #447

Merged
merged 3 commits into from
May 9, 2021
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ For information on use cases and background material on causal inference and het

# News

**March 22, 2021:** Release v0.10.0, see release notes [here](https://github.com/Microsoft/EconML/releases/tag/v0.10.0)
**May 8, 2021:** Release v0.11.0, see release notes [here](https://github.com/Microsoft/EconML/releases/tag/v0.11.0)

<details><summary>Previous releases</summary>

**March 22, 2021:** Release v0.10.0, see release notes [here](https://github.com/Microsoft/EconML/releases/tag/v0.10.0)

**March 11, 2021:** Release v0.9.2, see release notes [here](https://github.com/Microsoft/EconML/releases/tag/v0.9.2)

**March 3, 2021:** Release v0.9.1, see release notes [here](https://github.com/Microsoft/EconML/releases/tag/v0.9.1)
Expand Down
5 changes: 5 additions & 0 deletions azure-pipelines-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ jobs:
displayName: 'Install graphviz on Linux'
condition: and(succeeded(), eq(variables['Agent.OS'], 'Linux'))

# Install OpenMP on Mac to support lightgbm
- script: 'brew install libomp'
displayName: 'Install OpenMP on Mac'
condition: and(succeeded(), eq(variables['Agent.OS'], 'Darwin'))

# Install the package
- script: 'python -m pip install --upgrade pip && pip install --upgrade setuptools wheel Cython && pip install ${{ parameters.package }}'
displayName: 'Install dependencies'
Expand Down
3 changes: 2 additions & 1 deletion econml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
'ortho_iv',
'policy',
'score',
'solutions',
'sklearn_extensions',
'tree',
'two_stage_least_squares',
'utilities',
'dowhy',
'__version__']

__version__ = '0.10.0'
__version__ = '0.11.0'
3 changes: 2 additions & 1 deletion econml/_cate_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from warnings import warn
from .inference import BootstrapInference
from .utilities import (tensordot, ndim, reshape, shape, parse_final_model_params,
inverse_onehot, Summary, get_input_columns)
inverse_onehot, Summary, get_input_columns, check_input_arrays)
from .inference import StatsModelsInference, StatsModelsInferenceDiscrete, LinearModelFinalInference,\
LinearModelFinalInferenceDiscrete, NormalInferenceResults, GenericSingleTreatmentModelFinalInference,\
GenericModelFinalInferenceDiscrete
Expand Down Expand Up @@ -809,6 +809,7 @@ def _postfit(self, Y, T, *args, **kwargs):
self._set_transformed_treatment_names()

def _expand_treatments(self, X=None, *Ts):
X, *Ts = check_input_arrays(X, *Ts)
n_rows = 1 if X is None else shape(X)[0]
outTs = []
for T in Ts:
Expand Down
6 changes: 4 additions & 2 deletions econml/dml/causal_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from ..sklearn_extensions.model_selection import WeightedStratifiedKFold
from ..inference import NormalInferenceResults
from ..inference._inference import Inference
from ..utilities import add_intercept, shape, check_inputs, _deprecate_positional, cross_product, Summary
from ..utilities import (add_intercept, shape, check_inputs, check_input_arrays,
_deprecate_positional, cross_product, Summary)
from ..grf import CausalForest, MultiOutputGRF
from .._cate_estimator import LinearCateEstimator
from .._shap import _shap_explain_multitask_model_cate
Expand Down Expand Up @@ -643,6 +644,8 @@ def tune(self, Y, T, *, X=None, W=None,
The tuned causal forest object. This is the same object (not a copy) as the original one, but where
all parameters of the object have been set to the best performing parameters from the tuning grid.
"""
Y, T, X, W, sample_weight, groups = check_input_arrays(Y, T, X, W, sample_weight, groups)

if params == 'auto':
params = {'max_samples': [.3, .5],
'min_balancedness_tol': [.3, .5],
Expand Down Expand Up @@ -737,7 +740,6 @@ def fit(self, Y, T, X=None, W=None, *, sample_weight=None, groups=None,
"""
if X is None:
raise ValueError("This estimator does not support X=None!")
Y, T, X, W = check_inputs(Y, T, X, W=W, multi_output_T=True, multi_output_Y=True)
return super().fit(Y, T, X=X, W=W,
sample_weight=sample_weight, groups=groups,
cache_values=cache_values,
Expand Down
21 changes: 21 additions & 0 deletions econml/inference/_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,19 @@ def _expand_outputs(self, n_rows):
"""
pass

def translate(self, offset):
"""
Update the results in place by translating by an offset.

Parameters
----------
offset: array-like
The offset by which to translate these results
"""
# NOTE: use np.asarray(offset) becuase if offset is a pd.Series direct addition would make the sum
# a Series as well, which would subsequently break summary_frame because flatten isn't supported
self.pred = self.pred + np.asarray(offset)


class NormalInferenceResults(InferenceResults):
"""
Expand Down Expand Up @@ -1081,6 +1094,14 @@ def _expand_outputs(self, n_rows):
return EmpiricalInferenceResults(self.d_t, self.d_y, pred, pred_dist, self.inf_type, self.fname_transformer,
self.feature_names, self.output_names, self.treatment_names)

def translate(self, other):
# offset preds
super().translate(other)
# offset the distribution, too
self.pred_dist = self.pred_dist + np.asarray(other)

translate.__doc__ = InferenceResults.translate.__doc__


class PopulationSummaryResults:
"""
Expand Down
6 changes: 6 additions & 0 deletions econml/solutions/causal_analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from ._causal_analysis import CausalAnalysis

__all__ = ["CausalAnalysis"]
Loading