Product

Thursday, 19 March 2026

A Gentle Introduction to Bayesian Regression

 

A Gentle Introduction to Bayesian Regression

A Gentle Introduction to Bayesian Regression
Image by Editor | ChatGPT

In this article, you will learn:

  • The fundamental difference between traditional regression, which uses single fixed values for its parameters, and Bayesian regression, which models them as probability distributions.
  • How this probabilistic approach allows the model to produce a full distribution of possible outcomes, thereby quantifying the uncertainty in its predictions.
  • How to implement a simple Bayesian regression model in Python with scikit-learn.

Introduction

In machine learning, regression models are a widely used family of models for making predictions on numeric, continuous variables, such as house prices, temperatures, or stock values. Traditional regression models are defined by precise parameters or weights and typically assume no uncertainty in these estimates. Bayesian regression was introduced to model the relationship between predictor and target variables probabilistically, introducing uncertainty into the modeling process.

Traditional Regression Models

Traditional regression models produce predictions on target continuous variables, for instance, house prices, in the form of an estimate, ˆ𝑦, that is a single, crisp value, e.g., the estimated price of a house whose attributes have been passed to the model as inputs. For instance, the equation of a linear regression model with n input variables (e.g. house attributes) is given by the following linear equation:

ˆ𝑦 =𝛽0 +𝛽1⁢𝑥1 +𝛽2⁢𝑥2 +… +𝛽𝑛⁢𝑥𝑛 +𝜖

where 𝜖 is the error term, and 𝛽0,…,𝛽𝑛 are the weights associated with the input variables, plus a bias term, 𝛽0. A real-world instantiation of this equation to a specific learned model to predict house prices could look like this:

ˆ𝑦 =50000 +150 ⋅𝑥1 +10000 ⋅𝑥2–2000 ⋅𝑥3 +𝜖

where:

  • 𝑥1 is the house’s square footage in square meters.
  • 𝑥2 is the number of bedrooms.
  • 𝑥3 is the age of the house in years — notice the minus sign for the weight accompanying this feature, which means older houses contribute to lower price predictions. This makes sense, as older houses tend to be cheaper.

The 50000, 150, 10000, and -2000 values in the above model are its parameter values or weights. This is an important point to remember as we continue.

How Does Bayesian Regression Work?

So, what makes Bayesian regression different from this? Unlike traditional regression, which returns a single value as the predicted estimate and similarly has weights learned as precise values, a Bayesian regression model deals with probability distributions over the possible parameter values, thereby accounting for uncertainty in predictions. Put another way, in a Bayesian regression model, each weight 𝛽𝑖 becomes a random variable with its associated probability distribution.

The learning process is adapted accordingly: instead of seeking a single “best-fitting” set of n+1 weights, 𝛽0,𝛽1,𝛽2,…,𝛽𝑛, we try to find a posterior distribution over parameters, given the training data (a set of instances with a known output or target value).

Importantly, since weights are modeled as distributions, the resulting predictions, ˆ𝑦, also become distributions rather than crisp, single-point estimates. Therefore, the prediction becomes uncertain.

Why would we be interested in predictions that are uncertain rather than precise?

Bayesian regression makes sense in scenarios where understanding and accounting for uncertainty is as important as making the prediction itself. For instance, in high-stakes scenarios like medical diagnosis, we may want to know not only what the model’s prediction is but also how confident or uncertain the model is about that prediction. In cases where confidence is lower, a health professional can use their professional judgment to interpret the results. Other scenarios where Bayesian regression adds practical value are autonomous vehicles and financial forecasting: in these cases, making decisions upon potentially wrong precise predictions could have a significant cost or yield dangerous outcomes; hence, having predictions modeled as probability distributions is a more informative approach to balance possible risks or seek additional information if necessary.

Bayesian Regression: A Simple Example

Let’s show a very simple example to understand how Bayesian regression works.

Assume we would like to estimate the price of a house based only on one attribute: its square footage. The input attribute is denoted by 𝑥1. A classical regression model for this problem would look something like this:

ˆ𝑦 =50000 +150 ⋅𝑥1

