From m@ech|er @end|ng |rom @t@t@m@th@ethz@ch  Sat Apr  8 23:53:12 2023
From: m@ech|er @end|ng |rom @t@t@m@th@ethz@ch (Martin Maechler)
Date: Sat, 8 Apr 2023 23:53:12 +0200
Subject: [Rd] 
 Error message for infinite probability parameters in rbinom()
 and rmultinom()
In-Reply-To: <3D1EFD77-1BA5-46E3-B69A-EE754906FAFD@gmail.com>
References: <3D1EFD77-1BA5-46E3-B69A-EE754906FAFD@gmail.com>
Message-ID: <25649.57928.875627.313028@stat.math.ethz.ch>

>>>>> Christophe Dutang 
>>>>>     on Sat, 8 Apr 2023 14:21:53 +0200 writes:

    > Dear all,

    > Using rmultinom() in a stochastic model, I found this function returns an error message 'NA in probability' for an infinite probability. 

    > Maybe, a more precise message will be helpful when debugging. 

    >> rmultinom(1, 3:5, c(1/2, 1/3, Inf))
    > Error in rmultinom(1, 3:5, c(1/2, 1/3, Inf)) : NA in probability vector
    >> rmultinom(1, 3:5, c(1/2, 1/3, NA))
    > Error in rmultinom(1, 3:5, c(1/2, 1/3, NA)) : NA in probability vector

Thank you.

I agree the first ('Inf') should not do what it currently does,
and probably the 2nd one should neither give an error.


Note that in rmultinom,  the 'prob' is allowed to be *NOT*
scaled to sum(.) = 1.

Therefore 'Inf' makes sense as the limit (of a sequence) of (a)
very large number(s).

I claim that 

  rmultinom(1, 3, c(1/2, 1/3, Inf))

should give the same as 

  rmultinom(1, 3, c(1/2, 1/3, 1e300))

even without a warning,
and OTOH,  an NA in prob may return NA (and signal a warning)
instead of an error.


    > For rgeom() or rbinom(), we got a warning for infinite probability :

Yes, but there, prob must be in [0,1] ... so that's somewhat differnt.

    >> rbinom(1, 3, Inf)
    > [1] NA
    > Warning message:
    > In rbinom(1, 3, Inf) : NAs produced
    >> rbinom(1, 3, NA)
    > [1] NA
    > Warning message:
    > In rbinom(1, 3, NA) : NAs produced
    >> rgeom(1, Inf)
    > [1] NA
    > Warning message:
    > In rgeom(1, Inf) : NAs produced
    >> rgeom(1, NA)
    > [1] NA
    > Warning message:
    > In rgeom(1, NA) : NAs produced


    > Maybe, it could be better to harmonize the behavior for infinite probability.

    > Kind regards, Christophe


    >> sessionInfo()
    > R version 4.2.3 (2023-03-15)
    > Platform: aarch64-apple-darwin20 (64-bit)
    > Running under: macOS Ventura 13.2.1

    > Matrix products: default
    > BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
    > LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

    > locale:
    > [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

    > attached base packages:
    > [1] stats     graphics  grDevices utils     datasets  methods   base     

    > loaded via a namespace (and not attached):
    > [1] compiler_4.2.3 tools_4.2.3   

    > -------------------------------------------------
    > Christophe DUTANG
    > LJK, Ensimag, Grenoble INP, UGA, France
    > Web: http://dutangc.free.fr

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Error message for infinite probability parameters in rbinom() and rmultinom()
Martin Maechler