Cox Proportional Hazards › Example

Michael BrenndoerferOctober 26, 20265 min read
Machine Learning from ScratchAll chapters
Part 6 of 15Survival Analysis
Chapter 50Cox Proportional Hazards › Example
Reading section 5 of 10 · Example · 5 minSection 5 of 10 · 5 min

Section 5 of 10 · 5 min read

Cox Proportional Hazards › ExampleSection view · Sections in this chapter5 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

We work through a minimal dataset of four subjects so every number can be checked by hand. Each subject has a single covariate x∈{0,1}x \in \{0, 1\}, an observed time, and an event indicator.

Four-subject teaching dataset with a binary covariate, observed time, and event indicator.
SubjectxxTTdd
1021
2141
3061
4180

There are three events, at times 22, 44, and 66. Subject 4 is censored at time 8, so they are at risk at every event time but never contribute a numerator term. The three risk sets are:

  • at t=2t=2: {1,2,3,4}\{1, 2, 3, 4\} (everyone is still at risk)
  • at t=4t=4: {2,3,4}\{2, 3, 4\} (subject 1 has already failed)
  • at t=6t=6: {3,4}\{3, 4\} (subject 2 has already failed)

The partial likelihood is

L(β)=e0e0+eβ+e0+eβ⏟t=2⋅eβeβ+e0+eβ⏟t=4⋅e0e0+eβ⏟t=6.L(\beta) = \underbrace{\frac{e^{0}}{e^{0} + e^{\beta} + e^{0} + e^{\beta}}}_{t=2} \cdot \underbrace{\frac{e^{\beta}}{e^{\beta} + e^{0} + e^{\beta}}}_{t=4} \cdot \underbrace{\frac{e^{0}}{e^{0} + e^{\beta}}}_{t=6}.

Writing q=eβq = e^{\beta} and simplifying,

L(β)=12+2q⋅q1+2q⋅11+q.L(\beta) = \frac{1}{2 + 2q} \cdot \frac{q}{1 + 2q} \cdot \frac{1}{1 + q}.

Taking logs,

ℓ(β)=−log⁡(2+2q)+β−log⁡(1+2q)−log⁡(1+q).\ell(\beta) = -\log(2 + 2q) + \beta - \log(1 + 2q) - \log(1 + q).

Let us evaluate this at three values of β\beta to see the shape of the surface. At β=0\beta = 0 (q=1q=1):

ℓ(0)=−log⁡4+0−log⁡3−log⁡2≈−1.386−1.099−0.693=−3.178.\ell(0) = -\log 4 + 0 - \log 3 - \log 2 \approx -1.386 - 1.099 - 0.693 = -3.178.

At β=−1\beta = -1 (q≈0.368q \approx 0.368):

ℓ(−1)=−log⁡(2.736)−1−log⁡(1.736)−log⁡(1.368)≈−1.006−1−0.552−0.313=−2.871.\ell(-1) = -\log(2.736) - 1 - \log(1.736) - \log(1.368) \approx -1.006 - 1 - 0.552 - 0.313 = -2.871.

At β=−1.5\beta = -1.5 (q≈0.223q \approx 0.223):

ℓ(−1.5)=−log⁡(2.446)−1.5−log⁡(1.446)−log⁡(1.223)≈−2.965.\ell(-1.5) = -\log(2.446) - 1.5 - \log(1.446) - \log(1.223) \approx -2.965.

The displayed evaluations bracket the maximum: ℓ(−1.5)≈−2.965\ell(-1.5) \approx -2.965, ℓ(−1)≈−2.871\ell(-1) \approx -2.871, and ℓ(0)≈−3.178\ell(0) \approx -3.178, so the largest value is at β=−1\beta=-1 and the optimum lies between −1.5-1.5 and 00. The score can be simplified to 1−2q/(1+q)−2q/(1+2q)1-2q/(1+q)-2q/(1+2q). At β=−1\beta=-1 it is positive, so increasing β\beta locally increases the log partial likelihood. Multiplying the zero-score equation 1−2q/(1+q)−2q/(1+2q)=01-2q/(1+q)-2q/(1+2q)=0 by its positive denominators yields 1−q−4q2=01-q-4q^2=0, whose positive root q=(17−1)/8q=(\sqrt{17}-1)/8 determines the finite optimum in this example. We compute it and an approximate Wald interval below; with only three events, the interval is a teaching calculation rather than a precise effect estimate.

PythonIn [3]
CollapseExpand
hand_q = (np.sqrt(17.0) - 1.0) / 8.0
hand_score = 1 - 2 * hand_q / (1 + hand_q) - 2 * hand_q / (1 + 2 * hand_q)
hand_beta = np.log(hand_q)
hand_information = (
    2 * hand_q / (1 + hand_q) ** 2 + 2 * hand_q / (1 + 2 * hand_q) ** 2
)
hand_se = 1 / np.sqrt(hand_information)
hand_hr_interval = np.exp(hand_beta + np.array([-1.96, 1.96]) * hand_se)
ConsoleOut [4]
CollapseExpand
hand optimum beta: -0.940614; hazard ratio: 0.390388
score at the hand optimum: 1.11e-16
approximate model-based 95% Wald hazard-ratio interval: [0.0343374  4.43839458]

The hand calculation also has a visual counterpart. Over a fixed grid of beta values, the log partial likelihood is a smooth concave curve with a single finite maximum at the value derived above.

PythonIn [5]
CollapseExpand
beta_grid = np.linspace(-2.5, 1.0, 400)
hand_q_grid = np.exp(beta_grid)
hand_loglike_grid = (
    -np.log(2.0 + 2.0 * hand_q_grid)
    + beta_grid
    - np.log(1.0 + 2.0 * hand_q_grid)
    - np.log(1.0 + hand_q_grid)
)
hand_loglike_opt = (
    -np.log(2.0 + 2.0 * hand_q)
    + hand_beta
    - np.log(1.0 + 2.0 * hand_q)
    - np.log(1.0 + hand_q)
)
VisualizationOut [6]
CollapseExpand
A concave log partial likelihood curve with a marked maximum near beta equals minus one.
Log partial likelihood for the four-subject hand example over a fixed beta grid. The curve is concave, with the marked point at the finite maximum derived from the score equation. This illustrates the optimization problem behind Cox fitting, not a general guarantee of finite estimability in every dataset.

The lesson from the hand calculation is not the specific coefficient. It is that the optimization operates on observed covariate values within risk sets. At an event where the failing individual has x=1x=1, increasing β\beta raises its numerator weight; when the failing individual has x=0x=0, that numerator stays at one. In either case, increasing β\beta raises the denominator weights of the subjects with x=1x=1 who remain at risk. A positive β\beta helps only when the failing individual has high covariate values relative to those still at risk. Fitting the Cox model means finding the β\beta that balances those two forces across all events.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy