Skip to content

Instantly share code, notes, and snippets.

@wahalulu
Created November 15, 2017 22:48
Show Gist options
  • Save wahalulu/27484ad65ef1f5c097f612147e3ff6fa to your computer and use it in GitHub Desktop.
Save wahalulu/27484ad65ef1f5c097f612147e3ff6fa to your computer and use it in GitHub Desktop.
# =============
# === Setup ===
# =============
# install packages
library(devtools)
install_github("azure/razurebatch")
install_github("azure/doazureparallel")
# import the doAzureParallel library and its dependencies
library(doAzureParallel)
# set your credentials
setCredentials("credentials.json")
# Create your cluster if not exist
cluster <- makeCluster("cluster.json")
# register your parallel backend
registerDoAzureParallel(cluster)
# check that your workers are up
getDoParWorkers()
# ======================================
# === Monte Carlo Pricing Simulation ===
# ======================================
# set the parameters for the monte carlo simulation
mean_change = 1.001
volatility = 0.01
opening_price = 100
# define a new function to simulate closing prices
getClosingPrice <- function() {
days <- 1825 # ~ 5 years
movement <- rnorm(days, mean=mean_change, sd=volatility)
path <- cumprod(c(opening_price, movement))
closingPrice <- path[days]
return(closingPrice)
}
start_s <- Sys.time()
# Run 10,000 simulations in series
closingPrices_s <- foreach(i = 1:10, .combine='c') %do% {
replicate(1000, getClosingPrice())
}
end_s <- Sys.time()
# plot the 50 closing prices in a histogram to show the distribution of outcomes
hist(closingPrices_s)
# Estimate runtime for 10 million
difftime(end_s, start_s)
1000 * difftime(end_s, start_s, unit = "min")
# Run 10 million simulations with doAzureParallel
# We will run 100 iterations where each iteration executes 100000 simulations
opt <- list(chunkSize = 2) # optimizie runtime
start_p <- Sys.time()
closingPrices_p <- foreach(i = 1:100, .combine='c', .options.azure = opt) %dopar% {
replicate(100000, getClosingPrice())
}
end_p <- Sys.time()
difftime(end_p, start_p, unit = "min")
# plot the 10 million closing prices in a histogram to show the distribution of outcomes
hist(closingPrices_p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment