Guozhen AIGlobal AI field notes and model intelligence

English translation

Generate synthetic data

Published:

Category: Bayesian Learning

Read time: 4 min

Reads: 0

Lesson #13Views are counted together with the original Chinese articleImages are preserved from the source page

Structure Diagram of Bayesian Linear Regression

The core idea of Bayesian learning is to combine prior beliefs with new evidence while explicitly quantifying uncertainty. While reading, structure your understanding in the following sequence: “Fundamental Concepts of Linear Regression → Bayesian Inference → Prior Distribution Selection → Joint Distribution and Posterior Inference,” then verify each component using the code, case studies, or evaluation metrics presented in the main text.

Bayesian Linear Regression Checklist Diagram

After reading, perform a quick reality check using a small, concrete task: identify what the inputs are, where the processing steps occur, and whether the outputs are verifiable and acceptable. If something fails, first revisit “Fundamental Concepts of Linear Regression,” then proceed to “Bayesian Inference.”

In the previous article, we discussed overfitting and regularization in model selection. In statistical modeling—especially regression analysis—there is always a need to control model complexity. Next, we delve into the linear regression model within Bayesian regression. This framework enables us to leverage Bayesian inference to deliver more robust parameter estimates and reliable predictions—while rigorously accounting for uncertainty.

Fundamental Concepts of Linear Regression

The basic form of a linear regression model is:

Bayesian Linear Regression Decision Card

When learning Bayesian linear regression, begin by examining: the linear model itself, assumptions about noise, choice of parameter priors, posterior updating mechanics, and prediction intervals.

y=Xβ+ϵy = X\beta + \epsilon

Where:

  • y is the response (dependent) variable
  • X is the design matrix of explanatory (independent) variables
  • \beta is the vector of unknown regression coefficients to be estimated
  • \epsilon is the error term, typically assumed to follow a normal distribution with zero mean and variance σ2\sigma^2: ϵN(0,σ2)\epsilon \sim \mathcal{N}(0, \sigma^2)

Bayesian linear regression treats parameter estimation probabilistically. We assign a prior distribution to the regression coefficients β\beta, then combine it with the likelihood function derived from observed data, applying Bayes’ theorem to obtain the posterior distribution.

Bayesian Inference

Within the Bayesian framework, our goal is to update our belief about parameters β\beta using Bayes’ theorem:

Bayesian Learning Reading Map Card

When reading “Bayesian Regression: The Linear Regression Model”, start by reviewing the task, concepts, exercises, and decision points illustrated in the accompanying figures—then return to the main text to fill in technical details. This approach helps you quickly assess where—and how—this content applies in real-world scenarios.

p(βy,X)=p(yX,β)p(β)p(yX)p(\beta \mid y, X) = \frac{p(y \mid X, \beta)\, p(\beta)}{p(y \mid X)}

Here:

  • p(yX,β)p(y \mid X, \beta) is the likelihood function
  • p(β)p(\beta) is the prior distribution; for linear regression, a normal distribution is commonly chosen
  • p(yX)p(y \mid X) is the marginal likelihood (also called the evidence), obtainable via integration—but often unnecessary to compute directly.

Choosing a Prior Distribution

In practice, a standard choice for the prior on β\beta in Bayesian linear regression is a multivariate normal distribution:

βN(μ0,Σ0)\beta \sim \mathcal{N}(\mu_0, \Sigma_0)

Where:

  • μ0\mu_0 is the prior mean
  • Σ0\Sigma_0 is the prior covariance matrix

Joint Distribution and Posterior Inference

Given observed data, the likelihood function can be written as:

p(yX,β)=i=1nN(yiXiβ,σ2)p(y \mid X, \beta) = \prod_{i=1}^{n} \mathcal{N}(y_i \mid X_i\beta,\, \sigma^2)

Combining this likelihood with the prior yields the posterior distribution of β\beta. Through analytical derivation, it turns out that the posterior remains Gaussian:

βy,XN(μn,Σn)\beta \mid y, X \sim \mathcal{N}(\mu_n, \Sigma_n)

with posterior mean μn\mu_n and covariance Σn\Sigma_n given by:

Σn=(Σ01+1σ2XX)1\Sigma_n = \left(\Sigma_0^{-1} + \frac{1}{\sigma^2} X^\top X\right)^{-1} μn=Σn(Σ01μ0+1σ2Xy)\mu_n = \Sigma_n \left( \Sigma_0^{-1}\mu_0 + \frac{1}{\sigma^2} X^\top y \right)

Practical Example: Bayesian Linear Regression

Below is a simple Python implementation demonstrating Bayesian linear regression.

First, install the required packages:

pip install pymc3 numpy matplotlib

Then, construct and fit a Bayesian linear regression model:

import numpy as np
import pymc3 as pm
import matplotlib.pyplot as plt

# Generate synthetic data
np.random.seed(42)
X = np.random.randn(100, 1)
y = 2 * X.flatten() + 1 + np.random.randn(100) * 0.5

# Define Bayesian linear regression model
with pm.Model() as model:
    # Priors
    alpha = pm.Normal('alpha', mu=0, sigma=10)
    beta = pm.Normal('beta', mu=0, sigma=10)
    sigma = pm.HalfNormal('sigma', sigma=1)
    
    # Expected value of the linear model
    mu = alpha + beta * X.flatten()
    
    # Likelihood
    Y_obs = pm.Normal('Y_obs', mu=mu, sigma=sigma, observed=y)
    
    # Sample from the posterior
    trace = pm.sample(1000, return_inferencedata=False)

# Visualize posterior traces
pm.plot_trace(trace)
plt.show()

In this example, we generate synthetic data exhibiting a linear relationship and use PyMC3 to build and fit a Bayesian linear regression model. Sampling yields posterior distributions for the intercept α\alpha and slope β\beta, enabling both inference and prediction under quantified uncertainty.

Bayesian Linear Regression Application Review Card

When reviewing “Bayesian Regression: The Linear Regression Model”, place key concepts, procedural steps, and observable outcomes side-by-side on a single page for efficient revision.

Bayesian Linear Regression Application Checklist Card

When practicing “Bayesian Regression: The Linear Regression Model”, write down input conditions, processing actions, and observable results together—making future verification straightforward.

Summary

In this article, we introduced the Bayesian linear regression model and demonstrated how Bayesian inference updates our beliefs about regression coefficients. We explained how prior choices influence posterior distributions and illustrated practical implementation in Python using real code. In the next article, we will explore advanced topics in Bayesian regression—including principled prior selection strategies and deeper posterior analysis—to further enhance model flexibility and accuracy. Stay tuned!

Continue

Keep reading from here

Browse English site

Reader Messages

Reader messages

Questions, corrections, extra sources, or hands-on results can be left here. No login is required.

Max 800 characters

To reduce spam, each message is checked for length, link count, and posting frequency.

0/800

Messages

0 messages
Loading messages...