Saturday, February 14, 2015

R19. Geometric Distribution in R

The Geometric Distribution gives the chance of success at trial n. The success at the first trial is p.


The success at at n is (1-p)^(n-1)*p.


The formula is contrasted to dgeom, which should be the same.

# ex19.R
prob <- 0.2
N <- 12
p <- numeric(N)
d <- numeric(N)
for (k in 1:N) {
  p[k] <- (1-prob)^(k-1)*prob
  d[k] <- dgeom(k-1,prob)
}
names(p) <- 1:N
barplot(p, xlab = 'n', ylab = 'p(n)', main = 'First Success at n')
cat('Probability success after', N ,'trials is',
    (1-sum(p)),'\naccording to formula.\n')
cat('Probability success after', N ,'trials is',
    (1-sum(d)),'\naccording to dgeom')
# Probability success after 12 trials is 0.06871948
# according to formula.
# Probability success after 12 trials is 0.06871948
# according to dgeom

Output

No comments:

Post a Comment