Cox Proportional Hazards › Example
Machine Learning from ScratchAll chapters 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
We work through a minimal dataset of four subjects so every number can be checked by hand. Each subject has a single covariate , an observed time, and an event indicator.
| Subject | |||
|---|---|---|---|
| 1 | 0 | 2 | 1 |
| 2 | 1 | 4 | 1 |
| 3 | 0 | 6 | 1 |
| 4 | 1 | 8 | 0 |
There are three events, at times , , and . 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 : (everyone is still at risk)
- at : (subject 1 has already failed)
- at : (subject 2 has already failed)
The partial likelihood is
Writing and simplifying,
Taking logs,
Let us evaluate this at three values of to see the shape of the surface. At ():
At ():
At ():
The displayed evaluations bracket the maximum: , , and , so the largest value is at and the optimum lies between and . The score can be simplified to . At it is positive, so increasing locally increases the log partial likelihood. Multiplying the zero-score equation by its positive denominators yields , whose positive root 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]Expand
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]Expand
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]Expand
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]Expand

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 , increasing raises its numerator weight; when the failing individual has , that numerator stays at one. In either case, increasing raises the denominator weights of the subjects with who remain at risk. A positive helps only when the failing individual has high covariate values relative to those still at risk. Fitting the Cox model means finding the 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!