NextStatNextStat

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 CI

Schoenfeld Residuals

# Proportional hazards assumption check
residuals = surv.schoenfeld_residuals(fit)
# Returns per-event residual matrix for correlation testing

Interval-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 checks

Simpler 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.json

Available Models

ModelParametersUse Case
kaplan_meiernon-parametricSurvival curve estimation
log_rank_testnon-parametricGroup comparison
exponentiallog_rateConstant hazard
weibulllog_k, log_lambdaMonotone hazard
lognormal_aftmu, log_sigmaAccelerated failure time
cox_phcovariate coefficientsSemi-parametric, partial likelihood
IntervalCensoredWeibullAftModellog_k, β0..pWeibull AFT with covariates + interval censoring
IntervalCensoredWeibullModellog_k, log_lambdaWeibull with interval censoring
IntervalCensoredExponentialModellog_rateExponential (k=1) with interval censoring
IntervalCensoredLogNormalModelmu, log_sigmaLogNormal AFT with interval censoring

Cross-Vertical Applications

The same survival primitives serve multiple industries:

  • PharmaClinical trial endpoints, treatment arm comparison, KM curves for FDA submissions.
  • InsurancePolicy lapse analysis, mortality tables, loss development.
  • EpidemiologyPopulation cohort studies, disease progression, intervention evaluation.
  • ReliabilityComponent failure analysis, MTBF estimation, warranty modeling.
  • SubscriptionCohort retention, churn risk models, causal uplift estimation.