Guest User

Untitled

a guest
Mar 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. BURN_IN = 10000
  2. DRAWS = 100000
  3.  
  4. f = function(x) exp(sin(x))
  5.  
  6. chain = numeric(BURN_IN + DRAWS)
  7.  
  8. x = 1/2
  9.  
  10. for (i in 1:(BURN_IN + DRAWS)) {
  11. y = runif(1) # proposal
  12. if (runif(1) < min(1, f(y)/f(x))) x = y
  13. chain[i] = x
  14. }
  15.  
  16. x_min = median(chain[BURN_IN : (BURN_IN + DRAWS)])
  17.  
  18. cat("Metropolis minimum found at", x_min, "nn")
  19.  
  20. # MONTE CARLO ENDS HERE. The integrations bellow are just to check the results.
  21.  
  22. A = integrate(f, 0, 1)$value
  23.  
  24. F = function(x) (abs(integrate(f, 0, x)$value - A/2))
  25.  
  26. cat("Optimize minimum found at", optimize(F, c(0, 1))$minimum, "n")
  27.  
  28. Metropolis minimum found at 0.6005409
  29. Optimize minimum found at 0.601365
Add Comment
Please, Sign In to add comment