RandomSurvivalForest<T>

Models & Types · AiDotNet.SurvivalAnalysis

Implements Random Survival Forest for survival analysis using ensemble of survival trees.

Inheritance: SurvivalModelBase<T> → RandomSurvivalForest<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

Random Survival Forest extends Random Forest to handle survival data. Instead of predicting classes or values, it predicts survival curves. Each tree uses log-rank statistics to find splits that maximize survival difference between groups.

How It Works

How it works:

  • Build many survival trees using bootstrap samples
  • At each node, select random features and find the split that maximizes log-rank statistic
  • Store survival estimates at each terminal node
  • Average survival curves from all trees for prediction

Key advantages:

  • Handles non-linear relationships and interactions automatically
  • Provides variable importance through permutation
  • Robust to outliers and doesn’t require proportional hazards assumption

Reference: Ishwaran et al., “Random Survival Forests” (2008)

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 RandomSurvivalForest<double>(numTrees: 100, maxDepth: 10))
    .Build(features, times, events);
            
var risk = result.Predict(features);

Constructors

Constructor Summary
RandomSurvivalForest(int numTrees, int maxDepth, int minSamplesLeaf, int maxFeatures, int? seed, bool bootstrap) Creates a new Random Survival Forest.

Properties

Property Summary
bool Bootstrap { get; } Gets whether each tree is grown on a bootstrap sample (true, as in Ishwaran et al.) or on the full data.
int MaxDepth { get; } Gets the maximum depth of each tree.
int MaxFeatures { get; set; } Gets the number of features to consider at each split.
int MinSamplesLeaf { get; } Gets the minimum samples per leaf.
int NumTrees { get; } Gets the number of trees in the forest.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
Matrix<T> PredictCumulativeHazard(Vector<T> times, Matrix<T> features) Predicts the ensemble cumulative hazard H_e(t | x) at the specified times.
Matrix<T> PredictSurvivalProbability(Matrix<T> x, Vector<T> times) Predicts survival probabilities at specified times.
Vector<T> GetBaselineSurvival(Vector<T> times) Gets the baseline survival function.
Vector<T> Predict(Matrix<T> input) Predicts median survival time.
Vector<T> PredictHazardRatio(Matrix<T> x) Predicts the ensemble mortality (higher = higher risk).