惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
量子位
腾讯CDC
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
B
Blog
S
SegmentFault 最新的问题
P
Privacy & Cybersecurity Law Blog
T
Threatpost
博客园 - 聂微东
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
C
Check Point Blog
N
Netflix TechBlog - Medium
D
DataBreaches.Net
爱范儿
爱范儿
IT之家
IT之家
S
Secure Thoughts
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
C
Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
有赞技术团队
有赞技术团队
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
Schneier on Security
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
雷峰网
雷峰网
Project Zero
Project Zero
博客园 - Franky
H
Heimdal Security Blog
A
About on SuperTechFans
Security Latest
Security Latest
Webroot Blog
Webroot Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Cox Proportional Hazards: The Workhorse of Survival Analysis
Berkan Sesen · 2026-05-18 · via DEV Community

Survival analysis starts with a question: how long until an event happens? A patient relapses, a customer churns, a borrower defaults on a loan, a prisoner is rearrested. Parametric models answer by assuming a shape for the hazard — Weibull, log-logistic, exponential — and estimating its parameters. The Cox model sidesteps the entire question. You get hazard ratios, survival curves, and covariate effects without ever specifying what the baseline hazard looks like.

In our Bayesian survival analysis post, we used PyMC to fit an accelerated failure time model with explicit distributional assumptions. The Cox model takes the opposite approach: it's semi-parametric, making no assumption about the baseline hazard. This is why it dominates applied survival analysis in medicine, criminal justice, and customer churn modelling.

By the end of this post, you'll fit a Cox model to real recidivism data, interpret hazard ratios, test the proportional hazards assumption, and extend the model with time-dependent covariates.

The Data: Recidivism After Prison

The Rossi recidivism dataset follows 432 male prisoners for one year after their release from prison. The primary question: does receiving financial aid reduce the risk of rearrest?

Each prisoner has seven baseline covariates (financial aid, age, race, work experience, marital status, parole, prior convictions) plus 52 weekly employment indicators. Of the 432 prisoners, 114 (26%) were rearrested within the year; the remaining 318 were censored (not rearrested during the observation period).

The Kaplan-Meier curve gives us a first look at the survival pattern:

Kaplan-Meier survival curve showing roughly 74% of prisoners avoid rearrest over 52 weeks

About 74% of prisoners avoid rearrest through the full year. But the curve doesn't tell us which factors predict rearrest. That's where Cox regression comes in.

Quick Win: Cox Regression in Action

Let's fit a Cox model and see which covariates matter. Click the badge to run this yourself:

Open In Colab

import numpy as np
import pandas as pd
from lifelines import CoxPHFitter, KaplanMeierFitter
from lifelines.datasets import load_rossi

rossi = load_rossi()
print(f"{len(rossi)} prisoners, {rossi['arrest'].sum()} rearrested")

Enter fullscreen mode Exit fullscreen mode

432 prisoners, 114 rearrested

Enter fullscreen mode Exit fullscreen mode

Fitting the Cox model is one line:

cph = CoxPHFitter()
cph.fit(rossi, duration_col="week", event_col="arrest")
cph.print_summary()

Enter fullscreen mode Exit fullscreen mode

The forest plot shows the hazard ratios for each covariate:

Forest plot of hazard ratios: age and financial aid are protective, prior convictions increase risk

Three findings jump out:

  • Age (HR = 0.94, p = 0.01): each additional year of age reduces the hazard of rearrest by 6%. Older prisoners are less likely to reoffend.
  • Prior convictions (HR = 1.10, p < 0.005): each additional prior conviction increases the hazard by 10%. Criminal history is the strongest risk factor.
  • Financial aid (HR = 0.68, p = 0.05): receiving financial aid reduces the hazard by 32%. This is the key finding for the original study, though it's borderline significant.

The model's concordance is 0.64, meaning it correctly ranks pairs of prisoners by risk 64% of the time.

Now let's see the effect of financial aid on the survival curve, holding all other covariates at their sample means:

Cox-adjusted survival curves showing financial aid recipients have higher survival probability

Prisoners who received financial aid (blue) have a visibly higher survival probability throughout the year. By week 52, the gap is roughly 7 percentage points: 80% vs 73% avoiding rearrest.

What Just Happened?

The Cox Model in One Equation

The Cox proportional hazards model expresses the hazard (instantaneous risk of the event) for individual $i$ at time $t$ as:

equation

where $h_0(t)$ is the baseline hazard (shared by everyone) and the exponential term scales it up or down based on covariates. The key insight: we never need to estimate $h_0(t)$. Cox's partial likelihood eliminates it entirely, letting us estimate the $\beta$ coefficients from the data alone.

Hazard Ratios: The Language of Cox Models

The quantity $\exp(\beta)$ is the hazard ratio (HR). For a binary covariate like financial aid:

  • HR < 1 means the covariate is protective (lower hazard)
  • HR > 1 means the covariate increases risk
  • HR = 1 means no effect

For our financial aid variable: HR = 0.68 means that, holding everything else constant, prisoners who received financial aid have 68% of the hazard of those who didn't. Equivalently, financial aid reduces the hazard by 32%.

For a continuous covariate like age: HR = 0.94 means each additional year of age multiplies the hazard by 0.94. A 30-year-old has $0.94^{10} = 0.54$ times the hazard of a 20-year-old (46% lower risk).

The Partial Likelihood Trick

The magic of the Cox model is the partial likelihood, introduced by Cox (1972). At each event time, we ask: "Given that someone in the risk set was about to fail, what's the probability it was this particular individual?" That probability depends only on the $\beta$ coefficients, not on $h_0(t)$:

equation

where $R_j$ is the set of individuals still at risk just before time $t_j$. The baseline hazard cancels out in the ratio. This is what makes the Cox model semi-parametric: parametric in the covariate effects, non-parametric in the baseline hazard.

Flow diagram showing how the partial likelihood works: at each event time, the risk set shrinks and a probability ratio is computed, then all ratios are multiplied together

In lifelines, this is all handled internally:

cph = CoxPHFitter()
cph.fit(rossi, duration_col="week", event_col="arrest")

Enter fullscreen mode Exit fullscreen mode

Reading the Output

The print_summary() output gives you:

Column Meaning
coef The $\beta$ coefficient (log hazard ratio)
exp(coef) The hazard ratio
se(coef) Standard error of $\beta$
z Wald test statistic ($\beta / \text{SE}$)
p p-value for $H_0: \beta = 0$
exp(coef) lower/upper 95% 95% CI for the hazard ratio

A hazard ratio whose 95% CI includes 1.0 is not statistically significant at the 5% level. In the forest plot, covariates in grey (race, work experience, marital status, parole) all span the HR = 1 line.

Going Deeper

Time-Dependent Covariates: When Risk Changes Over Time

The basic Cox model assumes each covariate is fixed at baseline. But what about employment, which changes week to week? The original R code addresses this by expanding the data into start-stop format: one row per person per week, with that week's employment status.

from lifelines import CoxTimeVaryingFitter

# Expand to one row per person-week with employment status
# (see notebook for full expansion code)
ctv = CoxTimeVaryingFitter()
ctv.fit(df_expanded, id_col="id", event_col="arrest",
        start_col="start", stop_col="stop")

Enter fullscreen mode Exit fullscreen mode

Employment turns out to be the strongest predictor: HR = 0.35, meaning employed weeks have only 35% of the rearrest hazard compared to unemployed weeks. But the causality is ambiguous: prisoners who avoid rearrest are also more likely to maintain employment. Being in jail prevents you from showing up to work.

The risk trajectories for three participants illustrate this:

Risk trajectories showing how employment status changes predicted risk over time

The never-employed participant (blue, arrested) maintains consistently high risk. The intermittently employed participant (red) shows risk that jumps up and down as employment changes. The mostly-employed participant (green) has consistently low risk.

The Proportional Hazards Assumption

The Cox model assumes that the hazard ratio between any two individuals stays constant over time. This is the "proportional" in proportional hazards. If financial aid halves your hazard at week 1, it should also halve it at week 50.

We test this with Schoenfeld residuals. For each event, the Schoenfeld residual measures how much the covariate value at that event differs from what the model expected. If the residuals trend with time, the proportional hazards assumption is violated.

Schoenfeld residual plots for financial aid, age, and prior convictions with LOWESS smoothers

For financial aid (left), the LOWESS smoother is roughly flat: the PH assumption holds well (p = 0.98). For age (centre), there's a subtle downward trend: the protective effect of age may weaken over time (p = 0.01, borderline violation). For prior convictions (right), the smoother is close to flat (p = 0.38).

from lifelines.statistics import proportional_hazard_test
ph_test = proportional_hazard_test(cph_reduced, df_reduced, time_transform="rank")

Enter fullscreen mode Exit fullscreen mode

When the PH assumption fails, options include: stratifying by the offending covariate, adding a time-interaction term, or switching to an accelerated failure time model (as in our Bayesian survival post).

The Baseline Hazard

Although the Cox model doesn't need the baseline hazard to estimate coefficients, we can recover it after fitting. The Breslow estimator gives a non-parametric estimate of the cumulative baseline hazard:

Cumulative baseline hazard increasing roughly linearly over 52 weeks

The roughly linear increase suggests an approximately constant baseline hazard rate: the risk of rearrest per unit time doesn't change much over the year, for someone with average covariates. This is consistent with an exponential baseline (a finding that would validate a Weibull or exponential parametric model, which is exactly what we used in Post 21).

Cox vs Bayesian AFT: When to Use Which

Our Bayesian survival analysis post used an accelerated failure time (AFT) model with PyMC. How does it compare to Cox?

Feature Cox PH Bayesian AFT
Approach Semi-parametric (no baseline assumption) Parametric (Weibull, log-logistic, etc.)
Interpretation Hazard ratios: "how much faster does the event happen?" Time ratios: "how much longer until the event?"
Uncertainty Frequentist CIs Full posterior distributions
Flexibility Time-dependent covariates straightforward Hierarchical structure, custom priors
Assumption Proportional hazards (testable) Distributional form of baseline hazard

Use Cox when you want an assumption-light, interpretable analysis that's the standard in your field (medicine, criminal justice). Use Bayesian AFT when you need uncertainty quantification, hierarchical structure, or when the PH assumption fails.

Comparison diagram showing Cox PH (semi-parametric, hazard ratios, frequentist CIs) vs Bayesian AFT (fully parametric, time ratios, posterior distributions)

Hyperparameter Choices

Parameter Value Why
Full model covariates fin, age, race, wexp, mar, paro, prio All baseline covariates from the original study
Reduced model fin, age, prio Only the statistically significant predictors
Time-dependent expansion 52 weekly employment indicators One row per person-week for start-stop format
Confidence level 95% Standard for medical/social science applications
PH test transform Rank More robust than identity transform for discrete event times

Where This Comes From

Cox (1972): The Most-Cited Statistics Paper

David Cox introduced the proportional hazards model in his 1972 paper "Regression Models and Life-Tables", published in the Journal of the Royal Statistical Society. With over 50,000 citations, it's one of the most influential statistics papers ever written.

Cox's insight was that for many survival problems, we care about the relative effect of covariates, not the absolute hazard function. By conditioning on the set of individuals at risk at each event time, the baseline hazard cancels out:

"The model is formulated in a very general way and is not restricted to any particular parametric family of distributions."
-- Cox (1972)

The partial likelihood was initially controversial. It's not a full likelihood in the classical sense, and the asymptotic theory required new developments. Andersen and Gill (1982) provided the rigorous counting-process framework that established the mathematical foundations.

The Rossi Dataset

Our dataset comes from Rossi, Berk, and Lenihan (1980), Money, Work, and Crime: Experimental Evidence. The study was a randomised experiment: prisoners were randomly assigned to receive financial aid (or not) upon release, then tracked for one year. This experimental design makes the financial aid coefficient more interpretable than typical observational studies, though compliance was imperfect.

The R analysis we translated follows John Fox's companion chapter to Applied Regression Analysis, which has been a standard teaching resource for Cox models in R since 2002.

From Cox to Modern Survival Analysis

Cox's framework has been extended in many directions:

  • Stratified Cox models: different baseline hazards for different groups (e.g., men vs women), but shared covariate effects
  • Frailty models: random effects for unobserved heterogeneity, analogous to hierarchical models in Bayesian statistics
  • Time-varying coefficients: let $\beta$ change over time, relaxing the PH assumption
  • Competing risks: multiple possible event types (rearrest vs death vs emigration)

For Python practitioners, lifelines provides a mature, well-documented implementation. For Bayesian alternatives, PyMC can fit both AFT and piecewise-exponential Cox models (see our Post 21).

Further Reading

  • The original paper: Cox (1972), "Regression Models and Life-Tables", JRSS Series B
  • Mathematical foundations: Andersen & Gill (1982), "Cox's Regression Model for Counting Processes"
  • The R companion chapter: Fox (2002), "Cox Proportional-Hazards Regression for Survival Data"
  • Comprehensive textbook: Kalbfleisch & Prentice (2002), The Statistical Analysis of Failure Time Data, Wiley
  • Python implementation: Davidson-Pilon (2019), lifelines documentation

Interactive Tools

Related Posts

Frequently Asked Questions

What does "semi-parametric" mean in the context of the Cox model?

The Cox model is parametric in how covariates affect the hazard (through the exponential term with beta coefficients) but non-parametric in the baseline hazard, which is left completely unspecified. This means you get interpretable covariate effects without having to assume the hazard follows a Weibull, exponential, or any other distribution.

How do I interpret a hazard ratio less than 1?

A hazard ratio below 1 means the covariate is protective. For example, a hazard ratio of 0.68 for financial aid means that prisoners receiving aid have 68% of the hazard of those who do not, or equivalently, a 32% reduction in the risk of rearrest at any given time point.

What happens if the proportional hazards assumption is violated?

If the hazard ratio between groups changes over time, the Cox model's estimates become averaged effects that may not accurately represent the relationship at any specific time point. Options include stratifying by the offending covariate, adding a time-interaction term, or switching to an accelerated failure time model that does not require proportional hazards.

Can the Cox model handle categorical covariates with more than two levels?

Yes. Categorical covariates with k levels are represented using k-1 dummy variables, just as in standard linear regression. Each dummy variable's hazard ratio is interpreted relative to the reference category. The lifelines library handles this automatically when you pass categorical columns.

What is the concordance index and what counts as a good value?

The concordance index (C-index) measures how well the model ranks individuals by risk. A value of 0.5 means the model is no better than random, while 1.0 means perfect ranking. In medical and social science applications, values between 0.6 and 0.7 are common and considered acceptable, while values above 0.8 are considered excellent.

Why use the Cox model instead of logistic regression for survival data?

Logistic regression ignores the time dimension entirely and cannot handle censored observations properly. If a prisoner was not rearrested during the study but might be rearrested later, logistic regression would treat them as a definitive non-event, wasting information. The Cox model uses the partial follow-up time from censored subjects to improve estimation.