Skip to article frontmatterSkip to article content

2. Use of conjugate priors

Authors
Affiliations
TNO
TNO
TNO

A conjugate prior is an algebraic convenience, giving a closed-form expression for the posterior; otherwise, numerical integration may be necessary.

More information about conjugate priors and tables of cojugate distribution can be found in wikipedia

conjugate example

Import Necessary Libraries

We will start by importing the required libraries for our analysis.

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

Define Constants and Sample Data

Next, we define the constants, including the standard deviation and the sample data.

std = 5  # MPa
sample = [30.1, 28.7, 21.2, 28.6, 25.8, 25.1, 24.2, 27.3, 20.0, 34.8]  # MPa
n = len(sample)

prior_mu_mean = 40  # MPa
prior_mu_std = 100  # MPa

Calculate the Mean of the Sample

We calculate the mean of the sample data.

## Write your code where the three dots are
posterior_mu_mean = ...
print(f"Mean of the sample: {mean_sample:.2f} MPa")

Posterior Distribution with Conjugate Prior

Using the sample mean and the prior information, we calculate the posterior distribution.

## Write your code where the three dots are
posterior_mu_mean = ...
posterior_mu_std = ...
print(f"Posterior mean: {posterior_mu_mean:.2f} MPa")
print(f"Posterior standard deviation: {posterior_mu_std:.2f} MPa")
Mean of the sample: 26.58 MPa
Posterior mean: 26.58 MPa
Posterior standard deviation: 1.58 MPa

Plot the Distributions

## Write your code where the three dots are

x = np.linspace(0, 100, 1000)
prior = ...
posterior = ...

fig, ax = plt.subplots()
ax.plot(x, prior, label="Prior")
ax.plot(x, posterior, label="Posterior")
ax.set_xlim(0, 100)
ax.set_ylim(0, 0.3)
ax.set_xlabel("Mean (MPa)")
ax.set_ylabel("Density")
ax.set_title("Distribution of the Mean")
ax.legend()
plt.show()