Kaplan–Meier and Nelson–Aalen Estimation › Formula

Michael BrenndoerferOctober 26, 202614 min read
Machine Learning from ScratchAll chapters
Part 6 of 15Survival Analysis
Chapter 49Kaplan–Meier and Nelson–Aalen Estimation › Formula
Reading section 4 of 10 · Formula · 14 minSection 4 of 10 · 14 min

Section 4 of 10 · 14 min read

Kaplan–Meier and Nelson–Aalen Estimation › FormulaSection view · Sections in this chapter4 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

The formulas in this chapter look different at first, one a product and the other a sum. The shared logic is easier to see if we start from the data and ask a single question at each event time: who is still eligible to have the event? The answer is the risk set. Once we can count the risk set and the events that occur inside it, both estimators follow almost immediately.

We begin with the observed data. For subject ii,

  • TiT_i is the true event time,
  • CiC_i is the censoring time,
  • Yi=min⁡(Ti,Ci)Y_i=\min(T_i,C_i) is the observed follow-up time,
  • Δi=1{Ti≤Ci}\Delta_i=\mathbf{1}\{T_i\le C_i\} is the event indicator, equal to 11 if we observed the event and 00 if the observation was censored.

We assume independent draws from the target population, strictly positive event times, and right censoring. For the basic marginal estimators, independence of TiT_i and CiC_i is a sufficient censoring assumption, with positive probability of remaining observed throughout the time range we estimate. Random treatment assignment alone does not establish this assumption about follow-up.

Risk sets, tied events, and censoring order

At any time tt, the risk set is the set of subjects still under observation and event-free just before tt:

R(t)={i:Yi≥t}.\mathcal{R}(t)=\{i:Y_i\ge t\}.

Its size is n(t)=∣R(t)∣n(t)=|\mathcal{R}(t)|. This definition is the bookkeeping center of the whole chapter. A subject who has already had the event is no longer eligible to have it again, and a subject who has been censored before tt cannot contribute information about an event at tt. Both leave the risk set. A subject who is censored exactly at tt is a boundary case, and we will handle it by convention below.

If the distinct observed event times are

t1<t2<⋯<tm,t_1<t_2<\cdots<t_m,

then at event time tjt_j we write

nj=n(tj)=∑i=1n1{Yi≥tj}n_j=n(t_j)=\sum_{i=1}^{n}\mathbf{1}\{Y_i\ge t_j\}

for the number at risk and

dj=∑i=1n1{Yi=tj, Δi=1}d_j=\sum_{i=1}^{n}\mathbf{1}\{Y_i=t_j,\ \Delta_i=1\}

for the number of events at that time. The ratio dj/njd_j/n_j is the empirical event fraction among those who were still eligible at tjt_j. That fraction will become the Nelson-Aalen increment, and its complement will become the Kaplan-Meier factor.

It is also where censoring enters: a censored subject remains in njn_j up to and including their censoring time, then leaves. Censoring does not create an event, but it changes who is available for later events.

Tied events occur when dj>1d_j>1. The product-limit estimator handles ties by grouping all events at tjt_j into one conditional factor. Tied event and censoring times require a convention. We use the standard convention that events are processed before censorings at the same time. A subject censored at tjt_j is therefore counted in njn_j for the event at tjt_j, and is removed only after that event time. Removing those censorings first would make the denominator smaller, increase the event fraction, and lower the estimated survival under a different convention.

This convention is worth checking in software. The tied-data check below confirms events before censorings for the installed SciPy and statsmodels implementations; consult documentation when exact tie behavior matters. The convention matters most when the risk set is small or when many subjects share the same recorded time.

Kaplan–Meier product-limit estimation

Kaplan-Meier estimates survival by chaining conditional probabilities. At event time tjt_j, suppose njn_j subjects are at risk and djd_j events occur. The estimated probability of surviving past tjt_j, given that a subject was still at risk just before tjt_j, is

1−djnj1-\frac{d_j}{n_j}

where:

  • djd_j: the number of events at event time tjt_j
  • njn_j: the number of subjects at risk at event time tjt_j

If no events occur at tjt_j, then dj=0d_j=0 and the factor is 11; censoring times do not create factors. If all subjects at risk have an event, the factor is 00 and the estimated survival drops to zero. In between, the factor is the fraction of the current risk set that survives the event time.

Multiplying these conditional factors across all event times up to tt gives the Kaplan-Meier estimator:

