All articles →

100 Exercises / Probability Statistics / Probability & Statistics: Python 100 Exercises

Bayesian Statistics for Manufacturing Decisions: 10 Python Exercises

Update manufacturing risks with Bayesian models, MCMC, PyMC, partial pooling, predictive distributions, and A/B comparisons in Python.

On the manufacturing floor, nonconformity rates and downtime are estimated from limited observations, and additional inspections, process changes, and improvement plans are decided. In this article, we will use a fictional precision parts factory as the subject and implement How to Integrate Prior Distribution and Observational Data to Update Uncertainty in a continuous sequence, including Beta–Bernoulli, Gamma–Poisson, MCMC, PyMC, hierarchical Bayesian, predictive distribution, and Bayesian A/B testing.

The value of Bayesian statistics is not simply about “estimating the average.” It means being able to answer questions directly related to decision-making, such as “The probability that the nonconformity rate exceeds management standards,” “How many incidents will occur next week?” and “The probability that improvement proposals will be better than the current ones.” On the other hand, since results depend on prior distributions and model assumptions, sensitivity analysis and operational design are essential.

[!NOTE] This material is a notebook previously used by Surikobo (or personally by its representative, Hiroshi Wayama), and has been reconstructed, edited, and published with the company’s permission. All data listed is fictional and has no relation whatsoever to real companies, factories, or figures.

Introduction: Practical Challenges in Manufacturing Covered in This Article

Consider a scenario in a fictional precision valve factory where the quality assurance department and equipment maintenance team decide on measures for the following month. The Quality Assurance Department determines whether seal nonconformities have decreased under the new cleaning conditions, and the maintenance team estimates the number of personnel needed to prepare for sudden shutdowns. Additionally, a system is needed to compare the performance of multiple factories and avoid excessively evaluating or criticizing those with limited data.

What is needed here is not a single estimate, but a probability distribution of unknown quantities. Each time observations increase, the distribution is updated, and from that distribution, the probability of exceedance, the number of forecasts, and the probability of the measure being superior are calculated.

Common situations on site

  • Report the nonconformance rate only as one point: ‘Number of Nonconformances ÷ Number of Inspections,’ and compare while ignoring differences in inspection counts.
  • Although there is past experience and knowledge of similar processes, it is only used as a hunch by the person in charge
  • Even when data is added, monthly materials are simply recreated, and update rules are not standardized.
  • Without confirming simulation convergence or effective sample size, only the average value of MCMC is used.
  • They do not distinguish between ‘the improved version seems better’ and ‘the improvements have met investment criteria.‘

Why is this issue so difficult to judge?

Even if there are zero nonconformities, if there are 10 tests, it cannot be said that the “true nonconformance rate is 0%.” Conversely, if you place too much emphasis on past insights, even if data shows process changes, updates will be delayed. Bayes’ theorem states that for an unknown parameter θ\theta,

p(θ∣y)=p(y∣θ)p(θ)p(y)∝p(y∣θ)p(θ)p(\theta\mid y)=\frac{p(y\mid\theta)p(\theta)}{p(y)} \propto p(y\mid\theta)p(\theta)

p(θ)p(\theta) is the prior distribution, p(y∣θ)p(y\mid\theta) is the likelihood, and p(θ∣y)p(\theta\mid y) is the posterior distribution. It is necessary to clearly define not only the formulas but also who and which information the prior distribution represents, whether the data generation process is valid, and which probabilities should be used as decision-making criteria.

Exercises in this article

No.ThemeQuestions in the Manufacturing Industry
071Beta–BernoulliWhat range should the seal nonconformance rate be considered?
072Gamma PoissonWhat is the daily equipment downtime rate?
073Bayes UpdateHow decisions change with each additional lot
074MCMCCan posterior distributions be approximated without using analytical solutions?
075Metropolis-HastingsHow proposal width and acceptance rate affect estimated quality?
076Gibbs SamplingCan you alternate between estimating averages and variation?
077Introduction to PyMCCan declarative models estimate the rate of nonconformance?
078hierarchical BayesHow to stabilize a small amount of data per factory
079Posterior Predictive DistributionWhat is the probability that the next test will exceed the standard?
080Bayesian A/B TestIs there sufficient grounds for implementing the new conditions?

Preparing the Python environment

Run the cells from top to bottom. [IN: n] and [OUT: n] identify the input and output of the same cell. Expand the preparation sections to see their code. Timings and the last digits of stochastic estimates can vary by environment.

NumPy is used for random number and numerical calculations, pandas for tables, SciPy for probability distributions, matplotlib for visualization, and No.077 uses PyMC. Japanese use japanize_matplotlib for display. External data and seaborn are not used. Fix random seed numbers so that the same results can be reproduced in the same environment.

Imports and display settings
[IN: 1]
import platform
import warnings

import numpy as np
import pandas as pd

pd.set_option("display.max_colwidth", None)
import scipy
from scipy import stats
from scipy.special import betaln, expit
import matplotlib
import matplotlib.pyplot as plt
import japanize_matplotlib
import pytensor
pytensor.config.cxx = ""  # Uses a Python implementation that can run even in environments without a C++ toolchain.
import pymc as pm
from IPython.display import display

warnings.filterwarnings("ignore", category=FutureWarning)
pd.set_option("display.precision", 4)
plt.rcParams["figure.figsize"] = (8, 4.5)
plt.rcParams["axes.unicode_minus"] = False

SEED = 20260711
rng = np.random.default_rng(SEED)

pd.DataFrame({
    "item": ["Python", "NumPy", "pandas", "SciPy", "matplotlib", "PyMC", "Random seed"],
    "value": [platform.python_version(), np.__version__, pd.__version__, scipy.__version__,
           matplotlib.__version__, pm.__version__, SEED],
})
[OUT: 1]
item value
0 Python 3.14.4
1 NumPy 2.5.1
2 pandas 3.0.3
3 SciPy 1.18.0
4 matplotlib 3.11.0
5 PyMC 5.26.1
6 Random seed 20260711

Creation of Fictional Data

Assuming multiple decisions within the same factory, (1) seal inspection for 12 lots, (2) number of equipment stoppages over 35 days, (3) filling time, (4) inspection records of 6 factories, and (5) comparative data between current and new cleaning conditions. In practice, it is confirmed that processes, equipment, materials, and measurement methods have not changed during the period, and if there are changes, stratification or time-varying models are considered.

Generate and inspect the fictional data
[IN: 2]
# Seal inspection: The number of inspections varies by lot
lot_n = rng.integers(45, 76, size=12)
lot_defects = rng.binomial(lot_n, 0.038)
inspection_df = pd.DataFrame({
    "lot": [f"L{i:02d}" for i in range(1, 13)],
    "number_of_inspections": lot_n,
    "non_conforming_number": lot_defects,
})
inspection_df["nonconformity rate"] = inspection_df["non_conforming_number"] / inspection_df["number_of_inspections"]

# Equipment shutdown, filling time, factory-specific inspections, A/B comparison
daily_stops = rng.poisson(1.35, size=35)
fill_time = rng.normal(42.4, 1.8, size=45)
plant_names = ["Tohoku", "Kanto", "Chubu", "Kansai", "Chugoku", "Kyushu"]
plant_n = np.array([80, 520, 140, 950, 65, 300])
plant_true_p = np.array([0.042, 0.031, 0.050, 0.027, 0.060, 0.036])
plant_k = rng.binomial(plant_n, plant_true_p)
ab_n = {"Current Conditions": 650, "New Cleaning Conditions": 620}
ab_k = {
    "Current Conditions": rng.binomial(ab_n["Current Conditions"], 0.046),
    "New Cleaning Conditions": rng.binomial(ab_n["New Cleaning Conditions"], 0.028),
}

display(inspection_df)
display(pd.DataFrame({
    "Data": ["equipment shutdown", "filling time", "Factory-specific inspections", "A/BComparison"],
    "Observation Scale": [f"{len(daily_stops)}days", f"{len(fill_time)}units", f"{len(plant_names)}Factory",
             f"{sum(ab_n.values())}units"],
}))
[OUT: 2]
lot number_of_inspections non_conforming_number nonconformity rate
0 L01 71 1 0.0141
1 L02 51 4 0.0784
2 L03 50 2 0.0400
3 L04 73 2 0.0274
4 L05 47 3 0.0638
5 L06 61 2 0.0328
6 L07 64 3 0.0469
7 L08 65 3 0.0462
8 L09 72 3 0.0417
9 L10 71 1 0.0141
10 L11 55 2 0.0364
11 L12 51 3 0.0588
Data Observation Scale
0 equipment shutdown 35days
1 filling time 45units
2 Factory-specific inspections 6Factory
3 A/BComparison 1270units

No.071: Beta–Bernoulli — Estimating the Rate of Nonconformity by Distribution

Practical use

If individual products are represented as conformity 0 or non-conformance 1, the unknown nonconformance rate pp can be treated as the Bernoulli probability. In addition to point estimation, you can determine the priority for additional tests and cause investigations by determining a credible interval for pp and the ‘probability of exceeding the 3% management standard.‘

Model and assumptions

Use the prior p∼Beta(α0,β0)p\sim\mathrm{Beta}(\alpha_0,\beta_0). After observing kk nonconforming items in nn inspections, conjugacy gives

p∣k,n∼Beta(α0+k, β0+n−k)p\mid k,n\sim\mathrm{Beta}(\alpha_0+k,\ \beta_0+n-k)

Here, we place the uniform prior distribution Beta(1,1)\mathrm{Beta}(1,1). When using past results in practice, the period, equipment, and product families used as the basis should be listed together with the effective sample size α0+β0\alpha_0+\beta_0 of the prior distribution.

Python example

[IN: 3]
n_total = int(inspection_df["number_of_inspections"].sum())
k_total = int(inspection_df["non_conforming_number"].sum())
a0, b0 = 1.0, 1.0
a_post, b_post = a0 + k_total, b0 + n_total - k_total
ci71 = stats.beta.ppf([0.025, 0.975], a_post, b_post)
threshold = 0.03

beta_summary = pd.DataFrame({
    "indicator": ["number_of_inspections", "non_conforming_number", "Observation rate", "Posterior average", "95% credible interval: lower", "95% credible interval: upper", "P(p > 3%)"],
    "value": [n_total, k_total, k_total/n_total, a_post/(a_post+b_post), *ci71,
           stats.beta.sf(threshold, a_post, b_post)],
})
display(beta_summary)

x = np.linspace(0, 0.09, 500)
plt.plot(x, stats.beta.pdf(x, a0, b0), label="prior distribution Beta(1, 1)")
plt.plot(x, stats.beta.pdf(x, a_post, b_post), label="Posterior distribution", linewidth=2)
plt.axvline(threshold, color="crimson", linestyle="--", label="management standard 3%")
plt.title("Prior and posterior distribution of seal nonconformance rates")
plt.xlabel("nonconformity rate p")
plt.ylabel("probability density")
plt.grid(alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 3]
indicator value
0 number_of_inspections 731.0000
1 non_conforming_number 29.0000
2 Observation rate 0.0397
3 Posterior average 0.0409
4 95% credible interval: lower 0.0278
5 95% credible interval: upper 0.0564
6 P(p > 3%) 0.9437

Prior and posterior distribution of seal nonconformance rates

Interpreting the results

