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.

Bayesian Logistic Regression With Latent Gaussian Sampler

In this notebook we reproduce the Logistic Regression example, but by directly leveraging the fact that the prior is Gaussian to use the latent Gaussian model. Most of the code is the same as in the previous notebook, but the sampler (and the adaptation step) will differ.

Notebook Cell

The data

We create two clusters of points using scikit-learn’s make_bicluster function.

Source
<Figure size 1200x800 with 1 Axes>

The model

We use a simple logistic regression model to infer to which cluster each of the points belongs. We note yy a binary variable that indicates whether a point belongs to the first cluster :

y∼Bernoulli⁡(p)y \sim \operatorname{Bernoulli}(p)

The probability pp to belong to the first cluster commes from a logistic regression:

p=logistic⁡(Φ w)p = \operatorname{logistic}(\Phi\,\boldsymbol{w})

where ww is a vector of weights whose priors are a normal prior centered on 0:

w∼Normal⁡(0,σ)\boldsymbol{w} \sim \operatorname{Normal}(0, \sigma)

And Φ\Phi is the matrix that contains the data, so each row Φi,:\Phi_{i,:} is the vector [1,X0i,X1i]\left[1, X_0^i, X_1^i\right]

Posterior sampling

We use blackjax’s Latent Gaussian sampler to sample from the posterior distribution.

We first define a calibration loop. The goal is to find the “step-size” delta that approximately corresponds to an acceptance probability of 0.5.

We can now run the inference:

Percentage of accepted samples (after calibration): 62.70%

And display the trace:

Source
<Figure size 1200x200 with 3 Axes>

Predictive distribution

Having infered the posterior distribution of the regression’s coefficients we can compute the probability to belong to the first cluster at each position (X0,X1)(X_0, X_1).

Source
<Figure size 1200x800 with 1 Axes>

We essentially recover the same contours as with the standard random walk approach.