Generating survival times for a piecewise constant hazard model with two change points When there are two change points in  a piecewise constant hazard model then the density function becomes some triangle exponential distribution. In this situation I can't generate the survival time from the CDF using probability integral transformation. Can any one help me to generate the survival time from this model?
 A: There are two basic approaches to generating data with piecewise constant hazard: inversion of the cumulative hazard and the composition method.


*

*Inversion of the cumulative hazard - essentially the inverse CDF method. Since $F(t) = 1-\exp(-H(t))$. If $U \sim Unif(0,1)$, then $F(X) = U$ is equivalent to $1-\exp(-H(X)) = U$, so $X=H^{-1}(-\log(1-U))$. You can also note that $-\log(1-U) \sim Exp(1)$, so you can apply the inverse cumulative hazard to an exponential random variable.
The cumulative hazard is piecewise linear for your case, and should be easy to invert.


Edit (more detail): with two change-points, the hazard is:
$$h(t) = \left\{ \begin{matrix} f_1 , & 0\leq t\leq t_1\\ 
                                f_2, & t_1 < t \leq t_2\\
                                f_3, & t > t_2 \end{matrix}\right.$$
The cumulative hazard is:
$$H(t) = \left\{ \begin{matrix} f_1 t , & 0\leq t\leq t_1\\ 
                                f_1 t_1 + f_2(t-t_1), & t_1 < t \leq t_2\\
                                f_1t_1 + f_2(t_2-t_1) + f_3(t-t_2), & t > t_2 \end{matrix}\right.$$
The inverse of the cumulative hazard is:
$$H^{-1}(x) =  \left\{ \begin{matrix} x/f_1 , & 0\leq x\leq f_1t_1\\ 
                                t_1 + (x-f_1t_1)/f_2, & f_1t_1 < x \leq f_1t_1 + f_2(t_2-t_1)\\
                                t_2 + (x-f_1t_1-f_2(t_2-t_1))/f_3, & x > f_1t_1 + f_2(t_2-t_1)\end{matrix}\right.$$
Now generate an exponentially distributed random variable, and plug it in into $H^{-1}$.
End edit


*The Composition method uses the fact that if $X_1$ has hazard $h_1$, and $X_2$ has hazard $h_2$, then $X=\min(X_1,X_2)$ has hazard  $h=h_1+h_2$. You can represent your piecewise constant hazard as a sum of hazards that are constant on an interval and 0 outside. Generate a value $X_i$ for each interval (it could be $\infty$, since the resulting distributions are not necessarily proper), and take their minimum.


Edit (more detail): with the above notation, the composition hazards are
$$h_1(t) = \left\{ \begin{matrix} f_1 , & 0\leq t\leq t_1\\ 
                                0, & t > t_1 \end{matrix}\right.$$
$$h_2(t) = \left\{ \begin{matrix} f_2 , & t_1 < t\leq t_2\\ 
                                0, & \text{otherwise} \end{matrix}\right.$$
$$h_3(t) = \left\{ \begin{matrix} f_3 , & t_2< t\\ 
                                0, & \text{otherwise} \end{matrix}\right.$$
You can easily calculate the CDF or the cumulative hazard for each of these hazards.
One resource with R-Code