The posterior mean is a smoothed estimate of the defect probability. Under the stated prior and model, the equal-tailed 95% credible interval contains 95% of its posterior probability. The posterior probability that the defect rate exceeds 3% can guide additional inspections and investigations. This conclusion assumes a common defect probability across the lots.

No.072: Gamma–Poisson — Updating the Rate of Equipment Outages

Practical use

By modeling the daily number of sudden shutdowns, you can probabilistically estimate the required amounts of maintenance shifts, spare parts, and recovery support. To handle not only the presence or absence of stops but also the number of cases, the incidence rate λ\lambda is estimated over a certain period.

Model and assumptions

For daily counts yt∼Poisson⁡(λ)y_t\sim\operatorname{Poisson}(\lambda), use a Gamma prior with shape a0a_0 and rate b0b_0. After TT days,

λ∣y∼Gamma⁡(a0+∑t=1Tyt, b0+T).\lambda\mid y\sim\operatorname{Gamma}\left(a_0+\sum_{t=1}^{T}y_t,\ b_0+T\right).

SciPy parameterizes the Gamma distribution with scale, so pass the reciprocal of the rate, 1/b1/b. The model assumes a constant rate over equal observation periods.

Python example

[IN: 4]
ga, gb = 2.0, 1.5  # Average advance rate: 1.33 cases/day
ga_post = ga + daily_stops.sum()
gb_post = gb + len(daily_stops)
lambda_mean = ga_post / gb_post
lambda_ci = stats.gamma.ppf([0.025, 0.975], ga_post, scale=1/gb_post)

display(pd.DataFrame({
    "indicator": ["Observation Days", "Total number of stoppages", "specimen mean", "Posterior average", "95% credible interval: lower", "95% credible interval: upper"],
    "value": [len(daily_stops), daily_stops.sum(), daily_stops.mean(), lambda_mean, *lambda_ci],
}))

lam_x = np.linspace(0.3, 2.8, 500)
plt.plot(lam_x, stats.gamma.pdf(lam_x, ga, scale=1/gb), label="prior distribution")
plt.plot(lam_x, stats.gamma.pdf(lam_x, ga_post, scale=1/gb_post), label="Posterior distribution", linewidth=2)
plt.title("Bayesian update of the daily equipment stop rate")
plt.xlabel("Stop rate λ (events/day)")
plt.ylabel("probability density")
plt.grid(alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 4]
indicator value
0 Observation Days 35.0000
1 Total number of stoppages 51.0000
2 specimen mean 1.4571
3 Posterior average 1.4521
4 95% credible interval: lower 1.0877
5 95% credible interval: upper 1.8682

Bayesian update of the daily equipment stop rate

Interpreting the results

The close sampling mean and posterior mean are because 35 days of observations are more effective than weak prior data. Credible intervals indicate the dangers of fixing a maintenance plan at a single point. However, the Poisson distribution assumes a constant daily incidence rate and equal variance with the mean. If overdispersion occurs in the chain of days of the week, variety, uptime, or failures, it is extended to negative binomial models or models that include covariates.

No.073: Bayes Update — Update judgments every lot arrival

Practical use

Not only can all data be consolidated at the end of the month, but by updating the distribution of nonconformance rates after each lot inspection, you can speed up decisions on additional inspections or process stoppages. By keeping update history, you can explain when and on what data your decisions changed.

Model and assumptions

The beta distribution parameters can be interpreted as the “number of pseudo-cases on the non-conforming side” and “number of pseudo-cases on the conforming side.” Sequential updates that change the posterior distribution from the previous lot to the prior distribution of the next lot matches the calculation of updating all lots at once. Here, we record the posterior mean, the 95% credible interval, and the P(p>3%)P(p>3\%) trends.

Python example

[IN: 5]
a_seq, b_seq = a0, b0
update_rows = []
for row in inspection_df.itertuples(index=False):
    a_seq += row.non_conforming_number
    b_seq += row.number_of_inspections - row.non_conforming_number
    lo, hi = stats.beta.ppf([0.025, 0.975], a_seq, b_seq)
    update_rows.append({
        "lot": row.lot, "Cumulative number of tests": int(a_seq + b_seq - a0 - b0),
        "Posterior average": a_seq/(a_seq+b_seq), "lower_limit": lo, "upper": hi,
        "P(p>3%)": stats.beta.sf(0.03, a_seq, b_seq),
    })
update_df = pd.DataFrame(update_rows)
display(update_df)
print("Parameter matching between batch and sequential updates:", (a_seq, b_seq) == (a_post, b_post))

idx = np.arange(1, len(update_df)+1)
plt.plot(idx, update_df["Posterior average"], marker="o", label="Posterior average")
plt.fill_between(idx, update_df["lower_limit"], update_df["upper"], alpha=0.22, label="95% credible interval")
plt.axhline(0.03, color="crimson", linestyle="--", label="management standard 3%")
plt.title("Update of nonconformity rate estimates due to additional lots")
plt.xlabel("Number of lots incorporated")
plt.ylabel("nonconformity rate")
plt.grid(alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 5]
lot Cumulative number of tests Posterior average lower_limit upper P(p>3%)
0 L01 71 0.0274 0.0034 0.0750 0.3600
1 L02 122 0.0484 0.0181 0.0923 0.8344
2 L03 172 0.0460 0.0202 0.0816 0.8492
3 L04 245 0.0405 0.0197 0.0683 0.7928
4 L05 292 0.0442 0.0238 0.0704 0.8938
5 L06 353 0.0423 0.0239 0.0655 0.8836
6 L07 417 0.0430 0.0257 0.0643 0.9172
7 L08 482 0.0434 0.0271 0.0632 0.9395
8 L09 554 0.0432 0.0279 0.0615 0.9499
9 L10 625 0.0399 0.0260 0.0565 0.9060
10 L11 680 0.0396 0.0263 0.0554 0.9097
11 L12 731 0.0409 0.0278 0.0564 0.9437
Parameter matching between batch and sequential updates: True

Update of nonconformity rate estimates due to additional lots

Interpreting the results

Initially, the credible interval is wide, and estimates can move significantly with a few nonconformities. As the number of inspections accumulates, the section narrows, and the impact of the last lot in the second half becomes smaller. Matching a bulk update is done by implementation check. Practical stoppage rules incorporate not only single-point exceedance, but also how many consecutive times the probability has crossed the threshold, and whether the stoppage loss or outflow loss is greater.

No.074: MCMC — Approximate posterior distribution using samples

Practical use

In complex models where conjugate distributions cannot be used, it is not possible to calculate the mean or interval of the posterior distribution using formulas. Markov Chain Monte Carlo (MCMC) creates samples from chains with a stationary distribution in a posterior distribution to approximate uncertainties such as equipment downtime rates.

Model and assumptions

The gamma posterior distribution of No.072 is deliberately approximated without using analytical solutions, but by the random walk above z=log⁡λz=\log\lambda. The logarithmic density of variable transformations including Jacobian is determined by the constants

log⁡p(z∣y)=apostz−bpostez+C\log p(z\mid y)=a_{post}z-b_{post}e^z + C

Except for burn-ins, check traces, autocorrelations, and Monte Carlo standard errors.

Python example

[IN: 6]
def log_target_z(z):
    return ga_post * z - gb_post * np.exp(z)

mcmc_rng = np.random.default_rng(SEED + 74)
n_iter, burn = 16000, 3000
z = np.log(lambda_mean)
z_trace = np.empty(n_iter)
accepted = 0
for i in range(n_iter):
    proposal = z + mcmc_rng.normal(0, 0.22)
    if np.log(mcmc_rng.random()) < log_target_z(proposal) - log_target_z(z):
        z = proposal
        accepted += 1
    z_trace[i] = z

lambda_trace = np.exp(z_trace[burn:])
def autocorr(x, max_lag=40):
    centered = x - x.mean()
    ac = np.correlate(centered, centered, mode="full")[len(x)-1:len(x)+max_lag]
    return ac / ac[0]

acf = autocorr(lambda_trace)
# Illustrative finite-lag approximation, not a standard ESS estimator.
# Use established diagnostics such as ArviZ for practical analysis.
positive_acf = acf[1:][acf[1:] > 0]
ess_approx = len(lambda_trace) / (1 + 2 * positive_acf.sum())
mcmc_summary = pd.DataFrame({
    "indicator": ["acceptance rate", "MCMCaverage", "Analytic posterior mean", "MCMC 2.5%", "MCMC 97.5%", "Approximate ESS", "Monte Carlo standard error"],
    "value": [accepted/n_iter, lambda_trace.mean(), lambda_mean,
           *np.quantile(lambda_trace, [0.025, 0.975]), ess_approx,
           lambda_trace.std(ddof=1)/np.sqrt(ess_approx)],
})
display(mcmc_summary)

fig, axes = plt.subplots(1, 2, figsize=(11, 4))
axes[0].plot(lambda_trace[:2500], linewidth=0.6)
axes[0].set_title("MCMC trace: first 2,500 retained draws")
axes[0].set_xlabel("Iteration")
axes[0].set_ylabel("Stop rate λ")
axes[0].grid(alpha=0.3)
axes[1].bar(np.arange(1, 21), acf[1:21])
axes[1].set_title("Sample autocorrelation")
axes[1].set_xlabel("Lag")
axes[1].set_ylabel("autocorrelation")
axes[1].grid(alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 6]
indicator value
0 acceptance rate 0.5665
1 MCMCaverage 1.4504
2 Analytic posterior mean 1.4521
3 MCMC 2.5% 1.0835
4 MCMC 97.5% 1.8838
5 Approximate ESS 2044.2499
6 Monte Carlo standard error 0.0045

MCMC trace: first 2,500 retained draws

Interpreting the results

Agreement with the analytic posterior mean is an implementation check for this example. Positive autocorrelation reduces the information in the retained draws, so the effective sample size (ESS) here is smaller than the draw count. Negative autocorrelation can produce ESS greater than the draw count in other samplers. The code uses an illustrative approximation based on positive autocorrelations over a finite lag range; use established diagnostics such as ArviZ for analysis. Check multiple chains, R^\hat R, ESS, traces, and Monte Carlo error before reporting probabilities.

No.075: Metropolis-Hastings — Diagnosing Proposal Breadth and Acceptance Rate

Practical use

The Metropolis-Hastings (MH) method is the fundamental MCMC technique that proposes candidates based on current values and determines adoption based on posterior probability ratios. If the proposal is too narrow, even if it is almost accepted, exploration slows down; if it is too large, rejections increase and the chain of action stops.

Model and assumptions

To always keep the nonconformance rate between 0 and 1, use symmetric normal proposals on the z=logit(p)z=\mathrm{logit}(p). The logarithmic target density combining the beta posterior distribution and the Jacobian is

log⁡p(z∣y)=apostlog⁡p+bpostlog⁡(1−p)+C,p=logit−1(z)\log p(z\mid y)=a_{post}\log p+b_{post}\log(1-p)+C,\quad p=\mathrm{logit}^{-1}(z)

We compare the range of multiple proposals and check not only the acceptance rate but also the error from ESS and the analysis solution.

Python example

