Hierarchical regression¶
The following notes are based on [GH06].
Setup¶
Install libraries¶
[1]:
# %run -i 'plotting.py'
[2]:
# !apt-get install -y fonts-lmodern
# !pip install -q arviz numpyro
Add latin modern fonts¶
[3]:
import matplotlib.pyplot as plt
import matplotlib.font_manager
[4]:
## fonts_path = "/usr/share/texmf/fonts/opentype/public/lm/" #ubuntu
## fonts_path = "~/Library/Fonts/" # macos
fonts_path = "/usr/share/fonts/OTF/" # arch
matplotlib.font_manager.fontManager.addfont(fonts_path + "lmsans10-regular.otf")
matplotlib.font_manager.fontManager.addfont(fonts_path + "lmroman10-regular.otf")
Set matplotlib to use latin modern fonts¶
[5]:
from IPython.display import set_matplotlib_formats
##%matplotlib inline
set_matplotlib_formats('svg') # use SVG backend to maintain vectorization
plt.style.use('default') #reset default parameters
## https://stackoverflow.com/a/3900167/446907
plt.rcParams.update({'font.size': 16,
'font.family': ['sans-serif'],
'font.serif': ['Latin Modern Roman'] + plt.rcParams['font.serif'],
'font.sans-serif': ['Latin Modern Sans'] + plt.rcParams['font.sans-serif']})
<ipython-input-5-5f8582c2894e>:3: DeprecationWarning: `set_matplotlib_formats` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.set_matplotlib_formats()`
set_matplotlib_formats('svg') # use SVG backend to maintain vectorization
Generative models¶
Example generative models¶
Univariate normal model¶
From a very simple perspective, generative modeling refers to the situation in which we develop a candidate probabilistic specification of the process from which our data are generated. Usually this will include the specification of prior distributions over all first-order parameters.
\begin{equation*} \begin{split} p(\mathbf{y}|\mu,\tau) &= \prod^{9}_{n=0} \mathcal{N}(y_n|\mu,\tau) \\ p(\mu) &= \mathcal{N}(\mu|0,10^{-6}) \\ p(\tau) &= \mathcal{G}(\tau|10^{-6},10^{-6}) \end{split} \end{equation*}
This comes from the library bayespy. The best description we are aware of regarding the syntax and semantics of graphical models via factor graph notation is in the tikz-bayesnet library technical report.
Multivariate normal models¶
Note that these are for illustrative purposes of the manner in which our data can share parameters and we have not yet defined priors over our parameters.
Example of linear regression¶
Setup¶
Load libraries¶
[6]:
## %pylab inline
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as stats
import seaborn as sns
import numpy as np
## plt.style.use(['seaborn-talk'])
## plt.rcParams["figure.figsize"] = (10,8)
import arviz as az
import jax
import jax.numpy as jnp
import numpyro
import numpyro.distributions as dist
from numpyro.infer import MCMC, NUTS, Predictive
print(numpyro.__version__)
print(jax.__version__)
print(az.__version__)
numpyro.set_platform("cpu")
numpyro.set_host_device_count(4)
0.6.0
0.2.13
0.11.2
define colors¶
[7]:
c_light ="#DCBCBC"
c_light_highlight ="#C79999"
c_mid ="#B97C7C"
c_mid_highlight ="#A25050"
c_dark ="#8F2727"
c_dark_highlight ="#7C0000"


