#source("c:/Work07/ShortCourse07/Programs/TruncatedIS.txt",print.eval=TRUE)# #This gives an importance sampling algorithm for a Truncated Normal# a<-4.5 #Truncation Point n<-50000 #Sample size #---------Note!! R can do this with pnorm(3.5) and pnorm(4.5) 1-pnorm(a) #---------Generate a Sample Naively------------------------- #---------Take n=50000, a=3.5 and 4.5-------------------------- X<-rnorm(n) mean((X>a)) #----------Use Accept Reject-------------------------------- nsim<-1000 #much smaller sample X<-rexp(nsim,a)+a mean(dnorm(X)/(a*exp(-a*(X-a)))) #sd(dnorm(X)/(a*exp(-a*(X-a)))) #-----------Use Uniform---------------------------------- U<-runif(nsim,0,1/a) mean(dnorm(1/U)/(a*U^2)) #sd(dnorm(1/U)/(a*U^2))