How to calculate implied volatility in R

5 finance r quantitative-finance

I am trying to create my own function in R based on black scholes variables and solve "backwards" i suppose for sigma.

I have created a function to find the call price; however, now I have to find the sigma (implied volatility) estimates in R and then test my function to see if it works... I have tried different functions but I can not seem to figure out what I am doing wrong, part of me thinks I need to know sigma to find sigma but I am not sure if that even makes sense.

Here is the function I created for the price of a European call option in the Black Scholes model:

call <- function(s0, K, r, T, sigma) {
  d1 <- (log(s0/K) + (r + sigma^2/2)*T) / (sigma*sqrt(T))
  d2 <- d1 - sigma*sqrt(T
  c <- s0*pnorm(d1) - K*exp(-r*T)*pnorm(d2)
  c
}
Run Code Online (Sandbox Code Playgroud)

I tested my function to see if it works properly with different code which it does:

call(100, 70, 0.05, 1, 0.16)
[1] 33.43686

call(300, 280, 0.03, 3, 0.18)
[1] 60.81694

call(400, 350, 0.04, 5, 0.20)
[1] 133.1626
Run Code Online (Sandbox Code Playgroud)

Now I need to use the following function to find sigma:

    sigma <- function(call, s0, K, r, T) {

???
    }
Run Code Online (Sandbox Code Playgroud)

After creating the function I need to test it using the following:

sigma(33.43686, 100, 70, 0.05, 1)

sigma(60.81694, 300, 280, 0.03, 3)

sigma(133.1626, 400, 350, 0.04, 5)
Run Code Online (Sandbox Code Playgroud)

Same format but I cannot figure it out. I need to include a range or sequence to find sigma?

I have tried this but I don't believe we are supposed to include 'v' in the function

sigma <- function(call, s0, K, r, T, v) {
  d1 <- (log(s0/K) + (r + v^2/2)*T) / (v*sqrt(T))
  d2 <- d1 - v*sqrt(T)
  c <- s0*pnorm(d1) - K*exp(-r*T)*pnorm(d2)
  sigma_value <- d1 - d2 / sqrt(T)
  sigma_value
}

sigma(33.43686, 100, 70, 0.05, 1, 0.16) 
[1] 0.16
sigma(60.81694, 300, 280, 0.03, 3, 0.18)
[1] 0.4614232
sigma(133.1626, 400, 350, 0.04, 5, 0.20)
[1] 0.7358743
Run Code Online (Sandbox Code Playgroud)

0.16 is "sigma" I am trying to find an estimate close to this by creating my own function.

I also feel my sigmas are way off then they are supposed to be

I also tried :

sigma <- function(call, s0, K, r, T) {
  v = seq(from = 0.1, to = .2, by = .01)
  k.range = floor(seq(from = 100, to = 400, length.out = length(v)))
  for (i in 1:length(v)) {
    d1 <- (log(s0/K[i]) + (r + (v^2)/2) * T) / (v * sqrt(T))
    d2 <- d1 - v * sqrt(T)
    C  <- s0 * pnorm(d1) - K[i] * exp(-r*T) * pnorm(d2) - call[i]
  }
  v
}
Run Code Online (Sandbox Code Playgroud)

Any help on the function aspect would be great. Thank you

Cet*_*ttt 2

您可以使用该uniroot函数来查找隐含波动率。uniroot求函数的根。以下是如何使用它。

call_fun <- function(s0, K, r, TT, sig) {
  d1 <- (log(s0/K) + (r + sig^2/2)*TT) / (sig*sqrt(TT))
  d2 <- d1 - sig*sqrt(TT)
  s0*pnorm(d1) - K*exp(-r*TT)*pnorm(d2)

}

sig_impl <- function(s0, K, r, TT, .c) {
  root_fun <- function(sig){
    call_fun(s0, K, r, TT, sig) - .c
  }

  uniroot(root_fun, c(0, 1))$root
}
call_fun(100, 70, 0.05, 1, 0.16)
[1] 33.43686
sig_impl(100, 70, 0.05, 1, 33.43686)
[1] 0.1599868
Run Code Online (Sandbox Code Playgroud)

请注意,我更改了一些变量名称:T通常是保留的,TRUE因此您不应命名变量T。另外,还有一个名为 sigma 的函数sigma,所以最好不要将变量命名为 sigma。