Generation of random sample from a given density function

585 Views Asked by At

How to generate random samples associated with a pdf is given by

\begin{equation} f_V(v) = \frac{1}{\pi\sqrt{v(2-v)}} \end{equation} where $v \in (0,2)$

in MATLAB. As we know in MATLAB 'randn()' is used to generate random numbers which follow a normal distribution and 'rand()' is used to generate random number associated with uniform distribution. But how to generate random samples whose associated pdf is $f_V(v)$.

2

There are 2 best solutions below

9
On

You can calculate analytically in this case the cdf of your random variable (for lazy people https://www.wolframalpha.com/input/?i=integrate+1%2Fsqrt%28x*%282-x%29%29 ). Than:

$$F(x)=1-\frac{2}{\pi} \arcsin \sqrt{1-\frac{x}{2}}$$

for $0\le x\le2$

And $F^{-1}(z)=2-2 \sin^2(\frac{\pi-\pi z}{2})$ (please check my calculations)

So now take $Z\sim U(0,1)$, i.e. use your random uniform enerator. According to theory:

$$G(Z)=2-2 \sin^2\left(\frac{\pi-\pi Z}{2}\right)$$

will be distributed according to the distribution you want. See also here:

https://en.wikipedia.org/wiki/Inverse_transform_sampling

0
On

\begin{align} v(2-v) & = {}\overbrace{-(v^2 - 2v) = -(v^2 - 2v+1) + 1}^\text{completing the square} \\[8pt] & = 1-(v-1)^2 \end{align}

The pdf is for $0<v<2.$ Note that if $v<0$ or $v>2$ then $v(2-v)$ is a negative number under the radical.

\begin{align} F_V(v) & = \int_0^v \frac{du}{\pi \sqrt{u(2-u)}} \\[8pt] & = \frac 1 \pi \int_0^v \frac{du}{\sqrt{1 - (u-1)^2}} \\[10pt] & = \frac 1 \pi \int_{-1}^{v-1} \frac{dw}{\sqrt{1 - w^2}} \\[10pt] & = \frac 1 \pi \big( \arcsin(v-1)-\arcsin(-1) \big) \\[8pt] & = \frac 1 2 + \frac 1 \pi \arcsin(v-1). \end{align}

$$ \text{So } F_V^{-1}(y) = \sin\left(\pi\left(y - \frac 1 2 \right)\right) + 1 \text{ for } 0\le y\le1.$$

If (capital) $Y$ is a random variable that is uniformly distribution between $0$ and $1,$ then $F_V^{-1}(Y)$ has the distribution that you gave us.