0

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • Is this a programming question or a math/statistics question? Note that StackOverflow is geared toward the former. If your question is more the latter, you might take this down and try [Math.StackExchange](http://math.stackexchange.com) or [Stats.Stackexhange](http://stats.stackexchange.com). – horchler Aug 02 '14 at 16:56
  • I'm trying to understand your code. So if you were to generate a random number from the exponential distribution for the j'th hazard rate, and if it doesn't meet the requirements in your `if` statements, it will proceed to the next piece of your overall function. Do you want to generate a **single** random number overall, or do you want to generate one random number for each **part** of your piecewise definiton? – rayryeng Aug 02 '14 at 17:06
  • @horchler I think that the theoretical background is correct... I'm just checking if the code is correct... – Alessandro Beretta Aug 02 '14 at 17:11
  • @rayryeng I would like to generate a single random number from a piecewise exponential random variable. – Alessandro Beretta Aug 02 '14 at 17:13
  • @AlessandroBeretta - If that's the case, you should first choose **which** part of the exponential distribution you want to select. Once you do that, then generate a random number from that distribution – rayryeng Aug 02 '14 at 17:14
  • @rayryeng - Well, but in this case I simply generate random number from a standard exponential distribution with a given parameter.... – Alessandro Beretta Aug 02 '14 at 17:19
  • @AlessandroBeretta - Yes I'm aware :)... but you want to figure out from **which** distribution you want to draw from ... from your piecewise definition. Once you do that, you can then generate the number such that it's less than the upper level interval. I'll write an answer soon. – rayryeng Aug 02 '14 at 17:20
  • 1
    It's unclear from your description whether want a Poisson with conditional rate (as described by @rayryeng) or a non-homogeneous Poisson process. If the former, rayryeng is correct and you should select which rate applies and then generate. For a non-homogeneous Poisson process you should use a technique called "[thinning](http://stackoverflow.com/questions/19556891/problems-simulating-interarrival-times/19568982#19568982)". – pjs Aug 02 '14 at 17:42
  • @pjs - I agree. I'm assuming the former though, as that is how I'm interpreting the question. – rayryeng Aug 02 '14 at 17:49
  • Sorry I'm not a statistician. Perhaps the theoretical background of the piecewise exponential distribution that I've in mind is not correct. For this reason I posted a question in [stats.stackexchange](http://stats.stackexchange.com/questions/110405/generate-random-number-from-a-piecewise-exponential-distribution). Let's see what statisticians think about it.... – Alessandro Beretta Aug 02 '14 at 17:52
  • Sorry everybody. I understand that my question is not clear at all... Try to look at this [link](http://stats.stackexchange.com/questions/110405/generate-random-number-from-a-piecewise-exponential-distribution), I hope is clearer... – Alessandro Beretta Aug 02 '14 at 17:54
  • @AlessandroBeretta - Looking at that link, then your code will work. However, you should consider the case where if you finish going through all of the distributions and you don't end up generating a random number because none of the numbers you generated satisfy condition #1, then you should start over from the beginning and sample from the first distribution and continue. – rayryeng Aug 02 '14 at 18:01
  • 1
    @rayryeng that is a really good remark! Thanks! Now I've just to check if the theoretical background is ok... – Alessandro Beretta Aug 02 '14 at 18:16

0 Answers0