cleverly.survival.CoxHazard#

class cleverly.survival.CoxHazard(terms: Literal['treatment', 'main', 'interaction'] = 'main', penalty: float = 0.0)[source]#

A Cox proportional-hazards model of one cause-specific hazard.

Parameters:
terms{“treatment”, “main”, “interaction”}

The linear predictor: the treatment alone, the treatment and the covariates, or those and every treatment-by-covariate product.

penaltyfloat

A ridge penalty \(\tfrac{1}{2}\,\text{penalty}\,\lVert\beta\rVert^2\) on the standardized coefficients. Zero fits the maximum partial-likelihood estimate.

Notes

Ties use the Breslow partial likelihood, and the baseline is the Breslow estimate \(d\Lambda_0(u) = d(u) / \sum_{i:\, T_i \ge u} e^{x_i^\top\beta}\), so CoxHazard("treatment") at \(\beta = 0\) is the Nelson-Aalen estimator.

Newton steps are halved until the likelihood does not fall and the standardized linear predictor’s spread over the fitting rows stays within \(0.8 \log(\text{max double})\), about 568, where the Breslow sums keep full precision. A regular fit ends inside that range. On separated data (a cause with no events in one arm, or a covariate that orders the event times) the partial likelihood keeps rising along one direction toward a limit it never reaches, so no maximum exists. The fit then either ends pinned at the range’s edge, or meets the score tolerance with the coefficients still running, which the infinite-coefficient test of R’s survival::coxph usually detects (a heuristic: a small separated sample can pass it). A detected case sets monotone_likelihood_ and warns with a ConvergenceWarning naming the coefficients that may be infinite. That fit is not a maximum-likelihood estimate: it is the point its Newton path reached, where the hazards the separating direction sends toward zero or infinity are effectively at their limit. A positive penalty gives the likelihood a finite maximum. converged_ says whether the score met its tolerance. A regular maximum with hazard ratios of about \(e^{568}\) between two fitting rows is beyond the range and is not reached.

Methods#

CoxHazard.cumulative_hazard(X: Any, times: Any) → NDArray[float64][source]#

\(\Lambda(t \mid x)\) at each requested time, right-continuous.

Parameters:
Xarray_like

(n, p) with the treatment in the first column.

timesarray_like

(m,) times.

Returns:
ndarray

(n, m) cumulative hazards.

CoxHazard.fit(X: Any, time: Any, event: Any) → CoxHazard[source]#

Fit the coefficients and the Breslow baseline.

Parameters:
Xarray_like

(n, p) with the treatment in the first column.

timearray_like

(n,) follow-up times.

eventarray_like

(n,) 0/1 indicators of the modelled cause.

Returns:
CoxHazard

This learner, fitted.

CoxHazard.hazard_factors(X: Any, times: Any) → tuple[NDArray[float64], NDArray[float64]][source]#

The two factors of cumulative_hazard(): one scale per row, one baseline per time.

cumulative_hazard(X, times) is scale[:, None] * base[None, :] bit for bit. The scale \(e^{x^\top\beta}\) of a row depends on the rows it is computed with in its last bits (a matrix-vector product), so a caller that rebuilds the hazards of some rows later keeps the scales of one call rather than recomputing them on a subset.

Parameters:
Xarray_like

(n, p) with the treatment in the first column.

timesarray_like

(m,) times.

Returns:
scalendarray

(n,) \(e^{x_i^\top\beta}\).

basendarray

(m,) the Breslow cumulative baseline hazard at each time, right-continuous.

CoxHazard.linear_predictor(X: Any) → NDArray[float64][source]#

The fitted \(x^\top\beta\) on the standardized scale the baseline reads.

Parameters:
Xarray_like

(n, p) with the treatment in the first column.

Returns:
ndarray

(n,) linear predictors.

Attributes#

CoxHazard.label#

The model in words, as a summary prints it.