Debugging an R Implied-Volatility Function by Checking Name Masking
Summary
The document describes an R error encountered while attempting to compute implied volatility by solving for the volatility that matches an observed option price. The reported failure occurs when the root-finding interval is constructed, even though the code appears to call R’s standard `c` function with two endpoints. The accepted explanation is that an earlier command in the session defined a different function named `c` that accepts only one argument, masking the standard function.
The practical lesson is to consider the state of the programming session when an error appears inconsistent with the code shown. Starting a clean R session can reveal whether an object or function defined earlier is interfering with a built-in function. The document does not validate the option-pricing formula or the implied-volatility setup itself; it only diagnoses the specific argument error. It also does not provide a full corrected implementation or discuss numerical requirements such as choosing a valid volatility search interval. Its value is a focused debugging example about name masking in R.
Key ideas
- A function defined earlier in an R session can mask a standard function with the same name.
- An error at an interval-construction call may originate from the session environment rather than the displayed code.
- Running the example in a fresh session can help identify unintended name masking.
- The answer diagnoses the reported error but does not validate the option-pricing or implied-volatility calculations.
Tags
Full text
# Problem with implementing a implied volaitility function in R
# 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
## Answer by VDZ (score 3, accepted)
https://quant.stackexchange.com/a/61736
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.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.