CSI 779, Spring 1995, J. E. Gentle Assignment I In the S code below, I have used variables for quantities that are fixed in the problem statement. This is a good practice in general. In fact, it would probably be a good idea to write the S code as functions, with these variables as dummy arguments. In all programs involving a random number generator, it is best to set the seed explicitly. This allows you to check your work, and it also provides information you may need if you want to combine simulations. For routine exploratory work, I usually include the statement set.seed(1) If you write your code as a function, you would not put this statement in the function, of course. It, together with the other initialization statements, would go before the invocation of the function. 1. A simple S code to estimate this integral is uis <- runif(mm,aa,bb) inthat <- (bb-aa)*sum(uis*exp(uis)*sin(uis))/mm where mm is the Monte Carlo sample size and aa and bb are the limits of integration, 1 and 3 in this case. (And, No! you cannot write it in one statement: **INCORRECT inthat <- (bb-aa)* sum(runif(mm,aa,bb)*exp(runif(mm,aa,bb))*sin(runif(mm,aa,bb)))/mm **INCORRECT because of the little thing called side effects. In random number generation, "side effects" cause us to get new random numbers, which of course, is exactly what we want. The estimate should be about 22.7 2. As pointed out, the weight function is logistic: exp(-x)/(1 + exp(-x))^2 Integrating x over the full range is equivalent to estimating the mean of a logistic: 0. A Monte Carlo estimate is just the sample mean. In S-Plus the statement is meanhat <- mean(rlogis(mm)) where mm is the Monte Carlo sample size. 3. Some S-Plus code is xx <- matrix(scan("law.dat"),byrow=T,ncol=2) nn <- dim(xx)[1] rs <- rep(0, mm) for (ii in 1:mm) { index <- sample(1:nn, replace = T) rs[ii] <- cor(xx[index,1], xx[index,2]) } stdstar <- sqrt(var(rs)) Where mm is the number of bootstrap replications. The solution comes out to be approximately 0.13.