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

Meta learners #170

Draft
wants to merge 62 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
2c0d551
implemented frequentist S, T and X learners
Feb 24, 2023
ab999b6
Reformatted. Added bootstrapping. Added DRLearner.
Feb 26, 2023
9791b9b
Fixed doc-string for DRLearner
Feb 26, 2023
100f8d7
renamed meta_learners.py to skl_meta_learners.py
Feb 26, 2023
5874281
imported skl_meta_learners
Feb 26, 2023
df90a52
minor code style fixes
Feb 27, 2023
b8a3dff
mostly stylistic changes
Feb 27, 2023
020a65f
fixed an import
Feb 27, 2023
667d3b4
bootstraping does not overwrite self.models anymore
matekadlicsko Feb 28, 2023
d05c156
fixed a citation in docstring
matekadlicsko Mar 1, 2023
542e129
added _fit function to reduce boilerplate code
matekadlicsko Mar 1, 2023
5f8a62f
refactored
matekadlicsko Mar 1, 2023
759b9e2
added BARTModel
matekadlicsko Mar 1, 2023
8c03319
outlined pymc meta-learners
matekadlicsko Mar 1, 2023
18baff5
minor changes helping pymc integration
matekadlicsko Mar 2, 2023
f9d9817
minor changes
matekadlicsko Mar 2, 2023
9917a83
continuing to integrate pymc models
matekadlicsko Mar 2, 2023
a8d6467
bugfix
matekadlicsko Mar 2, 2023
55b43df
more minor bugfixes
matekadlicsko Mar 2, 2023
9d5bb61
added logistic regression
matekadlicsko Mar 2, 2023
3f77e76
added bayesian DRLearner
matekadlicsko Mar 4, 2023
faf0db5
fixed some issues with X and DR learners
matekadlicsko Mar 5, 2023
c1bbf33
small bugfixes
matekadlicsko Mar 6, 2023
2f689dd
added (incomplete) notebook explaining meta-learners
matekadlicsko Mar 6, 2023
b57e31a
wrote section on X-learner
matekadlicsko Mar 7, 2023
483d55b
fixed major error in DRLearner implementation
matekadlicsko Mar 7, 2023
d62eb18
minor changes
matekadlicsko Mar 8, 2023
95e010e
implemented cross_fitting option for DR-learner
matekadlicsko Mar 9, 2023
3e1182d
wrote subsection on DR-learner
matekadlicsko Mar 9, 2023
806cd0f
added docstring + some small changes suggested by @juanitorduz
matekadlicsko Mar 10, 2023
21d0b15
fixed a dependency
matekadlicsko Mar 12, 2023
c4f124b
improvements on LogisticRegression
matekadlicsko Mar 12, 2023
90fddd7
several improvements
matekadlicsko Mar 12, 2023
917216c
BayesianDR now works
matekadlicsko Mar 15, 2023
bb588b9
BayesianXLearner now works
matekadlicsko Mar 15, 2023
f39b856
removed redundant _compute_cate function
matekadlicsko Mar 15, 2023
2ca0ebd
formatting
matekadlicsko Mar 15, 2023
48c8105
added score method
matekadlicsko Mar 16, 2023
ddaebb4
formatting
matekadlicsko Mar 16, 2023
3bb16fe
reworded introduction + included some suggestions by @juanitorduz
matekadlicsko Mar 16, 2023
0d98c53
minor changes
matekadlicsko Mar 16, 2023
02b78e1
formatting
matekadlicsko Mar 17, 2023
3e845bf
added correct docstring
matekadlicsko Mar 17, 2023
d4830cc
added aesera to list of dependencies
matekadlicsko Mar 22, 2023
02d592c
improved docstrings.
matekadlicsko Mar 27, 2023
2007685
XLearner computations were wrong
matekadlicsko Mar 27, 2023
a936306
added summary file
matekadlicsko Mar 29, 2023
e682b27
summary now returns a summary object
matekadlicsko Mar 29, 2023
4751aeb
minor fix
matekadlicsko Mar 29, 2023
aba9255
new summary objects are displayed
matekadlicsko Mar 29, 2023
5fe6c53
changed plot method
matekadlicsko Apr 2, 2023
8fd71ec
Added some docstrings
matekadlicsko Apr 2, 2023
14fac30
fixed pymc-bart import
matekadlicsko Apr 9, 2023
1cbe477
summary now performs bootstrapping only once
matekadlicsko Apr 9, 2023
46a33d2
added summary
matekadlicsko Apr 9, 2023
d88472c
imported summary
matekadlicsko Apr 9, 2023
c154979
Merge branch 'pymc-labs:main' into meta-learners
matekadlicsko Apr 13, 2023
18b6934
made notebook a bit more clear
matekadlicsko Apr 17, 2023
1beda78
Merge branch 'meta-learners' of https://github.com/matekadlicsko/Caus…
matekadlicsko Apr 17, 2023
b43752e
Merge branch 'pymc-labs:main' into meta-learners
matekadlicsko Apr 20, 2023
92b655d
Merge branch 'pymc-labs:main' into meta-learners
matekadlicsko May 10, 2023
9d26c40
Merge branch 'pymc-labs:main' into meta-learners
matekadlicsko Jun 8, 2023
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
Prev Previous commit
Next Next commit
added summary
  • Loading branch information
matekadlicsko committed Apr 9, 2023
commit 46a33d2ab937437d9bac27199415f65de5cd2ac3
66 changes: 60 additions & 6 deletions causalpy/pymc_meta_learners.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TLearner,
XLearner,
)
from causalpy.summary import Summary
from causalpy.utils import _fit


Expand Down Expand Up @@ -43,6 +44,54 @@ def fit(
"""
raise NotImplementedError()

def ate_hdi(self, X) -> np.array:
"""
Estimates high density interval of average treatement effect on X.

Parameters
----------
X : pandas.DataFrame of shape (n_samples, n_featues).
Feature matrix.
"""
cate = self.predict_cate(X)
hdi = az.hdi(cate.mean(dim="obs_ind")).mu.values
return hdi

def cate_hdi(self, X) -> np.array:
"""
Estimates high density interval of conditional average treatement effect on X.

Parameters
----------
X : pandas.DataFrame of shape (n_samples, n_featues).
Feature matrix.
"""
cate = self.predict_cate(X)
hdi = az.hdi(cate).mu.values
return hdi

def summary(self) -> Summary:
"Returns summary."
hdi = self.ate_hdi(self.X)
ate = self.ate()

s = Summary()

s.add_title(["Conditional Average Treatment Effect Estimator Summary"])
s.add_row("Number of observations", [self.index.shape[0]], 2)
s.add_row("Number of treated observations", [self.treated.sum()], 2)
s.add_row("Average treatement effect (ATE)", [ate], 2)
s.add_row("HDI for ATE", [tuple(hdi)], 1)
# Can bias be estimated in this setting?
# s.add_row("Estimated bias", [bias], 2)
s.add_title(["Base learners"])
s.add_header(["", "Model", "R^2"], 1)

for name, model in self.models.items():
s.add_row(name, model, 1)

return s

def predict_cate(self, X: pd.DataFrame) -> DataArray:
"""
Predicts distribution of treatement effect.
Expand Down Expand Up @@ -211,7 +260,7 @@ def fit(
).mean(axis=1)

tau_t = y_t - pred_u_t
tau_u = - y_u + pred_t_u
tau_u = -y_u + pred_t_u

# Estimate CATE separately on treated and untreated subsets
_fit(treated_cate_estimator, X_t, tau_t, coords)
Expand Down Expand Up @@ -360,14 +409,19 @@ def fit(
return self

def predict_cate(self, X: pd.DataFrame) -> DataArray:
pred = self.models["pseudo_outcome"].predict(X)["posterior_predictive"].mu
pred = az.extract(
self.models["pseudo_outcome"].predict(X),
group="posterior_predictive",
var_names="mu",
)

if self.cross_fitting:
pred2 = (
self.cross_fitted_models["pseudo_outcome"]
.predict(X)["posterior_predictive"]
.mu
pred2 = az.extract(
self.cross_fitted_models["pseudo_outcome"].predict(X),
group="posterior_predictive",
var_names="mu",
)

pred = (pred + pred2) / 2

return pred