简体   繁体   English

在 R 中使用 nlm 函数查找期权的隐含波动率

[英]Finding Implied Volatility of Option using nlm function in R

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.我知道这样做的简单方法是在 R 中使用compute.implied.volatility函数,但是这个问题需要我使用nlm函数来解决这个问题。 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.我在这个问题上遇到的麻烦是让nlm函数工作,因为我们在本课程中没有学到太多关于它的知识。

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.我知道我打算向nlm提供一个循环,使其能够迭代计算,直到找到产生结果的最小值。 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".我相信我没有提供与nlm一起使用的函数,因为我目前收到“ nlm优化器中的函数值无效”的错误。

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

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 :您正在寻找的软件包是 RND,它可以通过 R 控制台使用以下命令安装:

install.packages("RND")

Also, be sure to load the package in your load library section as follows:另外,请确保在您的加载库部分加载包,如下所示:

library(RND)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM