# [R] uniroot() problem

Mon Feb 9 15:31:25 CET 2009

```Megh,

The problem is due to jump discontinuity in your function at x=0.  It is
always good practice to plot the function over the range of interest.

x <- seq(-20, 20, by=0.01)

plot(x, th.price(x), type="l")

This will reveal the problem.  The function value jumps from -384.4 to 36.29
at x=0.

If the singularity at x=0 is not an essential one, you may be able to
anayticallty remove this singularity.

Ravi.

----------------------------------------------------------------------------
-------

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

----------------------------------------------------------------------------
--------

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of megh
Sent: Monday, February 09, 2009 4:27 AM
To: r-help at r-project.org
Subject: Re: [R] uniroot() problem

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
 6.331672e-13

\$f.root
 36.28816

\$iter
 55

\$estim.prec
 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
>  49.99997
>
> \$f.root
>  -1.640646e+39
>
> \$iter
>  4
>
> \$estim.prec
>  6.103516e-05
>
>> .Machine\$double.eps^0.25/2
>  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
>  50
>
> \$f.root
>  1.337393e+31
>
> \$iter
>  4
>
> \$estim.prec
>  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
>>  4.216521e-05
>>
>> \$f.root
>>  16.66423
>>
>> \$iter
>>  27
>>
>> \$estim.prec
>>  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-tp21227702p21909423.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