- You may recall that random variables live on sample space, but has anyone told you the address of this sample space?
- You have generated random variables on R, how does it do that?
Functions of independent random variables are again independent.
It suffices to have independent uniform random variables.
If \(U\) is uniformly distributed on \([0,1]\), express \(U\) as a \[ U= X_1 X_2 X_3 X_4 \cdots\] be the usual base-\(10\) expansion of \(U\), so that \(X_i\) are random variables that take integer-values \(0,1,2, \ldots, 9\).
It is not hard to show that \(X_i\) are independent and uniformly distributed.
By rearranging and recombining these digits \(X_i\), from one \(U\) uniform random variables, you can produce as many independent ones as you like.
The inverse transform method is often too slow
The Box-Muller method for generating normal random variables. Ann. Math. Stat. 1958
Let \(U\) and \(V\) be independent random variables that uniformly distributed on the unit interval. Set \[X = \sqrt{ -2 \log U} \cos (2 \pi V)\] and \[Y = \sqrt{ -2 \log U} \sin (2 \pi V).\] Then \(X\) and \(Y\) are independent standard normals.
We solve for \((U,V)\) to obtain that \[U = \exp[ -(X^2 + Y^2)/2]\] and \[V = \frac{1}{2\pi} \arctan(Y /X).\] Thus the map \[(u, v) \mapsto \big (\sqrt{ -2 \log u} \cos (2 \pi v), \sqrt{ -2 \log u} \sin (2 \pi v) \big)\] is a bijection from \((0,1) \times (0,1)\) to \(\mathbb{R}^2 \setminus \{ (0,0)\}.\)
It has Jacobian is given by \[J(x,y) = -\frac{1}{2\pi } e^{-(x^2 + y^2)/2}\] Hence the (joint) pdf for \((X,Y)\) is given by \[(x,y) \mapsto \Big( \frac{1}{\sqrt{2 \pi} } e^{-x^2/2} \cdot \frac{1}{\sqrt{2 \pi}} e^{-y^2/2} \Big),\] as desired.
We simulate a single uniformly distributed point on the disc, by simulating a uniform point on a square, and accept it if lands inside an inscribed disc, or reject otherwise, and repeat until we have acceptance.
point <- function(){ z=2 while(length(z)==1){ x = 2*runif(1) -1 y = 2*runif(1) -1 if (x^2 + y^2 <1) { z<-c(x,y)} } z }
re=replicate(500, point()) plot(re[1,], re[2,], xlim=c(-1.1,1.1), ylim=c(-1.1,1.1), asp=1)
Suppose we want to generate a random variable \(X\) with pdf \(f\).
Suppose \(f\) is a complicated pdf with an inverse that is difficult to compute.
Suppose we can easily generate \(Y\) which has pdf \(g\).
Suppose there exists \(M\) so that \(f(x) \leq M g(x)\).
Then you are in good shape!
Observe that by the change of variables formula and Fubini, we have \[ \begin{eqnarray*} && \mathbb{P}(Y \leq x, MU g(Y) \leq f(Y)) \\ &=& \int_{-\infty} ^x \int_0 ^1 \mathbf{1}[Mu g(y) \leq f(y)]g(y)dudy \\ &=& \int_{-\infty} ^x \mathbb{P}[U \leq f(y)/Mg(y)] \cdot g(y) dy \\ &=& \int_{-\infty} ^x f(y)/M dy \\ &=& F(x)/M. \end{eqnarray*} \]
Thus \[ \mathbb{P}(Y \leq x | MU g(Y) \leq f(Y)) = F(x).\] So, given that we accepted the distribution of \(Y\), is the distribution of \(X\), that random variable we want to sample/simulate.
samplef <- function(){ x=-1 while(x==-1){ y = runif(1) u = runif(1) if( u*3 < 3*(y^2)){ x<- y} } x }
accepted=replicate(100000, samplef()) B = seq(0, 1, by=0.01) hist(accepted, prob=TRUE, B)