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.coxph(time, event, X, *, terms=None, names=None, strata=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.
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) – Strata labels for stratified Cox (not yet implemented).
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, 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) – Strata labels for stratified KM (not yet implemented).
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)[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 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.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 timing¶