torchsurv.loss.weibull#

Functions

cumulative_hazard(log_params, time[, all_times])

Cumulative hazard for the Weibull Accelerated Time Failure (AFT) survival model.

log_hazard(log_params, time[, all_times])

Log hazard of the Weibull Accelerated Time Failure (AFT) survival model.

neg_log_likelihood(log_params, event, time)

Negative of the log likelihood for the Weibull Accelerated Time Failure (AFT) survival model.

survival_function(log_params, time[, all_times])

Survival function for the Weibull Accelerated Time Failure (AFT) survival model.

torchsurv.loss.weibull.neg_log_likelihood(log_params: Tensor, event: Tensor, time: Tensor, reduction: str = 'mean', checks: bool = True) Tensor[source]#

Negative of the log likelihood for the Weibull Accelerated Time Failure (AFT) survival model.

Parameters:
  • log_params (torch.Tensor, float) – Parameters of the Weibull distribution of shape = (n_samples, 1) or (n_samples, 2). The first column corresponds to the log scale parameter. The second column corresponds to the log shape parameter. If the log shape parameter is missing, it is imputed with 0.

  • event (torch.Tensor, bool) – Event indicator of length n_samples (= True if event occured).

  • time (torch.Tensor, float) – Time-to-event or censoring of length n_samples.

  • reduction (str) – Method to reduce losses. Defaults to “mean”. Must be one of the following: “sum”, “mean”.

  • checks (bool) – Whether to perform input format checks. Enabling checks can help catch potential issues in the input data. Defaults to True.

Returns:

Negative of the log likelihood.

Return type:

(torch.Tensor, float)

Note

For each subject \(i \in \{1, \cdots, N\}\), denote \(X_i\) as the survival time and \(D_i\) as the censoring time. Survival data consist of the event indicator, \(\delta_i=1(X_i\leq D_i)\) (argument event) and the time-to-event or censoring, \(T_i = \min(\{ X_i,D_i \})\) (argument time).

The log hazard function for the Weibull AFT survival model [Car03] of subject \(i\) at time \(t\) has the form:

\[\log h_i(t) = \log{\rho_i} - \log{\lambda_i} + (\rho_i -1) \left( \log{t} - \log{\lambda_i}\right)\]

where \(\log{\lambda_i}\) is the log scale parameter (first column of argument log_params) and \(\log{\rho_i}\) is the log shape parameter (second column of argument log_params). The cumulative hazard for the Weibull survival model of subject \(i\) at time \(t\) has the form:

\[H_i(t) = \left(\frac{t}{\lambda_i}\right)^{\rho_i}\]

The survival function for the Weibull survival model of subject \(i\) at time \(t\) has the form:

\[S_i(t) = 1 - F(t | \lambda_i, \rho_i)\]

where \(F(t | \lambda, \rho)\) is the cumulative distribution function (CDF) of the Weibull distribution given scale parameter \(\lambda\) and shape parameter \(\rho\).

The log likelihood of the Weibull survival model is

\[ll = \sum_{i: \delta_i = 1} \log h_i(T_i) - \sum_{i = 1}^N H_i(T_i)\]

Examples

>>> _ = torch.manual_seed(42)
>>> n = 4
>>> log_params = torch.randn((n, 2))
>>> event = torch.randint(low=0, high=2, size=(n,), dtype=torch.bool)
>>> time = torch.randint(low=1, high=100, size=(n,))
>>> neg_log_likelihood(log_params, event, time) # Default: mean of log likelihoods across subject
tensor(47.5035)
>>> neg_log_likelihood(log_params, event, time, reduction = 'sum') # Sum of log likelihoods across subject
tensor(190.0141)
>>> neg_log_likelihood(torch.randn((n, 1)), event, time)  # Missing shape: exponential decrease
tensor(66.7203)

References

[Car03]

Kevin J. Carroll. On the use and utility of the weibull model in the analysis of survival data. Controlled Clinical Trials, 24(6):682–701, December 2003.

torchsurv.loss.weibull.survival_function(log_params: Tensor, time: Tensor, all_times: bool = True) Tensor[source]#

Survival function for the Weibull Accelerated Time Failure (AFT) survival model.

Parameters:
  • log_params (torch.Tensor, float) – Parameters of the Weibull distribution of shape = (n_samples, 1) or (n_samples, 2). The first column corresponds to the log scale parameter. The second column corresponds to the log shape parameter. If the log shape parameter is missing, it is imputed with 0.

  • time (torch.Tensor, float) – Time at which to evaluate the survival function. Should be of length n_samples to evaluate the survival function at observed time-to-event or censoring, or of length one to evaluate the survival function at a new time.

  • all_times (bool) – If True, subject-specific survival function is evaluated at all time (used for evaluation metrics). If False, subject-specific survival function is evaluated at respective time. Defaults is True. Ignored if time is of length one.

Returns:

Subject-specific survival function evaluated at time.

Return type:

(torch.Tensor, float)

Examples

>>> _ = torch.manual_seed(42)
>>> time = torch.randint(low=1, high=100, size=(4,))
>>> log_params = torch.randn((4, 2))
>>> survival_function(log_params, time, all_times = False)  # Survival at respective time
tensor([0.0002, 0.0000, 0.0299, 0.0000])
>>> survival_function(log_params, time, all_times = True)  # Default. Survival at all observed time
tensor([[1.7941e-04, 0.0000e+00, 0.0000e+00, 0.0000e+00],
        [2.8610e-06, 0.0000e+00, 0.0000e+00, 0.0000e+00],
        [4.1870e-01, 3.1040e-02, 2.9881e-02, 6.8224e-02],
        [9.5576e-04, 0.0000e+00, 0.0000e+00, 0.0000e+00]])
>>> survival_function(log_params, time=torch.tensor(10.0))  # Survival at one new time (e.g., 10 years)
tensor([1.3709e-06, 5.9605e-08, 3.4954e-01, 1.5438e-05])
>>> for t in torch.tensor([100.0, 150.0]): survival_function(log_params, time=t)  # Subject-specific survival at multiple new times
tensor([0.0000, 0.0000, 0.0288, 0.0000])
tensor([0.0000, 0.0000, 0.0123, 0.0000])
torchsurv.loss.weibull.log_hazard(log_params: Tensor, time: Tensor, all_times: bool = True) Tensor[source]#

Log hazard of the Weibull Accelerated Time Failure (AFT) survival model.

Parameters:
  • log_params (torch.Tensor, float) – Parameters of the Weibull distribution of shape = (n_samples, 1) or (n_samples, 2). The first column corresponds to the log scale parameter. The second column corresponds to the log shape parameter. If the log shape parameter is missing, it is imputed with 0.

  • time (torch.Tensor, float) – Time at which to evaluate the log hazard. Should be of length n_samples to evaluate the log hazard at observed time-to-event or censoring, or of length one to evaluate the log hazard at a new time.

  • all_times (bool) – If True, subject-specific log hazard is evaluated at all time (used for evaluation metrics). If False, subject-specific log hazard is evaluated at respective time. Defaults is True. Ignored if time is of length one.

Returns:

Subject-specific log hazard evaluated at time.

Return type:

(torch.Tensor, float)

Examples

>>> _ = torch.manual_seed(42)
>>> time = torch.randint(low=1, high=100, size=(4,))
>>> log_params = torch.randn((4, 2))
>>> log_hazard(log_params, time, all_times = False)  # Log hazard at respective time
tensor([ 0.4392, -0.0303, -3.9672,  0.9140])
>>> log_hazard(log_params, time, all_times = True)  # Default. Log hazard at all time
tensor([[ 0.4392,  1.1174,  1.1227,  0.9913],
        [ 0.4148, -0.0303, -0.0338,  0.0525],
        [-2.7225, -3.9575, -3.9672, -3.7279],
        [ 0.2606,  1.0632,  1.0695,  0.9140]])
>>> log_hazard(log_params, time=torch.tensor(10.0))  # Log hazard at one new time (e.g., 10 years)
tensor([ 0.5316,  0.3542, -2.8907,  0.3699])
>>> for t in torch.tensor([100.0, 150.0]): log_hazard(log_params, time=t)  # Subject-specific log hazard at multiple new times
tensor([ 1.1280, -0.0372, -3.9767,  1.0757])
tensor([ 1.2330, -0.1062, -4.1680,  1.1999])
>>> log_params  *= 1e2  # Increase scale
>>> log_hazard(log_params, time, all_times = False)  # Check for Torch.Inf values
tensor([-1.0000e+10, -2.3197e+01, -6.8385e+01, -1.0000e+10])
torchsurv.loss.weibull.cumulative_hazard(log_params: Tensor, time: Tensor, all_times: bool = True) Tensor[source]#

Cumulative hazard for the Weibull Accelerated Time Failure (AFT) survival model.

Parameters:
  • log_params (torch.Tensor, float) – Parameters of the Weibull distribution of shape = (n_samples, 1) or (n_samples, 2). The first column corresponds to the log scale parameter. The second column corresponds to the log shape parameter. If the log shape parameter is missing, it is imputed with 0.

  • time (torch.Tensor, float) – Time-to-event or censoring of length n_samples.

  • all_times (bool) – If True, subject-specific cumulative hazard is evaluated at all time (used for evaluation metrics). If False, subject-specific cumulative hazard is evaluated at respective time. Defaults is True.

Returns:

Subject-specific cumulative hazard evaluated at time.

Return type:

(torch.Tensor, float)

Examples

>>> _ = torch.manual_seed(42)
>>> time = torch.randint(low=1, high=100, size=(4,))
>>> log_params = torch.randn((4, 2))
>>> cumulative_hazard(log_params, time, all_times=False) # Cumulative hazard at respective time
tensor([  8.6257, 112.2115,   3.5105, 112.6339])
>>> cumulative_hazard(log_params, time, all_times=True) # Default. Cumulative hazard at all time
tensor([[  8.6257, 233.0865, 239.2167, 126.2805],
        [ 12.7698, 112.2115, 114.1484,  74.9134],
        [  0.8706,   3.4725,   3.5105,   2.6850],
        [  6.9530, 212.7592, 218.5687, 112.6339]])