(R question for Monte Carlo etimates) please give me a detail explantion. here s
ID: 2908738 • Letter: #
Question
(R question for Monte Carlo etimates)
please give me a detail explantion. here s the method 1&2 which you need for this problems
-----------------------------------
# Method one
integrand <- function(x)x^(2/3) * cos(x) * sin(x)
num_nodes <- 1e4
set.seed(3701)
pairs <- matrix(runif(2 * num_nodes), ncol = 2)
plot(integrand, 0, 1, xlim = c(0, 1), ylim = c(0, 1))
points(pairs[, 1], pairs[, 2])
less_pairs <- pairs[pairs[, 2] <= integrand(pairs[, 1]), ]
points(less_pairs[, 1], less_pairs[, 2], col = "orange", pch = 2)
prop <- nrow(less_pairs) / num_nodes
# Method two
mean(integrand(runif(1e7)))
------------------------------------------------------------------------------------
1. Let g(t) t5 sin(t)le-. We will consider to Monte Carlo estimates of J gt)dt 15 and approximate the integral using the first a method described in class. That is, generate points uniformly at random on an appropriately selected rectangle and take the proportion of them that falls under the graph of g(t) to be an estimate of the area under the graph / total area of the rectangle Hint: The lower left corner of your rectangle can be (-15,0) and the lower right (15,0), so its width is 30 - you have to look at g(t) to select the height.] (b) Let b- -a - 15 and approximate the integral using the seconod method described in class. That is, generate i.i.d. copies of a random variable with expectation g(t)dt and take the average of these as vour estimate.Explanation / Answer
# Method one
rm(list=ls(all=TRUE))
integrand <- function(t){abs((t^5)*sin(t))*exp(-abs(t))}
num_nodes <- 1e4
pairs1 <- matrix(runif(2*num_nodes, -15,0), ncol = 2)
pairs2 <- matrix(runif(2*num_nodes, 0,15), ncol = 2)
pairs11=cbind(pairs1[,1], pairs2[,1])
pairs22=cbind(pairs2[,1], pairs2[,2])
plot(integrand, -15, 15)
points( pairs11[,1], pairs11[,2])
points( pairs22[,1], pairs22[,2])
less_pairs1 <- pairs11[pairs11[, 2] <= integrand(pairs11[, 1]),]
less_pairs2 <- pairs22[pairs22[, 2] <= integrand(pairs22[, 1]),]
points(less_pairs1[,1], less_pairs1[,2], col = "orange", pch = 2)
points(less_pairs2[,1], less_pairs2[,2], col = "orange", pch = 2)
prop <- (nrow(less_pairs1)+nrow(less_pairs2)) / num_nodes
prop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.