Path: blob/master/deprecated/notebooks/linreg_height_weight_numpyro.ipynb
1192 views
Kernel: Python 3
Linear regression for predicting height from weight
We illustrate priors for linear and polynomial regression using the example in sec 4.4 of Statistical Rethinking ed 2. The numpyro code is from Du Phan's site.
In [1]:
Out[1]:
Building wheel for numpyro (setup.py) ... done
|████████████████████████████████| 1.6MB 11.7MB/s
|████████████████████████████████| 768kB 79.1MB/s
|████████████████████████████████| 4.7MB 68.7MB/s
|████████████████████████████████| 317kB 81.7MB/s
In [2]:
Out[2]:
WARNING:absl:No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
WARNING:absl:No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
jax version 0.2.12
jax backend cpu
Data
We use the "Howell" dataset, which consists of measurements of height, weight, age and sex, of a certain foraging tribe, collected by Nancy Howell.
In [3]:
Out[3]:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 544 entries, 0 to 543
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 height 544 non-null float64
1 weight 544 non-null float64
2 age 544 non-null float64
3 male 544 non-null int64
dtypes: float64(3), int64(1)
memory usage: 17.1 KB
In [4]:
Out[4]:
Prior predictive distribution
In [25]:
Gaussian prior
In [26]:
Out[26]:
Log-Gaussian prior
In [27]:
Out[27]:
Posterior
We use the log-gaussian prior. We compute a Laplace approximation to the posterior.
In [7]:
Out[7]:
100%|██████████| 2000/2000 [00:01<00:00, 1165.38it/s, init loss: 40631.5430, avg. loss [1901-2000]: 1078.9297]
In [8]:
Out[8]:
{'a': [154.36615, 154.78511, 154.73534, 154.53842, 154.53549],
'b': [0.974645, 0.8900048, 0.81902206, 0.8334104, 1.011918],
'mu': [157.12938, 146.0771, 141.5733, 162.21344, 150.74669],
'sigma': [4.9764595, 4.94353, 5.2826037, 4.877722, 4.89487]}
Posterior predictive
In [9]:
Out[9]:
(1000, 46)
Polynomial regression
We will now consider the full dataset, including children. The resulting mapping from weight to height is now nonlinear.
Data
In [10]:
Out[10]:
In [19]:
Out[19]:
Fit model
In [28]:
Linear
In [29]:
Out[29]:
100%|██████████| 3000/3000 [00:02<00:00, 1344.90it/s, init loss: 49746.6445, avg. loss [2851-3000]: 2001.7004]
mean std median 2.5% 97.5% n_eff r_hat
a 138.31 0.41 138.32 137.55 139.15 931.50 1.00
b1 25.95 0.41 25.94 25.19 26.75 1101.83 1.00
sigma 9.36 0.29 9.36 8.84 9.99 949.32 1.00
Quadratic
In [30]:
Out[30]:
100%|██████████| 3000/3000 [00:02<00:00, 1302.20it/s, init loss: 68267.6406, avg. loss [2851-3000]: 1770.2694]
mean std median 2.5% 97.5% n_eff r_hat
a 146.05 0.36 146.03 145.33 146.71 1049.96 1.00
b1 21.75 0.30 21.75 21.18 22.32 886.88 1.00
b2 -7.79 0.28 -7.79 -8.33 -7.26 1083.62 1.00
sigma 5.78 0.17 5.78 5.46 6.14 973.22 1.00
In [31]:
Out[31]:
100%|██████████| 3000/3000 [00:02<00:00, 1198.08it/s, init loss: 66975.6406, avg. loss [2851-3000]: 2008.9690]
mean std median 2.5% 97.5% n_eff r_hat
a 138.21 0.40 138.19 137.42 138.93 1049.96 1.00
b1 26.00 0.41 26.00 25.22 26.81 824.23 1.00
b2 0.08 0.04 0.07 0.02 0.16 935.60 1.00
sigma 9.40 0.28 9.40 8.83 9.91 947.68 1.00