Skip to content

Instantly share code, notes, and snippets.

@felixlindemann
Last active December 21, 2015 00:29
Show Gist options
  • Save felixlindemann/6220271 to your computer and use it in GitHub Desktop.
Save felixlindemann/6220271 to your computer and use it in GitHub Desktop.
Transformation Rechteckverteilter Zufallszahlen in standardnormalverteilte
# define function
# found in:
# Wilmott, Paul (2007):
# Paul Wilmott introduces quantitative finance.
# 2. Edition. Chichester: Wiley.
# r: number of random numbers to be calculated
StandNorm<-function(r=30){
# how many iterations should be calculated?
# 50 proofed to be a good number to get robust
# random numbers
n<-50
# introduce return value
z<-NULL
#iterate
for(i in 1:r){
#add new number
z<-c(z,sqrt(12/n)*(sum(runif(n))-n/2))
}
z #return
}
# call function and store 100 numbers in z
z<-rep(StandNorm(),100)
#define a sequence
x<-seq(-5,5,by=0.05)
#plot histogram
hist(z,freq=FALSE, main="Histogramm Zufallszahlen")
#draw gaussian density function
lines(x,dnorm(x,0,1),col=2,lty=2)
#draw kernel-density of sample
lines(density(z),lwd=2)
#plot observations
rug(z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment