Find Mean of a geometric distribution using R

574 Views Asked by At

I am currently struggling to find a way to calculate the mean of the geometric function (or any other function for that matter using R. So basically I want R to calculate for me

$\frac{1-p}{p}$

for the geometric distribution. I feel like it is extremely obvious and I just don't get it. I would very much appreciate if someone could point it out in this case.

Sample question: How often do you have to draw a card from a standard deck of cards until you win (by drawing the ace of spades). Where the mean would be

$\frac{1-\frac{1}{52}}{\frac{1}{52}}$ = 51

How do I get this result in R?

2

There are 2 best solutions below

2
On

If you know a formula for the expected value in terms of the parameters, R will happily accept the formula you type in. :)

If not, R provides the density functions and cdfs for most distributions you might want to use, and also has facilities (e.g., the package cubature) for numerical integration. Using those together will solve your problem. (For discrete distributions, rather than using cubature you probably just need good judgment as to how many terms of an otherwise infinite sum are enough.)

It also turns out that the package actuar provides moment-generating functions for a number of distributions. That may be what you are looking for, if it handles the distributions you care about. See https://cran.r-project.org/web/views/Distributions.html for a complete discussion of what is available in various packages.

Also, if the mgf is not available, easier than using cubature (though perhaps slower and less accurate) would be to generate several thousand random variates of whatever distribution you care about and taking their mean. Most facilities in R that deal with distributions provide a function that generates random variates. And even if random variates are not available, you just need the quantile function to use this method, since you can always generate uniform random numbers and apply the quantile function to each and then average.

0
On

If $\mu$ denotes the number of failures that preceed the first success then we have the equality:$$\mu=p\cdot0+(1-p)\cdot(1+\mu)=(1-p)(1+\mu)$$which lead easily to: $$\mu=\frac{1-p}{p}$$

If the first trial is a success already (probability on that is $p$) then we have $0$ failures. If the first trial is not a success (probability on that is $1-p$) then the process starts again, but we have $1$ failure in our pocket (explaining the factor $1+\mu$).

If $\nu$ denotes the number of trials needed to arrive at the first success then: $$\nu=1+\mu=1+\frac{1-p}{p}=\frac1p$$

Also $\nu$ can be found directly on base of equality:$$\nu=p\cdot1+(1-p)\cdot(1+\nu)$$