import arviz as az
import numpy as np
import pymc as pm
rng = np.random.default_rng(6420)
5. Time-to-event Models: Example 2*#
Consider the model
with data
where *’s represent censored points. The goal is to estimate the parameter \(\lambda\) in Bayesian fashion. The likelihood is the Weibull PDF and Survival function for non-censored and censored points, respectively
We can elimate constants, and see that the likelihood is proportional to
Prior of \(\lambda\) is specified as \(Ga(\alpha,\beta)\), which is also simplified
With the joint likelihood and prior, we can find the posterior distribution
And by substituting in the known values, we arrive at
The estimator \(\hat{\lambda}_{bayes}\) is the mean of this Gamma distribution
PyMC implementation#
Now, let’s use PyMC to find the \( \hat{\lambda}_{bayes} \). Note, we are converting between Weibull parameterizations to be compatible with the courses historical solution in WINBUGS.
censored = np.array([np.inf, np.inf, 1, np.inf, 3])
y = np.array([2, 3, 1, 2.5, 3])
with pm.Model() as m:
λ = pm.Gamma("λ", 2, 3)
α = 1.5
β = λ ** (-1 / α)
obs_latent = pm.Weibull.dist(α, β)
likelihood = pm.Censored(
"likelihood", obs_latent, lower=None, upper=censored, observed=y
)
trace = pm.sample(1000, random_seed=rng)
az.summary(trace)
NUTS[nutpie]: [λ]
| mean | sd | eti89_lb | eti89_ub | ess_bulk | ess_tail | r_hat | mcse_mean | mcse_sd | |
|---|---|---|---|---|---|---|---|---|---|
| λ | 0.237 | 0.106 | 0.095 | 0.43 | 1706 | 1704 | 1.00 | 0.0024 | 0.0022 |
%load_ext watermark
%watermark -n -u -v -iv -p pytensor
Last updated: Mon, 19 Oct 2026
Python implementation: CPython
Python version : 3.14.7
IPython version : 9.16.1
pytensor: 3.3.0
arviz: 1.3.0
numpy: 2.4.6
pymc : 6.3.1