# R code to approximate expected values of fcns of r.v.'s # Examples 3 and 4 # Example 3 my.n <- 1000 rand.norm <- rnorm(my.n, 10, 1) expect.val.1 <- (1/my.n)*sum(rand.norm^2) print(expect.val.1) # Example 4 # A plot of the beta(5,1) density: X <- seq(0.01,0.99,by=0.01) plot(X,dbeta(X,5,1), ylab="Density",type="l") my.n <- 1000 rand.beta <- rbeta(my.n, 5, 1) expect.val.2 <- (1/my.n)*sum(-log(rand.beta)) print(expect.val.2)