Examples Of The Inverse CDF Method In R

Hi. This is a follow up math and stats post on the The Inverse CDF Method. Examples on how this Monte Carlo simulation method works will be shown here. The main idea is that we generate various random variables with standard uniform random variables.

June 2017 Updates: Included more examples and more R code with ggplot2 visuals.


Sections

Example One – Generating Uniform Random Variables With Standard Uniform Random Variables

Example Two – Exponential Distribution

Example Three – Pareto Distribution

References


Example One – Generating Uniform Random Variables With Standard Uniform Random Variables

Before doing any random number generation in R, the ggplot2 package is loaded by typing in

I also implement a random seed such that the results can be reproduced. Not using a random seed would yield different results everytime random numbers/variables are generated.

This first batch of code is about generating random numbers from a uniform distribution with a minimum of 2 and a maximum of 4. A plot (using ggplot2) is included.

It may not be obvious to see how there are 1 million observations in the plot but I will explain it here. Each bin has a binwidth of 0.1 so there would be 20 bins from 2.0 to 4.0. The height of the (near) rectangle is 50000. The one million comes from 20 x 50000.

This second batch of code is about generating the same uniform random variables with a minimum of 2 and a maximum of 4 but from the Inverse CDF Method. The generating formula is:

    \[a + (b - a)U\]

where U is a standard uniform random variable with a minimum of 0 and a maximum of 1.

Since I want to generate uniform random variables with a minimum of 2 and a maximum of 4, I have a = 2 and b = 4. The code and output can be found below.

For the most part, the plot here is not much different from the plot above. The plots are not exactly the same due to randomness.


Example Two – Exponential Distribution

Recall that from the previous post that we generated an exponential random variable with x > 0 and the rate lambda was \lambda > 0. The continuous probability distribution of the exponential random variable is:

\displaystyle f(x) = \lambda \text{e}^{-\lambda x

From finding the inverse of the cumulative distribution function (CDF) we came to the generating formula for X as X = -\dfrac{1}{\lambda} \text{ln}(1 - U).

Alternatively, we can use X = -\dfrac{\text{ln}(U)}{\lambda} since (1 - U) \sim \text{Unif}(0, 1) from U \sim \text{Unif}(0,1).

R Code

We use this generating formula X to simulate exponential random variables from standard uniform random variables. A sample size of 10000 is used and the rate/lambda is 5. The code is for simulating exponential random variables directly from the rexp() function R and from the Inverse CDF Method.

For the most part, the frequency distributions are quite similar. The Inverse CDF Method for approximating/simulating an exponential random variable does a good job from standard uniform random variables.

 


Example Three – Pareto Distribution

Despite people knowing about Pareto’s 80-20 rule, the Pareto distribution is not really discussed much in introductory probability & statistics books (in my opinion).

Given the shape parameter k and the scale parameter \lambda, the Pareto distribution has a probability density function (pdf) of:

    \[f(x) = \dfrac{k \lambda^k}{x^{(k + 1)}} \text{ for } x > \lambda\]

The generating formula for the Pareto distribution is:

    \[\frac{\lambda}{U^{1/k}}\]

with U as the standard uniform random variable.

Details of finding this generating formula involves integration and some algebra which can be found here.

The code for generating Pareto random variables involves creating a function for the generating formula. This function allows the user to input the size of the generating sample and the parameter size for lambda and k. I show the code and plots for Pareto random variables for lambda and k being 1 and for lambda and k being 2.

 

 

 


References

  • Financial Mathematics: A Comprehensive Treatment by Giuseppe Campolieti and Roman N. Makarov
  • Featured image from https://theclevermachine.files.wordpress.com/2012/09/inversetransformstudent3.png
  • http://www.math.wm.edu/~leemis/chart/UDR/PDFs/Pareto.pdf
  • http://isites.harvard.edu/fs/docs/icb.topic1475777.files/stat110hw5solutions.pdf

One thought on “Examples Of The Inverse CDF Method In R

Leave a Reply