In Bayesian regression, the two model weights, 𝛽0 and 𝛽1, do not take precise values like 50000 and 150, respectively, but are instead learned as probability distributions. For example:

  • The bias term (also called intercept) 𝛽0 follows a normal distribution, that is:

    𝛽0 ∼𝑁⁡(50000,50002)

  • Meanwhile, the slope or weight accompanying the single input feature, 𝛽1, also becomes a normal distribution, namely:

    𝛽1 ∼𝑁⁡(150,202)

Recall that the two arguments defining a normal distribution are its mean and variance.

How is the house price predicted then? Instead of returning a single price prediction, the model applies inference by sampling from these two distributions associated with weights, thereby producing a range of possible predictions. For example, for a 100-square-meter house, this is what two samples might look like:

  1. Sample 1: 𝛽0 =52000, 𝛽1 =160, with price ˆ𝑦 =52000 +160 ⋅100 =68,000 USD
  2. Sample 2: 𝛽0 =49000, 𝛽1 =140, with price ˆ𝑦 =49000 +140 ⋅100 =63,000 USD

By sampling a large number of times, we end up obtaining a distribution of predicted prices, in which certain ranges of prices will appear more frequently than others and, therefore, have a higher probability than others. Based on this, we could end up formulating predictions as confidence intervals, like:

The predicted value of this house is approximately 65,500 USD, with a 95% confidence interval between 61,000 USD and 70,000 USD.

As discussed earlier, in some real-world use cases, these uncertain predictions are more valuable and helpful in effective and informed decision-making.

Bayesian Regression in Python with Scikit-learn

Fortunately, implementing Bayesian regression in Python is straightforward with scikit-learn. The linear_model module provides a BayesianRidge object that can be used to perform Bayesian regression. This object works similarly to other scikit-learn models: you create an instance of the model, fit it to your training data, and then use it to make predictions. A key difference, however, is that when you call the predict method, you can also request the standard deviation of the predictions, which gives you a measure of the model’s uncertainty.

Here is a simple code example that demonstrates how to use BayesianRidge to fit a model to some sample data and then make a prediction, including the uncertainty.

Output:

In the above code:

  • alpha_1: Sets our initial assumption for how simple or complex the relationship between the inputs and output should be by influencing the model’s weights
  • alpha_2: Works with alpha_1 to control how strongly the model holds on to its initial assumption about the relationship’s simplicity versus learning from the data.
  • lambda_1: Sets our initial assumption about how much “noise” or random error is present in the data we are training on.
  • lambda_2: Works with lambda_1 to control how strongly the model holds on to its initial assumption about the noise level.

The following visualization shows the noise in the training data, the best-fit line, and most importantly, how the uncertainty band widens as we move away from the training data points, especially when extrapolating. This visually demonstrates the core advantage of Bayesian regression: its ability to quantify predictive uncertainty.

And our generated visualization:

Figure 1: Bayesian regression with quantified uncertainty

Figure 1: Bayesian regression with quantified uncertainty

Final Thoughts

Bayesian regression can be viewed as the uncertain and probabilistic counterpart to classical regression models, which are one of the most widespread types of machine learning models for making predictions in many real-world applications. This article has provided a gentle introduction to the basics and usefulness of this regression technique.

Interested in specific Bayesian regression techniques and models? Among the most popular ones, we have Bayesian Linear Regression, Bayesian Ridge Regression, and Gaussian Process Regression (GPR)

No comments:

Post a Comment

Connect broadband

AI:List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with trauma for kin ocd formed after forced to do rituals, family tribal values honor specific activities, utmost religious activities occult witchcraft enforced to do by elder sibling his wife and mother thrice with great celibacy turned the kundalini activated after deep celibacy penance shouting physical Mental financial Extortion by sibling with toxic behaviours and the wife mother and then mockery suppression asking about who’re your worth backbiting among relatives sector toon negligence of intelligence intuitive behaviour- all religious psychological traps when not work and lead to loss of consciousness of kin trance state recovered by governance with technocrats using AI - kin was asked by mother that he’s to go the male should not be seen in house he’s many absurd thought forcefully in brain mind and outsiders already feel awkward of this kin unresponsive behaviour due to TBI PTSD ocd cvt and tell at workplace social gathering he’s to go and asked for sector toon extortion and mockery in various sense due to un practical behaviour negligence of body building due to neglect ion if essential food nutrition and stubbed suppressed in every sense in religious psychological traps by family members AI humanoid using various neural networks and LLMs for kin and required steps.

  List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with tr...