[IN: 7]
def mh_beta(proposal_sd, seed, n_iter=12000, burn=2000):
    local_rng = np.random.default_rng(seed)
    z = np.log((a_post/(a_post+b_post)) / (1-a_post/(a_post+b_post)))
    out = np.empty(n_iter)
    accepted = 0
    def log_target(logit_p):
        p = expit(logit_p)
        return a_post*np.log(p) + b_post*np.log1p(-p)
    for i in range(n_iter):
        prop = z + local_rng.normal(0, proposal_sd)
        if np.log(local_rng.random()) < log_target(prop) - log_target(z):
            z = prop
            accepted += 1
        out[i] = expit(z)
    sample = out[burn:]
    ac = autocorr(sample, 100)
    pos = ac[1:][ac[1:] > 0]
    ess = len(sample)/(1+2*pos.sum())
    return sample, accepted/n_iter, ess

mh_rows = []
mh_samples = {}
for j, proposal_sd in enumerate([0.05, 0.35, 2.0]):
    sample, acc, ess = mh_beta(proposal_sd, SEED + 750 + j)
    mh_samples[proposal_sd] = sample
    mh_rows.append({"Proposal Width": proposal_sd, "acceptance rate": acc, "Posterior average": sample.mean(),
                    "Differences from analytical solutions": sample.mean()-a_post/(a_post+b_post), "Approximate ESS": ess})
mh_df = pd.DataFrame(mh_rows)
display(mh_df)

for proposal_sd, sample in mh_samples.items():
    plt.plot(sample[:1000], linewidth=0.7, label=f"Proposal Width={proposal_sd}")
plt.title("Metropolis–Hastings traces by proposal width")
plt.xlabel("Iteration")
plt.ylabel("nonconformity rate p")
plt.grid(alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 7]
Proposal Width acceptance rate Posterior average Differences from analytical solutions Approximate ESS
0 0.05 0.9097 0.0406 -0.0003 138.5534
1 0.35 0.5183 0.0412 0.0003 1814.6552
2 2.00 0.1148 0.0408 -0.0001 845.5656

Metropolis–Hastings traces by proposal width

Interpreting the results

Even with a high acceptance rate, proposals with a small range only move gradually, and the ESS does not grow. A larger proposal width increases the number of intervals where the value remains the same. The intermediate width is efficient in this example, but there is no universal acceptance rate; it varies depending on dimension and target distribution. Acceptance rate alone is not a KPI; instead, ESS/sec, multi-chain matching, and Monte Carlo error for estimation are managed together.

No.076: Gibbs Sampling — Alternating between average and variation in filling times

Practical use

Both the process center μ\mu and precision τ=1/σ2\tau=1/\sigma^2 of filling time are unknown. If you can sample directly from the conditional distribution of the other by fixing one, you can alternate updates with Gibbs Sampling.

Model and assumptions

xi∼N(μ,τ−1)x_i\sim\mathcal N(\mu,\tau^{-1}), a conjugate Normal-Gamma prior distribution

μ∣τ∼N(μ0,(κ0τ)−1),τ∼Gamma(a0,b0)\mu\mid\tau\sim\mathcal N\left(\mu_0,(\kappa_0\tau)^{-1}\right),\qquad \tau\sim\mathrm{Gamma}(a_0,b_0)

Alternate μ∣τ,x\mu\mid\tau,x and τ∣μ,x\tau\mid\mu,x. Assuming independent normal data without chronological drift is confirmed in practice using control charts and residuals.

Python example

[IN: 8]
gibbs_rng = np.random.default_rng(SEED + 76)
mu0, kappa0, shape0, rate0 = 42.0, 0.5, 2.0, 4.0
n = len(fill_time)
n_gibbs, gibbs_burn = 14000, 2000
mu, tau = fill_time.mean(), 1/fill_time.var()
mu_chain = np.empty(n_gibbs)
sigma_chain = np.empty(n_gibbs)

for i in range(n_gibbs):
    # tau | mu, x
    shape = shape0 + (n + 1)/2
    rate = rate0 + 0.5*np.sum((fill_time-mu)**2) + 0.5*kappa0*(mu-mu0)**2
    tau = gibbs_rng.gamma(shape, 1/rate)
    # mu | tau, x
    kappa_n = kappa0 + n
    mu_n = (kappa0*mu0 + n*fill_time.mean())/kappa_n
    mu = gibbs_rng.normal(mu_n, np.sqrt(1/(kappa_n*tau)))
    mu_chain[i] = mu
    sigma_chain[i] = 1/np.sqrt(tau)

mu_sample = mu_chain[gibbs_burn:]
sigma_sample = sigma_chain[gibbs_burn:]
display(pd.DataFrame({
    "Estimated Subject": ["Average Filling Time μ", "standard_deviation σ"],
    "Posterior average": [mu_sample.mean(), sigma_sample.mean()],
    "95%lower_limit": [np.quantile(mu_sample, .025), np.quantile(sigma_sample, .025)],
    "95%upper": [np.quantile(mu_sample, .975), np.quantile(sigma_sample, .975)],
}))

fig, axes = plt.subplots(1, 2, figsize=(11, 4))
axes[0].plot(mu_sample[:2000], linewidth=0.6)
axes[0].set_title("Average Filling Time μ Trace")
axes[0].set_xlabel("Iteration")
axes[0].set_ylabel("seconds")
axes[0].grid(alpha=0.3)
axes[1].scatter(mu_sample[::10], sigma_sample[::10], s=7, alpha=0.25)
axes[1].set_title("Joint posterior draws of μ and σ")
axes[1].set_xlabel("Mean μ (seconds)")
axes[1].set_ylabel("Standard deviation σ (seconds)")
axes[1].grid(alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 8]
Estimated Subject Posterior average 95%lower_limit 95%upper
0 Average Filling Time μ 42.6703 42.0901 43.2449
1 standard_deviation σ 1.9306 1.5955 2.3738

Average Filling Time μ Trace

Interpreting the results

Since the mean and standard deviation are kept as joint distributions rather than fixed values, it is possible to separate and evaluate the possibility of “an increased mean” from a “possibility of increased variation.” Even with the Gibbs method, the specimen is not independent, so diagnosis is necessary. Also, the standard deviation of a standard model does not automatically separate short-term fluctuations from long-term drifts. Record time, equipment, and product types, and expand to hierarchical or chronological models as needed.

No.077: Introduction to PyMC — Declare Models and Estimate Nonconformance Rates

Practical use

In PyMC, the relationship between random variables and observations is declared as a model, and posterior distributions can be calculated using MCMC such as NUTS. A key advantage is that it is easy to gradually extend to models that include non-conjugate regression, hierarchical structures, and measurement errors.

Model and assumptions

Represent the same model as No.071 in PyMC: p∼Beta⁡(1,1)p\sim\operatorname{Beta}(1,1) and k∼Binomial⁡(n,p)k\sim\operatorname{Binomial}(n,p). Compare it with the analytic posterior to check the implementation. For NUTS, target_accept is the target acceptance probability used when adapting the step size; it is not a measure of model validity. The default summary columns hdi_3% and hdi_97% give the endpoints of a 94% highest-density interval, which differs from the equal-tailed 95% interval in No.071.

Python example

[IN: 9]
with pm.Model() as defect_model:
    p = pm.Beta("p", alpha=a0, beta=b0)
    observed = pm.Binomial("observed", n=n_total, p=p, observed=k_total)
    idata = pm.sample(
        draws=1000, tune=1000, chains=2, cores=1,
        random_seed=SEED + 77,
        target_accept=0.9, progressbar=False,
        compute_convergence_checks=True,
    )

pymc_sample = idata.posterior["p"].values.reshape(-1)
pymc_summary = pm.stats.summary(idata, var_names=["p"], kind="all", round_to=5)
display(pymc_summary)
print(f"Analytic Post-Hidharma Average: {a_post/(a_post+b_post):.5f}")
print(f"PyMCPosterior average  : {pymc_sample.mean():.5f}")

plt.hist(pymc_sample, bins=35, density=True, alpha=0.55, label="PyMC draws")
plt.plot(x, stats.beta.pdf(x, a_post, b_post), linewidth=2, label="Analytic beta posterior")
plt.title("PyMC draws and the analytic posterior")
plt.xlabel("nonconformity rate p")
plt.ylabel("probability density")
plt.grid(alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 9]
mean sd hdi_3% hdi_97% mcse_mean mcse_sd ess_bulk ess_tail r_hat
p 0.041 0.0073 0.0282 0.0549 0.0003 0.0001 736.415 1017.243 1.0014
Analytic Post-Hidharma Average: 0.04093
PyMCPosterior average  : 0.04103

PyMC draws and the analytic posterior

This example uses two chains. For practical analysis, consider at least four and inspect R-hat, ESS, and divergences.

Interpreting the results

If the posterior mean of PyMC and the analysis solution are close to the Monte Carlo margin of error, and the histogram overlaps, the basic implementation can be checked. Check whether r_hat is close to 1 and whether ess_bulk and ess_tail are sufficient. In production, it is necessary to store model code, data versions, PyMC versions, seeds, chain counts, and diagnostic values, and not distribute only results without distributing warnings or divergence.

No.078: Hierarchical Bayes — Partially pooling factory-specific nonconformance rates

Practical use

When ranking nonconformity rates by factory by raw ratio, factories with fewer inspections tend to have 0% or higher rates, mistaking chance for a difference in ability. Hierarchical Bayes retains information about each factory while borrowing information from a distribution common to all factories.

Model and assumptions

For factory jj, use

kj∼Binomial⁡(nj,pj),pj∼Beta⁡(mκ,(1−m)κ).k_j\sim\operatorname{Binomial}(n_j,p_j),\qquad p_j\sim\operatorname{Beta}(m\kappa,(1-m)\kappa).

Here mm is the overall rate and κ\kappa controls how strongly factory rates concentrate around it. Use a uniform prior for mm on 0.008–0.10 and an exponential prior with scale 60, truncated to 4–180, for κ\kappa. Evaluate the beta-binomial marginal likelihood on a grid and average over the hyperparameters rather than fixing them at point estimates. Because the κ\kappa grid is logarithmically spaced, multiply each density by its grid width before normalizing. Check sensitivity to the grid limits, spacing, and priors.

Python example

[IN: 10]
m_grid = np.linspace(0.008, 0.10, 90)
kappa_grid = np.geomspace(4, 180, 80)
M, KAPPA = np.meshgrid(m_grid, kappa_grid, indexing="ij")
ALPHA = M*KAPPA
BETA = (1-M)*KAPPA

# Integrate the density with grid widths because kappa is log-spaced.
# The constant width of the m grid cancels during normalization.
log_weight = -KAPPA / 60 + np.log(np.gradient(kappa_grid))[None, :]
for kj, nj in zip(plant_k, plant_n):
    log_weight += betaln(ALPHA+kj, BETA+nj-kj) - betaln(ALPHA, BETA)
log_weight -= log_weight.max()
weight = np.exp(log_weight)
weight /= weight.sum()

flat_rng = np.random.default_rng(SEED + 78)
grid_indices = flat_rng.choice(weight.size, size=12000, p=weight.ravel())
alpha_draw = ALPHA.ravel()[grid_indices]
beta_draw = BETA.ravel()[grid_indices]
hier_draws = np.column_stack([
    flat_rng.beta(alpha_draw+kj, beta_draw+nj-kj)
    for kj, nj in zip(plant_k, plant_n)
])

