# Calculates m replications of the Monte Carlo # integration in part (a) with a = y and n = n1 monte <- function(y,n1,m){ vect<-c(1:m) for (i in 1:m) { x <- rnorm(n1) total <- length(x[x > y]) vect[i] <- total/n1 } return(vect) } # Calculates m replications of the importance sampling # in part (c) with a = y1, mu = y2 and n = n1 import <- function(y1,y2,n1,m) { vect <- c(1:m) for (i in 1:m) { x <- rnorm(n1,y2) weight <- dnorm(x)/dnorm(x, y2) vect[i] <- sum(weight[x > y1])/n1 } return(vect) } # Calculates the importance sampling in part (d) for # P(X > y) and q(x) the Exp(1) truncated at y # Plots the cumulative estimate of P(X > y) with n = n1 import1 <- function(y,n1){ x<-rexp(n1)+y weight=dnorm(x)/dexp(x-y) plot(cumsum(weight)/1:n1,type="l",ylab="Cumulative sum of estimate",xlab="n") abline(a=pnorm(-y),b=0,col="red") }