🚀
Finter
PlaygroundData Catalog
Research Guide
Research Guide
  • 📄Quantitative Research Handbook
  • Financial Essentials
    • Asset Allocation Overview
      • Strategic vs Tactical Allocation
      • Benchmarking in Asset Allocation
    • Fundamentals of Financial Analysis
      • Quantitative Finance Glossary
      • Asset Pricing Factors
      • Modigliani-Miller Theorem
      • Ken Fisher's Financial Analysis
      • Options Pricing Introduction
      • Fixed Income for Quants
  • Quantitative Analysis
    • Robustness and Bias
      • Bias in Investment Strategies
      • Forward Looking Bias
      • Overfitting in Quant Models
      • Mindset for Robust Quant
      • Investment Horizon and Rebalancing
    • Quant Modeling Basics
      • Idea Generation for Quant Modeling
      • Portfolio Construction
      • Cointegration and Pair Trading
      • Using Technical Indicators
      • Portfolio Performance Metrics
    • Risk Management Techniques
      • Risk in Quant Finance
      • Market Risk Measurement
  • Data Science for Finance
    • Data Characteristics and Methodologies
      • Point-in-Time Data
      • Stock Price Adjustment
      • Understanding Financial Data
      • ID Structures in Quant Finance
    • Statistical Analysis in Finance
      • Correlation vs Causality
      • Sentiment Analysis Using News
      • Optimizing Pandas Performance
      • Bayesian Linear Regression with Gibbs Sampling
    • Machine Learning Techniques
      • Challenges in Financial Time-Series
  • Modeling and Backtesting
    • Backtesting Framework
      • Assumptions in Backtesting
Powered by GitBook
On this page
  • Bayesian Linear Regression and Gibbs Sampling
  • Prior Distributions
  • Gibbs Sampling
  • Gibbs Sampling Procedure

Was this helpful?

Edit on GitHub
  1. Data Science for Finance
  2. Statistical Analysis in Finance

Bayesian Linear Regression with Gibbs Sampling

An in-depth guide on implementing Bayesian linear regression and Gibbs sampling for parameter estimation.

TL; DR

  • Bayesian linear regression assumes data follows a normal distribution given parameters.

  • Prior distributions for regression coefficients and variance are normally and inverse-gamma distributed respectively.

  • Gibbs sampling is used to sample from the posterior distribution when direct computation is infeasible.

  • The full conditional distributions for coefficients and variance are derived to facilitate sampling.

  • The Gibbs sampling procedure iterates between sampling regression coefficients and variance to approximate the posterior distribution.


Bayesian Linear Regression and Gibbs Sampling

Bayesian linear regression is a statistical method in which the statistical analysis is undertaken within the context of Bayesian inference. When performing Bayesian linear regression, we assume that the observed data, yyy, given the parameters β\betaβ and σ2\sigma^2σ2, follows a normal distribution:

y∣β,σ2∼N(Xβ,σ2)y|\beta, \sigma^2 \sim N(X\beta, \sigma^2)y∣β,σ2∼N(Xβ,σ2)

Prior Distributions

The prior distributions for the parameters are set as follows:

  • For β\betaβ:

    β∼N(β0,Λ0)\beta \sim N(\beta_0, \Lambda_0)β∼N(β0​,Λ0​)

    with the prior probability density function (pdf) given by:

    f(x;β0,Λ0)=(2π)−k2det(Λ0)−12exp⁡(−12(β−β0)′Λ0−1(β−β0))f(x; \beta_0, \Lambda_0) = (2\pi)^{-\frac{k}{2}} \text{det}(\Lambda_0)^{-\frac{1}{2}} \exp\left(-\frac{1}{2} (\beta - \beta_0)' \Lambda_{0}^{-1} (\beta - \beta_0)\right)f(x;β0​,Λ0​)=(2π)−2k​det(Λ0​)−21​exp(−21​(β−β0​)′Λ0−1​(β−β0​))
  • For σ2\sigma^2σ2:

    σ2∼IG(a02,b02)\sigma^2 \sim IG\left(\frac{a_0}{2}, \frac{b_0}{2}\right)σ2∼IG(2a0​​,2b0​​)

    with the prior pdf given by:

    f(x;a02,b02)=(b02)a02Γ(a02)x−(a02+1)exp⁡(−b02x)f(x; \frac{a_0}{2}, \frac{b_0}{2}) = \frac{\left(\frac{b_0}{2}\right)^{\frac{a_0}{2}}}{\Gamma\left(\frac{a_0}{2}\right)} x^{-\left(\frac{a_0}{2} + 1\right)} \exp\left(-\frac{b_0}{2x}\right)f(x;2a0​​,2b0​​)=Γ(2a0​​)(2b0​​)2a0​​​x−(2a0​​+1)exp(−2xb0​​)
Expand for Bayesian Theorem and Posterior Distribution

By Bayes' theorem, the posterior distribution is proportional to the product of the likelihood and the prior distributions:

Where:

  • The likelihood is given by:

    f(y∣β,σ2)∝(σ2)−T2exp⁡(−(y−Xβ)′(y−Xβ)2σ2)f(y | \beta, \sigma^2) \propto (\sigma^2)^{-\frac{T}{2}} \exp\left(-\frac{(y-X\beta)' (y-X\beta)}{2\sigma^2}\right)f(y∣β,σ2)∝(σ2)−2T​exp(−2σ2(y−Xβ)′(y−Xβ)​)
  • The prior for β\betaβ is:

    π(β)∝exp⁡(−12(β−β0)′Λ0−1(β−β0))\pi(\beta) \propto \exp\left(-\frac{1}{2} (\beta - \beta_0)' \Lambda_{0}^{-1} (\beta - \beta_0)\right)π(β)∝exp(−21​(β−β0​)′Λ0−1​(β−β0​))
  • The prior for σ2\sigma^2σ2 is:

    π(σ2)∝(σ2)−(a02+1)exp⁡(−b02σ2)\pi(\sigma^2) \propto (\sigma^2)^{-\left(\frac{a_0}{2} + 1\right)} \exp\left(-\frac{b_0}{2\sigma^2}\right)π(σ2)∝(σ2)−(2a0​​+1)exp(−2σ2b0​​)

Gibbs Sampling

Directly computing β,σ2∣y\beta, \sigma^2 | yβ,σ2∣y can be challenging, so we use Gibbs sampling by sampling from the full conditional distributions of β∣σ2,y\beta | \sigma^2, yβ∣σ2,y and σ2∣β,y\sigma^2 | \beta, yσ2∣β,y.

Expand for Full Conditional Distributions

The full conditional for β\betaβ is: β∣σ2,y∝exp⁡(−(y−Xβ)′(y−Xβ)2σ2)exp⁡(−12(β−β0)′Λ0−1(β−β0)) \beta | \sigma^2, y \propto \exp\left(-\frac{(y-X\beta)' (y-X\beta)}{2\sigma^2}\right) \exp\left(-\frac{1}{2} (\beta - \beta_0)' \Lambda_{0}^{-1} (\beta - \beta_0)\right) β∣σ2,y∝exp(−2σ2(y−Xβ)′(y−Xβ)​)exp(−21​(β−β0​)′Λ0−1​(β−β0​))

This simplifies to:

∝exp⁡(−12[β′(X′Xσ2+Λ0−1)β−2β′(X′yσ2+Λ0−1β0)])\propto \exp\left(-\frac{1}{2} \left[ \beta' \left(\frac{X'X}{\sigma^2} + \Lambda_0^{-1}\right)\beta -2 \beta' \left(\frac{X'y}{\sigma^2} + \Lambda_0^{-1} \beta_0\right) \right]\right)∝exp(−21​[β′(σ2X′X​+Λ0−1​)β−2β′(σ2X′y​+Λ0−1​β0​)])

The full conditional for β\betaβ is a normal distribution. If we denote β∣σ2,y∼N(β1,Λ1)\beta | \sigma^2, y \sim N(\beta_1, \Lambda_1)β∣σ2,y∼N(β1​,Λ1​), then:

Λ1=(X′Xσ2+Λ0−1)−1\Lambda_1 = \left(\frac{X'X}{\sigma^2} + \Lambda_0^{-1}\right)^{-1}Λ1​=(σ2X′X​+Λ0−1​)−1

β1=Λ1(X′yσ2+Λ0−1β0)\beta_1 = \Lambda_1 \left(\frac{X'y}{\sigma^2} + \Lambda_0^{-1} \beta_0\right)β1​=Λ1​(σ2X′y​+Λ0−1​β0​)

The full conditional for σ2\sigma^2σ2 is:

σ2∣β,y∝(σ2)−T2exp⁡(−(y−Xβ)′(y−Xβ)2σ2)(σ2)−(a02+1)exp⁡(−b02σ2)\sigma^2 | \beta, y \propto (\sigma^2)^{-\frac{T}{2}} \exp\left(-\frac{(y-X\beta)' (y-X\beta)}{2\sigma^2}\right) (\sigma^2)^{-\left(\frac{a_0}{2} + 1\right)} \exp\left(-\frac{b_0}{2\sigma^2}\right)σ2∣β,y∝(σ2)−2T​exp(−2σ2(y−Xβ)′(y−Xβ)​)(σ2)−(2a0​​+1)exp(−2σ2b0​​)

This simplifies to an inverse-gamma distribution:

σ2∣β,y∼IG(a12,b12)\sigma^2 | \beta, y \sim IG\left(\frac{a_1}{2}, \frac{b_1}{2}\right)σ2∣β,y∼IG(2a1​​,2b1​​)

Where:

a1=a0+Ta_1 = a_0 + Ta1​=a0​+T

b1=b0+(y−Xβ)′(y−Xβ)b_1 = b_0 + (y-X\beta)'(y-X\beta)b1​=b0​+(y−Xβ)′(y−Xβ)

Gibbs Sampling Procedure

The Gibbs sampling procedure for obtaining samples from β,σ2∣y\beta, \sigma^2 | yβ,σ2∣y is as follows:

  1. Sample β(1)\beta^{(1)}β(1) from β∣σ2(0),y. \beta | \sigma^{2^{(0)}}, y.β∣σ2(0),y.

  2. Sample σ2∣β(1),y. \sigma^2 | \beta^{(1)}, y.σ2∣β(1),y. from σ2∣β(1),y\sigma^2 | \beta^{(1)}, yσ2∣β(1),y.

  3. Sample β(2)\beta^{(2)}β(2) from β∣σ2(1),y. \beta | \sigma^{2^{(1)}}, y.β∣σ2(1),y.

  4. Sample σ2(2)\sigma^{2^{(2)}} σ2(2) from σ2∣β(2),y.\sigma^2 | \beta^{(2)}, y.σ2∣β(2),y.

  5. Continue this process for nnn iterations.

After nnn iterations, we have nnn samples from the full posterior distribution.

Last updated 1 year ago

Was this helpful?