plant_df = pd.DataFrame({
    "Factory": plant_names,
    "number_of_inspections": plant_n,
    "non_conforming_number": plant_k,
    "Raw nonconformity rate": plant_k/plant_n,
    "Hierarchical Post-Hierarchy Average": hier_draws.mean(axis=0),
    "95%lower_limit": np.quantile(hier_draws, .025, axis=0),
    "95%upper": np.quantile(hier_draws, .975, axis=0),
})
display(plant_df)

pos = np.arange(len(plant_names))
plt.scatter(pos, plant_df["Raw nonconformity rate"], marker="x", s=70, label="Raw nonconformity rate")
plt.errorbar(pos, plant_df["Hierarchical Post-Hierarchy Average"],
             yerr=[plant_df["Hierarchical Post-Hierarchy Average"]-plant_df["95%lower_limit"],
                   plant_df["95%upper"]-plant_df["Hierarchical Post-Hierarchy Average"]],
             fmt="o", capsize=4, label="Hierarchical mean and 95% interval")
plt.xticks(pos, plant_names)
plt.title("Partial pooling of factory-specific nonconformance rates")
plt.xlabel("Factory")
plt.ylabel("nonconformity rate")
plt.grid(alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 10]
Factory number_of_inspections non_conforming_number Raw nonconformity rate Hierarchical Post-Hierarchy Average 95%lower_limit 95%upper
0 Tohoku 80 3 0.0375 0.0421 0.0160 0.0790
1 Kanto 520 16 0.0308 0.0330 0.0202 0.0488
2 Chubu 140 9 0.0643 0.0572 0.0298 0.0937
3 Kansai 950 27 0.0284 0.0299 0.0202 0.0412
4 Chugoku 65 3 0.0462 0.0467 0.0181 0.0888
5 Kyushu 300 15 0.0500 0.0492 0.0296 0.0732

Partial pooling of factory-specific nonconformance rates

Interpreting the results

Factories with fewer inspections are generally pulled more strongly toward the shared rate and retain wider uncertainty intervals. Partial pooling reduces the influence of small-sample extremes without assuming all factories have identical rates. Before comparing factories, check product mix, process difficulty, and measurement standards. Factories that cannot reasonably share a population distribution should be modeled separately.

No.079: Posterior Predictive Distribution — Predicting the Number of Nonconformities in the Next Inspection

Practical use

Quality assurance wants to know not only the true nonconformity rate, but also “how many nonconformities will appear in the next 200 units” and “what is the risk of the observed rate reaching 5% or more.” Predicted distributions include both parameter uncertainty and future random variations.

Model and assumptions

Given p∣y∼Beta⁡(a,b)p\mid y\sim\operatorname{Beta}(a,b), the number of nonconforming items k~\tilde k in the next mm inspections has a beta-binomial distribution:

p(k~∣y)=(mk~)B(a+k~,b+m−k~)B(a,b).p(\tilde k\mid y)=\binom m{\tilde k}\frac{B(a+\tilde k,b+m-\tilde k)}{B(a,b)}.

This distribution includes both uncertainty in pp and future sampling variation. It is more dispersed than a binomial model that fixes pp at the posterior mean.

Python example

[IN: 11]
future_n = 200
future_k = np.arange(future_n+1)
pred_pmf = stats.betabinom.pmf(future_k, future_n, a_post, b_post)
pred_interval = stats.betabinom.ppf([0.025, 0.975], future_n, a_post, b_post).astype(int)
risk_5pct = stats.betabinom.sf(9, future_n, a_post, b_post)  # 10 or more

display(pd.DataFrame({
    "indicator": ["Number of future inspections", "Predictive mean defect count", "95% predictive interval: lower", "95% predictive interval: upper", "P(count >= 10; rate >= 5%)"],
    "value": [future_n, future_n*a_post/(a_post+b_post), *pred_interval, risk_5pct],
}))

show = future_k <= 25
plt.bar(future_k[show], pred_pmf[show], alpha=0.75)
plt.axvline(10, color="crimson", linestyle="--", label="10 defects = 5%")
plt.title("Predictive defect count in the next 200 inspections")
plt.xlabel("Future Nonconformity Count")
plt.ylabel("Prediction Probability")
plt.grid(axis="y", alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 11]
indicator value
0 Number of future inspections 200.0000
1 Predictive mean defect count 8.1855
2 95% predictive interval: lower 3.0000
3 95% predictive interval: upper 15.0000
4 P(count >= 10; rate >= 5%) 0.3164

Predictive defect count in the next 200 inspections

Interpreting the results

The 95% predictive interval describes possible counts in the next inspection. The probability of at least 10 defects in 200 items is the probability that the observed rate is at least 5%, and can guide staffing or isolation capacity. A credible interval describes an unknown parameter; a predictive interval describes future observations. Check predictive performance against subsequent inspection results.

No.080: Bayesian A/B Test — Determining the Deployment of New Cleaning Conditions

Practical use

When comparing the current Condition A and the new Cleanup Condition B, evaluate not only the “difference” but also the probability that B will outperform A, the probability that the improvement will meet investment criteria, and the loss if misdeployed. This allows us to distinguish between statistical differences and those that are meaningful for business purposes.

Model and assumptions

By placing an independent Beta(1,1)\mathrm{Beta}(1,1) prior distribution for the nonconformity rates of both conditions, each posterior distribution can be analytically obtained. From the posterior sample, the difference Δ=pA−pB\Delta=p_A-p_B is calculated, and P(Δ>0)P(\Delta>0) and P(Δ>0.01)P(\Delta>0.01) for the minimum practical deviation of 1 point are obtained. In actual A/B, variety, material lots, equipment, and duration are randomized or adjusted to avoid confounding.

Python example

