简体   繁体   中英

Why does changing the p-value with rgeom not give the expected result

So I'm trying to prove that when X ~ Geo(X) the expected value is equal to: E[X] = p / (1 - p)

p <- 0.50

#1 Exact
(p/(1 - p))

nrRuns <- 100000

#1 Simulation
x <- rep(0, nrRuns)
for (i in 1:nrRuns){
  x[i]=rgeom(n = 1, prob = p)
}
mean(x)

With p = 0.50 the Exact calculation gives 1 and the simulation outputs 1.00134 as expected, but when I change the p-value to 0.20 the exact calculation gives 0.25, but my simulation reports 3.99477. I would expect the simulation to report 0.25. So how is this possible?

This is an example of R's vectorized functions and of reproducibility.

f <- function(p, R){
  x <- rgeom(R, prob = p)
  c(Exact = (1 - p)/p, Sim = mean(x))
}
nrRuns <- 100000

set.seed(2020)    # make the results reproducible
f(p = 0.2, R = nrRuns)
#  Exact     Sim 
#4.00000 4.02563

Now the code in the question.

set.seed(2020)    # Reproduce the result above
p <- 0.2
x <- rep(0, nrRuns)
for (i in 1:nrRuns){
  x[i] <- rgeom(n = 1, prob = p)
}
mean(x)
#[1] 4.02563

The results are the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM