survivalgpu.CoxPHSurvivalAnalysis

class survivalgpu.CoxPHSurvivalAnalysis(alpha=0.0, ties='efron', maxiter=20, eps=1e-05, doscale=False, verbosity=0, mode=None, dtype=<class 'numpy.float64'>, device=None, nbootstraps=None, batchsize=None)

Bases: object

Cox Proportional Hazards model.

The API of this object is loosely based on scikit-survival.

Parameters:
  • alpha (float) – L2 regularization parameter.

  • ties (str) – Ties handling method. One of “efron”, “breslow”.

  • bootstrap (int) – Number of bootstrap samples to use.

  • batchsize (int) – Number of bootstrap samples to process in parallel.

  • maxiter (int) – Maximum number of Newton iterations.

  • eps (float) – Convergence tolerance.

  • doscale (bool) – Whether to scale the covariates for stability.

  • verbosity (int) – Verbosity level.

  • mode (str or None) – One of “unit length”, “start zero”, “any”. Assumptions made on the (start, stop] intervals. Defaults to None, which selects automatically the fastest backend.

  • dtype – np.float32 or np.float64. Defaults to np.float64.

__init__(alpha=0.0, ties='efron', maxiter=20, eps=1e-05, doscale=False, verbosity=0, mode=None, dtype=<class 'numpy.float64'>, device=None, nbootstraps=None, batchsize=None)

Methods

__init__([alpha, ties, maxiter, eps, ...])

bootstrap(*, dataset, n_batch, n_covariates, ...)

Fits the CoxPH model on a collection of bootstrap resamples.

fit(covariates, stop, *[, start, event, ...])

Fits the CoxPH model to the data and stores the results as attributes.

to_numpy()

bootstrap(*, dataset, n_batch, n_covariates, loss, init=None, indices=None, keep_indices=False)

Fits the CoxPH model on a collection of bootstrap resamples.

Shared building block for fit()’s own bootstrap loop, and for external callers (e.g. WCESurvivalAnalysis) that need to reuse the exact same resamples across several model configurations (e.g. several nknots candidates), so that each replicate is compared fairly across configurations.

Parameters:
  • dataset – the (sorted, scaled) TorchSurvivalDataset built by _prepare().

  • n_batch (int) – number of independent batches, as in fit().

  • n_covariates (int) – number of covariates, as in fit().

  • loss – the loss(*, bootstrap) closure built by _prepare().

  • init ((D,) float64 array, optional) – initial values for the coefficients. Defaults to zeros.

  • indices (optional) – a list of raw (B,P) patient-index tensors to replay instead of drawing new ones (see TorchSurvivalDataset.bootstrap_indices()). Defaults to None, i.e. draw self.nbootstraps fresh indices, chunked by self.batchsize.

  • keep_indices (bool, optional) – if True, store the index chunks (freshly-drawn, or replayed from indices) as bootstrap_indices_, so a caller can replay the exact same resamples in a later bootstrap() call. Defaults to False.

Sets self.bootstrap_coef_, of shape (nbootstraps, n_batch, n_covariates); self.bootstrap_loglik_, of shape (nbootstraps, n_batch); and self.bootstrap_n_events_, of shape (nbootstraps,) – the number of events in each replicate’s own resampled data (not the original dataset’s fixed event count), needed to compute a per-replicate information criterion.

fit(covariates, stop, *, start=None, event=None, patient=None, strata=None, batch=None, init=None, bootstrap_indices=None, keep_bootstrap_indices=False)

Fits the CoxPH model to the data and stores the results as attributes.

Parameters:
  • covariates ((I,D) float64 array) – covariate values for each interval.

  • stop ((I,) int64 array) – end time of each interval.

  • start ((I,) int64 array, optional) – start time of each interval. Defaults to 0 for every interval.

  • event ((I,) int64 array, optional) – 1 if the interval ends with an event (death), 0 if it is censored. Defaults to 1 for every interval.

  • patient ((I,) int64 array, optional) – patient id for each interval. Required to draw consistent bootstrap samples when several intervals belong to the same patient. Defaults to one interval per patient, i.e. patient = [0, 1, …, I-1].

  • strata ((I,) int64 array, optional) – stratum id for each interval. Defaults to a single stratum for all patients.

  • batch ((P,) int64 array, optional) – batch id for each patient. Independent CoxPH models are fitted for each batch. Defaults to a single batch for all patients.

  • init ((D,) float64 array, optional) – initial values for the coefficients. Defaults to zeros.

  • bootstrap_indices (optional) – a list of raw (B,P) patient-index tensors to replay instead of drawing new ones – see bootstrap(). Defaults to None, i.e. draw fresh indices.

  • keep_bootstrap_indices (bool, optional) – if True, store the (freshly-drawn or replayed) index chunks as bootstrap_indices_, so they can be replayed later by another fit() call. Defaults to False.

Results are stored as attributes: coef_, std_, means_, score_, loglik_, loglik_init_, sctest_init_, hessian_, imat_, iter_, and (if nbootstraps is set) bootstrap_coef_, bootstrap_loglik_ and bootstrap_n_events_ (see bootstrap()).

to_numpy()