Survival Analysis¶
Kaplan-Meier estimation, log-rank test (G-rho family), Cox proportional hazards (CPU), and discrete-time survival models (GPU-accelerated).
Survival analysis.
- Public API:
kaplan_meier(time, event, …) -> KMSolution survdiff(time, event, group, …) -> LogRankSolution coxph(time, event, X, …) -> CoxSolution # CPU only discrete_time(time, event, X, …) -> DiscreteTimeSolution # GPU accelerated
- pystatistics.survival.cox_zph(fit, *, transform='km')[source]¶
Test the proportional-hazards assumption of a fitted Cox model.
Score test on scaled Schoenfeld residuals — the survival >= 3.0 formulation of
survival::cox.zph(not the pre-3.0 correlation test). Reports one chi-square per covariate plus a GLOBAL test, and carries the scaled Schoenfeld residuals for plotting.- Parameters:
fit (CoxSolution) – A fitted Cox model from
coxph()(stratified or not). The fit retains its data for exactly this purpose.transform (str) – Time transform g(t): “km” (default, left-continuous KM CDF of the pooled sample — R’s default), “rank”, “identity”, or “log”.
- Return type:
- pystatistics.survival.coxph(time, event, X, *, terms=None, names=None, strata=None, start=None, robust=False, cluster=None, ties='efron', tol=1e-09, max_iter=20, conf_level=0.95)[source]¶
Cox proportional hazards model.
CPU only — no backend parameter. Matches R’s survival::coxph().
- Parameters:
time (array-like) – Time to event or censoring. With
start, this is the risk-interval exit (R’sstop).event (array-like) – Event indicator (1=event, 0=censored).
X (array-like or DataSource) – Covariate matrix (n, p). No intercept — Cox model has no intercept. When
termsis given,Xis instead the data source (a DataSource or column mapping) from which the term spec pulls columns.terms (sequence or None) – Structured term spec for categorical predictors and interactions (bare column names, C(name, ref=…), or tuples of those). When given, the covariate matrix is built from
X(the source) with no intercept, and the expanded column labels are used automatically. Mutually exclusive withnames.strata (array-like or None) – Stratum label per observation. When given, fits a stratified Cox model: one shared coefficient vector, a separate baseline hazard (separate risk sets) per stratum. Matches
coxph(Surv(t, e) ~ x + strata(g)).start (array-like or None) – Counting-process start time per ROW: the row is at risk on
(start, time]. A subject may span several rows with different covariate values (time-varying covariates), or a single row with a delayed entry (left truncation). Matchescoxph(Surv(start, stop, event) ~ x)withtimeas the stop. Each start must be strictly less than itstime(R NA-drops such rows with a warning; PyStatistics refuses loudly instead).robust (bool) – If True, report the Lin-Wei sandwich (robust / Huber-White) standard errors instead of the model-based ones;
.z_values/.p_values/.conf_intthen use the robust SE, and.naive_standard_errorskeeps the model-based SE. Matchescoxph(..., robust=TRUE). (Distinct fromtimeseries’s LOESS-robustnessrobust— CONVENTIONS A9: a robust variance estimator here, not outlier downweighting.)cluster (array-like or None) – Cluster label per row for grouped-robust SE (correlated rows of one subject/cluster count as one independent unit). Implies
robust=True. Matchescoxph(..., cluster=id).ties (str) – Method for handling tied event times: “efron” (default) or “breslow”.
tol (float) – Convergence tolerance for Newton-Raphson.
max_iter (int) – Maximum Newton-Raphson iterations.
conf_level (float) – Confidence level for
.conf_int(default 0.95). Wald intervals on the coefficient (log-hazard-ratio) scale;exp(.conf_int)gives hazard- ratio intervals.
- Return type:
- pystatistics.survival.discrete_time(time, event, X, *, names=None, intervals=None, backend=None, conf_level=0.95)[source]¶
Discrete-time survival via person-period logistic regression.
GPU-accelerated — delegates to regression.fit(family=’binomial’).
- Parameters:
time (array-like) – Time to event or censoring.
event (array-like) – Event indicator (1=event, 0=censored).
X (array-like) – Covariate matrix (n, p). No intercept — interval dummies serve as the intercept.
intervals (array-like or None) – Time interval boundaries. If None, uses unique event times.
backend (str or None) – Backend for the person-period logistic regression, forwarded to
regression.fit. Default None → ‘cpu’ (the R-reference path). Explicit values: “cpu” (float64), “gpu” (float32, CUDA or Apple Silicon), “gpu_fp64” (float64, CUDA only — the exact double-precision GPU path), or “auto” (GPU-fp32 if CUDA present, else CPU).conf_level (float) – Confidence level for
.conf_int(default 0.95). Wald intervals on the covariate coefficient scale;exp(.conf_int)gives discrete-time hazard-ratio intervals.
- Return type:
- pystatistics.survival.kaplan_meier(time, event, *, strata=None, entry=None, conf_level=0.95, conf_type='log')[source]¶
Kaplan-Meier survival curve estimation.
Matches R’s survival::survfit(Surv(time, event) ~ 1).
- Parameters:
time (array-like) – Time to event or censoring.
event (array-like) – Event indicator (1=event, 0=censored).
strata (array-like or None) – Stratum label per observation. When given, returns a
StratifiedKMSolutionholding one product-limit curve per stratum (matchingsurvfit(Surv(time, event) ~ g)).entry (array-like or None) – Delayed-entry (left-truncation) time per subject: the subject is at risk on
(entry, time], so early risk sets exclude subjects who have not yet entered. Matchessurvfit(Surv(entry, time, event)). Each entry must be strictly less than the correspondingtime.conf_level (float) – Confidence level for CI (default 0.95).
conf_type (str) – CI transformation: “log” (R default), “plain”, “log-log”.
- Return type:
- pystatistics.survival.survdiff(time, event, group, *, rho=0.0)[source]¶
Log-rank test (and G-rho family).
Matches R’s survival::survdiff().
- Parameters:
time (array-like) – Time to event or censoring.
event (array-like) – Event indicator (1=event, 0=censored).
group (array-like) – Group labels (e.g. treatment vs control).
rho (float) – G-rho weight parameter. rho=0 (default) gives the standard log-rank test. rho=1 gives Peto & Peto / Gehan-Wilcoxon.
- Return type:
- class pystatistics.survival.CoxSolution(_result, _names=None, _design=None)[source]¶
Bases:
SolutionReprMixinCox proportional hazards solution.
Properties mirror R’s coxph() output.
- property coefficients¶
- property hazard_ratios¶
- property standard_errors¶
- property z_values¶
- property p_values¶
- property loglik¶
- property naive_standard_errors¶
Model-based SEs when
robust— else the same asstandard_errors.
- property conf_int¶
Wald confidence intervals for the coefficients, shape (p, 2).
coef ± z * seon the coefficient (log-hazard-ratio) scale, wherezis the normal quantile forconf_level(Cox inference is asymptotic-normal).exp(conf_int)gives hazard-ratio intervals.
- property timing¶
- class pystatistics.survival.CoxZphSolution(_data, _names)[source]¶
Bases:
SolutionReprMixinProportional-hazards test result (one row per covariate + GLOBAL).
Mirrors R’s
cox.zphobject:.chisq/.df/.p_valuesare aligned with.row_names(covariates first,"GLOBAL"last);.residualsholds the scaled Schoenfeld residuals (one row per event, time-ordered within stratum),.xthe transformed event times,.timethe raw event times.- property residuals: ndarray[tuple[Any, ...], dtype[_ScalarT]]¶
Scaled Schoenfeld residuals, shape (n_events, p).
- property x: ndarray[tuple[Any, ...], dtype[_ScalarT]]¶
Transformed event times aligned with
residualsrows.
- property time: ndarray[tuple[Any, ...], dtype[_ScalarT]]¶
Raw event times aligned with
residualsrows.
- class pystatistics.survival.DiscreteTimeSolution(_result, _names=None, _conf_level=0.95)[source]¶
Bases:
SolutionReprMixinDiscrete-time survival model solution.
Properties mirror the logistic regression on person-period data.
- Parameters:
- property coefficients¶
- property standard_errors¶
- property z_values¶
- property p_values¶
- property conf_int¶
Wald confidence intervals for the covariate coefficients, shape (p, 2).
coef ± z * seon the (discrete-time log-hazard) coefficient scale, with the normal quantile forconf_level(the person-period logistic fit is asymptotic-normal).exp(conf_int)gives discrete-time hazard-ratio intervals.
- property hazard_ratios¶
- property baseline_hazard¶
- property interval_labels¶
- property converged: bool¶
Whether the person-period binomial IRLS converged.
Falsemeans the underlying logistic fit hit the iteration cap (or, for an all-censored design with no events, that no fit ran) — treat the coefficients as unreliable. MirrorsCoxSolution.converged.
- property n_iter: int¶
IRLS iterations used by the person-period binomial GLM.
Mirrors
CoxSolution.n_iter. A value at the solver’s iteration cap alongsideconverged == Falseindicates a fit that did not settle.
- property timing¶
- class pystatistics.survival.KMSolution(_result)[source]¶
Bases:
SolutionReprMixinKaplan-Meier survival curve solution.
Properties mirror R’s survfit() output.
- Parameters:
_result (Result[KMParams])
- property time¶
Unique event times.
- property survival¶
S(t) at each event time.
- property n_risk¶
Number at risk just before each event time.
- property n_events¶
Number of events at each event time.
- property n_censored¶
Number censored in each interval.
- property se¶
Greenwood standard error of S(t).
- property standard_errors¶
Greenwood standard error of S(t) (uniform-accessor alias of
se).
- property ci_lower¶
Lower confidence bound for S(t).
- property ci_upper¶
Upper confidence bound for S(t).
- property conf_int¶
Confidence band for S(t) at each event time, shape (m, 2).
Columns are
[lower, upper]— the uniform-accessor form ofci_lower/ci_upper(which remain available).
- property median_survival: float | None¶
Median survival time, matching R survfit’s
minminconvention.The median is the smallest
twithS(t) <= 0.5; but when the curve touches exactly 0.5 (a flat step at 0.5), R averages that time with the next time the curve drops strictly below 0.5. Returns None if the curve never reaches 0.5.
- property timing¶
- class pystatistics.survival.LogRankSolution(_result)[source]¶
Bases:
SolutionReprMixinLog-rank test solution.
Properties mirror R’s survdiff() output.
- Parameters:
_result (Result[LogRankParams])
- property observed¶
- property expected¶
- property n_per_group¶
- property group_labels¶
- property timing¶
- class pystatistics.survival.StratifiedKMSolution(_curves, _timing=None)[source]¶
Bases:
SolutionReprMixinStratified Kaplan-Meier result: one survival curve per stratum.
Wraps an ordered mapping
{stratum_label: KMSolution}. Index by label (sol["A"]) or iteratesol.curves.items()to reach each stratum’s ordinaryKMSolution.- Parameters:
_curves (dict)
- stratum(label)[source]¶
The
KMSolutionfor one stratum.
- property timing¶