####################################################
#    Scripts for Chapter 12 of
#    P. Brandimarte
#    Handbook in Monte Carlo Simulation
#    Applications in Financial Engineering,
#    Risk Management, and Economics
#    Wiley, 2014

#  Required packages
require(car)

#  Source the function file
source("Chapter12_Functs.R")

####################################################
#   Chunk 1: Naive estimation of delta by MC
####################################################

S0 <- 50; K <- 55; T <-  1
r <- 0.1; sigma <-0.4
numRepl <- 50000
dS <- 0.5
set.seed(55555)
DeltaOK <- VanillaCallDelta(S0,K,T,r,sigma)
OutDeltaMCNaive <- VanillaDeltaMCNaive(S0,K,T,r,sigma,
                                       dS,numRepl)
DeltaOK
OutDeltaMCNaive

####################################################
#   Chunk 2: Improving the estimation of delta by MC
#   using common random numbers and symmetric differences
####################################################

set.seed(55555)
OutDeltaMC <- VanillaDeltaMC(S0,K,T,r,sigma,dS,numRepl)
DeltaOK 
OutDeltaMC 


####################################################
#   Chunk 3: Using finite differences to estimate
#   delta for an Asian option (and check with linear model)
####################################################

S0 <- 90 
vetS <- seq(from=88, to=92, by=0.5)
K <- 90 
T <-  1 
r <- 0.05
sigma <-0.4
numSamples <- 12 
numRepl <- 45000
numPilot <- 5000; N <- length(vetS)
prices <- numeric(N)
set.seed(55555)
for (i in 1:N)
  prices[i] <- EuAsianCVGeom(vetS[i],K,T,r,0,sigma,
                             numSamples,numRepl,numPilot)$estimate
mod <- lm(prices~vetS)
p <- as.numeric(mod$coeff) 
delta <- p[2] 
ds <- 0.5
set.seed(55555)
numReplTot <- numRepl+numPilot
OutAsianDeltaCentral <- AsianDeltaCentral(S0,K,r,T,
                                          sigma,dS,numReplTot,numSamples)
delta
OutAsianDeltaCentral

####################################################
#   Chunk 4: Vanilla call delta by a pathwise estimator
####################################################

S0 <- 50; K <- 55; T <-  1
r <- 0.1; sigma <-0.4
numRepl <- 50000
DeltaOK <- VanillaCallDelta(S0,K,T,r,sigma)
DeltaOK
dS <- 0.5
set.seed(55555)
OutDeltaPath <- VanillaDeltaMCPath(S0,K,T,r,sigma,numRepl)
OutDeltaPath 

####################################################
#   Chunk 5: Asian call delta by a pathwise estimator
####################################################

S0 <- 90; K <- 90; > T <-  1
r <- 0.05; sigma <-0.4
numSamples <- 12
numRepl <- 50000
set.seed(55555)
AsianDeltaPath(S0,K,r,T,sigma,numRepl,numSamples)


####################################################
#   Chunk 6: Vanilla call delta by likelihood ratios
####################################################

S0 <- 50; K <- 55;T <-  1
r <- 0.1; sigma <-0.4
numRepl <- 50000
VanillaCallDelta(S0,K,T,r,sigma)
set.seed(55555)
VanillaDeltaMCLR(S0,K,T,r,sigma,numRepl)


####################################################
#   Chunk 7: Asian call delta by likelihood ratios
####################################################

S0 <- 90; K <- 90; T <- 1
r <- 0.05; sigma <- 0.4
numSamples <- 12; numRepl <- 50000
set.seed(55555)
AsianDeltaLR(S0,K,r,T,sigma,numRepl,numSamples)
 