Skip to content
LumenaryAcademy
Advanced 70 min

Survival analysis

How long until something happens, when some stories have not finished yet — censoring, the Kaplan–Meier estimator, the log-rank test, hazards and the Cox model, in medicine and engineering.

Code tested · 6AI-draftedAwaiting review by a statistician

Your guides: Chloe · The Statistician, Margaret · The Simulator, Gideon · The Sceptic

MotivationH

How long until?

A trial in several hospitals tests whether a new maintenance drug delays relapse in patients recovering from a serious illness. Patients join over two years and the trial closes after three. When it closes, some patients have relapsed, at known times. Many have not: one joined eight months ago and is well; another was well at her last visit before she moved to another city. How long, on average, until relapse?

A water agency in a dry region faces the same question about hand pumps in villages: how long until a pump fails, and does a new seal design make it last longer? When the study ends, many pumps are still working.

These are time-to-event questions, and their data have three features that defeat the methods of earlier lessons. Times are positive and usually skewed, with a long right tail. The interesting quantity is often not the mean but the chance of lasting past a certain time. And, decisively, many observations are incomplete: for the patient who has been well for eight months, we know only that her time to relapse is more than eight months. Throwing such observations away, or pretending they are relapses, gives badly wrong answers. Survival analysis is the set of methods built to use them correctly. The name comes from its origin in mortality tables, but the "event" can be anything that happens once: relapse, machine failure, finding a job, a customer cancelling a subscription, a student completing a degree.

IntuitionH

Censoring, and why it matters

An observation is right-censored when we know only that the event had not happened by some time. In the trial, censoring happens for two reasons: administrative censoring when the study closes, and loss to follow-up when a patient moves away or stops attending. Because patients join at different calendar times, the study's closing date cuts their follow-up at different lengths, as the figure shows.

calendar time0122436study closesmonths since the study openedtime on study0122436months since the patient joined
Eight patients in a trial that recruits for two years and closes at month 36. Filled dots are relapses; open circles are censored observations (still well when the study closed, or, for the fourth patient, moved away). Survival analysis works on the right-hand view, time since each patient joined, where every line starts at zero.

Each patient contributes a pair: a time tit_i and an indicator δi\delta_i, equal to 1 if the event was observed at tit_i and 0 if the observation was censored then. Other kinds of incompleteness exist (left censoring, when the event happened before observation began, and interval censoring, when we know only that it happened between two check-ups), but right censoring is by far the most common, and this lesson deals with it.

The methods below rest on one assumption that deserves to be stated plainly.

Definition— non-informative censoring

Censoring is non-informative (or independent) if, among people with the same covariates, those censored at time tt have the same future risk of the event as those who remain under observation. Being censored must tell us nothing about one's prognosis.

Administrative censoring at the close of a study usually satisfies this. Loss to follow-up may not: if patients stop attending because they feel too ill to travel, the people left in the study are healthier than those who left, and every method in this lesson becomes optimistic.

How badly do the naive approaches fail? The simulation below creates trials of 200 patients with a true median time to relapse of 20 months, staggered entry over two years, closure at month 36 and some random loss to follow-up, and estimates the median three ways.

python
import numpy as np

rng = np.random.default_rng(1958)
true_median = 20.0                                   # months; exponential time to relapse
rate = np.log(2) / true_median

def km_median(time, event):
    """Smallest event time at which the Kaplan-Meier curve reaches 0.5 or below (nan if it never does)."""
    order = np.lexsort((1 - event, time))            # at tied times, events before censorings
    time, event = time[order], event[order]
    at_risk = len(time) - np.arange(len(time))
    s = np.cumprod(1 - event / at_risk)
    hit = np.nonzero(s <= 0.5)[0]
    return time[hit[0]] if hit.size else np.nan

results = {"KM median": [], "median, censored treated as relapses": [], "median of relapses only": []}
share_censored = []
for rep in range(2000):
    n = 200
    entry = rng.uniform(0, 24, n)                    # patients join over two years
    t_relapse = rng.exponential(1 / rate, n)
    t_dropout = rng.exponential(60, n)               # some move away, unrelated to their health
    follow = np.minimum(36 - entry, t_dropout)       # the study closes at month 36
    time = np.minimum(t_relapse, follow)
    event = (t_relapse <= follow).astype(float)
    share_censored.append(1 - event.mean())
    results["KM median"].append(km_median(time, event))
    results["median, censored treated as relapses"].append(np.median(time))
    results["median of relapses only"].append(np.median(time[event == 1]))

print(f"true median {true_median:.1f} months; on average {np.mean(share_censored):.0%} of patients are censored")
for name, est in results.items():
    print(f"{name:38} average {np.nanmean(est):5.1f}   middle 95% of estimates {np.nanpercentile(est, 2.5):5.1f} to {np.nanpercentile(est, 97.5):5.1f}")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

More than half the patients are censored in a typical trial (54%). Treating their censoring times as relapse times gives an average median of 12.8 months; keeping only the patients who relapsed gives 8.3, because a relapse is more likely to be observed when it comes early. Both are far below 20, and both are confidently wrong: their middle 95% of estimates never come near the truth. The Kaplan–Meier estimate, introduced below, averages 20.3. Its spread (16.0 to 26.1) is wider, which is the honest price of having less information.

DefinitionsH

Survival, hazard and cumulative hazard

Let T≥0T \ge 0 be the time to the event, with density ff and distribution function FF.

Definition— survival function and hazard

The survival function is S(t)=P(T>t)=1−F(t)S(t) = P(T > t) = 1 - F(t), the probability of lasting beyond tt. The hazard function is the event rate at time tt among those who have lasted that long, h(t)=lim⁡Δt→0P(t≤T<t+Δt∣T≥t)Δt=f(t)S(t),h(t) = \lim_{\Delta t \to 0} \frac{P(t \le T \lt t + \Delta t \mid T \ge t)}{\Delta t} = \frac{f(t)}{S(t)}, and the cumulative hazard is H(t)=∫0th(u) duH(t) = \int_0^t h(u)\,du. They are linked by S(t)=exp⁡(−H(t))S(t) = \exp\big(-H(t)\big).

The last identity follows because h(t)=f(t)/S(t)=−ddtln⁡S(t)h(t) = f(t)/S(t) = -\frac{d}{dt}\ln S(t); integrating from 0 to tt with S(0)=1S(0) = 1 gives ln⁡S(t)=−H(t)\ln S(t) = -H(t). So any one of SS, hh and HH determines the others (see calculus of one variable for the integration).

The hazard is the most natural way to think about time to an event, because its shape tells a story. A constant hazard λ\lambda gives the exponential distribution, S(t)=e−λtS(t) = e^{-\lambda t}, with median ln⁡2/λ\ln 2/\lambda and mean 1/λ1/\lambda: failure by random shocks, with no ageing. A Weibull distribution, S(t)=exp⁡(−(t/η)k)S(t) = \exp\big(-(t/\eta)^k\big), has hazard proportional to tk−1t^{k-1}: falling if k<1k \lt 1 (early failures from manufacturing defects, or risk just after surgery), constant if k=1k = 1, rising if k>1k \gt 1 (wear-out). Engineers know the combination as the bathtub curve, and human mortality has the same shape over a lifetime: high risk in the first year of life, a long low stretch, then risk rising steadily with age.

constanttimerandom shocksfallingtimeearly failuresrisingtimewear-outbathtubtimea whole lifetime
Four shapes of hazard. The shape matters more than any single summary: two products with the same mean lifetime can have very different failure patterns, and need different maintenance policies.
ComputationC

The Kaplan–Meier estimator

Edward Kaplan and Paul Meier's 1958 idea is to break survival into a chain of conditional probabilities, one for each time an event happens. To survive past month 5, a patient must survive month 2, then (given that) month 3, then (given that) month 5. Each conditional probability is estimated from the people still at risk at that moment, the ones neither failed nor censored yet, and censored patients count as at risk for as long as they were observed.

Definition— Kaplan–Meier estimator

Let t(1)<t(2)<⋯t_{(1)} \lt t_{(2)} \lt \cdots be the distinct times at which events occur, djd_j the number of events at t(j)t_{(j)}, and njn_j the number at risk just before t(j)t_{(j)}. Then S^(t)=∏j:  t(j)≤t(1−djnj),\hat S(t) = \prod_{j:\; t_{(j)} \le t} \left(1 - \frac{d_j}{n_j}\right), a step function that drops only at event times. Greenwood's formula estimates its variance: Var^(S^(t))=S^(t)2∑t(j)≤tdjnj(nj−dj)\widehat{\text{Var}}\big(\hat S(t)\big) = \hat S(t)^2 \sum_{t_{(j)} \le t} \frac{d_j}{n_j(n_j - d_j)}.

Example— Twelve patients by hand

Twelve patients' months to relapse, with censored times marked +: 2, 3, 3+, 5, 6+, 7, 9, 10+, 12, 14+, 15, 18+. At month 2 all 12 are at risk and one relapses: S^(2)=11/12=0.917\hat S(2) = 11/12 = 0.917. At month 3, 11 are at risk and one relapses: S^(3)=0.917×10/11=0.833\hat S(3) = 0.917 \times 10/11 = 0.833. The patient censored at month 3 leaves the risk set after that month's events (the usual convention for ties), as does no one else until month 5, so 9 are at risk there: S^(5)=0.833×8/9=0.741\hat S(5) = 0.833 \times 8/9 = 0.741. The censored patient at month 6 never causes a drop; she simply shrinks the risk set for later months.

The code below finishes the table, adds Greenwood standard errors, and checks the result against SciPy's implementation.

python
import numpy as np
from scipy import stats

# Months from starting treatment to relapse (event = 1) or to the last check-up without relapse (event = 0).
time = np.array([2, 3, 3, 5, 6, 7, 9, 10, 12, 14, 15, 18])
event = np.array([1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0])

def kaplan_meier(time, event):
    """Return the distinct event times, numbers at risk, events, S(t) and Greenwood standard errors."""
    rows, s, green = [], 1.0, 0.0
    for t in np.unique(time[event == 1]):
        n = np.sum(time >= t)                    # at risk just before t
        d = np.sum((time == t) & (event == 1))   # events at t
        s *= 1 - d / n
        green += d / (n * (n - d)) if n > d else 0.0
        rows.append((t, n, d, s, s * np.sqrt(green)))
    return rows

print(" month  at risk  relapses   S(t)   std. error")
for t, n, d, s, se in kaplan_meier(time, event):
    print(f"{t:5d} {n:7d} {d:8d}     {s:.3f}    {se:.3f}")

# The same estimate from SciPy (version 1.11 or later), which handles censoring through CensoredData.
data = stats.CensoredData(uncensored=time[event == 1], right=time[event == 0])
res = stats.ecdf(data)
print("SciPy S(t) at the same months:", np.round(res.sf.evaluate(np.unique(time[event == 1])), 3))
print(f"naive: mean of all 12 times = {time.mean():.1f} months; share relapsed = {event.mean():.0%}")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The estimated median, the first time S^\hat S reaches 0.5 or below, is 12 months (S^(12)=0.397\hat S(12) = 0.397). Notice how the standard error grows along the curve, from 0.080 at month 2 to 0.163 at month 15, as the risk set shrinks to two patients: the right-hand end of any Kaplan–Meier curve rests on very few people, and large late drops (here from 0.397 to 0.198 at a single relapse) should be read with that in mind. Good plots print the number at risk beneath the time axis for exactly this reason.

ComputationC

Comparing groups: the log-rank test

Is the new pump seal better? Plot a Kaplan–Meier curve for each group, then test whether the curves differ. The log-rank test asks, at each event time: given how many units of each group were at risk and how many failed in total, how many failures would we expect in group B if the groups had the same hazard? It adds up observed minus expected over all event times and divides by the standard deviation of that sum. Under the null hypothesis of equal hazards, the squared standardised sum is approximately chi-square with one degree of freedom.

python
import numpy as np
from scipy import stats

rng = np.random.default_rng(31)
# Village hand pumps fitted with an old (A) or new (B) seal design, followed for up to 5 years.
# Failure times are Weibull with shape 1.5 (wear-out); design B has 0.6 times the hazard of A.
n = 80
shape, scale_a = 1.5, 4.0
scale_b = scale_a * 0.6 ** (-1 / shape)              # proportional hazards with ratio 0.6
fail = np.r_[scale_a * rng.weibull(shape, n), scale_b * rng.weibull(shape, n)]
group = np.r_[np.zeros(n), np.ones(n)]
installed = rng.uniform(0, 2, 2 * n)                 # installed over the first two years
follow = 5 - installed                               # observation ends after year 5
time = np.minimum(fail, follow)
event = (fail <= follow).astype(int)

def log_rank(time, event, group):
    o_minus_e, var = 0.0, 0.0
    for t in np.unique(time[event == 1]):
        at_risk = time >= t
        n_all, n_b = at_risk.sum(), (at_risk & (group == 1)).sum()
        d_all = ((time == t) & (event == 1)).sum()
        d_b = ((time == t) & (event == 1) & (group == 1)).sum()
        o_minus_e += d_b - d_all * n_b / n_all       # observed minus expected failures in group B
        if n_all > 1:
            var += d_all * (n_b / n_all) * (1 - n_b / n_all) * (n_all - d_all) / (n_all - 1)
    return o_minus_e, o_minus_e ** 2 / var

for g, name in [(0, "A (old seal)"), (1, "B (new seal)")]:
    m = group == g
    print(f"{name}: {event[m].sum()} failures, {(1 - event[m]).sum()} still working at the end of observation")
o_e, chi2 = log_rank(time, event, group)
print(f"group B: observed minus expected failures = {o_e:.1f}; log-rank chi-square = {chi2:.2f}, "
      f"p = {stats.chi2.sf(chi2, df=1):.4f}")
res = stats.logrank(stats.CensoredData(uncensored=time[(group == 0) & (event == 1)], right=time[(group == 0) & (event == 0)]),
                    stats.CensoredData(uncensored=time[(group == 1) & (event == 1)], right=time[(group == 1) & (event == 0)]))
print(f"SciPy's logrank: statistic {res.statistic:.3f} (its square is {res.statistic ** 2:.2f}), p = {res.pvalue:.4f}")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Pumps with the new seal had 10.0 fewer failures than expected under equal hazards; the log-rank chi-square is 4.34 with p=0.037p = 0.037. SciPy reports the same test as a standard normal statistic, 2.084, whose square is the chi-square. The curves below show where the difference comes from: they run close together for the first year and separate steadily after it.

01234500.250.50.751B (new seal)A (old seal)years since installationproportion still working
Kaplan–Meier curves for the simulated pump study, 80 pumps per seal design. Short vertical ticks mark censored pumps, still working when observation ended. The log-rank test compares the curves over their whole length.

The log-rank test gives every event time equal weight, which makes it most powerful when one group's hazard is a constant multiple of the other's. If the curves cross, as when surgery carries an early risk but a later benefit compared with medication, early and late differences cancel in the sum, and the test can report a large pp-value for two very different treatments. Weighted versions emphasise early or late differences, but the weighting must be chosen before seeing the data.

ExplanationH

Hazard ratios and the Cox model

A test says whether groups differ; we also want how much, and to adjust for other variables such as age. In 1972 David Cox proposed modelling the hazard of unit ii with covariates xix_i as h(t∣xi)=h0(t) exp⁡(β1xi1+⋯+βpxip),h(t \mid x_i) = h_0(t)\, \exp(\beta_1 x_{i1} + \cdots + \beta_p x_{ip}), where h0(t)h_0(t) is a baseline hazard left completely unspecified. Each covariate multiplies the hazard by a constant factor at every time: eβke^{\beta_k} is the hazard ratio for a one-unit increase in xkx_k, holding the others fixed. Because the ratio is the same at all times, this is the proportional hazards model.

Cox's insight was that β\beta can be estimated without knowing h0h_0 at all. At each event time, ask: given that someone in the risk set failed now, what is the probability that it was the one who did? Under the model, it is that unit's share of the risk set's total hazard, and h0(t)h_0(t) cancels: L(β)=∏events iexp⁡(xi⊤β)∑j∈R(ti)exp⁡(xj⊤β),L(\beta) = \prod_{\text{events } i} \frac{\exp(x_i^\top \beta)}{\sum_{j \in R(t_i)} \exp(x_j^\top \beta)}, where R(ti)R(t_i) is the set at risk at time tit_i. Maximising this partial likelihood gives estimates that behave like maximum-likelihood estimates, with standard errors from its curvature (Likelihood and model selection). The code fits it directly on two simulated trials with very different baseline hazards, one falling and one rising; the model does not need to be told which.

python
import numpy as np
from scipy.optimize import minimize

rng = np.random.default_rng(1972)

def simulate(n, shape):
    """Randomised treatment (hazard ratio 0.7) and age (hazard ratio 1.35 per 10 years) on top of a
    Weibull baseline hazard with the given shape; censoring at a uniform time up to 6 years."""
    treat = rng.integers(0, 2, n)
    age10 = rng.normal(0, 1, n)                      # age in decades, centred at the mean age
    eta = np.log(0.7) * treat + np.log(1.35) * age10
    t_event = (rng.exponential(1, n) / np.exp(eta)) ** (1 / shape) * 3.0
    t_cens = rng.uniform(0, 6, n)
    return np.minimum(t_event, t_cens), (t_event <= t_cens).astype(int), np.column_stack([treat, age10])

def neg_log_partial_likelihood(beta, time, event, X):
    order = np.argsort(-time)                        # longest times first, so cumulative sums give risk sets
    eta = X[order] @ beta
    log_risk = np.logaddexp.accumulate(eta)          # log of the sum of exp(eta) over everyone still at risk
    return -np.sum((eta - log_risk)[event[order] == 1])

def hessian(f, x, h=1e-4):
    """Central-difference second derivatives of f at x."""
    k = len(x)
    H = np.zeros((k, k))
    for i in range(k):
        for j in range(k):
            ei, ej = np.eye(k)[i] * h, np.eye(k)[j] * h
            H[i, j] = (f(x + ei + ej) - f(x + ei - ej) - f(x - ei + ej) + f(x - ei - ej)) / (4 * h * h)
    return H

for shape, story in [(0.7, "falling hazard (early risk)"), (2.5, "rising hazard (wear-out)")]:
    time, event, X = simulate(1500, shape)
    fit = minimize(neg_log_partial_likelihood, np.zeros(2), args=(time, event, X), method="BFGS")
    se = np.sqrt(np.diag(np.linalg.inv(hessian(lambda b: neg_log_partial_likelihood(b, time, event, X), fit.x))))
    lo, hi = np.exp(fit.x - 1.96 * se), np.exp(fit.x + 1.96 * se)
    print(f"{story}: {event.sum()} events")
    print(f"   treatment:       hazard ratio {np.exp(fit.x[0]):.2f}, 95% CI {lo[0]:.2f} to {hi[0]:.2f}   (true 0.70)")
    print(f"   age, per decade: hazard ratio {np.exp(fit.x[1]):.2f}, 95% CI {lo[1]:.2f} to {hi[1]:.2f}   (true 1.35)")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

In both trials the estimates land near the truth (treatment 0.66 and 0.73, against 0.70), and every 95% interval contains the true value, although one baseline hazard falls with time and the other rises steeply. That robustness to the baseline is why the Cox model became the standard regression model of clinical research; Cox's paper is one of the most cited in all of statistics.

The price is the proportional-hazards assumption itself. It is checked by plotting ln⁡(−ln⁡S^(t))\ln(-\ln \hat S(t)) for each group, which gives parallel curves under the model, or with tests based on Schoenfeld residuals (Grambsch and Therneau, 1994). When it fails, options include stratifying the baseline hazard by the offending variable, letting a coefficient change with time, or switching summaries.

Worked examplesH

Survival analysis in medicine and engineering

Medicine. Survival methods are the core of clinical trials with time-to-event endpoints (overall survival, progression-free survival, time to hospital readmission) and of cancer registries, which publish survival at one, five and ten years by cancer type and stage, such as the US National Cancer Institute's SEER programme. Two complications recur. Competing risks: in a study of time to kidney failure among elderly patients, death from heart disease prevents kidney failure from ever being observed, and treating such deaths as ordinary censoring overstates the risk of kidney failure; the cumulative incidence function and the Fine–Gray model handle this. And trial data concern real patients, so they are analysed under consent, ethics approval and data-protection law; shared data sets are de-identified, and the lesson's examples are simulated.

Engineering reliability. Reliability engineers call the same ideas life data analysis. Failure times of bearings, pumps, turbine blades and electronic boards are fitted with Weibull models (the Swedish engineer Waloddi Weibull proposed the distribution in 1951), and the fitted shape parameter is read directly: k<1k \lt 1 suggests manufacturing defects worth screening out by "burn-in"; k>1k \gt 1 suggests wear-out, for which preventive replacement before the hazard climbs makes sense. Test units still running when a test stops are censored, exactly like patients still well at the end of a trial. Accelerated life tests run units at higher temperature or load to provoke failures sooner, then extrapolate to normal use with a physical model; the extrapolation is only as good as that model. The NIST e-Handbook covers these methods in detail.

The same mathematics measures unemployment duration in labour economics, customer churn in subscription businesses, time to degree in education research and the timing of first marriage in demography.

History & global context

From life tables to software

John Graunt's analysis of London's bills of mortality in 1662 and Edmond Halley's life table for the city of Breslau in 1693 are the ancestors of survival analysis; actuaries used life tables to price annuities for two centuries before statisticians formalised censoring. Kaplan and Meier's 1958 paper (written after the two discovered they had submitted similar work), the log-rank test of Nathan Mantel and of Richard and Julian Peto in the 1960s and early 1970s, and Cox's 1972 model created the modern subject, and counting-process theory in the 1970s and 1980s, led by Odd Aalen and others, gave it rigorous foundations.

In practice, the R survival package by Terry Therneau is the reference implementation; in Python, lifelines and scikit-survival provide Kaplan–Meier, Cox and parametric models, and SciPy has Kaplan–Meier (stats.ecdf) and the log-rank test. A typical R analysis looks like this (not run here):

r
# R, not run here
library(survival)
fit_km  <- survfit(Surv(months, relapsed) ~ arm, data = trial)
survdiff(Surv(months, relapsed) ~ arm, data = trial)            # log-rank test
fit_cox <- coxph(Surv(months, relapsed) ~ arm + age, data = trial)
cox.zph(fit_cox)                                                # check proportional hazards

Machine learning has added flexible models such as random survival forests and neural networks that predict individual survival curves; they still need censoring-aware loss functions and censoring-aware evaluation, such as the concordance index, and calibration checked against Kaplan–Meier estimates.

Common misconceptionsH

Common misconceptions

Misconception

"Censored observations are missing data, so drop them." They carry information (the event had not happened by time tt), and dropping them keeps only those whose events came early. In the simulation, the median of relapses only was 8.3 months against a truth of 20.

Misconception

"Kaplan–Meier works whatever the reason for censoring." It assumes censored units have the same future risk as those still observed. If the sickest patients drop out, the curve is too optimistic, and no amount of data fixes it. Ask why each observation was censored.

Misconception

"A hazard ratio of 0.5 means patients live twice as long." It means that, at each moment, a treated patient still at risk has half the event rate of a comparable untreated one. How that translates into extra time depends on the baseline hazard: for an exponential distribution the median doubles, but for other shapes it does not, and a hazard ratio alone says nothing about absolute benefit. Report survival probabilities or restricted mean survival alongside it.

Misconception

"The proportion who had the event is the risk of the event." Fifty-eight per cent of the twelve patients in the example relapsed, but the Kaplan–Meier estimate of relapse by month 15 is 1−0.198≈80%1 - 0.198 \approx 80\%. The raw proportion depends on how long each person happened to be followed.

Exercises

Exercises

1. Survival analysis

Warm-upHuman

Five patients in a small study in Hanoi are followed after surgery. Their months to recurrence, with censored times marked +, are: 3, 5+, 6, 9, 12+. What is the Kaplan–Meier estimate S^(6)\hat S(6), as a percentage rounded to the nearest whole number?

2. Survival analysis

CoreHuman

In a study of a home-based treatment for heart failure, patients who feel too unwell to attend clinic visits often stop attending and are recorded as censored at their last visit. What does this do to the Kaplan–Meier estimate of survival?

Choose one

3. Survival analysis

CoreHuman

Pumps in a reliability study fail at a constant hazard of 0.02 per month (an exponential model). What is the median time to failure, to the nearest month?

4. Survival analysis

CoreHuman

A randomised trial reports a hazard ratio for death of 0.6 (95% CI 0.48 to 0.75) for a new drug against standard care, from a Cox model. Which statement is correct?

Choose one

5. Survival analysis

StretchHuman

Two treatments for a heart condition are compared: surgery has a higher risk of death in the first months but a lower risk afterwards; medication has a steady risk. The Kaplan–Meier curves cross at about two years. The log-rank test gives p=0.71p = 0.71. What is the best conclusion?

Choose one

6. Survival analysis

CoreComputational

Write km_at(time, event, t), which returns the Kaplan–Meier estimate S^(t)\hat S(t) as a float. time holds each unit's observed time and event is 1 for an observed event and 0 for a censored observation.

At each distinct event time u≤tu \le t, multiply by 1−d/n1 - d/n, where dd is the number of events at uu and nn the number of units with observed time ≥u\ge u (so a unit censored at exactly uu still counts as at risk at uu).

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

7. Survival analysis

CoreComputational

For an exponential model with rate λ\lambda, a unit that fails at time tit_i contributes λe−λti\lambda e^{-\lambda t_i} to the likelihood and a unit censored at tit_i contributes S(ti)=e−λtiS(t_i) = e^{-\lambda t_i}. So the log-likelihood is ℓ(λ)=dln⁡λ−λ∑iti\ell(\lambda) = d \ln \lambda - \lambda \sum_i t_i, where dd is the number of failures.

Write exp_rate_mle(time, event) that returns the maximum-likelihood estimate of λ\lambda as a float. (Set the derivative of ℓ\ell to zero.)

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Further reading and resources