R Studio

Monte Carlo Simulation Step by Step Approach

  1. ## How to perform a Monte Carlo Simulation?
  2. ## First Step:
  3. # Write a function to generate the option's innovations.
  4. # Use scrambled normal Sobol numbers:
  5. sobolInnovations = function(mcSteps, pathLength, init, ...) {
  6. # Create Normal Sobol Innovations:
  7. innovations = rnorm.sobol(mcSteps, pathLength, init, ...)
  8. # Return Value:
  9. innovations }
  10. ## Second Step:
  11. # Write a function to generate the option's price paths.
  12. # Use a Wiener path:
  13. wienerPath = function(eps) {
  14. # Note, the option parameters must be globally defined!
  15. # Generate the Paths:
  16. path = (b-sigma*sigma/2)*delta.t + sigma*sqrt(delta.t)*eps
  17. # Return Value:
  18. path }
  19. ## Third Step:
  20. # Write a function for the option's payoff
  21. # Example 1: use the payoff for a plain Vanilla Call or Put:
  22. plainVanillaPayoff = function(path) {
  23. # Note, the option parameters must be globally defined!
  24. # Compute the Call/Put Payoff Value:
  25. ST = S*exp(sum(path))
  26. if (TypeFlag == "c") payoff = exp(-r*Time)*max(ST-X, 0)
  27. if (TypeFlag == "p") payoff = exp(-r*Time)*max(0, X-ST)
  28. # Return Value:
  29. payoff }
  30. # Example 2: use the payoff for an arithmetic Asian Call or Put:
  31. arithmeticAsianPayoff = function(path) {
  32. # Note, the option parameters must be globally defined!
  33. # Compute the Call/Put Payoff Value:
  34. SM = mean(S*exp(cumsum(path)))
  35. if (TypeFlag == "c") payoff = exp(-r*Time)*max(SM-X, 0)
  36. if (TypeFlag == "p") payoff = exp(-r*Time)*max(0, X-SM)
  37. # Return Value:
  38. payoff }
  39. ## Final Step:
  40. # Set Global Parameters for the plain Vanilla / arithmetic Asian Options:
  41. TypeFlag <- "c"; S <- 100; X <- 100
  42. Time <- 1/12; sigma <- 0.4; r <- 0.10; b <- 0.1
  43. # Do the Asian Simulation with scrambled random numbers:
  44. mc = MonteCarloOption(delta.t = 1/360, pathLength = 30, mcSteps = 5000,
  45. mcLoops = 100, init = TRUE, innovations.gen = sobolInnovations,
  46. path.gen = wienerPath, payoff.calc = arithmeticAsianPayoff,
  47. antithetic = TRUE, standardization = FALSE, trace = TRUE,
  48. scrambling = 2, seed = 4711)
  49. # Plot the MC Iteration Path:
  50. par(mfrow = c(1, 1))
  51. mcPrice = cumsum(mc)/(1:length(mc))
  52. plot(mcPrice, type = "l", main = "Arithmetic Asian Option",
  53. xlab = "Monte Carlo Loops", ylab = "Option Price")
  54. # Compare with Turnbull-Wakeman Approximation:
  55. # TW = TurnbullWakemanAsianApproxOption(TypeFlag = "c", S = 100, SA = 100,
  56. # X = 100, Time = 1/12, time = 1/12, tau = 0 , r = 0.1, b = 0.1,
  57. # sigma = 0.4)
  58. # print(TW)
  59. # abline(h = TW, col = 2)
## How to perform a Monte Carlo Simulation?
## First Step:
# Write a function to generate the option's innovations.
# Use scrambled normal Sobol numbers:
sobolInnovations = function(mcSteps, pathLength, init, ...) {
  
  # Create Normal Sobol Innovations:
  innovations = rnorm.sobol(mcSteps, pathLength, init, ...)
  
  # Return Value:
  innovations }

## Second Step:
# Write a function to generate the option's price paths.
# Use a Wiener path:
wienerPath = function(eps) {
  
  # Note, the option parameters must be globally defined!
  # Generate the Paths:
  path = (b-sigma*sigma/2)*delta.t + sigma*sqrt(delta.t)*eps
  
  # Return Value:
  path }

## Third Step:
# Write a function for the option's payoff
# Example 1: use the payoff for a plain Vanilla Call or Put:
plainVanillaPayoff = function(path) {
  
  # Note, the option parameters must be globally defined!
  # Compute the Call/Put Payoff Value:
  ST = S*exp(sum(path))
  if (TypeFlag == "c") payoff = exp(-r*Time)*max(ST-X, 0)
  if (TypeFlag == "p") payoff = exp(-r*Time)*max(0, X-ST)
  
  # Return Value:
  payoff }

# Example 2: use the payoff for an arithmetic Asian Call or Put:
arithmeticAsianPayoff = function(path) {
  
  # Note, the option parameters must be globally defined!
  # Compute the Call/Put Payoff Value:
  SM = mean(S*exp(cumsum(path)))
  if (TypeFlag == "c") payoff = exp(-r*Time)*max(SM-X, 0)
  if (TypeFlag == "p") payoff = exp(-r*Time)*max(0, X-SM)
  
  # Return Value:
  payoff }

## Final Step:
# Set Global Parameters for the plain Vanilla / arithmetic Asian Options:
TypeFlag <- "c"; S <- 100; X <- 100
Time <- 1/12; sigma <- 0.4; r <- 0.10; b <- 0.1

# Do the Asian Simulation with scrambled random numbers:
mc = MonteCarloOption(delta.t = 1/360, pathLength = 30, mcSteps = 5000,
                      mcLoops = 100, init = TRUE, innovations.gen = sobolInnovations,
                      path.gen = wienerPath, payoff.calc = arithmeticAsianPayoff,
                      antithetic = TRUE, standardization = FALSE, trace = TRUE,
                      scrambling = 2, seed = 4711)

# Plot the MC Iteration Path:
par(mfrow = c(1, 1))
mcPrice = cumsum(mc)/(1:length(mc))
plot(mcPrice, type = "l", main = "Arithmetic Asian Option",
     xlab = "Monte Carlo Loops", ylab = "Option Price")

# Compare with Turnbull-Wakeman Approximation:
# TW = TurnbullWakemanAsianApproxOption(TypeFlag = "c", S = 100, SA = 100,
# X = 100, Time = 1/12, time = 1/12, tau = 0 , r = 0.1, b = 0.1,
# sigma = 0.4)
# print(TW)
# abline(h = TW, col = 2)