Survival Analysis
NextStat provides non-parametric estimators, parametric survival models, and Cox Proportional Hazards — all computed via the Rust engine. Right-censoring is natively supported. The same primitives power pharma, insurance, epidemiology, reliability, and subscription/churn analytics.
Kaplan-Meier Estimator
Non-parametric survival curve with Greenwood variance and log-log confidence intervals.
import nextstat
km = nextstat.kaplan_meier(times, events, conf_level=0.95)
print(f"N = {km['n']}, events = {km['n_events']}")
print(f"Median survival: {km['median']}")
# Per-step arrays: time, survival, ci_lower, ci_upper
for t, s, lo, hi in zip(km['time'], km['survival'],
km['ci_lower'], km['ci_upper']):
print(f" t={t:.1f} S={s:.3f} [{lo:.3f}, {hi:.3f}]")Log-Rank Test
Mantel-Cox test comparing survival distributions of 2+ groups.
lr = nextstat.log_rank_test(times, events, groups)
print(f"chi² = {lr['chi_squared']:.2f}, df = {lr['df']}, p = {lr['p_value']:.4f}")
# Per-group observed vs expected
for g, o, e in zip(lr['group_ids'], lr['observed'], lr['expected']):
print(f" group {g}: observed={o}, expected={e:.1f}")Parametric Models
import nextstat.survival as surv
# Exponential
fit = surv.exponential.fit(times, events, x)
# Weibull
fit = surv.weibull.fit(times, events, x)
# LogNormal AFT
fit = surv.lognormal_aft.fit(times, events, x)
# All return ParametricSurvivalFit with .params, .se, .nll, .confint()Cox Proportional Hazards
fit = surv.cox_ph.fit(
times, events, x,
ties="efron", # or "breslow"
robust=True, # sandwich SE
)
print(fit.params) # log-hazard ratios
print(fit.robust_se) # robust standard errors
print(fit.hazard_ratio_confint()) # exp(coef) CI
print(fit.confint(robust=True)) # coefficient CISchoenfeld Residuals
# Proportional hazards assumption check
residuals = surv.schoenfeld_residuals(fit)
# Returns per-event residual matrix for correlation testingInterval-Censored Models
NextStat supports exact, right, left, and interval censoring for parametric survival models. The Interval-Censored Weibull AFT includes covariates: S(t|x) = exp(-(t/λ(x))^k) where log(λi) = xiT β. This is critical for reliability/aviation where failure times are often known only within inspection intervals.
import nextstat
# censor_type: 0=exact, 1=right, 2=left, 3=interval
model = nextstat.IntervalCensoredWeibullAftModel(
time_lower, time_upper, censor_type, covariates
)
result = nextstat.fit(model)
print(f"Shape k = {result.parameters[0]:.3f}")
print(f"Covariate effects (beta): {result.parameters[1:]}")
print(f"Warnings: {result.warnings}") # identifiability checksSimpler 2-parameter variants (no covariates) are also available:
# Weibull (IC, no covariates)
m = nextstat.IntervalCensoredWeibullModel(time_lower, time_upper, censor_type)
# Exponential (IC, k=1)
m = nextstat.IntervalCensoredExponentialModel(time_lower, time_upper, censor_type)
# LogNormal (IC)
m = nextstat.IntervalCensoredLogNormalModel(time_lower, time_upper, censor_type)CLI
# Kaplan-Meier from JSON
nextstat survival km -i data.json --conf-level 0.95
# Log-rank test
nextstat survival log-rank-test -i data.json
# Cox PH fit
nextstat survival cox-ph-fit -i data.json
# Weibull AFT with interval censoring
nextstat survival weibull-aft -i data.jsonAvailable Models
| Model | Parameters | Use Case |
|---|---|---|
| kaplan_meier | non-parametric | Survival curve estimation |
| log_rank_test | non-parametric | Group comparison |
| exponential | log_rate | Constant hazard |
| weibull | log_k, log_lambda | Monotone hazard |
| lognormal_aft | mu, log_sigma | Accelerated failure time |
| cox_ph | covariate coefficients | Semi-parametric, partial likelihood |
| IntervalCensoredWeibullAftModel | log_k, β0..p | Weibull AFT with covariates + interval censoring |
| IntervalCensoredWeibullModel | log_k, log_lambda | Weibull with interval censoring |
| IntervalCensoredExponentialModel | log_rate | Exponential (k=1) with interval censoring |
| IntervalCensoredLogNormalModel | mu, log_sigma | LogNormal AFT with interval censoring |
Cross-Vertical Applications
The same survival primitives serve multiple industries:
- Pharma — Clinical trial endpoints, treatment arm comparison, KM curves for FDA submissions.
- Insurance — Policy lapse analysis, mortality tables, loss development.
- Epidemiology — Population cohort studies, disease progression, intervention evaluation.
- Reliability — Component failure analysis, MTBF estimation, warranty modeling.
- Subscription — Cohort retention, churn risk models, causal uplift estimation.
