-1

I am struggling with the Monte Carlo integral problem in R.

y= x^2+cos(x); for x= [0,2]

I am supposed to use HitMiss <- function(T,S,method="halton") to solve this problem. T is the number of trails run per sample size in S. S is number of sample points

The function should return T*|S| matrix, |S| is length of S.

Please help me out and give me some clue on solving this problem. Really appreciate it!!!

Ives Shi
  • 31
  • 1
  • 5

1 Answers1

0

A few hints:

(1) Have you seen this package? There are some Halton methods: https://cran.r-project.org/web/packages/randtoolbox/randtoolbox.pdf

(2) Do you want to solve it only with the Halton method? For this easy example an easy solution would be this one:

set.seed(1)

N = 10000

f = function(x) x^2 + cos(x)
points(runif(N, 0, 2), runif(N, 0, 4), pch = 20)
curve(f(x), 0,2, ylim=c(0, 4), col='white', lwd = 2, add=TRUE)

sum(f(runif(N, 0, 2)) > runif(N, 0, 4))/N * 2*4
#[1] 3.5592

enter image description here

J_F
  • 9,956
  • 2
  • 31
  • 55
  • Thanks for the quick reply ! But how could I incorporate halton randomization into the problem ? The parameter for halton should be usetime=T, init=F... – Ives Shi Sep 23 '16 at 16:50