English translation
Generate synthetic data
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.
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:
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.
Where:
yis the response (dependent) variableXis the design matrix of explanatory (independent) variables\betais the vector of unknown regression coefficients to be estimated\epsilonis the error term, typically assumed to follow a normal distribution with zero mean and variance :
Bayesian linear regression treats parameter estimation probabilistically. We assign a prior distribution to the regression coefficients , 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 using Bayes’ theorem:
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.
Here:
- is the likelihood function
- is the prior distribution; for linear regression, a normal distribution is commonly chosen
- 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 in Bayesian linear regression is a multivariate normal distribution:
Where:
- is the prior mean
- is the prior covariance matrix
Joint Distribution and Posterior Inference
Given observed data, the likelihood function can be written as:
Combining this likelihood with the prior yields the posterior distribution of . Through analytical derivation, it turns out that the posterior remains Gaussian:
with posterior mean and covariance given by:
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 and slope , enabling both inference and prediction under quantified uncertainty.
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.
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