[R] Numerical precision problem
Duncan Murdoch
murdoch at stats.uwo.ca
Fri Dec 21 21:15:02 CET 2007
On 21/12/2007 2:52 PM, C.Rosa at lse.ac.uk wrote:
> Dear All,
>
>
>
> I have a numerical problem:
>
> R, as other statistical software, can tell the difference between very small numbers and 0, while apparently cannot distinguish a number close to 1 and 1. In the example below, is it possible to instruct R to recognize that q is different from 1?
No, the limitations are inherent in the floating point representation
that R uses.
>> p=.00000000000000000000000000000001
>
>> p==0
>
> [1] FALSE
>
>> q=.99999999999999999999999999999
>
>> q==1
>
> [1] TRUE
>
> Interestingly,
>
>> (1-p)==1
>
> [1] TRUE
>
> The main problem I have is that I need to evaluate the inverse of the normal Cumulative Distribution Function near 1 (but not exactly 1) and the PC cannot recognize it.
In this particular case, it's easy: make use of the symmetry of the
distribution. If you want the 1-epsilon quantile of a normal with mean
mu, find the epsilon quantile, and reflect it through mu:
mu <- 3
-qnorm(1e-30, mean=-mu)
More generally, you can make use of the log.p argument to the quantile
functions, and the fact that log(1-epsilon) is close to -epsilon:
qnorm(-1e-30, mean=mu, log.p=TRUE)
Duncan Murdoch
>
> Thank you very much for your help!
>
> Carlo
>
>
> Please access the attached hyperlink for an important electronic communications disclaimer: http://www.lse.ac.uk/collections/secretariat/legal/disclaimer.htm
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list