CoxProportionalHazards<T>
Models & Types · AiDotNet.SurvivalAnalysis
Implements the Cox Proportional Hazards model for survival analysis with covariates.
Inheritance: SurvivalModelBase<T> → CoxProportionalHazards<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
Cox Proportional Hazards is the most widely used survival model because it can tell you HOW features affect survival risk without making assumptions about the shape of survival over time.
The key equation is: h(t|X) = h₀(t) × exp(β₁X₁ + β₂X₂ + …)
Where:
- h(t|X) = hazard (instantaneous risk) at time t for a subject with features X
- h₀(t) = baseline hazard (unknown, estimated non-parametrically)
- β = coefficients that tell you how each feature affects risk
- X = feature values
Interpreting coefficients:
- β > 0: Higher feature value → Higher risk (shorter survival)
- β < 0: Higher feature value → Lower risk (longer survival)
- exp(β) = Hazard Ratio: How much risk changes per unit increase in feature
Example: If β_age = 0.05, then exp(0.05) ≈ 1.05, meaning each year of age increases the hazard (risk) by about 5%.
The “proportional hazards” assumption means that hazard ratios are constant over time. If this assumption is violated, consider stratified Cox or time-varying coefficients.
References:
- Cox (1972). “Regression Models and Life-Tables”
How It Works
The Cox model is a semi-parametric survival model that estimates the effect of covariates on the hazard (risk) without assuming a specific form for the baseline hazard function.
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 CoxProportionalHazards<double>(learningRate: 0.01, maxIterations: 1000))
.Build(features, times, events);
var risk = result.Predict(features);
Constructors
| Constructor | Summary |
|---|---|
CoxProportionalHazards(double learningRate, int maxIterations, double tolerance, double l2Penalty, int? seed) |
Gets the model type. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> GetFeatureImportance() |
Gets the feature importance scores based on coefficient magnitudes. |
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> GetCoefficients() |
Gets the estimated coefficients (log hazard ratios). |
Vector<T> GetFeatureHazardRatios() |
Gets the hazard ratios for each feature. |
Vector<T> Predict(Matrix<T> x) |
Standard prediction - returns hazard ratios. |
Vector<T> PredictHazardRatio(Matrix<T> x) |
Predicts hazard ratios for each subject. |