(lecture_19)=
:::{post} Jan 7, 2024 :tags: statistical rethinking, bayesian inference, generalized linear models :category: intermediate :author: Dustin Stansbury :::
This notebook is part of the PyMC port of the Statistical Rethinking 2023 lecture series by Richard McElreath.
Video - Lecture 19 - Generalized Linear Madness# Lecture 19 - Generalized Linear Madness
Alternative Approach - Start with simple sceintific principles, and build in estimation
Now that parameters $p,k$ have real-world interpretations, determining priors for them is now more straight-forward than for a typical GLM/GLMM
Prior Development Workflow
1. Measuremnt Scales
$\mu$ - $kg$
$H$ - $cm^3$
$k$ - $kg/cm^3$
You can always rescale a measurement scale using a reference
Try dividing out measuremnt units when possible
Often easier to deal with dimensionless quantities
2. Simulate
proportion, $p$
density, $k$
3. Think
Bayesian inference has discovered that $k$ and $p$ are functionally related
You can derive the same relationship using algebra, and the notion that the average human has a hieght of 1
It turns out that we dont' even need the parameters $k, p$. If we can take the product all parameters in the model to give a single parameter $\theta=kp^2$, we can solve the above equation using the same average person trick
Thus the parameter $\theta = k p^2$ is just a constant, $1/\pi$, so there are no parameters to estimate 🤯
We can see that we're able to recover the simulated probability of the majority strategy, which was 0.5.
PyMC has a built-in ODE solver, but the docs for that solver warn that it's pretty slow, and recommends that we use Sunode ODE Solver. AFAICT, at this point in time, the sunode solver isn't available for Mac M1 (i.e. it's not available on conda-forge, and installing from source is a bit of a mess on ARM64 chipset).
Indeed, I was finding the built-in ODE solver in PyMC (pm.ode.DifferentialEquation) prohibitive for building and debugging models (it was taking over an hour per model to sample; quite difficult to iterate at that pace). As an alternative approach, I'm using the implementation outlined in Lotke-Volterra examples in the PyMC docs. The approach is to wrap scipy's odeint function using Pytensor's as_op decorator so that variable input/output types are available via PyMC. This allows us to to use non-gradient based samplers (e.g. Slice sampler), which are a lot more forgiving to exotic models like ones that embed ODE integration.
⚠️ These models are super-sensitive to starting conditions, and I wasn't able to get the models to converge using the priors suggested in lecture. Instead, we perform an initial least-squares fit to the data, and inform our priors using the resulting least squares parameter estimates.
Note that we plot the posterior over a finer grid than the data itself. This can be done using the posterior params, and running the ODE at a higher resolution than the data
:::{include} ../page_footer.md :::