# Problem with implementing a implied volaitility function in R

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

## 1 Answer

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.

• Or try rm(c). – Bob Jansen Mar 15 at 5:38
• 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? – Brian Smith Mar 15 at 9:39
• 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? – Pedro Gomes Mar 15 at 9:53
• 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. – Bob Jansen Mar 15 at 10:05
• @BobJansen Thanks for your comment. That was indeed the problem. I have just used "rm" and it is working now. – Pedro Gomes Mar 15 at 10:42