S^KM(t)=∏j:tj≤t(1−djnj)\boxed{ \hat S_{\mathrm{KM}}(t) = \prod_{j:t_j\le t} \left(1-\frac{d_j}{n_j}\right) }

with the empty product defined as 11 for times before the first event. The word product-limit refers to this product of estimated conditional survival probabilities. It is the nonparametric maximum likelihood estimator of the survival function under independent right censoring.

where:

  • S^KM(t)\hat S_{\mathrm{KM}}(t): the estimated survival probability of surviving past time tt
  • tjt_j: the jj-th distinct observed event time
  • djd_j: the number of events at event time tjt_j
  • njn_j: the number of subjects at risk at event time tjt_j
  • 1−dj/nj1-d_j/n_j: the estimated conditional probability of surviving past tjt_j given survival to just before tjt_j
  • the product over j:tj≤tj:t_j\le t: multiply the conditional factors for all event times up to and including tt

We can read the formula as a sequence of updates. Start with S^(0)=1\hat S(0)=1. At each event time, multiply the current survival probability by the fraction of the risk set that did not have an event. Censoring changes the next risk set, but it does not directly multiply the curve by anything.

The variance of the Kaplan-Meier estimator is usually estimated with Greenwood’s formula. Greenwood’s cumulative variance factor is

σ^2(t)=∑j:tj≤tdjnj(nj−dj),\hat\sigma^2(t) = \sum_{j:t_j\le t} \frac{d_j}{n_j(n_j-d_j)},

where:

  • σ^2(t)\hat\sigma^2(t): Greenwood’s cumulative variance factor for the survival curve up to time tt, which combines with the squared survival estimate to give the variance of S^KM(t)\hat S_{\mathrm{KM}}(t)
  • djd_j: the number of events at event time tjt_j
  • njn_j: the number of subjects at risk at event time tjt_j
  • nj−djn_j-d_j: the number at risk who do not have the event at tjt_j

Then

Var⁡^(S^KM(t))=S^KM(t)2 σ^2(t).\widehat{\operatorname{Var}}\bigl(\hat S_{\mathrm{KM}}(t)\bigr) = \hat S_{\mathrm{KM}}(t)^2\,\hat\sigma^2(t).

where:

  • Var⁡^(S^KM(t))\widehat{\operatorname{Var}}\bigl(\hat S_{\mathrm{KM}}(t)\bigr): the estimated variance of the Kaplan-Meier survival estimate at time tt
  • S^KM(t)\hat S_{\mathrm{KM}}(t): the Kaplan–Meier survival estimate at time tt
  • σ^2(t)\hat\sigma^2(t): Greenwood’s cumulative variance factor at time tt

To see where this comes from, take logs:

log⁡S^KM(t)=∑j:tj≤tlog⁡(1−djnj).\log \hat S_{\mathrm{KM}}(t) = \sum_{j:t_j\le t} \log\left(1-\frac{d_j}{n_j}\right).

For a binomial proportion p^j=dj/nj\hat p_j=d_j/n_j, the delta method gives

Var⁡(log⁡(1−p^j))≈pjnj(1−pj).\operatorname{Var}\left(\log(1-\hat p_j)\right) \approx \frac{p_j}{n_j(1-p_j)}.

where:

  • p^j=dj/nj\hat p_j=d_j/n_j: the estimated event fraction at event time tjt_j
  • pjp_j: the event probability in the local binomial working model, conditional on membership in the current risk set at tjt_j
  • njn_j: the number of subjects at risk at event time tjt_j
  • djd_j: the number of events at event time tjt_j

Plugging in p^j=dj/nj\hat p_j=d_j/n_j yields dj/[nj(nj−dj)]d_j/[n_j(n_j-d_j)]. This binomial calculation is a sketch of the local variance contribution. The full large-sample derivation uses counting-process martingales, since earlier events change later risk sets. Summing the contributions and applying the delta method once more to S=exp⁡(log⁡S)S=\exp(\log S) gives the factor S^2\hat S^2. The denominator nj−djn_j-d_j shows why Greenwood’s formula fails when nj=djn_j=d_j: at that point the curve hits zero and the variance formula is not finite.

Nelson–Aalen cumulative hazard estimation

For an absolutely continuous event-time distribution, at times with positive survival where the following limit exists, the hazard is the instantaneous event rate among those still at risk:

h(t)=lim⁡Δt↓0P(t≤T<t+Δt∣T≥t)Δt.h(t) = \lim_{\Delta t\downarrow 0} \frac{P(t\le T<t+\Delta t\mid T\ge t)}{\Delta t}.

where:

  • h(t)h(t): the hazard function, the instantaneous event rate at time tt among those still at risk
  • TT: the true event time
  • Δt\Delta t: a small time increment
  • P(t≤T<t+Δt∣T≥t)P(t\le T<t+\Delta t\mid T\ge t): the conditional probability of an event in [t,t+Δt)[t,t+\Delta t) given survival to time tt

The cumulative hazard is

H(t)=∫0th(u) du.H(t)=\int_0^t h(u)\,du.

where:

  • H(t)H(t): the cumulative hazard up to time tt
  • h(u)h(u): the hazard function at time uu

This integral describes a continuous-time hazard with no initial event mass. For inherently discrete event times, cumulative hazard is instead a sum of conditional hazard jumps and need not equal −log⁡S(t)-\log S(t). The implementation below groups events that share a recorded time; it does not split a tied group into a sequence of separate failures.

Nelson-Aalen estimates HH by accumulating estimated hazard increments at the observed event times. At tjt_j, the observed event fraction among those at risk is dj/njd_j/n_j, so we set

ΔH^(tj)=djnj,\Delta \hat H(t_j)=\frac{d_j}{n_j},

where:

  • ΔH^(tj)\Delta \hat H(t_j): the estimated hazard increment at event time tjt_j
  • djd_j: the number of events at event time tjt_j
  • njn_j: the number of subjects at risk at event time tjt_j

Adding these increments gives

H^NA(t)=∑j:tj≤tdjnj\boxed{ \hat H_{\mathrm{NA}}(t) = \sum_{j:t_j\le t} \frac{d_j}{n_j} }

with H^(0)=0\hat H(0)=0, where:

  • H^NA(t)\hat H_{\mathrm{NA}}(t): the Nelson–Aalen cumulative hazard estimate at time tt
  • tjt_j: the jj-th distinct observed event time
  • djd_j: the number of events at event time tjt_j
  • njn_j: the number of subjects at risk at event time tjt_j
  • the sum over j:tj≤tj:t_j\le t: add the hazard increments for all event times up to and including tt

This is a nondecreasing step function. It jumps at event times; censoring alone creates no jump. The jump size is large when many events occur among few subjects at risk, and small when one event occurs among a large risk set.

A grouped Greenwood-type variance estimate is

Var⁡^(H^NA(t))=∑j:tj≤tdj(nj−dj)nj3.\widehat{\operatorname{Var}}\bigl(\hat H_{\mathrm{NA}}(t)\bigr) = \sum_{j:t_j\le t} \frac{d_j(n_j-d_j)}{n_j^3}.

This is a binomial plug-in, Greenwood-type estimate. The Aalen counting-process estimate uses ∑dj/nj2\sum d_j/n_j^2 instead. The two differ by the factor 1−dj/nj1-d_j/n_j in each contribution. A smaller reported variance does not by itself establish better uncertainty coverage, especially with few subjects at risk.

where:

  • Var⁡^(H^NA(t))\widehat{\operatorname{Var}}\bigl(\hat H_{\mathrm{NA}}(t)\bigr): the estimated variance of the Nelson–Aalen cumulative hazard at time tt
  • djd_j: the number of events at event time tjt_j
  • njn_j: the number of subjects at risk at event time tjt_j

A local plug-in argument treats the event count as binomial given the current risk set, with njn_j trials and event probability estimated by dj/njd_j/n_j. This motivates the contribution; it is not an exact finite-sample variance proof for the accumulated estimator. The variance of dj/njd_j/n_j is approximately

1nj2⋅njdjnj(1−djnj)=dj(nj−dj)nj3.\frac{1}{n_j^2}\cdot n_j\frac{d_j}{n_j}\left(1-\frac{d_j}{n_j}\right) = \frac{d_j(n_j-d_j)}{n_j^3}.

Nelson-Aalen is closely related to Kaplan-Meier, but the relationship is not exact in finite samples. Since

log⁡S^KM(t)=∑tj≤tlog⁡(1−djnj),\log \hat S_{\mathrm{KM}}(t) = \sum_{t_j\le t} \log\left(1-\frac{d_j}{n_j}\right),

and log⁡(1−x)≈−x\log(1-x)\approx -x for small xx, we have

S^KM(t)≈exp⁡(−∑tj≤tdjnj)=exp⁡(−H^NA(t)).\hat S_{\mathrm{KM}}(t) \approx \exp\left(-\sum_{t_j\le t}\frac{d_j}{n_j}\right) = \exp\left(-\hat H_{\mathrm{NA}}(t)\right).

The approximation is useful on a time range where the event fractions dj/njd_j/n_j and their accumulated second-order error are small. Large risk sets with few events per time make the individual fractions small. It is not an identity. For 0<x<10<x<1, where xx represents an event fraction dj/njd_j/n_j,

1−x<e−x1-x < e^{-x}

so the product-limit estimator lies below the exponential transform of Nelson–Aalen whenever events occur:

S^KM(t)≤exp⁡(−H^NA(t)).\hat S_{\mathrm{KM}}(t) \le \exp\left(-\hat H_{\mathrm{NA}}(t)\right).

The exact product-integral relationship is

S^KM(t)=∏tj≤t(1−ΔH^NA(tj)),\hat S_{\mathrm{KM}}(t) = \prod_{t_j\le t} \left(1-\Delta \hat H_{\mathrm{NA}}(t_j)\right),

not exp⁡(−H^NA)\exp(-\hat H_{\mathrm{NA}}). We should not report exp⁡(−H^NA)\exp(-\hat H_{\mathrm{NA}}) as if it were Kaplan–Meier. Their relative difference tends to zero in a small-increment limit where the accumulated second-order error also vanishes. Heavy ties or small risk sets can make the difference noticeable.

The finite-time gap has a simple source: for every event fraction xx strictly between zero and one, the product-limit factor 1−x1-x is smaller than the exponential factor e−xe^{-x}. We can display that inequality directly.

PythonIn [3]
CollapseExpand
import matplotlib.pyplot as plt
import numpy as np

event_fraction_grid = np.linspace(0.001, 0.999, 499)
factor_product = 1.0 - event_fraction_grid
factor_exp = np.exp(-event_fraction_grid)
factor_gap = factor_exp - factor_product
VisualizationOut [4]
CollapseExpand
Two curves compare the product-limit factor 1-x with the exponential factor exp(-x). The product-limit curve stays below the exponential curve, and the shaded gap widens as the event fraction grows.
The product-limit factor 1-x and the exponential factor exp(-x) for an event fraction x=d/n. The product-limit factor is always smaller when events occur, so exp(-Nelson-Aalen) lies above Kaplan-Meier.

Mathematical Properties

The formulas above imply a few structural facts that are useful when reading software output or checking a plot:

  • S^KM(t)\hat S_{\mathrm{KM}}(t) is a right-continuous, nonincreasing step function with values in [0,1][0,1] and S^KM(0)=1\hat S_{\mathrm{KM}}(0)=1.
  • H^NA(t)\hat H_{\mathrm{NA}}(t) is a right-continuous, nondecreasing step function with H^NA(0)=0\hat H_{\mathrm{NA}}(0)=0.
  • Both estimators change only at observed event times. A censoring time changes future risk sets but does not create a jump.
  • Ties are handled by grouping: the Kaplan–Meier factor is 1−dj/nj1-d_j/n_j, and the Nelson–Aalen increment is dj/njd_j/n_j.
  • The exponential transform satisfies exp⁡(−H^NA(t))≥S^KM(t)\exp(-\hat H_{\mathrm{NA}}(t))\ge \hat S_{\mathrm{KM}}(t), with exact equality only when no events have occurred at or before time tt.
  • Greenwood’s variance requires nj>djn_j>d_j. If nj=djn_j=d_j, the survival estimate reaches zero and the variance formula is not finite at that point.

Uncertainty, median survival, and tail limitations

The Kaplan-Meier intervals below describe uncertainty at a fixed time; they do not give simultaneous coverage for the whole curve. We use the log-log transformation because its interior limits stay within [0,1][0,1], whereas a symmetric normal interval can extend outside that range. Its coverage accuracy still depends on censoring, sample support, and distance from the endpoints. Let

σ^2(t)=∑j:tj≤tdjnj(nj−dj).\hat\sigma^2(t) = \sum_{j:t_j\le t} \frac{d_j}{n_j(n_j-d_j)}.

For 0<S^(t)<10<\hat S(t)<1, a pointwise (1−α)(1-\alpha) interval is

[S^(t)exp⁡(z1−α/2σ^(t)/∣log⁡S^(t)∣),S^(t)exp⁡(−z1−α/2σ^(t)/∣log⁡S^(t)∣)],\left[ \hat S(t)^{\exp\left(z_{1-\alpha/2}\hat\sigma(t)/|\log \hat S(t)|\right)},\quad \hat S(t)^{\exp\left(-z_{1-\alpha/2}\hat\sigma(t)/|\log \hat S(t)|\right)} \right],

where:

  • S^(t)\hat S(t): the Kaplan–Meier survival estimate at time tt
  • σ^(t)\hat\sigma(t): the square root of Greenwood’s cumulative variance factor, ∑j:tj≤tdj/[nj(nj−dj)]\sqrt{\sum_{j:t_j\le t}d_j/[n_j(n_j-d_j)]}
  • z1−α/2z_{1-\alpha/2}: the standard normal quantile, such as 1.961.96 for a 95% interval
  • α\alpha: the significance level (one minus the confidence level)

The log-log transformation is undefined at S^(t)=0\hat S(t)=0 or 11. We return missing limits at these boundaries instead of treating a zero-width display as statistical certainty. The known starting value S(0)=1S(0)=1 is separate because event times are strictly positive. These are approximate pointwise intervals. Simultaneous coverage over a specified, adequately supported time interval requires a method calibrated for the whole curve, such as a Hall–Wellner band; separate pointwise bootstrap percentiles would still be pointwise.

For H^(t)>0\hat H(t)>0 in a supported regime with a reliable nondegenerate variance and normal approximation, an approximate pointwise log-transformed interval for H(t)H(t) is

[H^(t)exp⁡(−z1−α/2σ^H(t)H^(t)), H^(t)exp⁡(z1−α/2σ^H(t)H^(t))],\left[ \hat H(t)\exp\left(-z_{1-\alpha/2}\frac{\hat\sigma_H(t)}{\hat H(t)}\right), \ \hat H(t)\exp\left(z_{1-\alpha/2}\frac{\hat\sigma_H(t)}{\hat H(t)}\right) \right],

where:

  • H^(t)\hat H(t): the Nelson–Aalen cumulative hazard estimate at time tt
  • σ^H(t)=∑j:tj≤tdj(nj−dj)/nj3\hat\sigma_H(t)=\sqrt{\sum_{j:t_j\le t} d_j(n_j-d_j)/n_j^3}: the standard error from the chosen Greenwood-type variance estimate
  • z1−α/2z_{1-\alpha/2}: the standard normal quantile for a (1−α)(1-\alpha) interval, such as 1.961.96 for a 95% interval
  • α\alpha: the significance level (one minus the confidence level)

The displayed log interval is undefined at H^(t)=0\hat H(t)=0. A no-event estimate at a positive follow-up time does not imply zero uncertainty; inference there needs a boundary-specific method. The chosen binomial variance can also vanish after every member of a tied risk set fails. A resulting zero-width interval is not justified merely because the hazard estimate is positive. The known origin H(0)=0H(0)=0 is a separate structural value.

The median survival time is the first time the estimated survival curve reaches or falls below one half:

m^=inf⁡{t:S^KM(t)≤0.5}.\hat m = \inf\{t:\hat S_{\mathrm{KM}}(t)\le 0.5\}.

Because the curve is a step function, the median is an observed event time, not an interpolated value between steps. If the curve never reaches or falls below 0.50.5 by the last follow-up, the median is not estimable. We should report “median not reached” rather than a fabricated number, together with the last follow-up time and the number still at risk. A confidence interval for the median can be obtained by inverting pointwise survival intervals, as in the Brookmeyer–Crowley approach, or by resampling observed time–event-indicator pairs and recomputing the Kaplan-Meier median. Adequate follow-up around the median is still needed; some resamples or interval endpoints may not reach it. A point estimate alone does not show its sampling uncertainty.

Tail limitations follow directly from the risk-set definition. At late times njn_j is small, so each event removes a larger fraction of the remaining survival estimate. The absolute Greenwood standard error need not grow: it also depends on the survival estimate, which is falling. If the largest observed time is censored, the curve remains above zero and we have no direct estimate beyond that time. A flat empirical tail does not establish a cure fraction. Beyond the last observed follow-up, there are no further observations to support its extension. If nj=djn_j=d_j, the curve drops to zero and Greenwood’s denominator vanishes. Sparse late risk sets limit tail precision. Tied times require the recorded-time convention and time-resolution assumptions to be checked; informative censoring can bias the curve throughout follow-up.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy