0
$\begingroup$

I am new to programming, and I have been exposed to the basic of R and Python. I have been trying to implement the volatility smile function using a unit root function(a traditional procedure) but I am running into problems. Here is the code I am writing:

BlackScholesFormulaBach  <- function (spot,timetomat,strike,r, q=0, sigma)
{ 
  
  d1<-(spot-strike)/(sigma*sqrt(timetomat))
  d2<-(spot-strike)/(sigma*sqrt(timetomat))
  
  
  result<-(spot-strike)*pnorm(d1)+sigma*sqrt(timetomat)*dnorm(d1)
  
  
  BlackScholesFormulaBach<-result
  
}

BlackScholesImpVol  <- function (obsprice,spot,timetomat,strike,r, q=0)
  
{ difference<- function(sigBS, obsprice,spot,timetomat,strike,r,q)
{BlackScholesFormulaBach(spot,timetomat,strike,r,q,sigBS)-obsprice
}

uniroot(difference, c(-1,1),obsprice=obsprice,spot=spot,timetomat=timetomat,strike=strike,r=r,q=q)$root

}

S_0<-100
cap_T<-1
sigma_1<-15
N<-1000

BlackScholesImpVol(S_0,cap_T,1,0,0,15)

Every time I run the code I get:

Error in c(-1, 1) : unused argument (1)

If I change the interval I get the same error. I have been checking the code but it seems right. I have consulted other people and none finds the problem. According to R the problem is on the unitroot function, but I fail to see where.

Question:

Can someone help me solve this implementation problem?

Thanks in Advnace

$\endgroup$
0
3
$\begingroup$

The problem is not in the code you posted, but somewhere before: you have apparently defined a function called c, with only one argument.

Try to run the code in a new session.

$\endgroup$
5
  • 1
    $\begingroup$ Or try rm(c). $\endgroup$ – Bob Jansen Mar 15 at 5:38
  • 1
    $\begingroup$ You should be crystal clear removing any default function like c. Given the widespread usage of this function removing it with rm is really bad idea. Why not use some other name? $\endgroup$ – Brian Smith Mar 15 at 9:39
  • $\begingroup$ The function "c" is the list function, in order to build a vector. How should I have defined the function? Is that what you are referring to? $\endgroup$ – Pedro Gomes Mar 15 at 9:53
  • 1
    $\begingroup$ As pointed out by VDZ, you have c defined somewhere yourself, probably as a one argument function. It's not possible to delete the builtin c, in a fresh session rm(c) yields Warning message: In rm("c") : object 'c' not found. If you overwrite c it does work, for example: c <- list; rm(c) doesn't give a warning. $\endgroup$ – Bob Jansen Mar 15 at 10:05
  • $\begingroup$ @BobJansen Thanks for your comment. That was indeed the problem. I have just used "rm" and it is working now. $\endgroup$ – Pedro Gomes Mar 15 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.