1

I have an assignment that requires me to calculate the implied volatility of a series of options using their parameters and market price. I understand that the easy way to do this would be to use the compute.implied.volatility function within R, however this question requires me to solve this using the nlm function. I understand that in this case I am wanting to minimise the distance between the actual price and my calculated price such that the distance is zero. To do this I obviously want to change the volatility in the option such that it sets my calculated price equal to the market price. The trouble I am having with this question is getting the nlm function to work, as we have not been taught much about it in this course.

I understand that I am meant to feed in a loop to nlm that enables it to iteratively calculate until it finds the minimum value that produces the result. I believe I'm not feeding in a function that works with the nlm, as I am currently getting an error of "Invalid function value in nlm optimizer".

I have attached my code as well as the inputs to work with, please let me know if I've written it incorrectly or if I need to tinker with it a little bit more to get an answer out for the required volatility. Thanks for any and all help!

```{r}
# Load in the library's and clear workspace
{cat("\014")  
   rm(list=ls(all=TRUE))  
   options(digits=6)}

library(fBasics)
library(knitr)
library(zoo)
library(psych)
library(lubridate)
library(stats)
library(boot)
library(matrixStats)

# First setup the parameter vectors to use in calculating IV
S <- rep(1200, 12) # Price at time = 0
r <- rep(0.01, 12) # Current interest rate
T <- rep(44/365, 12) # Time till maturity of the options
X <- c(1100,1120,1140,1160,1180,1200,1220,1240,1260,1280,1300,1320) # 
Strike prices of each option
type <- c(1,1,1,1,1,1,0,0,0,0,0,0) # 1 = Put and 0 = Call variable
mktprice <- c(10.5,13.8,18.2,23.9,31.2,40,31.8,23.9,17.5,12.5,9.0,6.3) 
# Market price of each option
sigma <- rep(0.2, 12) # Initial guess for sigma

options.df <- data.frame(S, X, r, T, type, mktprice, sigma)

# 1. First specify the Black-Scholes Function

BS.function.call <- function(sigma, options.df){

  d1 <- (log(S/X) + (r + sigma^2/2)*T) / (sigma*sqrt(T))
  d2 <- d1 - sigma*sqrt(T)
  st <- S * pnorm(d1) - X*exp(-r*T)*pnorm(d2)
  distance <- abs(mktprice - st)
  return(distance) # We want to set the distance between market price 
  and calculated price = 0 using nlm by changing sigma
  }

BS.function.put <- function(sigma, options.df){

  d1 <- (log(S/X) + (r + sigma^2/2)*T) / (sigma*sqrt(T))
  d2 <- d1 - sigma*sqrt(T)
  st <- -S * pnorm(-d1) + X*exp(-r*T)*pnorm(-d2)
  distance <- abs(mktprice - st)
  return(distance)
}

# 2. Create an initial guess for sigma

sigma.guess <- 0.2

# 3. Run the optimization function

for (i in 1:nrow(options.df)){
  if(type == 0){
    result[i] <- nlm(BS.function.call, sigma.guess, options.df)
  }
  else{
    result[i] <- nlm(BS.function.put, sigma.guess, options.df)
  }
}
anothermh
  • 9,815
  • 3
  • 33
  • 52
cbayntun
  • 11
  • 3
  • It's possible that some package has a `compute.implied.volatility` function. But asking us to track it down is disrespectful. Making a [MCVE] requires adding the `library` call and any needed data to your code that allows cutting in this to be pasted into an R console session. – IRTFM Oct 13 '18 at 04:14
  • Hi 42, thanks for your reply! I'm really sorry about not making my question perfect, in no way do I intend to be disrespectful to such an awesome community. I've now edited my post to contain the library calls, and a better explanation of my problem. I've also checked by cutting and pasting this into a new R session and it does replicate the same problem. Specifically to this question I am required to use `nlm` to solve this problem, otherwise I'd be happy to track down that implied volatility function. Hopefully this clears things up a little more? – cbayntun Oct 15 '18 at 00:24
  • The `fn` argument to nlm needs to ba function that gets data-x and a vector of starting values for parameters and the p argument to nlm needs to be starting values of that vector. Also you are not itterating properly through the options df since you are not indexing the rows. Look at the examples on the `nlm` help page. – IRTFM Oct 15 '18 at 00:51

1 Answers1

0

I know this post is old but it is unresolved so through I would provide some input. The package you are looking for is RND and it can be installed through the R console using :

install.packages("RND")

Also, be sure to load the package in your load library section as follows:

library(RND)
Frank Castle
  • 335
  • 3
  • 23