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

In this notebook we demonstrate the use of the random walk Rosenbluth-Metropolis-Hasting algorithm on a simple logistic regression.

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 Random Walk RMH kernel to sample from the posterior distribution.

Since blackjax does not provide an inference loop we need to implement one ourselves:

We can now run the inference:

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>