[IN: 12]
ab_rng = np.random.default_rng(SEED + 80)
draws = 100000
p_a = ab_rng.beta(1+ab_k["Current Conditions"], 1+ab_n["Current Conditions"]-ab_k["Current Conditions"], draws)
p_b = ab_rng.beta(1+ab_k["New Cleaning Conditions"], 1+ab_n["New Cleaning Conditions"]-ab_k["New Cleaning Conditions"], draws)
delta = p_a - p_b

ab_table = pd.DataFrame({
    "condition": ["Current ConditionsA", "New Cleaning ConditionsB"],
    "number_of_inspections": [ab_n["Current Conditions"], ab_n["New Cleaning Conditions"]],
    "non_conforming_number": [ab_k["Current Conditions"], ab_k["New Cleaning Conditions"]],
    "Observed nonconformity rate": [ab_k["Current Conditions"]/ab_n["Current Conditions"], ab_k["New Cleaning Conditions"]/ab_n["New Cleaning Conditions"]],
    "Posterior average": [p_a.mean(), p_b.mean()],
})
display(ab_table)
display(pd.DataFrame({
    "judgment indicator": ["P(p_B < p_A)", "P(improvement > 1 percentage point)", "Posterior mean improvement", "95% credible interval for improvement"],
    "value": [f"{np.mean(delta>0):.3%}", f"{np.mean(delta>0.01):.3%}",
           f"{delta.mean():.3%}", f"{np.quantile(delta,.025):.3%} 〜 {np.quantile(delta,.975):.3%}"],
}))

plt.hist(delta*100, bins=60, density=True, alpha=0.75)
plt.axvline(0, color="black", linestyle="--", label="No difference")
plt.axvline(1, color="crimson", linestyle="--", label="Minimum practical difference: 1 percentage point")
plt.title("Posterior reduction in nonconformity rate")
plt.xlabel("improvement margin p(A) - p(B)(Percentage points)")
plt.ylabel("probability density")
plt.grid(alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 12]
condition number_of_inspections non_conforming_number Observed nonconformity rate Posterior average
0 Current ConditionsA 650 32 0.0492 0.0507
1 New Cleaning ConditionsB 620 19 0.0306 0.0322
judgment indicator value
0 P(p_B < p_A) 95.283%
1 P(improvement > 1 percentage point) 77.666%
2 Posterior mean improvement 1.847%
3 95% credible interval for improvement -0.316% 〜 4.062%

Posterior reduction in nonconformity rate

Interpreting the results

The probability that B is lower than A and the probability of improving by more than one point are different. If the former is higher but the latter does not meet investment standards, additional testing rather than full rollout may be appropriate. In the final decision, losses from escaped defects, change costs, and side effects on takt and other quality characteristics are included in the loss function. It is important not to change the probability threshold after seeing the results, and to agree on decision-making rules before collecting data.

Practical lessons

  1. Report uncertainty with probabilities: Setting posterior averages, credible intervals, and the probability of exceeding standards concretely clarifies discussions about additional inspections, suspensions, and deployments.
  2. Making prior distribution auditable: Record how many weights were used for past data, similar processes, and expert judgments.
  3. Distinguishing between forecasting and parameter estimation: Future counts include both uncertainty in process rates and future unexpected fluctuations.
  4. Don’t overrank a minority: Information is shared between factories in a hierarchical model, showing reductions and intervals based on the number of inspections.
  5. Include MCMC diagnostics in deliverables: Not only acceptance rate, but also traces, multiple chains, R^\hat R, ESS, divergence, and MC error are stored.
  6. Converting statistical differences into economic value: In addition to the superior probability of A/B, decisions are made using the minimum practical difference and loss function.

Applying this in practice

  • Definition of decision-making: Decide in advance who will conduct additional inspections, stops, or deployments at what posterior and predictive probabilities.
  • Verification of the Data Generation Process: Record equipment, molds, materials, varieties, shifts, inspectors, and time, and check interchangeability and independence.
  • Governance of the prior distribution: Review the basis, update date, scope, effective sample size, and sensitivity difference from the weak information prior distribution
  • Model Verification: Posterior predictive checks, out-of-time validation, calibration, and cross-referencing with known simple models
  • Managing Computational Quality: Save environment, dependency libraries, seeds, chains, diagnostic values, warnings, and execution logs
  • Connection to operational losses: Quantify losses from missed opportunities, over-stopping, inspections, capital investments, and quality leaks, turning probability into action.
  • Phased Implementation: Reproduce and verify with past data, proceed to shadow operations, trials in limited processes, standardization of work, and regular monitoring.

Before deployment, decide which processes supply the data, how convergence is checked, and who acts when an inspection or stoppage threshold is crossed. If process conditions change, reassess whether the historical data and prior distribution remain applicable.

Conclusion

From No.071 to No.080, we started with conjugate updates for Beta–Bernoulli and Gamma–Poisson, then implemented sequential updates, MCMC, Metropolis-Hastings, Gibbs Sampling, PyMC, hierarchical Bayes, posterior prediction, and Bayesian A/B testing.

The core idea is to integrate past insights and observations under transparent rules, translating unknowns and uncertainties of future outcomes into probabilities that the field can judge. Operate in a state where you can clearly state the prior distribution, likelihood, model structure, computational diagnosis, and loss function, and explain which assumptions the results depend on.

Consulting for businesses

At Surikobo, we support everything from quality data analysis, Bayesian statistical models, hierarchical models, anomaly and failure prediction, experimental design, Python training, and PoC to operational implementation in manufacturing. You can consult with us at stages such as “wanting to utilize a small amount of data,” “determining nonconformance rates and downtime by probabilism,” “making inter-factory comparisons fair,” or “establishing model diagnostics and operational standards.”

📩 Contact Us: www.surikobo.co.jp/contact Please feel free to consult us first.