# [R] uniroot() problem

megh megh700004 at yahoo.com
Mon Feb 9 10:28:39 CET 2009

```Sorry, in my previous post I forgot to include strike price, which is K =
1160

megh wrote:
>
> Thanks for this reply. Here I was trying to calculate implied volatility
> using BS formula. This is my code :
>
> oo = 384.40 # traded option price
>     uu = 1563.25 # underlying price
>     tt = 0.656 # time to maturity in year
>     ii = 2.309/100 # interest rate, annualized
>     th.price = function(x)
>        {
>         d1 = (log(uu/K) + (ii + x^2/2)*tt) / (x*sqrt(tt)); d2 = d1 -
> x*sqrt(tt)
>         option.price = uu * pnorm(d1) - K * exp(-ii*tt) * pnorm(d2)
>         return(option.price - oo)
>        }
>
> uniroot(th.price, c(-20, 20), tol=1/10^12)
>
> I got following result :
>
>> uniroot(th.price, c(-20, 20), tol=1/10^12)
> \$root
> [1] 6.331672e-13
>
> \$f.root
> [1] 36.28816
>
> \$iter
> [1] 55
>
> \$estim.prec
> [1] 7.385592e-13
>
> Hence using implied volatility, difference between traded price and
> theoretical price is coming as high as 36.28816, even I increse the
> precision level. Any idea how to crack this problem?
>
>
>
> Albyn Jones wrote:
>>
>> One can't tell for sure without seeing the function, but I'd guess
>> that you have   a numerical issue.  Here is an example to reflect upon:
>>
>>> f=function(x) (exp(x)-exp(50))*(exp(x)+exp(50))
>>> uniroot(f,c(0,100))
>> \$root
>> [1] 49.99997
>>
>> \$f.root
>> [1] -1.640646e+39
>>
>> \$iter
>> [1] 4
>>
>> \$estim.prec
>> [1] 6.103516e-05
>>
>>> .Machine\$double.eps^0.25/2
>> [1] 6.103516e-05
>>
>> uniroot thinks it has converged, at least in relative terms.  Note
>> that the estimated precision is related to the machine epsilon, used
>> in the default value for "tol".  try fiddling with the tol argument.
>>
>>> uniroot(f,c(0,100),tol=1/10^12)
>> \$root
>> [1] 50
>>
>> \$f.root
>> [1] 1.337393e+31
>>
>> \$iter
>> [1] 4
>>
>> \$estim.prec
>> [1] 5.186962e-13
>>
>> albyn
>>
>>
>> Quoting megh <megh700004 at yahoo.com>:
>>
>>>
>>> I have a strange problem with uniroot() function. Here is the result :
>>>
>>>> uniroot(th, c(-20, 20))
>>> \$root
>>> [1] 4.216521e-05
>>>
>>> \$f.root
>>> [1] 16.66423
>>>
>>> \$iter
>>> [1] 27
>>>
>>> \$estim.prec
>>> [1] 6.103516e-05
>>>
>>> Pls forgive for not reproducing whole code, here my question is how
>>> "f.root"
>>> can be 16.66423? As it is finding root of a function, it must be near
>>> Zero.
>>> Am I missing something?
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/uniroot%28%29-problem-tp21227702p21227702.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>

--
View this message in context: http://www.nabble.com/uniroot%28%29-problem-tp21227702p21909458.html
Sent from the R help mailing list archive at Nabble.com.

```