-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInverseCDFNormal.R
More file actions
21 lines (20 loc) · 814 Bytes
/
InverseCDFNormal.R
File metadata and controls
21 lines (20 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Inverse CDF Method to Generate Random Variable cc @mathsorosThe following R code is used to generate certain number of random variables under N(0,1) distribution. The main function is ?sampler? and it take an integer as input.
## Main function ##
sampler <- function(n){
rv <- NULL ## Initialization ##
for (i in 1:n){
Y <- runif(1,min=0,max=1)
## Change this to generate rv's under other distribution ##
X <- qnorm(Y,mean=0,sd=1)
rv <- c(rv,X)
}
return(rv)
}
## Generate 10000 N(0,1) random variables ##
genrv <- sampler(10000)
## Plot histogram of samples ##
hist(genrv,30,prob=TRUE,xlab='X',
main='Normal random samples generated by inverse CDF')
## Compare with N(0,1) distribution
xx <- seq(-3,3,length.out=10000)
lines(xx,dnorm(xx),type="l",col="blue")