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.
- penalty
float 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::coxphusually detects (a heuristic: a small separated sample can pass it). A detected case setsmonotone_likelihood_and warns with aConvergenceWarningnaming 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 positivepenaltygives 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:
CoxHazardThis 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)isscale[:, 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:
- 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.