My previous post is about a method to simulate a Brownian motion. A friend of mine emailed me yesterday to tell me that this is useless if we do not know how to simulate a normally distributed variable.

My first remark is: use the rnorm() function if the quality of your simulation is not too important (Later, I'll try to explain you why the R "default random generation" functions are not perfect). However, it may be fun to generate a normal distribution from a simple uniform distribution. So, yes, I lied, I won't create the variable from scratch but from a uniform distribution. 

The method proposed is really easy to implement and this is why I think it is a really good one. Besides, the result is far from being trivial and is really unexpected. This method is called the Box-Muller method. You can find the proof of this method here. The proof is not very complicated, however, you will need a few mathematical knowledges to understand it.

Let u and v be two independent variables uniformly distributed.  Then we can define:

x = sqrt(-2log(u))sin(2 PI v)
y = sqrt(-2log(u))cos(2 PI v)

x and y are two independent and normally distributed variables. The interest of this method is its extreme simplicity in term of programming (We only need 9 lines if we don't want to test the normality of the new variables neither plot the estimation of the density).

We can obtain a vector of variables normally distributed. The Lillie test doesn't reject the null hypothesis of normal distribution. Besides, we can plot the estimation of the density of the variables. We obtain the following plot that looks indeed similar to the Gaussian density.



The program (R):

 # import the library to test the normality of the distribution
library(nortest)

size = 100000

u = runif(size)
v = runif(size)

x=rep(0,size)
y=rep(0,size)

for (i in 1:size){
  x[i] = sqrt(-2*log(u[i]))*cos(2*pi*v[i])
  y[i] = sqrt(-2*log(u[i]))*sin(2*pi*v[i])
}

#a test for normality
lillie.test(c(x,y))

#plot the estimation of the density
plot(density(c(x,y)))


0

Add a comment

Pricing of a financial product : A pricer of a call option.
Temporal network model - Barabási-Albert model with the library igraph
3
How to plot a network (package network) - Tip 2
1
Have you tried to understand your network? - Random generation of network models
Function apply() - Tip 1
1
Want to win "Guess who?" - Have an institutional neural network approach
How to choose your next holidays destination - Uniform distribution on a sphere
4
Generation of a normal distribution from "scratch" - The box-muller method
Generate stock option prices - How to simulate a Brownian motion
1
The consequence of merging insurance companies - Risk simulation and probability of ruin
Estimation of the number PI - A Monte Carlo simulation
The movement of a drunk guy - Random Walk and exponential regression
How does my computer know what language I am using? – An approach of statistical learning (Language, Computer Science)
How does my computer know what language I am using? – An approach of statistical learning (Language, Computer Science)
The fear-index: is the VIX efficient to be warned about high volatility? (Finance & Systematic Processus)
Who is the most complete athlete? – An insight with the Mahalanobis distance (sport & data analysis)
Who is the most complete athlete? – An insight with the Mahalanobis distance (sport & data analysis)
ProbaPerception: Introduction
Blog Archive
Translate
Translate
Loading