## Generating Geometric and Truncated Exponential Random Variates

If you do numerical simulations you often find yourself needing to generate random variates from a specific distribution.  There are several techniques for doing this, such as the inverse transform method and the acceptance-and-rejection method.  This post will talk about a simple way to generate variates from the geometric and truncated exponential distributions, where the latter is the exponential distribution restricted to the range $[0,1]$.

The classic example of the inverse transform method is generating random variates from an exponential$(\lambda)$ distribution.  (This is because the exponential cdf is so easy to invert.)  If $U \sim$ uniform$(0,1)$, and $X = -\ln U/\lambda$, then $X \sim$ exponential$(\lambda)$.  So it’s easy to generate exponential random variates.

Then, to get geometric and truncated exponential random variates, all you have to do is take $\lfloor X \rfloor$ and $\{X\}$, the integer and fractional parts of $X$, respectively!  More explicitly, the integer part $\lfloor X \rfloor$ has a geometric$(1-e^{-\lambda})$ distribution, and the fractional part $\{X\}$ has the truncated exponential$(\lambda)$ distribution on $(0,1)$.

Proof:

$\displaystyle P(\lfloor X \rfloor = k, \{X\} \leq x) = P(k \leq X < k + x) = e^{-\lambda k} - e^{-\lambda (k+x)} = e^{-\lambda k} (1 - e^{-\lambda x}).$

If we fix $k$ and substitute 1 for $x$ we have $P(\lfloor X \rfloor = k, \{X\} \leq 1) = P(\lfloor X \rfloor = k) = e^{-\lambda k} (1 - e^{-\lambda}) = (e^{-\lambda})^k (1 - e^{-\lambda})$.
Thus $\lfloor X \rfloor \sim$ geometric$(1-e^{-\lambda})$.

If we fix $x$ and sum over all possible values of $k$ we have $\displaystyle P(\{X\} \leq x) = (1 - e^{-\lambda x}) \sum_{k=0}^{\infty} e^{-\lambda k} = \frac{1 - e^{-\lambda x}}{1- e^{-\lambda}}.$

Now, $f(x) = 1 - e^{-\lambda x}$ is the cdf of an exponential$(\lambda)$ distribution, and $1- e^{-\lambda}$ is the probability that an exponential$(\lambda)$ random variable is no greater than 1.  Thus $\{X\}$ has this truncated exponential$(\lambda)$ distribution — an exponential$(\lambda)$ distribution whose values are not allowed to be greater than 1.

See, for example, Eisenberg [1] or my answer to “Random exponential-like distribution” on math.SE.

References

1.  Bennett Eisenberg, “On the expectation of the maximum of IID geometric random variables,” Statistics and Probability Letters 78 (2008) 135-143.