Generate R.V.

An important Lemma.

Note: Unless otherwise stated, the algorithms and the corresponding screenshots are adopted from Robert and Casella (2005).

Box-Muller Algorithm

Accept-Reject Method

Example:

f(x)exp(x2/2)(sin(6x)2+3cos(x)2sin(4x)2+1)f(x)\propto \exp(-x^2/2)(\sin(6x)^2+3\cos(x)^2\sin(4x)^2+1)

The Julia code is as follows:

Envelope Accept-Reject

It is easy to write the Julia code:

Atkinson's Poisson Simulation

It is necessary to note that the parameters in the algorithm are not same with those in the density function. In other words, the corresponding density function of the algorithm should be

f(x)=βexp(αβx)[1+exp(αβx)]2.f(x) = \beta \frac{\exp(\alpha-\beta x)}{[1+\exp(\alpha-\beta x)]^2}.

The following Julia code can generate the poisson random variable with respect to λ\lambda.

As mentioned in the above exercise, another poisson generation method can be derived from the following exercise.

We can write the following Julia code to implement this generation procedure.

ARS Algorithm

ARS is based on the construction of an envelope and the derivation of a corresponding Accept-Reject algorithm. It provides a sequential evaluation of lower and upper envelopes of the density ff when h=logfh=\log f is concave.

Let Sn{\cal S}_n be a set of points xi,i=0,1,,n+1x_i,i=0,1,\ldots,n+1, in the support of ff such that h(xi)=logf(xi)h(x_i)=\log f(x_i) is known up to the same constant. Given the concavity of hh, the line Li,i+1L_{i,i+1} through (xi,h(xi))(x_i,h(x_i)) and (xi+1,h(xi+1))(x_{i+1},h(x_{i+1})) is below the graph of hh in [xi,xi+1][x_i,x_{i+1}] and is above this graph outside this interval.

For x[xi,xi+1]x\in [x_i,x_{i+1}], define

hˉn(x)=min{Li1,i(x),Li+1,i+2(x)}andhn(x)=Li,i+1(x),\bar h_n(x)=\min\{L_{i-1,i}(x),L_{i+1,i+2}(x)\}\quad\text{and}\quad \underline h_n(x)=L_{i,i+1}(x)\,,

the envelopes are

hn(x)h(x)hˉn(x)\underline h_n(x)\le h(x)\le \bar h_n(x)

uniformly on the support of ff. Thus,

exphn(x)=fn(x)f(x)fˉn(x)=exphˉn(x)=wˉngn(x).\exp \underline h_n(x) = \underline f_n(x) \le f(x)\le \bar f_n(x)=\exp\bar h_n(x) = \bar w_ng_n(x)\,.

Davison (2008) provides another version of ARS

and gives an illustration example.

Let's consider the slightly simple verison in which we do not need to consider h(y)h_-(y). It is obvious that

Consider the CDF of g+=exp(h+)g_+=\exp(h_+):

G+(y)=yexp(h+(x))dx=min{z1,y}exp(h+(x))dx+z1min{z2,max{y,z1}}exp(h+(x))dx++zk1min{zk,max{y,zk1}}exp(h+(x))dx+zkmax{zk,y}exp(h+(x))dx\begin{aligned} G_+(y) & = \int_{-\infty}^y \exp(h_+(x)) dx \\ & = \int_{-\infty}^{\min\{z_1,y\}} \exp(h_+(x)) dx \\ & \qquad + \int_{z_1}^{\min\{z_2,\max\{y, z_1\}\}} \exp(h_+(x)) dx \\ & \qquad + \cdots \\ & \qquad + \int_{z_{k-1}}^{\min\{z_k,\max\{y, z_{k-1}\}\}} \exp(h_+(x)) dx \\ & \qquad + \int_{z_k}^{\max\{z_k,y\}} \exp(h_+(x)) dx \end{aligned}

and

zjzj+1exp(h+(x))dx=exp(h(yj+1))1h(yj+1)exp((yyj+1)h(yj+1))zjzj+1,  j=1,,k1\int_{z_{j}}^{z_{j+1}}\exp(h_+(x))dx = \exp({h(y_{j+1})})\cdot \frac{1}{h'(y_{j+1})}\exp((y-y_{j+1})h'(y_{j+1}))\mid_{z_j}^{z_{j+1}},\; j=1,\ldots,k-1

Then use the inverse of G+(y)G_+(y) to sample random variable whose density function is g+(x)g_+(x). So we can use the following Julia program to sample from g+(x)g_+(x).

Back to the main sampling algorithm, we can implement the procedure as follows:

Based on the ARS algorithm, we can also get the Supplemental ARS algorithm:

Exponential Random Variable

The inverse transform sampling from a uniform distribution can be easily used to sample an exponential random variable. The CDF is

F(x)=1exp(λx),F(x) = 1-\exp(-\lambda x)\,,

whose inverse function is

F1(y)=log(1y)λ.F^{-1}(y) = -\frac{\log(1-y)}{\lambda}\,.

Thus, we can sample UU(0,1)U\sim U(0, 1), and then perform the transfomation

logUλ.-\frac{\log U}{\lambda}\,.

Today, I came across another method from Xi'an's blog, which points to the question on StackExchange.

Still confused about the details, as commented in the code.

The theoretical part is as follows,

I rewrite the provided C code in Julia, and compare the distribution with samples from inverse CDF and the package Distributions

Last updated