Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Change of Variable in HMC

Rat tumor problem: We have J certain kinds of rat tumor diseases. For each kind of tumor, we test NjN_{j} people/animals and among those yjy_{j} tested positive. Here we assume that yjy_{j} is distrubuted with Binom(NjN_{j}, θj\theta_{j}). Our objective is to approximate θj\theta_{j} for each type of tumor.

In particular we use following binomial hierarchical model where yjy_{j} and NjN_{j} are observed variables.

yj∼Binom(Nj,θj)θj∼Beta(a,b)p(a,b)∝(a+b)−5/2\begin{align} y_{j} &\sim \text{Binom}(N_{j}, \theta_{j}) \\ \theta_{j} &\sim \text{Beta}(a, b) \\ p(a, b) &\propto (a+b)^{-5/2} \end{align}
Notebook Cell
Notebook Cell
Source
<Figure size 1200x600 with 2 Axes>

Posterior Sampling

Now we use Blackjax’s NUTS algorithm to get posterior samples of aa, bb, and θ\theta

We take initial parameters from uniform distribution

Array(-1145.5424, dtype=float32)

Now we use blackjax’s window adaption algorithm to get NUTS kernel and initial states. Window adaption algorithm will automatically configure inverse_mass_matrix and step size

CPU times: user 9.29 s, sys: 214 ms, total: 9.5 s
Wall time: 6.93 s

Now we write inference loop for multiple chains

CPU times: user 8.7 s, sys: 86 ms, total: 8.78 s
Wall time: 6.16 s

Arviz Plots

We have all our posterior samples stored in states.position dictionary and infos store additional information like acceptance probability, divergence, etc. Now, we can use certain diagnostics to judge if our MCMC samples are converged on stationary distribution. Some of widely diagnostics are trace plots, potential scale reduction factor (R hat), divergences, etc. Arviz library provides quicker ways to anaylze these diagnostics. We can use arviz.summary() and arviz_plot_trace(), but these functions take specific format (arviz’s trace) as a input.

Notebook Cell
Loading...

r_hat is showing measure of each chain is converged to stationary distribution. r_hat should be less than or equal to 1.01, here we get r_hat far from 1.01 for each latent sample.

Source
<Figure size 1536x3238.28 with 76 Axes>

Trace plots also looks terrible and does not seems to be converged! Also, black band shows that every sample is diverged from original distribution. So what’s wrong happeing here?

Well, it’s related to support of latent variable. In HMC, the latent variable must be in an unconstrained space, but in above model theta is constrained in between 0 to 1. We can use change of variable trick to solve above problem

Change of Variable

We can sample from logits which is in unconstrained space and in joint_logdensity() we can convert logits to theta by suitable bijector (sigmoid). We calculate jacobian (first order derivaive) of bijector to tranform one probability distribution to another

Alternatively, using the bijector class in TFP directly:

except for the change of variable in joint_logdensity() function, everthing will remain same

Array(-1085.318, dtype=float32)
CPU times: user 12.9 s, sys: 177 ms, total: 13.1 s
Wall time: 9.09 s
CPU times: user 9.77 s, sys: 49.4 ms, total: 9.82 s
Wall time: 5.05 s
Loading...
Source
<Figure size 1536x3238.28 with 76 Axes>
Number of divergence: 0

We can see that r_hat is less than or equal to 1.01 for each latent variable, trace plots looks converged to stationary distribution, and only few samples are diverged.

Using a PPL

Probabilistic programming language usually provides functionality to apply change of variable easily (often done automatically). In this case for TFP, we can use its modeling API tfd.JointDistribution*.

CPU times: user 17.6 s, sys: 152 ms, total: 17.8 s
Wall time: 11.9 s
CPU times: user 11.2 s, sys: 35.8 ms, total: 11.3 s
Wall time: 5.51 s
Loading...
Source
<Figure size 1536x3238.28 with 76 Axes>