NaN in R distribution functions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

NaN in R distribution functions

Gregor Gorjanc-2
Dear R developers,

I noticed that core R distribution functions return NaN, when parameter
values are out of parameter space. I have looked in source code and
found that warnings and return of NaN are done internally in C code. For
dgamma.c the line 49 is:

     if (shape <= 0 || scale <= 0) ML_ERR_return_NAN;

OK. How should this be implemented if distribution functions are written
directly in R? I came up with this

     if (any(shape <= 0)) {
         warning("shape must be positive")
         return(NaN)
     }

I think that it would be nice that returning NaN for undefined parameter
values in distribution functions would also be documented in Writing R
Extension.

--
Lep pozdrav / With regards,
     Gregor Gorjanc

----------------------------------------------------------------------
University of Ljubljana     PhD student
Biotechnical Faculty
Zootechnical Department     URI: http://www.bfro.uni-lj.si/MR/ggorjan
Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si

SI-1230 Domzale             tel: +386 (0)1 72 17 861
Slovenia, Europe            fax: +386 (0)1 72 17 888

----------------------------------------------------------------------
"One must learn by doing the thing; for though you think you know it,
  you have no certainty until you try." Sophocles ~ 450 B.C.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: NaN in R distribution functions

Prof Brian Ripley
On Wed, 28 Dec 2005, Gregor Gorjanc wrote:

> Dear R developers,
>
> I noticed that core R distribution functions return NaN, when parameter
> values are out of parameter space. I have looked in source code and
> found that warnings and return of NaN are done internally in C code. For
> dgamma.c the line 49 is:
>
>     if (shape <= 0 || scale <= 0) ML_ERR_return_NAN;
>
> OK. How should this be implemented if distribution functions are written
> directly in R? I came up with this
>
>     if (any(shape <= 0)) {
>         warning("shape must be positive")
>         return(NaN)
>     }

As the R-level code is vectorized, NaN is unlikely to be the appropriate
return value. You will find that only the relevant entries are NaN, for
example

> dgamma(1, shape=-1:2)
[1]       NaN       NaN 0.3678794 0.3678794
Warning message:
NaNs produced in: dgamma(x, shape, scale, log)

> I think that it would be nice that returning NaN for undefined parameter
> values in distribution functions would also be documented in Writing R
> Extension.

It is purely a convention for use in the standalone libRmath with which
the code is shared.  Otherwise returning NA or giving an error would seem
more appropriate.

`Writing R Extensions' does not cover writing distribution functions, and
is certainly not intended to mandate how such extensions are written.

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: NaN in R distribution functions

Gregor Gorjanc-2
Prof Brian Ripley wrote:

> On Wed, 28 Dec 2005, Gregor Gorjanc wrote:
>
>> Dear R developers,
>>
>> I noticed that core R distribution functions return NaN, when parameter
>> values are out of parameter space. I have looked in source code and
>> found that warnings and return of NaN are done internally in C code. For
>> dgamma.c the line 49 is:
>>
>>     if (shape <= 0 || scale <= 0) ML_ERR_return_NAN;
>>
>> OK. How should this be implemented if distribution functions are written
>> directly in R? I came up with this
>>
>>     if (any(shape <= 0)) {
>>         warning("shape must be positive")
>>         return(NaN)
>>     }
>
>
> As the R-level code is vectorized, NaN is unlikely to be the appropriate
> return value. You will find that only the relevant entries are NaN, for
> example
>
>> dgamma(1, shape=-1:2)
>
> [1]       NaN       NaN 0.3678794 0.3678794
> Warning message:
> NaNs produced in: dgamma(x, shape, scale, log)
>

Yes, you are right.

>> I think that it would be nice that returning NaN for undefined parameter
>> values in distribution functions would also be documented in Writing R
>> Extension.
>
> It is purely a convention for use in the standalone libRmath with which
> the code is shared.  Otherwise returning NA or giving an error would
> seem more appropriate.
>
> `Writing R Extensions' does not cover writing distribution functions,
> and is certainly not intended to mandate how such extensions are written.

I understand, however it would be really nice to have some guideline so
that developers would write more or less similar code. I had a problem
with one such functions, since it stopped if parameters values were out
of parameter space. I used that function in optim() and it was
annoyingly stopping the optimization. It was not much to change the
function to return NaN (this could be as well NA as you have pointed
out), but it would be nice that this would be the default.

--
Lep pozdrav / With regards,
     Gregor Gorjanc

----------------------------------------------------------------------
University of Ljubljana     PhD student
Biotechnical Faculty
Zootechnical Department     URI: http://www.bfro.uni-lj.si/MR/ggorjan
Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si

SI-1230 Domzale             tel: +386 (0)1 72 17 861
Slovenia, Europe            fax: +386 (0)1 72 17 888

----------------------------------------------------------------------
"One must learn by doing the thing; for though you think you know it,
  you have no certainty until you try." Sophocles ~ 450 B.C.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel