torchsurv.loss.cox

torchsurv.loss.cox#

Functions

neg_partial_log_likelihood(log_hz, event, time)

Compute the negative of the partial log likelihood for the Cox proportional hazards model.

torchsurv.loss.cox.neg_partial_log_likelihood(log_hz: Tensor, event: Tensor, time: Tensor, ties_method: str = 'efron', reduction: str = 'mean', checks: bool = True) Tensor[source]#

Compute the negative of the partial log likelihood for the Cox proportional hazards model.

Parameters:
  • log_hz (torch.Tensor, float) – Log relative hazard of length n_samples.

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

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

  • ties_method (str) – Method to handle ties in event time. Defaults to “efron”. Must be one of the following: “efron”, “breslow”.

  • 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 partial 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 Cox proportional hazards model has the form:

\[\log \lambda_i (t) = \log \lambda_{0}(t) + \log \theta_i\]

where \(\log \theta_i\) is the log relative hazard (argument log_hz).

No ties in event time. If the set \(\{T_i: \delta_i = 1\}_{i = 1, \cdots, N}\) represent unique event times (i.e., no ties), the standard Cox partial likelihood can be used [Cox72]. Let \(\tau_1 < \tau_2 < \cdots < \tau_N\) be the ordered times and let \(R(\tau_i) = \{ j: \tau_j \geq \tau_i\}\) be the risk set at \(\tau_i\). The partial log likelihood is defined as:

\[pll = \sum_{i: \: \delta_i = 1} \left(\log \theta_i - \log\left(\sum_{j \in R(\tau_i)} \theta_j \right) \right)\]

Ties in event time handled with Breslow’s method. Breslow’s method [Bre75] describes the approach in which the procedure described above is used unmodified, even when ties are present. If two subjects A and B have the same event time, subject A will be at risk for the event that happened to B, and B will be at risk for the event that happened to A. Let \(\xi_1 < \xi_2 < \cdots\) denote the unique ordered times (i.e., unique \(\tau_i\)). Let \(H_k\) be the set of subjects that have an event at time \(\xi_k\) such that \(H_k = \{i: \tau_i = \xi_k, \delta_i = 1\}\), and let \(m_k\) be the number of subjects that have an event at time \(\xi_k\) such that \(m_k = |H_k|\).

\[pll = \sum_{k} \left( {\sum_{i\in H_{k}}\log \theta_i} - m_k \: \log\left(\sum_{j \in R(\tau_k)} \theta_j \right) \right)\]

Ties in event time handled with Efron’s method. An alternative approach that is considered to give better results is the Efron’s method [Efr77]. As a compromise between the Cox’s and Breslow’s method, Efron suggested to use the average risk among the subjects that have an event at time \(\xi_k\):

\[\bar{\theta}_{k} = {\frac {1}{m_{k}}}\sum_{i\in H_{k}}\theta_i\]

Efron approximation of the partial log likelihood is defined by

\[pll = \sum_{k} \left( {\sum_{i\in H_{k}}\log \theta_i} - \sum_{r =0}^{m_{k}-1} \log\left(\sum_{j \in R(\xi_k)}\theta_j-r\:\bar{\theta}_{j}\right)\right)\]

Examples

>>> log_hz = torch.tensor([0.1, 0.2, 0.3, 0.4, 0.5])
>>> event = torch.tensor([1, 0, 1, 0, 1], dtype=torch.bool)
>>> time = torch.tensor([1., 2., 3., 4., 5.])
>>> neg_partial_log_likelihood(log_hz, event, time) # default, mean of log likelihoods across patients
tensor(1.0071)
>>> neg_partial_log_likelihood(log_hz, event, time, reduction = 'sum') # sun of log likelihoods across patients
tensor(3.0214)
>>> time = torch.tensor([1., 2., 2., 4., 5.])  # Dealing with ties (default: Efron)
>>> neg_partial_log_likelihood(log_hz, event, time, ties_method = "efron")
tensor(1.0873)
>>> neg_partial_log_likelihood(log_hz, event, time, ties_method = "breslow")  # Dealing with ties (Bfron)
tensor(1.0873)

References

[Bre75]

N. E. Breslow. Analysis of survival data under the proportional hazards model. International Statistical Review / Revue Internationale de Statistique, 43(1):45, April 1975.

[Cox72]

D. R. Cox. Regression models and life‐tables. Journal of the Royal Statistical Society: Series B (Methodological), 34(2):187–202, January 1972.

[Efr77]

Bradley Efron. The efficiency of cox’s likelihood function for censored data. Journal of the American Statistical Association, 72(359):557–565, September 1977.