[Returnanalytics-commits] r3878 - pkg/Dowd/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 30 01:08:04 CEST 2015


Author: dacharya
Date: 2015-07-30 01:08:04 +0200 (Thu, 30 Jul 2015)
New Revision: 3878

Added:
   pkg/Dowd/R/BlackScholesCallESSim.R
Log:
Function BlackScholesCallESSim added.

Added: pkg/Dowd/R/BlackScholesCallESSim.R
===================================================================
--- pkg/Dowd/R/BlackScholesCallESSim.R	                        (rev 0)
+++ pkg/Dowd/R/BlackScholesCallESSim.R	2015-07-29 23:08:04 UTC (rev 3878)
@@ -0,0 +1,70 @@
+#' ES of Black-Scholes call using Monte Carlo Simulation
+#' 
+#' Estimates ES of Black-Scholes call Option using Monte Carlo simulation
+#' 
+#' @param amountInvested Total amount paid for the Call Option and is positive 
+#' (negative) if the option position is long (short)
+#' @param stockPrice Stock price of underlying stock
+#' @param strike Strike price of the option
+#' @param r Risk-free rate
+#' @param mu Expected rate of return on the underlying asset and is in 
+#' annualised term
+#' @param sigma Volatility of the underlying stock and is in annualised 
+#' term
+#' @param maturity The term to maturity of the option in days
+#' @param numberTrials The number of interations in the Monte Carlo simulation
+#' exercise
+#' @param cl Confidence level for which ES is computed and is scalar
+#' @param hp Holding period of the option in days and is scalar
+#' @return ES 
+#' @references Dowd, Kevin. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Lyuu, Yuh-Dauh. Financial Engineering & Computation: Principles, 
+#' Mathematics, Algorithms, Cambridge University Press, 2002.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Market Risk of American call with given parameters.
+#'    BlackScholesCallESSim(0.20, 27.2, 25, .16, .2, .05, 60, 30, .95, 30)
+#'    
+#' @export
+BlackScholesCallESSim <- function(amountInvested, stockPrice, strike, r, mu, 
+                                 sigma, maturity, numberTrials, cl, hp){
+  # Precompute Constants
+  annualMaturity <- maturity / 360 # Annualised maturity
+  annualHp <- hp / 360 # Annualised holding period
+  N <- 1 # Number of steps - only one needed for black scholes option
+  dt <- annualHp / N # Size of time-increment equal to holding period
+  nudt <- (mu - .5 * sigma^2) * dt
+  sigmadt <- sigma * sqrt(dt)
+  lnS <- log(stockPrice)
+  M <- numberTrials
+  initialOptionPrice <- BlackScholesCallPrice(stockPrice, strike, 
+                                                 r, sigma, maturity)
+  numberOfOptions <- abs(amountInvested) / initialOptionPrice
+  # Stock price simulation process
+  lnSt <- matrix(0, M, N)
+  newStockPrice <- matrix(0, M, N)
+  for (i in 1:M){
+    lnSt[i] <- lnS + rnorm(1, nudt, sigmadt) # Random stock price movement
+    newStockPrice[i] <- exp(lnSt[i, 1]) # New stock price
+  }
+  # Profit/Loss camculation
+  profitOrLoss <- double(M)
+  if (amountInvested > 0) { # If option position is long
+    for (i in 1:M) {
+      profitOrLoss[i] <- (BlackScholesCallPrice(newStockPrice[i], strike, r, 
+                                               sigma, maturity - hp) - initialOptionPrice) * numberOfOptions
+    }
+  }
+  if (amountInvested < 0) { # If option position is short
+    for (i in 1:M) {
+      profitOrLoss[i] <- (-BlackScholesCallPrice(newStockPrice[i], strike, r, 
+                                                sigma, maturity - hp) + initialOptionPrice) * numberOfOptions
+    }
+  }
+  # VaR estimation
+  y <- HSES(profitOrLoss, cl) # VaR
+  return(y)
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list