Solution (source code)

= Solution

Under <independent censoring>, order the distinct observed failure times as $t_1<\cdots<t_m$. Let $r_j=\#\{i:x_i\geq t_j\}$ be the size of the <risk set> just before $t_j$, and let $d_j=\#\{i:x_i=t_j,v_i=1\}$ be the number of failures there. The estimated <conditional probability> of surviving that event time is $1-d_j/r_j$. Multiplying these conditional survival <probabilities> gives the <Kaplan–Meier estimator>
$$
\boxed{\widehat S(t)=\prod_{t_j\leq t}\left(1-\frac{d_j}{r_j}\right).}
$$
A censored observation leaves the <risk set> after its <censoring> time but does not create a downward survival jump. With tied failure and <censoring> times, the displayed risk-set convention includes those censored at the time while processing failures, then removes them.

Since $H=-\log S$, the <Kaplan–Meier estimator> of the <cumulative hazard function> is
$$
\boxed{\widehat H_{\mathrm{KM}}(t)=-\log\widehat S(t)=\sum_{t_j\leq t}-\log\left(1-\frac{d_j}{r_j}\right).}
$$
This is not exactly the <Nelson–Aalen estimator> $\sum_{t_j\leq t}d_j/r_j$, although the two are close when the individual fractions are small. If a jump exhausts the <risk set>, $\widehat S$ becomes zero and $\widehat H_{\mathrm{KM}}$ becomes infinite; transformed residuals beyond that point require a finite-tail modelling convention or restriction of follow-up.