-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepr_ueb_l_4.Rnw
48 lines (36 loc) · 946 Bytes
/
epr_ueb_l_4.Rnw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<<child='epr_ueb_a_4.Rnw'>>=
@
<<echo = FALSE>>=
monte_carlo = function(f, a = 0, b = 5, n, ...) {
opt = optimize(f, interval = c(a, b), maximum = TRUE)
bot = 0
top = opt$objective
rect_area = (b - a)*(top - bot)
x = runif(n, a, b)
y = runif(n, bot, top)
x_eval = sapply(x, f)
hit = (y <= x_eval) & (x_eval >= 0)
return(rect_area * mean(hit))
}
@
<<eval = FALSE>>=
f = function(x) sqrt(x) * sin(x) * cos(x) + 1
N = 100
library(parallelMap)
parallelStartSocket(1)
res1 = system.time(parallelMap(monte_carlo, n = rep(10^6, N), more.args = list(f = f)))
parallelStop()
parallelStartSocket(4)
res2 = system.time(parallelMap(monte_carlo, n = rep(10^6, N), more.args = list(f = f)))
parallelStop()
res1
res2
@
<<eval = FALSE>>=
library(batchtools)
reg = makeRegistry(file.dir = NA, seed = 1)
batchMap(fun = monte_carlo, n = rep(10^6, 100), more.args = list(f = f))
submitJobs()
getStatus()
res = reduceResultsList()
@