havakv / pycox

Survival analysis with PyTorch
BSD 2-Clause "Simplified" License
822 stars 191 forks source link

MTLR - transform predicted survival probability to hazard rate #51

Closed Karenou closed 4 years ago

Karenou commented 4 years ago

Hi, I wonder if I could ask about the transformation from predicted survival probability to hazard rates using MTLR model.

In discrete-time model such as MTLR, could we simply apply this formula h(t) = ( S(t-1) - S(t) ) / S(t-1) to transform the predicted survival probability to hazard rate in specific time interval?

But in the below implementation in class LogisticHazard(models.base.SurvBase) in pycox.models, the transformation from hazard to survival function is a bit different from the formula above. May I know which one should I adopt in discrete-time models when the predicted survival probability is already discretized by time interval?

def predict_surv(self, input, batch_size=8224, numpy=None, eval_=True, to_cpu=False,
                     num_workers=0, epsilon=1e-7):
        hazard = self.predict_hazard(input, batch_size, False, eval_, to_cpu, num_workers)
        surv = (1 - hazard).add(epsilon).log().cumsum(1).exp()
        return tt.utils.array_or_tensor(surv, numpy, input)

Many thanks!

havakv commented 4 years ago

Hi, The two formulas are equivalent. We can rewrite your first formula in terms of S(t) giving S(t) = [1 - h(t)] * S(t-1) = [1 - h(t)] * [1 - h(t-1)] * [1 - h(t-2)] * ... * [1 - h(0)] This is what is computed in predict_surv, but for numerical stability we instead compute the sum of the log values. a * b = exp[log(a*b)] = exp[log(a) + log(b)].

Does this clear things up?

Karenou commented 4 years ago

Thank you very much! It is clear to me now.