Skip to content

Commit ef1c532

Browse files
Update Jupyter style in lasso notebook (#279)
* update of jupyter style * addressed oriol's comments * run pre-commit * rerun notebook Co-authored-by: Oriol (ZBook) <[email protected]>
1 parent 2972766 commit ef1c532

File tree

2 files changed

+249
-174
lines changed

2 files changed

+249
-174
lines changed

examples/pymc3_howto/lasso_block_update.ipynb

+175-145
Large diffs are not rendered by default.

myst_nbs/pymc3_howto/lasso_block_update.myst.md

+74-29
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,119 @@ jupytext:
66
format_version: 0.13
77
jupytext_version: 1.13.7
88
kernelspec:
9-
display_name: Python PyMC (Dev)
9+
display_name: Python 3 (ipykernel)
1010
language: python
11-
name: pymc-dev-py38
11+
name: python3
1212
---
1313

14+
(lasso_block_update)=
1415
# Lasso regression with block updating
1516

16-
Sometimes, it is very useful to update a set of parameters together. For example, variables that are highly correlated are often good to update together. In PyMC 3 block updating is simple, as example will demonstrate.
17-
18-
Here we have a LASSO regression model where the two coefficients are strongly correlated. Normally, we would define the coefficient parameters as a single random variable, but here we define them separately to show how to do block updates.
19-
20-
First we generate some fake data.
17+
:::{post} Feb 10, 2022
18+
:tags: pymc3.Exponential, pymc3.Laplace, pymc3.Metropolis, pymc3.Model, pymc3.Normal, pymc3.Slice, pymc3.Uniform, regression
19+
:category: beginner
20+
:author: Chris Fonnesbeck, Raul Maldonado, Michael Osthege, Thomas Wiecki, Lorenzo Toniazzi
21+
:::
2122

2223
```{code-cell} ipython3
23-
%matplotlib inline
24+
:tags: []
2425
26+
%matplotlib inline
2527
import arviz as az
28+
import matplotlib.pyplot as plt
2629
import numpy as np
2730
import pymc as pm
2831
29-
from matplotlib import pylab
32+
print(f"Running on PyMC v{pm.__version__}")
33+
```
34+
35+
```{code-cell} ipython3
36+
RANDOM_SEED = 8927
37+
rng = np.random.default_rng(RANDOM_SEED)
38+
az.style.use("arviz-darkgrid")
39+
```
40+
41+
Sometimes, it is very useful to update a set of parameters together. For example, variables that are highly correlated are often good to update together. In PyMC block updating is simple. This will be demonstrated using the parameter `step` of {class}`pymc.sample`.
42+
43+
Here we have a [LASSO regression model](https://en.wikipedia.org/wiki/Lasso_(statistics)#Bayesian_interpretation) where the two coefficients are strongly correlated. Normally, we would define the coefficient parameters as a single random variable, but here we define them separately to show how to do block updates.
44+
45+
First we generate some fake data.
3046

31-
d = np.random.normal(size=(3, 30))
32-
d1 = d[0] + 4
33-
d2 = d[1] + 4
34-
yd = 0.2 * d1 + 0.3 * d2 + d[2]
47+
```{code-cell} ipython3
48+
x = rng.standard_normal(size=(3, 30))
49+
x1 = x[0] + 4
50+
x2 = x[1] + 4
51+
noise = x[2]
52+
y_obs = x1 * 0.2 + x2 * 0.3 + noise
3553
```
3654

3755
Then define the random variables.
3856

3957
```{code-cell} ipython3
40-
lam = 3
58+
:tags: []
59+
60+
lam = 3000
4161
4262
with pm.Model() as model:
43-
s = pm.Exponential("s", 1)
44-
tau = pm.Uniform("tau", 0, 1000)
63+
sigma = pm.Exponential("sigma", 1)
64+
tau = pm.Uniform("tau", 0, 1)
4565
b = lam * tau
46-
m1 = pm.Laplace("m1", 0, b)
47-
m2 = pm.Laplace("m2", 0, b)
66+
beta1 = pm.Laplace("beta1", 0, b)
67+
beta2 = pm.Laplace("beta2", 0, b)
4868
49-
p = d1 * m1 + d2 * m2
69+
mu = x1 * beta1 + x2 * beta2
5070
51-
y = pm.Normal("y", mu=p, sigma=s, observed=yd)
71+
y = pm.Normal("y", mu=mu, sigma=sigma, observed=y_obs)
5272
```
5373

54-
For most samplers, including Metropolis and HamiltonianMC, simply pass a list of variables to sample as a block. This works with both scalar and array parameters.
74+
For most samplers, including {class}`pymc.Metropolis` and {class}`pymc.HamiltonianMC`, simply pass a list of variables to sample as a block. This works with both scalar and array parameters.
5575

5676
```{code-cell} ipython3
5777
with model:
58-
start = pm.find_MAP()
78+
step1 = pm.Metropolis([beta1, beta2])
5979
60-
step1 = pm.Metropolis([m1, m2])
80+
step2 = pm.Slice([sigma, tau])
6181
62-
step2 = pm.Slice([s, tau])
63-
64-
idata = pm.sample(10000, [step1, step2], start=start)
82+
idata = pm.sample(draws=10000, step=[step1, step2])
6583
```
6684

85+
We conclude by plotting the sampled marginals and the joint distribution of `beta1` and `beta2`.
86+
6787
```{code-cell} ipython3
88+
:tags: []
89+
6890
az.plot_trace(idata);
6991
```
7092

7193
```{code-cell} ipython3
72-
pylab.hexbin(idata.posterior["m1"], idata.posterior["m2"], gridsize=50)
73-
pylab.axis("off");
94+
az.plot_pair(
95+
idata,
96+
var_names=["beta1", "beta2"],
97+
kind="hexbin",
98+
marginals=True,
99+
figsize=(10, 10),
100+
gridsize=50,
101+
)
74102
```
75103

104+
## Authors
105+
106+
* Authored by [Chris Fonnesbeck](https://github.com/fonnesbeck) in Dec, 2020
107+
* Updated by [Raul Maldonado](https://github.com/CloudChaoszero) in Jan, 2021
108+
* Updated by Raul Maldonado in Mar, 2021
109+
* Reexecuted by [Thomas Wiecki](https://github.com/twiecki) and [Michael Osthege](https://github.com/michaelosthege) with PyMC v4 in Jan, 2022 ([pymc-examples#264](https://github.com/pymc-devs/pymc-examples/pull/264))
110+
* Updated by [Lorenzo Toniazzi](https://github.com/ltoniazzi) in Feb, 2022 ([pymc-examples#279](https://github.com/pymc-devs/pymc-examples/pull/279))
111+
112+
+++
113+
114+
## Watermark
115+
76116
```{code-cell} ipython3
117+
:tags: []
118+
77119
%load_ext watermark
78-
%watermark -n -u -v -iv -w
120+
%watermark -n -u -v -iv -w -p aesara,aeppl,xarray
79121
```
122+
123+
:::{include} ../page_footer.md
124+
:::

0 commit comments

Comments
 (0)