import pymc as pm
import numpy as np
import pytensor
import pytensor.tensor as pt
import arviz as az
# Set values for the prior parameters
gamma_mode = 0.5
gamma_variance = 1.0
t_star_mode = 15.0
t_star_variance = 100.0
s_star = 0.88
lambda_ = -pt.log(s_star)
# Definition of prior for gamma
aux1 = 2.0 + (gamma_mode**2.0) / gamma_variance
a = 0.5 * (aux1 + np.sqrt(aux1**2.0 - 4.0))
b = gamma_mode / (a - 1.0)
b1 = 1.0 / b
# Definition of prior for t_star
aux2 = 2.0 + (t_star_mode**2.0) / t_star_variance
ap = 0.5 * (aux2 + np.sqrt(aux2**2.0 - 4.0))
bp = t_star_mode / (ap - 1.0)
b2 = 1.0 / bp
# Define the model
model = pm.Model()
# Observed data (censored)
t = np.array([25.62191781, 25.35616438, 25.34246575, 25.29589041, 24.61643836, 24.34794521, 24.30410959,
24.06027397, 23.81643836, 23.50410959, 23.2109589, 23.14520548, 22.97808219, 22.70410959,
21.90684932, 21.81917808, 21.36986301, 20, 19.5260274])
# Function to calculate log complementary cdf of Weibull distribution
def weibull_lccdf(x, t_star, gamma):
return -(lambda_ * (x / t_star) ** gamma)
with model:
# Definition of priors for gamma and t_star
gamma = pm.Gamma('gamma', alpha=a, beta=1/b)
t_star = pm.Gamma('t_star', alpha=ap, beta=1/bp)
# Bus survivor calculation
surv = pm.Deterministic('surv', pt.exp(-lambda_ * (pt.arange(0, 26) / t_star) ** gamma))
# Define likelihood
y_cens = pm.Potential("y_cens", weibull_lccdf(t, t_star, gamma))
# Sample from the model
trace = pm.sample(draws=10000, tune=1000)
summary = pm.summary(trace)
print(summary)
I wanted to plot the survival fonction “surv” in my code but it is not working. Does anyone can help on this task ?