inst/resources/scripts/hit_miss2.r

hit_miss2 <- function(ftn, a, b, c, d, n) {
  # Monte-Carlo integration using the hit & miss method
  # partially vectorised version
  X <- runif(n, a, b)
  Y <- runif(n, c, d)
  Z <- (Y <= sapply(X, ftn))
  I <- (b - a)*c + (cumsum(Z)/(1:n))*(b - a)*(d - c)
  plot(1:n, I, type = "l")
  return(I[n])
}

Try the spuRs package in your browser

Any scripts or data that you put into this service are public.

spuRs documentation built on May 2, 2019, 12:44 p.m.