SurvivalModelBase<T>
Base Classes · AiDotNet.SurvivalAnalysis
Abstract base class for survival analysis models.
Implements: IModelShape, IParameterChunkSource<T>, IParameterManifestProvider, ISurvivalModel<T>
Known derived types: CoxProportionalHazards<T>, KaplanMeierEstimator<T>, LogNormalAFT<T>, NelsonAalenEstimator<T>, RandomSurvivalForest<T>, WeibullAFT<T>
For Beginners
This class contains shared code that all survival models need, so each specific model (like Cox or Kaplan-Meier) doesn’t have to reimplement it.
Key shared functionality:
- Validating input data (times must be positive, events must be 0 or 1)
- Calculating the concordance index (how well the model predicts)
- Finding median survival times from survival curves
- Managing trained model state
How It Works
This base class provides common functionality for survival models including data validation, concordance index calculation, and baseline survival estimation.
Properties
| Property | Summary |
|---|---|
ILossFunction<T> DefaultLossFunction { get; } |
Gets the default loss function. |
ParameterLayoutSnapshot ParameterLayout { get; } |
|
Vector<T> BaselineSurvival { get; } |
Gets the baseline survival function values at event times. |
Vector<T> EventTimes { get; } |
Gets the unique event times from the training data. |
bool IsTrained { get; } |
Gets whether the model is trained. |
bool SupportsParameterInitialization { get; } |
|
long ParameterCount { get; } |
|
string[] FeatureNames { get; set; } |
Gets or sets the feature names. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> GetFeatureImportance() |
Gets the feature importance scores. |
DynamicShapeInfo GetDynamicShapeInfo() |
|
IEnumerable<ParameterChunk<T>> GetParameterStateChunks() |
Enumerates canonical persistent state without flattening tensor-backed components. |
IEnumerable<Tensor<T>> GetParameterChunks() |
Streams state in the same order as the parameter manifest. |
IEnumerable<int> GetActiveFeatureIndices() |
Gets the indices of features that are actively used in the model. |
IFullModel<T, Matrix<T>, Vector<T>> Clone() |
Creates a clone of the model. |
IFullModel<T, Matrix<T>, Vector<T>> DeepCopy() |
Creates a deep copy of the model. |
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
Creates a new instance of the model with specified parameters. |
Matrix<T> PredictCumulativeHazard(Vector<T> times, Matrix<T> features) |
Predicts cumulative hazard at specified times (interface method). |
Matrix<T> PredictSurvival(Vector<T> times, Matrix<T> features) |
Predicts survival probability at specified times (interface method). |
Matrix<T> PredictSurvivalProbability(Matrix<T> x, Vector<T> times) |
Predicts survival probabilities at specified time points. |
ModelMetadata<T> GetModelMetadata() |
Gets the model type. |
T CalculateConcordanceIndex(Matrix<T> x, Vector<T> times, Vector<int> events) |
Calculates the concordance index (C-index) for model evaluation. |
Vector<T> ComputeGradients(Matrix<T> input, Vector<T> target, ILossFunction<T> lossFunction) |
Computes gradients for the given input and target. |
Vector<T> GetBaselineSurvival(Vector<T> times) |
Gets the baseline survival function. |
Vector<T> GetParameters() |
|
Vector<T> Predict(Matrix<T> input) |
Standard prediction - returns hazard ratios or survival at median time. |
Vector<T> PredictHazardRatio(Matrix<T> x) |
Predicts hazard ratios relative to a baseline. |
Vector<T> PredictMedianSurvivalTime(Matrix<T> features) |
Gets the estimated median survival time (interface method). |
Vector<T> PredictRisk(Matrix<T> features) |
Predicts risk scores for subjects (interface method). |
Vector<T> SanitizeParameters(Vector<T> parameters) |
|
bool IsFeatureUsed(int featureIndex) |
Determines whether a specific feature is used in the model. |
byte[] Serialize() |
Serializes the model to a byte array. |
int[] GetInputShape() |
Saves the model to a file. |
int[] GetOutputShape() |
|
void ApplyGradients(Vector<T> gradients, T learningRate) |
Applies gradients to update model parameters. |
void Deserialize(byte[] modelData) |
Deserializes the model from a byte array. |
void Dispose() |
|
void Fit(Vector<T> times, Vector<T> events, Matrix<T> features) |
Fits the survival model to time-to-event data (interface method). |
void FitSurvival(Matrix<T> x, Vector<T> times, Vector<int> events) |
Fits the survival model to time-to-event data. |
void LoadModel(string filePath) |
Loads the model from a file. |
void LoadState(Stream stream) |
Loads the model’s state from a stream. |
void SaveModel(string filePath) |
|
void SaveState(Stream stream) |
Saves the model’s state to a stream. |
void SetActiveFeatureIndices(IEnumerable<int> featureIndices) |
Sets the active feature indices for this model. |
void SetParameterChunks(IEnumerable<Tensor<T>> chunks) |
Restores tensor-backed chunks on their device and retains custom-setter semantics. |
void SetParameters(Vector<T> parameters) |
|
void Train(Matrix<T> x, Vector<T> y) |
Standard model training — splits the design matrix and redirects to survival-specific training. |