KaplanMeierEstimator<T>

Models & Types · AiDotNet.SurvivalAnalysis

Implements the Kaplan-Meier estimator for non-parametric survival analysis.

Inheritance: SurvivalModelBase<T> → KaplanMeierEstimator<T>

For Beginners

Kaplan-Meier is the simplest and most widely used survival method. It creates a “staircase” survival curve that shows the probability of survival over time.

How it works:

  1. Sort all observation times
  2. At each time point where an event occurs:
  • Count how many subjects are “at risk” (still in the study)
  • Count how many had the event
  • Survival probability = (at risk - events) / at risk
  1. Cumulative survival = product of all these probabilities up to time t

Example:

  • Time 0: 100 patients, all alive → S(0) = 1.0
  • Time 1: 100 at risk, 10 die → S(1) = 1.0 × (90/100) = 0.90
  • Time 2: 90 at risk (some left the study), 5 die → S(2) = 0.90 × (85/90) = 0.85

Key features:

  • No assumptions about the shape of survival (non-parametric)
  • Handles censoring naturally
  • Does NOT use patient features - same curve for everyone

For comparing groups or using features, use Cox Proportional Hazards instead.

References:

  • Kaplan & Meier (1958). “Nonparametric Estimation from Incomplete Observations”

How It Works

The Kaplan-Meier estimator is a non-parametric method for estimating the survival function from lifetime data. It doesn’t use covariates - it estimates a single survival curve for all subjects in the dataset.

Example

// age and treatment arm per patient
var features = new Matrix<double>(new double[,]
{
    { 45, 1 }, { 52, 0 }, { 38, 1 }, { 61, 0 }, { 47, 1 }, { 55, 0 }
});
// months each patient was observed
var times = new Vector<double>(new double[] { 5.0, 12.0, 3.0, 18.0, 9.0, 21.0 });
// 1 = the event happened; 0 = censored, still fine when the study ended or lost to follow-up
var events = new Vector<int>(new int[] { 1, 0, 1, 0, 1, 1 });
            
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new KaplanMeierEstimator<double>())
    .Build(features, times, events);
            
var risk = result.Predict(features);

Constructors

Constructor Summary
KaplanMeierEstimator() Gets the model type.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) Creates a new instance of the model with specified parameters.
Matrix<T> PredictSurvivalProbability(Matrix<T> x, Vector<T> times) Predicts survival probabilities at specified time points.
Vector<T> GetBaselineSurvival(Vector<T> times) Gets the baseline survival function at specified time points.
Vector<T> GetEventTimes() Gets the event times used in the survival curve.
Vector<T> GetSurvivalProbabilities() Gets the survival probability at each event time.
Vector<T> Predict(Matrix<T> x) Standard prediction - returns survival probability at a reference time.
Vector<T> PredictHazardRatio(Matrix<T> x) Predicts hazard ratios (all 1.0 for Kaplan-Meier since it doesn’t use covariates).
Vector<int> GetNumberAtRisk() Gets the number at risk at each event time.
Vector<int> GetNumberEvents() Gets the number of events at each event time.