I would like to generate a random number from a piecewise exponential distribution, which has different hazard rates at different time-intervals.
What I understood is that it is possible to apply the memoryless property of the standard exponential distribution.
Anybody knows if the following code is correct for this purpose? In particular, the vector 'S' contains the intervals' upper-bounds associated with the corresponding hazard rates (contained in vector 'lambda'.
function rand_PEXP=rand_PEXP(S,lambda)
for j=1:numel(S)
x=exprnd(lambda(j));
if j==1 && x<S(j)
rand_PEXP=x;
break
elseif x<S(j)
rand_PEXP=x+S(j-1);
break
end
end
I hope the question is clear.