Description
To visualize the motivation behind rejection sampling, imagine graphing the density function of a random variable onto a large rectangular board and throwing darts at it. Assume that the darts are uniformly distributed around the board. Now take off (reject) all of the darts that are outside the area under the curve. The remaining darts will be distributed uniformly within the area under the curve, and the x-positions of these darts will be distributed according to the random variable's density. This is because there is the most room for the darts to land where the curve is highest and thus the probability density is greatest.
The visualization as just described is equivalent to a particular form of rejection sampling where the proposal distribution is uniform (hence its graph is a rectangle). The general form of rejection sampling assumes that the board is not necessarily rectangular but is shaped according to some distribution that we know how to sample from (e.g. using inversion sampling), and which is at least as high at every point as the distribution we want to sample from, so that the former completely encloses the latter. (Otherwise, there will be parts of the curved area we want to sample from that can never be reached.) Rejection sampling works as follows:
Sample a point (an x-position) from the proposal distribution.
- Draw a vertical line at this x-position, up to the curve of the proposal distribution.
- Sample uniformly along this line (i.e. uniformly from 0 to the value of the proposal distribution (maximum of the probability density function)). If the sampled value is greater than the value of the desired distribution at this vertical line, return to step 1.
- Note also that this algorithm can be used to sample from the area under any curve, regardless of whether the function integrates to 1. In fact, scaling a function by a constant has no effect on the sampled x-positions. This means that the algorithm can be used to sample from a distribution whose probability density function is only known up to a constant (i.e. whose normalizing constant is unknown), which is common in computational statistics.
Now we perform the rejection sampling algorithm as follows:
1. Sample x from g(x) and u from U(0,1).
2. Check whether u<f(x)/Mg(x) or not.
3. If this holds, accept x as a realization of f(x).
4. If not, reject the value of x and repeat the sampling step.
Let us choose the uniform distribution as g(x). We see that we can choose M=8/5 since f (x) < 8/5 g(x) . The following code implements the rejection sampling:
- M=8/5;N=10000;
- f=@(x) 4/5*((x>0 & x<1)+(x>3/8&x<5/8));
- g=@(x) (x<1 & x>0);
- xsamples=[];
- for i=1:N
- u=rand(1);
- x=rand(1);
- if u<f(x)/(M*g(x))
- xsamples=[xsamples x];
- end
- end
- hist(xsamples);
- We can also implement the same algorithm using vectorization. We can sample N number of us and xs and accept
- or reject them in one shot.
- u=rand(N,1);x=rand(N,1);
- accept=u<f(x)./(M*g(x));
- xsamples=x(accept);
- hist(xsamples)
Reference
Numerical Methods using Matlab Abhishek Gupta Apress


雷达卡




京公网安备 11010802022788号







