Advertisement
Guest User

MonteCarlo Parisian Option

a guest
Aug 27th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.32 KB | None | 0 0
  1. require(fOptions)
  2.  
  3. sobolInnovations = function(mcSteps, pathLength, init, ...) {
  4.   innovations = rnorm.sobol(mcSteps, pathLength, init, ...)
  5.   innovations
  6. }
  7.  
  8. wienerPath = function(eps) {
  9.   path = (b-sigma*sigma/2)*delta.t + sigma*sqrt(delta.t)*eps
  10.   path
  11. }
  12.  
  13. ParisianPayoff = function(path) {
  14.   ST = S*exp(sum(path))
  15.  
  16.   path = S*exp(cumsum(path))
  17.   path = (path <= H) + 0
  18.   path = (rle(path)$values[rle(path)$lengths >= k] == 1)
  19.  
  20.   if (sum(path) > 0) {payoff = 0}
  21.   else {
  22.     if (TypeFlag == "c") payoff = exp(-r*Time)*max(ST-X, 0)
  23.     if (TypeFlag == "p") payoff = exp(-r*Time)*max(0, X-ST)
  24.   }
  25.   payoff
  26. }
  27.  
  28. MonteCarloOption2 = function (delta.t, pathLength, k, mcSteps, mcLoops, init = TRUE,        
  29.                               innovations.gen, path.gen, payoff.calc, antithetic = TRUE,        
  30.                               standardization = FALSE, trace = TRUE, ...)
  31. {
  32.   delta.t <<- delta.t
  33.   k <<- k
  34.   if (trace)
  35.     cat("\nMonte Carlo Simulation Path:\n\n")
  36.   iteration = rep(0, length = mcLoops)
  37.   cat("\nLoop:\t", "No\t")
  38.   for (i in 1:mcLoops) {
  39.     if (i > 1)
  40.       init = FALSE
  41.     eps = innovations.gen(mcSteps, pathLength, init = init, ...)
  42.     if (antithetic)
  43.       eps = rbind(eps, -eps)
  44.     if (standardization)
  45.       eps = (eps - mean(eps))/sqrt(var(as.vector(eps)))
  46.     path = t(path.gen(eps))
  47.     payoff = NULL
  48.    
  49.     # Wrong in MonteCarloOption: for (j in 1:dim(path)[1])
  50.     for (j in 1:dim(path)[2]) payoff = c(payoff, payoff.calc(path[,j]))
  51.     iteration[i] = mean(payoff)
  52.     if (trace)
  53.       cat("\nLoop:\t", i, "\t:", iteration[i], sum(iteration)/i)
  54.   }
  55.   if (trace)
  56.     cat("\n")
  57.   iteration
  58. }
  59.  
  60. TypeFlag <<- "c"; S <<- 47; X <<- 50; H <<- 45
  61. Time <<- 0.5; sigma <<- 0.4; r <<- 0.1; b <<- 0.1
  62.  
  63. mc = MonteCarloOption2(delta.t = 1/2000, pathLength = floor(Time/delta.t), k = 0.1*floor(Time/delta.t), mcSteps = 50,
  64.                       mcLoops = 3000, init = TRUE, innovations.gen = sobolInnovations,
  65.                       path.gen = wienerPath, payoff.calc = ParisianPayoff,
  66.                       antithetic = TRUE, standardization = FALSE, trace = TRUE,
  67.                       scrambling = 2, seed = 4711)
  68.  
  69. par(mfrow = c(1, 1))
  70. mcPrice = cumsum(mc)/(1:length(mc))
  71. plot(mcPrice, type = "l", main = "Parisian Option", xlab = "Monte Carlo Loops", ylab = "Option Price")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement