O'Reilly logo

Mastering Scientific Computing with R by Radia M. Johnson, Paul Gerrard

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Rejection sampling

So far, we have showed you how to simulate data from well-defined distributions. However, you may also wish to simulate data from an unknown distribution. The first method we will show you to simulate random variables is through rejection sampling. This method is based on the idea that if you want to sample a random variable from a target distribution, you just have to sample the region under the curve of its density function. For example, let's consider a triangle distribution defined by the following density function:

Rejection sampling

First, let's plot the function in R by creating a triangle() function, as follows:

> triangle <- function(x) {(abs(x) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required