Build a step curve from observed events, keep censoring visible and stop trusting a thin tail.
Survival curves: show who remains at risk after each event
Define the event and the clock
A survival curve estimates the share of a starting cohort that has not yet experienced a specified event by elapsed time. A device-failure chart, for instance, needs a clear start such as commissioning and an event such as first unrecoverable shutdown. Mixing calendar dates with elapsed operating days changes the risk set. Units matter too: a device observed for 47 calendar days but active for 31 operating days is not interchangeable with one observed continuously. Event-aligned time series can show measurements around a milestone, while a survival curve asks when the first event occurs. Decide how replacement devices, repeat failures and delayed entry will be treated before fitting the curve; the simple right-censored calculation below assumes every device enters at elapsed time zero.
A censor mark is not a failure
Right censoring means observation ended without the event being seen. A study cutoff, withdrawal or lost sensor feed can create one, but those reasons need separate counts because their relationship to failure risk may differ. The Kaplan–Meier step drops only when events occur. A censor at time 59 leaves the height unchanged while reducing the group available for later event times. When events and censors share a recorded time, declare whether events are processed before same-time censors; the code uses that convention. Put the censor marks on the curve and provide an at-risk table at chosen time ticks. Missing periods are a different data problem: do not silently turn an unobserved failure time into a clean censor without checking what was actually known.
Read the tail against its denominator
A flat tail can look reassuring even when only two devices remain under observation. Show the number at risk at regular, meaningful intervals and consider ending the displayed curve where support becomes too sparse for the decision. Do not stretch the last estimated step into a guarantee beyond follow-up. If two cohorts enter in different years or face different servicing policies, their curves are descriptive until the cohort definitions and confounding are reviewed. Small-sample uncertainty belongs beside the estimate, and a confidence band needs a stated method. An ordinary percent-without-failure line is not equivalent when follow-up lengths differ because it treats early withdrawals as though they had complete observation.
Keep the underlying event ledger
The plot is a compression of the event ledger, not a substitute for it. Preserve device ID, entry time, exit time, event flag, censor reason, cohort, and data-cutoff date. Reconcile the at-risk count before each event: prior risk minus prior events and censors must equal the next risk count. If failures can be precluded by a different event, a conventional one-event survival curve may answer the wrong question; define a competing-event analysis instead. Empirical CDFs are useful when event times are fully observed, but they do not automatically handle censoring. Export a table of each step time, event count, censor count, risk count and estimated survival so a reader can audit every drop without estimating coordinates from pixels.
Worked contract
from collections import Counter
from math import isfinite
def device_survival_steps(records: list[tuple[str, float, bool]]):
identifiers = [device_id for device_id, _, _ in records]
if len(identifiers) != len(set(identifiers)) or any(not item for item in identifiers):
raise ValueError("device IDs must be unique and nonempty")
if any(not isfinite(duration) or duration < 0 or type(failed) is not bool
for _, duration, failed in records):
raise ValueError("duration and event flags must be valid")
events = Counter(duration for _, duration, failed in records if failed)
censors = Counter(duration for _, duration, failed in records if not failed)
risk_count, survival, steps = len(records), 1.0, []
for duration in sorted(events.keys() | censors.keys()):
failed_count = events[duration]
censored_count = censors[duration]
survival *= 1 - failed_count / risk_count
steps.append((duration, risk_count, failed_count,
censored_count, survival))
risk_count -= failed_count + censored_count
return steps
depot_devices = [
("station-47", 3, True), ("station-59", 4, False),
("station-83", 4, True), ("station-97", 7, False),
]
depot_steps = device_survival_steps(depot_devices)
assert depot_steps[0] == (3, 4, 1, 0, 0.75)
assert depot_steps[1] == (4, 3, 1, 1, 0.5)
assert depot_steps[-1][-1] == 0.5Performance and operating cost
Counting and sorting N event or censor records takes O(N log N) time and O(N) space. The function returns every observed exit time so censors can be marked even when the step height stays level. It assumes entry at time zero, one terminal event, and a declared event-before-censor tie rule; it does not calculate confidence limits or correct informative censoring. Validate the event ledger and at-risk table before drawing smooth-looking output.
Common Mistakes
- Treating a censored record as an observed failure or as complete follow-up.
- Omitting at-risk counts while interpreting the last flat segment.
- Combining elapsed operating time with calendar time on one axis.
- Comparing cohorts without checking different entry policies, follow-up windows or censor reasons.
Validation drill
Audit 47 devices across two installation cohorts. Print the risk set at months 0, 6, 12 and 18, mark withdrawals, then explain where the tail no longer supports a useful comparison.
