SMBI CH3 Final Update
SMBI CH3 Final Update
Chapter 03
More Than One Parameter
Reference
Lesaffre, E., & Lawson, A. B. (2012). Bayesian biostatistics. John Wiley & Sons.
2 / 96
SMBI: Chapter 3, More Than One Parameter
Introduction
Contents
1 Introduction
4 Multivariate Distributions
3 / 96
SMBI: Chapter 3, More Than One Parameter
Introduction
Introduction
In this chapter:
Derivation of multivariate (multi-parameter) posterior and its summary measures
Derivation of marginal posterior distributions
Examples
(multivariate) Gaussian distribution
Multinomial distribution data
Bayesian linear and generalized linear regression models
Multivariate sampling approach: Method of Composition
4 / 96
SMBI: Chapter 3, More Than One Parameter
Joint Versus Marginal Posterior Inference
Contents
1 Introduction
4 Multivariate Distributions
5 / 96
SMBI: Chapter 3, More Than One Parameter
Joint Versus Marginal Posterior Inference
6 / 96
SMBI: Chapter 3, More Than One Parameter
Joint Versus Marginal Posterior Inference
7 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
Contents
1 Introduction
4 Multivariate Distributions
8 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
Three priors:
No prior knowledge is available
Previous study is available
Expert knowledge is available
9 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
10 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
11 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
12 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
13 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
14 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
15 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
16 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
17 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
18 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
Python Code
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
unl_mu = 6.2
from scipy.stats import t, chi2
upl_mu = 7.5
# Read the text file into a Dataframe
unl_sigma2 = 1.2
# (Dataset is provided)
upl_sigma2 = 4
ALP = pd.read_table("D:/Data/Bayesian/ALP.txt",
sep="\t")
nmu = 100
nsigma2 = 100
# Access the columns directly
alp = ALP.loc[ALP['artikel']==0, 'alkfos']
# Valye using non-informative prior
talp = 100 * alp ** (-1/2)
mu = np.linspace(unl_mu, upl_mu, num=nmu)
nalp = len(alp)
mus = (mu - mtalp) / (sdtalp / np.sqrt(ntalp))
pmu = t.pdf(mus, df=ntalp-1) * np.sqrt(ntalp) / sdtalp
# Calculate mean, variance, std and length of talp
mtalp = np.mean(talp)
sigma2 = np.linspace(unl_sigma2, upl_sigma2, num=nsigma2)
vartalp = np.var(talp)
sigma2s = (ntalp-1) * vartalp / sigma2
sdtalp = np.sqrt(vartalp)
psigma2 = chi2.pdf(sigma2s, ntalp-1)
ntalp = len(talp)
* (ntalp-1) * vartalp / (sigma2**2)
print(f"Mean (mu):{mtalp},
Standard Deviation (mu):{sdtalp}")
19 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
plt.figure(figsize=(12, 6))
# For sigma^2
plt.subplot(1, 2, 1)
sigma2postmean = vartalp * (ntalp - 1) / (ntalp - 3)
# Plot for mean
sigma2postmode = vartalp * (ntalp - 1) / (ntalp + 1)
plt.plot(mu, pmu, color='darkred', linewidth=3)
sigma2postmedian = vartalp * (ntalp - 1)
plt.xlabel(r'$\mu$', fontsize=15)
/ chi2.ppf(0.5, ntalp - 1)
plt.ylabel('Posterior', fontsize=15)
sigma2postvar = vartalp**2 * 2 * (ntalp - 1)**2
/ ((ntalp - 3)**2 * (ntalp - 5))
plt.subplot(1, 2, 2)
sigma2_post_unl = (ntalp - 1) * vartalp
# Plot for sigma^2
/ chi2.ppf(0.975, ntalp - 1)
plt.plot(sigma2, psigma2, color='darkred',
sigma2_post_upl = (ntalp - 1) * vartalp
linewidth=3)
/ chi2.ppf(0.025, ntalp - 1)
plt.xlabel(r'$\sigma^2$', fontsize=15)
plt.ylabel('Posterior', fontsize=15)
# HPD
unl_sigma2 = 1.2
# Show the plots
upl_sigma2 = 2.7
plt.show()
nsigma2 = 300
sigma2 = np.linspace(unl_sigma2, upl_sigma2, num=nsigma2)
# Summary statistics
sigma2s = (ntalp - 1) * vartalp / sigma2
# For mu
psigma2 = chi2.pdf(sigma2s, ntalp - 1) * (ntalp - 1)
mupostmean = mtalp
* vartalp / (sigma2**2)
mupostvar = (ntalp - 1) * vartalp
/ (ntalp * (ntalp - 2))
minimum = 9999
mu_post_unl = mupostmean - t.ppf(0.975, ntalp - 1)
min_idx = 0
* sdtalp / np.sqrt(ntalp)
max_idx = 0
mu_post_upl = mupostmean + t.ppf(0.975, ntalp - 1)
eps = 0.001
* sdtalp / np.sqrt(ntalp)
20 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
min_value = sigma2[min_idx]
max_value = sigma2[max_idx]
21 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
22 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
23 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
PPD
1
p(ỹ |y ) = tν̄ ȳ , s 2 1 +
κ0 + n
24 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
25 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
26 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
For σ 2
n
Y
p(σ 2 |y ) ∝ σ̄N(µ̄|µ0 , σ02 )Inv-χ2 (σ 2 |ν0 , τ02 ) N(yi |µ̄, σ 2 )
i=1
27 / 96
SMBI: Chapter 3, More Than One Parameter
The Normal Distribution with µ and σ 2 unknown
28 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
Contents
1 Introduction
4 Multivariate Distributions
29 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
Multivariate distributions
Two distributions:
Multivariate normal distribution + related distributions
Multinomial distribution
30 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
P P
Multivariate normal distribution (MVN): N(µ, ) or Np (µ, )
For a p-dimensional continuous random vector y :
1 1 T −1
p(y |µ, Σ) = exp − (y − µ) Σ (y − µ)
(2π)p/2 |Σ|1/2 2
Properties:
Marginal distributions are normal
Conditional distributions are normal
Distributions of linear combinations of y are normal
31 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
Related distributions - 1
Properties:
Heavier tails than the MVN distribution
Posterior in a classical Bayesian regression model (see below)
Also used as a ’robust’ data distribution
Multivariate extension of location-scale t-distribution with ν degrees of freedom
32 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
Related distribution - 2
with
p
−1 νp/2 p(p−1)/4
Y ν+1−j
c =2 π Γ
2
j=1
Properties:
Extension of χ2 (ν)-distribution: S 2 /σ 2 ∼ χ2 (n − 1)
Inverse Wishart distribution IW(D, ν)
R ∼ IW(D, ν) ⇐⇒ R −1 ∼ Wishart(D, ν) with D a precision matrix
33 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
Pk Pk
with n = j=1 yj , θ = (θ1 , ..., θk )T , θj > 0, (j = 1, ..., k), j=1 θj = 1
Properties:
Binomial distribution = special case of the multinomial distribution with k = 2
Marginal distribution yj = binomial distribution Bin(n, θj )
ConditionalPdistribution of yj given y S = {ym : m ∈ S, j ∈ S} = Mult(y S , θ S ) with
θ S = {θj / m∈S θm }
34 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
35 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
36 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
1 Y αij −1
θ∼ θij
B(α)
i,j
37 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
38 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
39 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
40 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
Python Code
import math
np.random.seed(777)
import numpy as np
import matplotlib.pyplot as plt
N = 100000
from scipy.stats import beta
w11 = np.random.gamma(alpha11, 1, N)
x = [180, 41, 216, 64]
w12 = np.random.gamma(alpha12, 1, N)
w21 = np.random.gamma(alpha21, 1, N)
or_ = (x[0] * x[3]) / (x[1] * x[2])
w22 = np.random.gamma(alpha22, 1, N)
lor = math.log(or_)
varlor = 1 / x[0] + 1 / x[1] + 1 / x[2] + 1 / x[3]
t = w11 + w12 + w21 + w22
sdlor = math.sqrt(varlor)
unlor = lor - 1.96 * sdlor
z11 = w11 / t
uplor = lor + 1.96 * sdlor
z12 = w12 / t
eunlor = math.exp(unlor)
z21 = w21 / t
euplor = math.exp(uplor)
z22 = w22 / t
print(f"95% CI of OR:{eunlor, euplor}")
plt.figure(figsize=(12, 8))
alpha11 = 1 + x[0]
plt.subplot(2, 2, 1)
alpha12 = 1 + x[1]
plt.hist(z11, density=True, bins=50, color="lightblue")
alpha21 = 1 + x[2]
zgrid = np.arange(0.28, 0.451, 0.001)
alpha22 = 1 + x[3]
plt.plot(zgrid, beta.pdf(zgrid, alpha11, salpha - alpha11),
lw=1, color="blue")
salpha = alpha11 + alpha12 + alpha21 + alpha22
41 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
plt.xlim(0.28, 0.45)
plt.ylim(0, 20)
plt.xlabel(r"$\theta_{11}$")
plt.subplot(2, 2, 4)
psi = (z11 * z22) / (z12 * z21)
plt.subplot(2, 2, 2)
lpsi = np.log(psi)
plt.hist(z12, density=True, bins=50, color="lightblue")
plt.hist(psi, density=True, bins=50,
zgrid = np.arange(0.04, 0.13, 0.001)
color="lightblue")
plt.plot(zgrid, beta.pdf(zgrid, alpha12, salpha - alpha12),
plt.xlabel(r"$\psi$")
lw=1, color="blue")
plt.tight_layout()
plt.xlim(0.04, 0.13)
plt.show()
plt.ylim(0,35)
plt.xlabel(r"$\theta_{12}$")
q = np.percentile(lpsi, [2.5, 97.5])
meanq = np.mean(q)
plt.subplot(2, 2, 3)
print(f"95% CI logOR:{q}")
plt.hist(z21, density=True, bins=50, color="lightblue")
print(f"LogOR:{meanq}")
zgrid = np.arange(0.35, 0.52, 0.001)
print(f"95% CI OR:{np.exp(q)}")
plt.plot(zgrid, beta.pdf(zgrid, alpha21, salpha - alpha21),
print(f"OR:{np.exp(meanq)}")
lw=1, color="blue")
plt.xlim(0.35, 0.52)
plt.xlabel(r"$\theta_{21}$")
42 / 96
SMBI: Chapter 3, More Than One Parameter
Multivariate Distributions
Exercise
43 / 96
SMBI: Chapter 3, More Than One Parameter
Frequentist properties of Bayesian inference
Contents
1 Introduction
4 Multivariate Distributions
44 / 96
SMBI: Chapter 3, More Than One Parameter
Frequentist properties of Bayesian inference
Not of prime interest to a Bayesian to know the sampling properties of Bayesian estimators
But: good frequentist properties of Bayesian estimators adds to their credibility
For instance: interval estimators (correct coverage)
Bayesian approach offers alternative interval estimators may be also useful in frequentist
calculations
Agresti and Min (2005): best frequentist properties for odds ratio when Jefferys prior for the
binomial parameters is taken
Rubin (1984): other examples where the Bayesian 100(1 − α)% CI gives at least
100(1 − α)% coverage even when the prior distribution is chosen incorrectly
45 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
Contents
1 Introduction
4 Multivariate Distributions
46 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
Sampling approach:
Sample θ̃d from p(θd |y )
Sample θ̃(d−1) from p(θ(d−1) |θ̃d , y )
...
Sample θ̃1 from p(θ1 |θ̃d , ..., θ̃2 , y )
47 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
48 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
Sample from p(µ, σ 2 |y ): Sample from p(σ 2 |y ) & Sample from p(µ|σ 2 , y )
1. Sample from p(σ 2 |y )
Sample ν̃ k from a χ2 (n − 1)-distribution
Solve σ̃ 2k in (n − 1)s 2 /σ̃ 2k = ν̃ k
2. Sample from p(µ|σ 2 , y )
Sample µ̃k from a N(ȳ , σ̃ 2k /n)-distribution
⇒ µ̃ , ..., µ̃K = random sample from p(µ|y )(tn−1 (ȳ , s 2 /n)-distribution)
1
49 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
Case 1: (continued)
To sample from the posterior predictive distribution p(ỹ |y ), 2 approaches:
1. Sample directly from tn−1 [ȳ , s 2 (1 + n1 )]-distribution
2. Use Method of Composition
Sample σ̃ 2k from Inv-χ2 (σ 2 |n − 1, s 2 )
Sample µ̃k from N(µ|ȳ , σ̃ 2k /n)
Sample ỹ k from N(y |µ̃k , σ̃ 2k )
50 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
51 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
52 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
Python Code
import pandas as pd
import numpy as np # sample from sigma then from mu given sigma (1000 times),
import matplotlib.pyplot as plt # make use of theoretical results
from scipy.stats import t, chi2 np.random.seed(122823)
53 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
nsimul = 1000
54 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
55 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
print(py_unl, py_upl)
plt.subplot(2, 2, 1)
plt.subplots_adjust(top=0.9, bottom=0.1, left=0.1, right=0.9, hspace=0.4, wspace=0.4)
plt.subplot(2, 2, 1)
plt.hist(sigma_u**2, bins=30, density=True, color="lightblue")
plt.plot(sigma2, psigma2, linewidth=3, linestyle="-", color="blue")
plt.xlabel(r"$\sigma^2$")
plt.ylabel("")
plt.title("")
plt.text(1.5, 2.3, "(a)", fontsize=12)
56 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
plt.subplot(2, 2 ,2)
plt.hist(mu_u, bins=30, density=True, color="lightblue")
plt.plot(mu, pmu, linewidth=3, linestyle="-", color="blue")
plt.xlabel(r"$\mu$")
plt.ylabel("")
plt.title("")
plt.text(6.9, 5, "(b)", fontsize=12)
plt.subplot(2, 2, 3)
#joint posterior distribution of mu and sigma^2
plt.scatter(mu_u, sigma_u**2, color="blue", s = 1)
plt.xlabel(r"$\mu$")
plt.ylabel(r"$\sigma^2$")
plt.title("")
plt.text(6.9, 2.4, "(c)", fontsize=12)
plt.subplot(2, 2, 4)
plt.hist(future_u, bins=30, density=True, color="lightblue")
plt.xlabel(r"$\tilde{y}$")
plt.ylabel("")
plt.title("")
plt.ylim(0, 0.3)
plt.text(3.5, 0.3, "(d)", fontsize=12)
plt.plot(tildey, ptildey, linewidth=3, linestyle="-", color="blue")
plt.show()
57 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
58 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
59 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
Method of Composition:
Stage I: sample σ 2
n
Y
p(σ 2 |y ) ∝ σ̄N(µ̄|µ0 , σ02 )Inv-χ2 (σ 2 |ν0 , τ02 ) N(yi |µ̄, σ 2 )
i=1
60 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
61 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
62 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
Python Code
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import t, chi2, norm
ALP = pd.read_table("D:/Data/Bayesian/ALP.txt",
sep="\t")
alkfos = ALP['alkfos']
artikel = ALP['artikel']
alphis = alkfos[artikel == 1]
talp0 = 100 * np.power(alphis, -1/2)
mtalp0 = np.mean(talp0)
vartalp0 = np.var(talp0)
sdtalp0 = np.sqrt(vartalp0)
ntalp0 = len(talp0)
63 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
mtalp = np.mean(talp)
vartalp = np.var(talp)
sdtalp = np.sqrt(vartalp)
ntalp = len(talp)
64 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
sigma2s = nu0*tau20/sigma2
psigma2 = chi2.pdf(sigma2s, nu0)*nu0*tau20/(sigma2**2)
loglastfact = np.zeros(nsigma2)
for i in range(ntalp):
loglastfact = loglastfact + np.log(norm.pdf(talp[i], loc=mubar, scale=np.sqrt(sigma2)))
sigma2s = df * s2 / sigma2
psigma2approx = chi2.pdf(sigma2s, df) * df * s2 / (sigma2 ** 2)
66 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
plt.xlabel(r'$\sigma^2$', fontsize=14)
plt.ylabel('Posterior density', fontsize=14)
plt.title('Posterior Densities', fontsize=16)
plt.legend()
plt.show()
plt.xlabel(r'$\sigma^2$', fontsize=14)
plt.ylabel('Ratio', fontsize=14)
plt.title('Ratio Plot', fontsize=16)
plt.show()
67 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
np.random.seed(122823)
68 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
loglastfact_u = np.zeros(NSIMUL)
for i in range(ntalp):
loglastfact_u = loglastfact_u + np.log(norm.pdf(talp[i], loc=mubar_u, scale=np.sqrt(sigma2_1)))
sigma2sappr_u = df * s2 / sigma2_1
post1 = chi2.pdf(sigma2sappr_u, df) * df * s2 / (sigma2_1 ** 2)
v = post2 / post1
w = v / np.sum(v)
# Now resample
sigma2_2 = np.random.choice(sigma2_1, size=nsimul, replace=False, p=w)
69 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
pmean = np.mean(sigma2_2)
pmedian = np.median(sigma2_2)
pvar = np.var(sigma2_2)
quantiles = np.quantile(sigma2_2, [0.025, 0.975])
unl_conf = pmean - 1.96 * np.sqrt(pvar) / np.sqrt(len(sigma2_2))
upl_conf = pmean + 1.96 * np.sqrt(pvar) / np.sqrt(len(sigma2_2))
print(pmean, pmedian, pvar, unl_conf, upl_conf)
#Now sample mu
mubar_2 = (mu0 / sigma20 + ntalp * mtalp / sigma2_2) / (1 / sigma20 + ntalp / sigma2_2)
sigma2bar_2 = 1 / (1 / sigma20 + ntalp / sigma2_2)
70 / 96
SMBI: Chapter 3, More Than One Parameter
The Method of Composition
pmean = np.mean(mu_2)
pmedian = np.median(mu_2)
pvar = np.var(mu_2)
quantiles = np.quantile(mu_2, [0.025, 0.975])
unl_conf = pmean - 1.96 * np.sqrt(pvar) / np.sqrt(len(mu_2))
upl_conf = pmean + 1.96 * np.sqrt(pvar) / np.sqrt(len(mu_2))
print(pmean, pmedian, pvar, unl_conf, upl_conf)
future_u_mean = np.mean(future_u)
print("Mean of future.u:", future_u_mean)
71 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
Contents
1 Introduction
4 Multivariate Distributions
72 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
73 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
74 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
75 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
76 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
Python code
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import statsmodels.api as sm
print(x.describe())
print(y.describe())
model = sm.OLS(y, sm.add_constant(x)).fit()
print(model.summary())
print(sm.OLS(y, sm.add_constant(x)).fit().params)
print(sm.OLS(y, sm.add_constant(x)).fit().fittedvalues)
77 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
Bayesian linear regression model = prior information on regression parameters & residual
variance + normal regression likelihood
Noninformative prior for (β, σ 2 ) : p(β, σ 2 |y ) ∝ σ −2
Notation: omit design matrix X
Posterior distributions:
78 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
79 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
ˆ ≤ ds 2 Fα (d + 1, n − d − 1)}
Cα (β) = {β : (β − β̂)T (X T X )(β − β)
80 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
PPD of ỹ with x̃
t-distribution with (n − d − 1) degrees of freedom with
location parameter: β T x̃
scale parameter: s 2 [1 + x̃ T (X T X )−1 x̃]
⇐⇒
ỹ − β T x̃
Given y : q ∼ tn−d−1
s 1 + x̃ T (X T X )−1 x̃
How to sample?
Directly from t-distribution
Method of Composition
81 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
Method of Composition
p(β|y ) = multivariate t-distribution: how to sample from it?
p(β|σ 2 , y ) = multivariate normal distribution
⇒ Sample in two steps
82 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
83 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
84 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
85 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
Python code
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import statsmodels.api as sm
mx = np.mean(x)
n = len(x)
my = np.mean(y)
model = sm.OLS(y, x)
s = sm.OLS(y,x).fit().scale
86 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
s = results.scale ** 0.5
s2 = s ** 2
df = n - 2
ones = np.ones(n)
X = np.column_stack((ones, x))
covpartial = np.linalg.inv(X.T @ X)
covmat = s2 * covpartial
mbeta = results.params
print(mbeta)
# sample from sigma then from beta given sigma (1000 times),
# make use of theoretical results
np.random.seed(50124)
nsimul = 1000
87 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
plt.hist(sigma2_u, bins=30)
plt.xlabel(r'$\tilde{\sigma}^2$')
plt.show()
88 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
print(meanbeta)
print(sdbeta)
print(unl_conf[0], upl_conf[0])
print(unl_conf[1], upl_conf[1])
print(quantiles_1)
print(quantiles_2)
89 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
# Histogram of coefficient 1
plt.subplot(2, 2, 1)
plt.hist(rbeta_u[:, 0], bins=30, density=True, color='lightblue')
plt.xlabel(r'$\beta_0$')
plt.text(0.55, 4, '(a)', fontsize=12)
# Histogram of coefficient 2
plt.subplot(2, 2, 2)
plt.hist(rbeta_u[:, 1], bins=30, density=True, color='lightblue')
plt.xlabel(r'$\beta_1$')
plt.text(0.028, 100, '(b)', fontsize=12)
90 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
future_u = np.empty(nsimul)
# Histogram of future.u
plt.hist(future_u, bins=30, density=True, color='lightblue')
plt.xlabel(r'$\tilde{y}$')
plt.ylabel('')
plt.title('')
plt.text(1.4, 1.5, '(d)', fontsize=12)
plt.show()
91 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian linear regression models
meanytilde = np.mean(future_u)
sdytilde = np.sqrt(np.var(future_u))
print("mean(y~):", meanytilde)
print("sd(y~):", sdytilde)
92 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian generalized linear models
Contents
1 Introduction
4 Multivariate Distributions
93 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian generalized linear models
Generalized Linear Model (GLM): extension of the linear regression model to a wide class of
regression models
Distribution part
Link function
Variance
Bayesian Generalized Linear Model (BGLIM): GLIM + priors on parameters
94 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian generalized linear models
Components of GLIM
Distribution part:
p(y |θ; ϕ) = exp[ y θ−b(θ)
a(ϕ) + c(y ; ϕ)], with a(·), b(·), c(·) known functions
Often a(ϕ) = ϕ/ω, with ω a prior weight. For ϕ known and ω = 1:
db(θ)
E (y ) = µ = dθ
d 2 b(θ)
Var (y ) = a(ϕ)V (µ) with V (µ) = d2θ
Link function: g (µ) = η = x T β, g = monotone (differentiable) function
When η = θ ⇒ link function = canonical, h = g −1
Variance function: ϕ = ’extra’ dispersion or ’scale’ parameter
Var (y ) = a(ϕ)V (µ) can depend on covariates via µ
95 / 96
SMBI: Chapter 3, More Than One Parameter
Bayesian generalized linear models
96 / 96