I am using sksurv.linear_model.CoxPHSurvivalAnalysis to fit a cox ph regression and I would like to recover the density function f(t). The sksurv class has methods to predict the survival function and cumulative distribution function S(t) = 1-F(t) and the cumulative hazard function $H(t)$ but it doesn't seem to produce the density function.
My use case has no censoring, so ere is an example:
import pandas as pd
import numpy as np
from sksurv.linear_model import CoxPHSurvivalAnalysis
data = np.random.randint(5,30,size=10)
X_train = pd.DataFrame(data, columns=['covariate'])
y_train = np.array(np.random.randint(0,100,size=10)/100,dtype=[('status',bool),('target',float)])
estimator = CoxPHSurvivalAnalysis()
estimator.fit(X_train,y_train)
X_test = pd.DataFrame({'covariate':[12,2]})
chf = estimator.predict_cumulative_hazard_function(X_test)
cdf = estimator.predict_survival_function(X_test)
fig, ax = plt.subplots(1,2)
for fn_h, fn_c in zip(chf, cdf):
ax[0].step(fn_h.x,fn_h(fn_h.x),where='post')
ax[1].step(fn_c.x,fn_c(fn_c.x),where='post')
ax[0].set_title('Cumulative Hazard Functions')
ax[1].set_title('Survival Functions')
plt.show()

The probability density function (PDF) can be obtained from the cumulative distribution function (CDF) as :
Now, in Survival Analysis (SA) the PDF
(f(t))can be expressed in terms of Survival FunctionS(t)and the hazard functionh(t)which is given by:where
S(t) = 1 - F(t)andh(t) = -dS(t)/dt x S(t) = dH(t)/dtSo, the PDF
f(t)can be expressed as :f(t) = dH(t)/dt x S(t)Now, to compute the hazard function
f(t)we need derivative of Cumulative Hazard Function (CHF)H(t). Since the CHF are all discrete data points, we needInterpolatedUnivariateSplinefrom thescipylibrary to differentiate it. It creates a smooth spline interpolation of the CHF, which can then be differentiated to obtainh(t). Here's a slight modification of the code that was pasted:which results in
References : Machine Learning for Survival Analysis: A Survey