Cox Proportional Hazards Model
From describing survival to explaining it
The Kaplan-Meier estimator describes how survival probability changes over time for a group, and the log-rank test checks whether two or three groups differ. But real questions usually involve several covariates at once: how do age, treatment, dosage, and prior history jointly affect the time until an event? For that you need a regression model for hazard — and the dominant one, introduced by David Cox in 1972, is the Cox proportional hazards model.
The model
Cox's model writes the hazard for subject $i$ at time $t$ as a baseline hazard multiplied by a covariate effect:
$h_i(t) = h_0(t) \cdot \exp(\beta_1 x_{i1} + \beta_2 x_{i2} + \dots + \beta_p x_{ip})$
Here $h_0(t)$ is the baseline hazard — an arbitrary, unspecified function of time shared by everyone — and the exponential term scales that baseline up or down depending on a subject's covariates. This is why it's called semi-parametric: the covariate part is parametric (a linear combination inside an exponential), but the baseline hazard's shape over time is left completely unspecified, estimated nonparametrically.
The coefficients $\beta_j$ are the main objects of interest. Exponentiating one, $\exp(\beta_j)$, gives a hazard ratio: the multiplicative change in hazard for a one-unit increase in $x_j$, holding everything else fixed. A hazard ratio of 1.5 means a one-unit increase in that covariate makes the event 50% more likely to occur at any given instant, relative to the baseline.
The proportional hazards assumption
Notice that $h_0(t)$ cancels out of any ratio of two subjects' hazards: $\frac{h_i(t)}{h_j(t)} = \exp(\beta^T(x_i - x_j))$, a quantity that doesn't depend on $t$ at all. This is the proportional hazards assumption: the ratio of hazards between any two subjects is constant over time, even though the absolute hazard for everyone can rise and fall together as $t$ changes. It's a strong assumption — it says covariate effects don't change over the follow-up period — and it's exactly what makes Cox's trick work: because $h_0(t)$ cancels, you can estimate $\beta$ without ever estimating $h_0(t)$.
Partial likelihood: estimating β without the baseline
Cox's key insight was the partial likelihood. At each observed event time $t_i$, among everyone still at risk, the probability that it was specifically subject $i$ who had the event (rather than any other at-risk subject) is:
$L_i(\beta) = \frac{\exp(\beta^T x_i)}{\sum_{j \in R(t_i)} \exp(\beta^T x_j)}$
where $R(t_i)$ is the risk set at time $t_i$. Multiplying these conditional probabilities across all observed event times gives the partial likelihood, which is maximized numerically (via Newton-Raphson) to get $\hat{\beta}$. Censored subjects still contribute to the denominator (the risk set) at every event time before their censoring, exactly as in Kaplan-Meier — so the same careful bookkeeping of who's at risk when carries over.
import pandas as pd
from lifelines import CoxPHFitter
df = pd.DataFrame({
"duration": [5, 8, 12, 3, 20, 15, 9, 6],
"event": [1, 1, 0, 1, 0, 1, 1, 1],
"age": [45, 60, 50, 70, 40, 55, 65, 48],
"treated": [1, 0, 1, 0, 1, 1, 0, 1],
})
cph = CoxPHFitter()
cph.fit(df, duration_col="duration", event_col="event")
cph.print_summary()
# Hazard ratios
print(cph.hazard_ratios_)
# Check the proportional hazards assumption
cph.check_assumptions(df, p_value_threshold=0.05)Always check proportional hazards before trusting the coefficients
Kaplan-Meier vs. Cox proportional hazards
| Aspect | Kaplan-Meier | Cox PH model |
|---|---|---|
| Purpose | Describe survival for a group | Estimate covariate effects on hazard |
| Covariates | None (or one grouping factor) | Multiple, continuous or categorical |
| Output | Survival curve S(t) | Hazard ratios exp(β) |
| Assumptions | Non-informative censoring | Non-informative censoring + proportional hazards |
| Baseline hazard | Not modeled explicitly | Left unspecified (semi-parametric) |
Hazard ratios are not risk ratios
What's next?
Cox regression answers 'which covariates predict faster or slower time-to-event, and by how much,' but like most regression models, it describes association, not causation. If the real question is what would happen to survival under a hypothetical intervention — holding all else equal — you need the tools of causal inference layered on top of statistical modeling.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.