Finding Implied Volatility of Option using nlm function in R

1.2k Views Asked by At

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)
  }
}
1

There are 1 best solutions below

0
Frank Castle On

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)