[R] [Tagged] Re: Fwd: r-stats: Geometric Distribution

Ben Bolker bbo|ker @end|ng |rom gm@||@com
Thu Oct 19 19:52:37 CEST 2023


   Jeff: I think you might be misdiagnosing the OP's problem; I'm not 
sure that the R parameterization is primarily intended to avoid FP 
problems, but rather is *one* possible sensible choice of definition. 
Note that the Wikipedia page on the geometric distribution: 
https://en.wikipedia.org/wiki/Geometric_distribution (obviously not an 
authoritative reference, but Wikipedia is generally very good for these 
things) gives *both* the starting-from-zero and starting-from-1 definitions.

   Sahil: this is not an error in R, it's simply a different (and 
equally sensible) choice of definition from yours. You can certainly 
write your code to do `dgeom(x-1, p)`: in fact, you could write a wrapper

my_dgeom <- function(x, ...) dgeom(x-1, ...)

so that you never have to think about it again ...

   cheers
     Ben Bolker


On 2023-10-19 3:20 a.m., Jeff Newmiller via R-help wrote:
> What makes sense in a math class is not necessarily the same as what makes sense in a floating point analysis environment.
> 
> You lose a lot of significant digits when you add 1 to a floating point number that is close to zero, and this implementation allows the user to avoid that structural deficiency inherent in your preferred formulation.
> 
> This is a case where you need to read the documentation and adapt your handling of numbers to get the most accurate results.
> 
> Or write your own version that destroys significant digits.
> 
> There are other functions that allow for similar maintenance of significant digits... like log1p and expm1. See i.e. https://en.m.wikipedia.org/wiki/Natural_logarithm#lnp1 for discussion.
> 
> On October 18, 2023 10:44:21 PM PDT, Sahil Sharma <sahilsharmahimalaya using gmail.com> wrote:
>> Hi, today I came across the same problem. And, I'm able to explain it with
>> an example as well.
>>
>> Suppose I want to PDF or P(X=5) in Geometric Distribution with P = 0.2.
>>
>> The theoretical formula is P * (1-P) ^ (x -1). But the R function dgeom(x,
>> p) works like P * (1-P) ^ x, it does not reduce 1 from x because in r the x
>> starts from 0. In that case, if I am writing x as 5 then in-principle it
>> should work like x = 4 because starting from zero, 4 is the 5th place of x.
>> E.g., 0,1,2,3,4 there are five digits.
>>
>> However, the x in dgeom(x,p) is exactly working like 5.
>>
>> Here are some codes that I used:
>>
>>> dgeom(5, 0.2)
>> [1] 0.065536
>>
>> If I use the formula manually, i.e., p(1-P)^x-1, I get this.
>>
>>> 0.2 * (1-0.2)^(5-1)
>> [1] 0.08192
>>
>> Even if x starts from 0 in r, that's why we do not minus 1 from x, it
>> should work like 4 when I'm writing 5, but not, it is working exactly 5.
>> For example, if I manually put the 5 at the place of X, I get same results
>> as dgeom(x,p).
>>
>>> 0.2 * (1-0.2)^(5)
>> [1] 0.065536
>>
>>
>>
>> I guess there is a need for solution to this problem otherwise, it may
>> result in erroneous calculations. Either the function dgeom(x,p) can
>> perform and result as per the theoretical definition of PDF in Geometric
>> Distribution, or the user applying this function must be prompted about the
>> nature of this function so that the user manually minus one from x and then
>> enter it into the function dgeom(x,p).
>>
>> Thanks, and Regards
>> Sahil
>>
>>
>>
>>
>>
>> On Tue, Oct 17, 2023 at 6:39 PM Ivan Krylov <krylov.r00t using gmail.com> wrote:
>>
>>> В Tue, 17 Oct 2023 12:12:05 +0530
>>> Sahil Sharma <sahilsharmahimalaya using gmail.com> пишет:
>>>
>>>> The original formula for Geometric Distribution PDF is
>>>> *((1-p)^x-1)*P*. However, the current r function *dgeom(x, p)* is
>>>> doing this: *((1-p)^x)*P, *it is not reducing 1 from x.
>>>
>>> Your definition is valid for integer 'x' starting from 1. ('x'th trial
>>> is the first success.)
>>>
>>> The definition in help(dgeom):
>>>
>>>>> p(x) = p (1-p)^x
>>>>> for x = 0, 1, 2, ..., 0 < p <= 1.
>>>
>>> ...is valid for integer x starting from 0. ('x' failures until the
>>> first success.)
>>>
>>> They are equivalent, but they use the name 'x' for two subtly different
>>> things.
>>>
>>> Thank you for giving attention to this and best of luck in your future
>>> research!
>>>
>>> --
>>> Best regards,
>>> Ivan
>>>
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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