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, *, strata=None, ties='efron', tol=1e-09, max_iter=20)[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) – Covariate matrix (n, p). No intercept — Cox model has no intercept.

  • 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.

Return type:

CoxSolution

pystatistics.survival.discrete_time(time, event, X, *, intervals=None, backend='auto')[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) – Backend for logistic regression: “auto”, “cpu”, or “gpu”. “auto” selects GPU if available, else CPU.

Return type:

DiscreteTimeSolution

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:

KMSolution

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:

LogRankSolution

class pystatistics.survival.CoxSolution(_result)[source]

Bases: object

Cox proportional hazards solution.

Properties mirror R’s coxph() output.

Parameters:

_result (Result[CoxParams])

property coefficients
property hazard_ratios
property standard_errors
property z_statistics
property p_values
property loglik
property concordance: float
property n_events: int
property n_observations: int
property n_iter: int
property converged: bool
property ties: str
property backend_name: str
property timing
summary()[source]

R-style summary of Cox PH fit.

Return type:

str

class pystatistics.survival.DiscreteTimeSolution(_result)[source]

Bases: object

Discrete-time survival model solution.

Properties mirror the logistic regression on person-period data.

Parameters:

_result (Result[DiscreteTimeParams])

property coefficients
property standard_errors
property z_statistics
property p_values
property hazard_ratios
property baseline_hazard
property interval_labels
property person_period_n: int
property n_intervals: int
property n_observations: int
property n_events: int
property glm_deviance: float
property glm_aic: float
property backend_name: str
property timing
summary()[source]

Summary of discrete-time survival model.

Return type:

str

class pystatistics.survival.KMSolution(_result)[source]

Bases: object

Kaplan-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 ci_lower

Lower confidence bound for S(t).

property ci_upper

Upper confidence bound for S(t).

property conf_level: float
property conf_type: str
property n_observations: int
property n_events_total: int
property median_survival: float | None

Median survival time (smallest t where S(t) <= 0.5).

property backend_name: str
property timing
summary()[source]

R-style summary of Kaplan-Meier fit.

Return type:

str

class pystatistics.survival.LogRankSolution(_result)[source]

Bases: object

Log-rank test solution.

Properties mirror R’s survdiff() output.

Parameters:

_result (Result[LogRankParams])

property statistic: float
property df: int
property p_value: float
property n_groups: int
property observed
property expected
property n_per_group
property rho: float
property group_labels
property backend_name: str
property timing
summary()[source]

R-style summary of log-rank test.

Return type:

str