• how to draw erlang distribution (two parameters k and cv? (k=1,5,10, cv=100%-45%-32%) ) in python by having k and CV and mean exponential distribution=1?

I know numpy.random.exponential(scale=1.0, size=None)¶ by this only has scale but I need two parameters k and cv? (k=1,5,10, cv=100%-45%-32%, )


1 Answer 1


Guessing, but: the Erlang distribution is a special case of the Gamma (with integer shape parameter).

numpy.random.gamma takes a shape ($k$) and a scale ($\theta$) parameter. Per Wikipedia, for the Gamma distribution mean=$k \theta$, var=$k \theta^2$, so CV=$\sqrt{\textrm{Var}}/m = 1/\sqrt{k}$. So the bad news is that you can't specify $k$ and CV as independent parameters: once you know $k=1,5,10$, you already know that the CV is (as you specify) 1, $1/\sqrt{5}=0.45$, $1/\sqrt{10}=0.32$. The good news is that you can pick the scale ($\theta$) parameter any way you want: e.g. you could set $\theta=1$ throughout (in which case the mean will equal $1/\textrm{CV}^2$), or you can specify the mean $m$ and set $\theta=m/k=m \cdot \textrm{CV}^2$.

So e.g. numpy.random.gamma(shape=5,size=100) would work (this uses the default scale=1).

If you really want to use numpy.random.exponential to generate 100 k-Erlang deviates, in a marginally less efficient and transparent way, you can generate a 100*k array and sum up the rows, e.g. for $k=5$/CV=0.45:

import numpy.random as npr 
k = 5
  • $\begingroup$ Very similar to @Glen_b's but leaving it anyway since it uses different words so might not be completely redundant. $\endgroup$
    – Ben Bolker
    Commented Feb 20, 2019 at 2:39
  • $\begingroup$ Actually, Ben, I think yours is better. Feel free to edit yours to incorporate anything that was in mine if you think there was anything worth adding (though I don't think there's a need, it looks fine to me). $\endgroup$
    – Glen_b
    Commented Feb 20, 2019 at 2:40
  • $\begingroup$ @BenBolker thanks the only data I saw in the paper is that mean exponential distribution=1? How to add this? Would you please check this link to see what is the input? pdfs.semanticscholar.org/1051/… $\endgroup$ Commented Feb 21, 2019 at 7:35
  • $\begingroup$ don't have time to read in detail. Best guess: they are looking at the flow through k serially linked compartments, each of which have an exponential residence time. The total residence time from start to finish will then be Erlang(k)-distributed ... $\endgroup$
    – Ben Bolker
    Commented Feb 21, 2019 at 14:28
  • $\begingroup$ @BenBolker How to add exponential mean to it? For instance made with exponential mean=1, k=1, and 100 points $\endgroup$ Commented Feb 23, 2019 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.