forked from pymc-labs/CausalPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpymc_models.py
485 lines (411 loc) · 16.8 KB
/
pymc_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# Copyright 2024 The PyMC Labs Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Defines generic PyMC ModelBuilder class and subclasses for
- WeightedSumFitter model for Synthetic Control experiments
- LinearRegression model
Models are intended to be used from inside an experiment
class (see :doc:`PyMC experiments</api_pymc_experiments>`).
This is why the examples require some extra
manipulation input data, often to ensure `y` has the correct shape.
"""
from typing import Any, Dict, Optional
import arviz as az
import numpy as np
import pandas as pd
import pymc as pm
import pytensor.tensor as pt
from arviz import r2_score
class ModelBuilder(pm.Model):
"""
This is a wrapper around pm.Model to give scikit-learn like API.
Public Methods
---------------
- build_model: must be implemented by subclasses
- fit: populates idata attribute
- predict: returns predictions on new data
- score: returns Bayesian :math:`R^2`
Example
-------
>>> import causalpy as cp
>>> import numpy as np
>>> import pymc as pm
>>> from causalpy.pymc_models import ModelBuilder
>>> class MyToyModel(ModelBuilder):
... def build_model(self, X, y, coords):
... with self:
... X_ = pm.Data(name="X", value=X)
... y_ = pm.Data(name="y", value=y)
... beta = pm.Normal("beta", mu=0, sigma=1, shape=X_.shape[1])
... sigma = pm.HalfNormal("sigma", sigma=1)
... mu = pm.Deterministic("mu", pm.math.dot(X_, beta))
... pm.Normal("y_hat", mu=mu, sigma=sigma, observed=y_)
>>> rng = np.random.default_rng(seed=42)
>>> X = rng.normal(loc=0, scale=1, size=(20, 2))
>>> y = rng.normal(loc=0, scale=1, size=(20,))
>>> model = MyToyModel(
... sample_kwargs={
... "chains": 2,
... "draws": 2000,
... "progressbar": False,
... "random_seed": rng,
... }
... )
>>> model.fit(X, y)
Inference data...
>>> X_new = rng.normal(loc=0, scale=1, size=(20,2))
>>> model.predict(X_new)
Inference data...
>>> model.score(X, y)
r2 0.390344
r2_std 0.081135
dtype: float64
"""
def __init__(self, sample_kwargs: Optional[Dict[str, Any]] = None):
"""
:param sample_kwargs: A dictionary of kwargs that get unpacked and passed to the
:func:`pymc.sample` function. Defaults to an empty dictionary.
"""
super().__init__()
self.idata = None
self.sample_kwargs = sample_kwargs if sample_kwargs is not None else {}
def build_model(self, X, y, coords) -> None:
"""Build the model, must be implemented by subclass."""
raise NotImplementedError("This method must be implemented by a subclass")
def _data_setter(self, X) -> None:
"""
Set data for the model.
This method is used internally to register new data for the model for
prediction.
"""
with self:
pm.set_data({"X": X})
def fit(self, X, y, coords: Optional[Dict[str, Any]] = None) -> None:
"""Draw samples from posterior, prior predictive, and posterior predictive
distributions, placing them in the model's idata attribute.
"""
# Ensure random_seed is used in sample_prior_predictive() and
# sample_posterior_predictive() if provided in sample_kwargs.
random_seed = self.sample_kwargs.get("random_seed", None)
self.build_model(X, y, coords)
with self:
self.idata = pm.sample(**self.sample_kwargs)
self.idata.extend(pm.sample_prior_predictive(random_seed=random_seed))
self.idata.extend(
pm.sample_posterior_predictive(
self.idata, progressbar=False, random_seed=random_seed
)
)
return self.idata
def predict(self, X):
"""
Predict data given input data `X`
.. caution::
Results in KeyError if model hasn't been fit.
"""
# Ensure random_seed is used in sample_prior_predictive() and
# sample_posterior_predictive() if provided in sample_kwargs.
random_seed = self.sample_kwargs.get("random_seed", None)
self._data_setter(X)
with self: # sample with new input data
post_pred = pm.sample_posterior_predictive(
self.idata,
var_names=["y_hat", "mu"],
progressbar=False,
random_seed=random_seed,
)
return post_pred
def score(self, X, y) -> pd.Series:
"""Score the Bayesian :math:`R^2` given inputs ``X`` and outputs ``y``.
.. caution::
The Bayesian :math:`R^2` is not the same as the traditional coefficient of
determination, https://en.wikipedia.org/wiki/Coefficient_of_determination.
"""
yhat = self.predict(X)
yhat = az.extract(
yhat, group="posterior_predictive", var_names="y_hat"
).T.values
# Note: First argument must be a 1D array
return r2_score(y.flatten(), yhat)
# .stack(sample=("chain", "draw")
class WeightedSumFitter(ModelBuilder):
"""
Used for synthetic control experiments
.. note::
Generally, the `.fit()` method should be used rather than
calling `.build_model()` directly.
Defines the PyMC model:
.. math::
\sigma &\sim \mathrm{HalfNormal}(1)
\\beta &\sim \mathrm{Dirichlet}(1,...,1)
\mu &= X * \\beta
y &\sim \mathrm{Normal}(\mu, \sigma)
Example
--------
>>> import causalpy as cp
>>> import numpy as np
>>> from causalpy.pymc_models import WeightedSumFitter
>>> sc = cp.load_data("sc")
>>> X = sc[['a', 'b', 'c', 'd', 'e', 'f', 'g']]
>>> y = np.asarray(sc['actual']).reshape((sc.shape[0], 1))
>>> wsf = WeightedSumFitter(sample_kwargs={"progressbar": False})
>>> wsf.fit(X,y)
Inference data...
""" # noqa: W605
def build_model(self, X, y, coords):
"""
Defines the PyMC model
"""
with self:
self.add_coords(coords)
n_predictors = X.shape[1]
X = pm.Data("X", X, dims=["obs_ind", "coeffs"])
y = pm.Data("y", y[:, 0], dims="obs_ind")
# TODO: There we should allow user-specified priors here
beta = pm.Dirichlet("beta", a=np.ones(n_predictors), dims="coeffs")
# beta = pm.Dirichlet(
# name="beta", a=(1 / n_predictors) * np.ones(n_predictors),
# dims="coeffs"
# )
sigma = pm.HalfNormal("sigma", 1)
mu = pm.Deterministic("mu", pm.math.dot(X, beta), dims="obs_ind")
pm.Normal("y_hat", mu, sigma, observed=y, dims="obs_ind")
class LinearRegression(ModelBuilder):
"""
Custom PyMC model for linear regression
.. note:
Generally, the `.fit()` method should be used rather than
calling `.build_model()` directly.
Defines the PyMC model
.. math::
\\beta &\sim \mathrm{Normal}(0, 50)
\sigma &\sim \mathrm{HalfNormal}(1)
\mu &= X * \\beta
y &\sim \mathrm{Normal}(\mu, \sigma)
Example
--------
>>> import causalpy as cp
>>> import numpy as np
>>> from causalpy.pymc_models import LinearRegression
>>> rd = cp.load_data("rd")
>>> X = rd[["x", "treated"]]
>>> y = np.asarray(rd["y"]).reshape((rd["y"].shape[0],1))
>>> lr = LinearRegression(sample_kwargs={"progressbar": False})
>>> lr.fit(X, y, coords={
... 'coeffs': ['x', 'treated'],
... 'obs_indx': np.arange(rd.shape[0])
... },
... )
Inference data...
""" # noqa: W605
def build_model(self, X, y, coords):
"""
Defines the PyMC model
"""
with self:
self.add_coords(coords)
X = pm.Data("X", X, dims=["obs_ind", "coeffs"])
y = pm.Data("y", y[:, 0], dims="obs_ind")
beta = pm.Normal("beta", 0, 50, dims="coeffs")
sigma = pm.HalfNormal("sigma", 1)
mu = pm.Deterministic("mu", pm.math.dot(X, beta), dims="obs_ind")
pm.Normal("y_hat", mu, sigma, observed=y, dims="obs_ind")
class InstrumentalVariableRegression(ModelBuilder):
"""Custom PyMC model for instrumental linear regression
Example
--------
>>> import causalpy as cp
>>> import numpy as np
>>> from causalpy.pymc_models import InstrumentalVariableRegression
>>> N = 10
>>> e1 = np.random.normal(0, 3, N)
>>> e2 = np.random.normal(0, 1, N)
>>> Z = np.random.uniform(0, 1, N)
>>> ## Ensure the endogeneity of the the treatment variable
>>> X = -1 + 4 * Z + e2 + 2 * e1
>>> y = 2 + 3 * X + 3 * e1
>>> t = X.reshape(10,1)
>>> y = y.reshape(10,1)
>>> Z = np.asarray([[1, Z[i]] for i in range(0,10)])
>>> X = np.asarray([[1, X[i]] for i in range(0,10)])
>>> COORDS = {'instruments': ['Intercept', 'Z'], 'covariates': ['Intercept', 'X']}
>>> sample_kwargs = {
... "tune": 5,
... "draws": 10,
... "chains": 2,
... "cores": 2,
... "target_accept": 0.95,
... "progressbar": False,
... }
>>> iv_reg = InstrumentalVariableRegression(sample_kwargs=sample_kwargs)
>>> iv_reg.fit(X, Z,y, t, COORDS, {
... "mus": [[-2,4], [0.5, 3]],
... "sigmas": [1, 1],
... "eta": 2,
... "lkj_sd": 1,
... }, None)
Inference data...
"""
def build_model(self, X, Z, y, t, coords, priors):
"""Specify model with treatment regression and focal regression data and priors
:param X: A pandas dataframe used to predict our outcome y
:param Z: A pandas dataframe used to predict our treatment variable t
:param y: An array of values representing our focal outcome y
:param t: An array of values representing the treatment t of
which we're interested in estimating the causal impact
:param coords: A dictionary with the coordinate names for our
instruments and covariates
:param priors: An optional dictionary of priors for the mus and
sigmas of both regressions
:code:`priors = {"mus": [0, 0], "sigmas": [1, 1],
"eta": 2, "lkj_sd": 2}`
"""
# --- Priors ---
with self:
self.add_coords(coords)
beta_t = pm.Normal(
name="beta_t",
mu=priors["mus"][0],
sigma=priors["sigmas"][0],
dims="instruments",
)
beta_z = pm.Normal(
name="beta_z",
mu=priors["mus"][1],
sigma=priors["sigmas"][1],
dims="covariates",
)
sd_dist = pm.Exponential.dist(priors["lkj_sd"], shape=2)
chol, corr, sigmas = pm.LKJCholeskyCov(
name="chol_cov",
eta=priors["eta"],
n=2,
sd_dist=sd_dist,
)
# compute and store the covariance matrix
pm.Deterministic(name="cov", var=pt.dot(l=chol, r=chol.T))
# --- Parameterization ---
mu_y = pm.Deterministic(name="mu_y", var=pm.math.dot(X, beta_z))
# focal regression
mu_t = pm.Deterministic(name="mu_t", var=pm.math.dot(Z, beta_t))
# instrumental regression
mu = pm.Deterministic(name="mu", var=pt.stack(tensors=(mu_y, mu_t), axis=1))
# --- Likelihood ---
pm.MvNormal(
name="likelihood",
mu=mu,
chol=chol,
observed=np.stack(arrays=(y.flatten(), t.flatten()), axis=1),
shape=(X.shape[0], 2),
)
def sample_predictive_distribution(self, ppc_sampler="jax"):
"""Function to sample the Multivariate Normal posterior predictive
Likelihood term in the IV class. This can be slow without
using the JAX sampler compilation method. If using the
JAX sampler it will sample only the posterior predictive distribution.
If using the PYMC sampler if will sample both the prior
and posterior predictive distributions."""
random_seed = self.sample_kwargs.get("random_seed", None)
if ppc_sampler == "jax":
with self:
self.idata.extend(
pm.sample_posterior_predictive(
self.idata,
random_seed=random_seed,
compile_kwargs={"mode": "JAX"},
)
)
elif ppc_sampler == "pymc":
with self:
self.idata.extend(pm.sample_prior_predictive(random_seed=random_seed))
self.idata.extend(
pm.sample_posterior_predictive(
self.idata,
random_seed=random_seed,
)
)
def fit(self, X, Z, y, t, coords, priors, ppc_sampler=None):
"""Draw samples from posterior distribution and potentially
from the prior and posterior predictive distributions. The
fit call can take values for the
ppc_sampler = ['jax', 'pymc', None]
We default to None, so the user can determine if they wish
to spend time sampling the posterior predictive distribution
independently.
"""
# Ensure random_seed is used in sample_prior_predictive() and
# sample_posterior_predictive() if provided in sample_kwargs.
# Use JAX for ppc sampling of multivariate likelihood
self.build_model(X, Z, y, t, coords, priors)
with self:
self.idata = pm.sample(**self.sample_kwargs)
self.sample_predictive_distribution(ppc_sampler=ppc_sampler)
return self.idata
class PropensityScore(ModelBuilder):
"""
Custom PyMC model for inverse propensity score models
.. note:
Generally, the `.fit()` method should be used rather than
calling `.build_model()` directly.
Defines the PyMC model
.. math::
\\beta &\sim \mathrm{Normal}(0, 1)
\sigma &\sim \mathrm{HalfNormal}(1)
\mu &= X * \\beta
p &= logit^{-1}(mu)
t &\sim \mathrm{Bernoulli}(p)
Example
--------
>>> import causalpy as cp
>>> import numpy as np
>>> from causalpy.pymc_models import PropensityScore
>>> df = cp.load_data('nhefs')
>>> X = df[["age", "race"]]
>>> t = np.asarray(df["trt"])
>>> ps = PropensityScore(sample_kwargs={"progressbar": False})
>>> ps.fit(X, t, coords={
... 'coeffs': ['age', 'race'],
... 'obs_indx': np.arange(df.shape[0])
... },
... )
Inference...
""" # noqa: W605
def build_model(self, X, t, coords):
"Defines the PyMC propensity model"
with self:
self.add_coords(coords)
X_data = pm.MutableData("X", X, dims=["obs_ind", "coeffs"])
t_data = pm.MutableData("t", t.flatten(), dims="obs_ind")
b = pm.Normal("b", mu=0, sigma=1, dims="coeffs")
mu = pm.math.dot(X_data, b)
p = pm.Deterministic("p", pm.math.invlogit(mu))
pm.Bernoulli("t_pred", p=p, observed=t_data, dims="obs_ind")
def fit(self, X, t, coords):
"""Draw samples from posterior, prior predictive, and posterior predictive
distributions. We overwrite the base method because the base method assumes
a variable y and we use t to indicate the treatment variable here.
"""
# Ensure random_seed is used in sample_prior_predictive() and
# sample_posterior_predictive() if provided in sample_kwargs.
random_seed = self.sample_kwargs.get("random_seed", None)
self.build_model(X, t, coords)
with self:
self.idata = pm.sample(**self.sample_kwargs)
self.idata.extend(pm.sample_prior_predictive(random_seed=random_seed))
self.idata.extend(
pm.sample_posterior_predictive(
self.idata, progressbar=False, random_seed=random_seed
)
)
return self.idata