Skip to content

Instantly share code, notes, and snippets.

@CamDavidsonPilon
Created May 14, 2019 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CamDavidsonPilon/b44a9a5d321bbc7a005af93782903395 to your computer and use it in GitHub Desktop.
Save CamDavidsonPilon/b44a9a5d321bbc7a005af93782903395 to your computer and use it in GitHub Desktop.
from lifelines import WeibullFitter
lambda_, rho_ = 2, 0.5
N = 10_000
T_actual = lambda_ * np.random.exponential(1, size=N)**(1/rho_)
T_censor = lambda_ * np.random.exponential(1, size=N)**(1/rho_)
T = np.minimum(T_actual, T_censor)
E = T_actual < T_censor
# pick a time on the curve to examine.
time = [1.0]
# lifelines computed confidence interval using delta method
print(wf.fit(T, E, timeline=time).confidence_interval_cumulative_hazard_)
bootstrap_samples = 10_000
results = []
for _ in range(bootstrap_samples):
ix = np.random.randint(0, 10_000, 10_000)
wf = WeibullFitter().fit(T[ix], E[ix], timeline=time)
results.append(wf.cumulative_hazard_at_times(time).values[0])
# should converge to the values above
print(np.percentile(results, [2.5, 97.5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment