I'm having trouble coming up with an algorithm that generates a sample (X1,...,Xn) of size n, considering several values for n, where the random variable Xi – “number of trials until the first success ” follows a geometric distribution:
f (x) = 0.7 exp(x-1) 0.3 , x =1, 2,L
i'm trying to implement this in R (without using rgeom) via the inverse transform method.
Can you help me?
I think this should do the trick. I used the sample function to draw 0 or 1 with probability $p$ at each trial.
This then corresponds to the pmf given by $$ p(x)=(1-p)^{x-1}p, \quad x=1, 2,\dots $$
Using the inverse transform method, the following seems to work: