Here, we discuss geometric distribution functions in R, plots, parameter setting, random sampling, mass function, cumulative distribution and quantiles.

The geometric distribution for the number of failures before the first success in a sequence of independent and identically distributed Bernoulli trials, with parameter \(\tt{probability\:of\:success}=p\) has probability mass function (pmf) formula as:

\[P(X=x)= p(1-p)^x,\] for \(x \in \{0, 1, 2, \ldots\}\) number of failures before the first success,

where \(p \in (0,1]\) is the probability of success in each trial.

The mean is \(\frac{1-p}{p}\), and the variance is \(\frac{1-p}{p^2}\).

See also probability distributions and plots and charts.

1 Table of Geometric Distribution Functions in R

The table below shows the functions for geometric distributions in R.

Table of Geometric Distribution Functions in R
Function Usage
rgeom(n, prob) Simulate a random sample with \(n\) observations
dgeom(x, prob) Calculate the probability mass at the point \(x\)
pgeom(q, prob) Calculate the cumulative distribution at the point \(q\)
qgeom(p, prob) Calculate the quantile value associated with \(p\)

2 Plot of Geometric Distributions in R

Single distribution:

Below is a plot of the geometric distribution function with \(\tt{probability\:of\:success}=0.2\).

x = 0:25; y = dgeom(x, 0.2)
plot(x, y, type = "h",
     xlim = c(0, 25),  ylim = c(0, max(y)),
     main = "Probability Mass Function of
Geometric Distribution (0.2)",
     xlab = "x", ylab = "Mass",
     col = "blue")
# Add legend
legend("topright", "prob = 0.2",
       fill = "blue",
       bty = "n")
Probability Mass Function (PMF) of a Geometric Distribution in R

Probability Mass Function (PMF) of a Geometric Distribution in R

Multiple distributions:

Below is a plot of multiple geometric distribution functions in one graph.

x1 = 0:10; y1 = dgeom(x1, 0.7)
x2 = 0:12; y2 = dgeom(x2, 0.5)
x3 = 0:15; y3 = dgeom(x3, 0.2)
plot(x1, y1,
     xlim = c(0, 15), ylim = range(c(y1, y2, y3)),
     main = "Probability Mass Functions of
Geometric Distributions",
     xlab = "x", ylab = "Mass",
     col = "blue")
points(x2, y2, col = "red")
points(x3, y3, col = "green")
# Add legend
legend("topright", c("prob = 0.7",
                    "prob = 0.5",
                    "prob = 0.2"),
       fill = c("blue", "red", "green"),
       bty = "n")
# Add lines
for(i in 1:10){
  segments(x1[i], dgeom(x1[i], 0.7),
           x1[i+1], dgeom(x1[i+1], 0.7),
           col = "blue")}
for(i in 1:12){
  segments(x2[i], dgeom(x2[i], 0.5),
           x2[i+1], dgeom(x2[i+1], 0.5),
           col = "red")}
for(i in 1:15){
  segments(x3[i], dgeom(x3[i], 0.2),
           x3[i+1], dgeom(x3[i+1], 0.2),
           col = "green")}
Probability Mass Functions (PMFs) of Geometric Distributions in R

Probability Mass Functions (PMFs) of Geometric Distributions in R

3 Examples for Setting Parameters for Geometric Distributions in R

To set the parameters for the geometric distribution function, with \(\tt{probability\:of\:success}=0.7\).

For example, for dgeom(), the following are the same:

dgeom(1, 0.7)
[1] 0.21
dgeom(1, prob = 0.7)
[1] 0.21

4 rgeom(): Random Sampling from Geometric Distributions in R

Sample 700 observations from the geometric distribution with \(\tt{probability\:of\:success}=0.3\):

rgeom(700, 0.3)
set.seed(10) # Line allows replication (use any number).
sample = rgeom(700, 0.3)
hist(sample,
     main = "Histogram of 700 Observations from
Geometric Distribution with Prob = 0.3",
     xlab = "x",
     col = "lightblue", border = "white")
Histogram of Geometric Distribution (0.3) Random Sample in R

Histogram of Geometric Distribution (0.3) Random Sample in R

5 dgeom(): Probability Mass Function for Geometric Distributions in R

Calculate the mass at \(x = 3\), in the geometric distribution with \(\tt{probability\:of\:success}=0.25\):

dgeom(3, 0.25)
[1] 0.1054688
x = 0:20; y = dgeom(x, 0.25)
plot(x, y,
     xlim = c(0, 20),  ylim = c(0, max(y)),
     main = "Probability Mass Function of
Geometric Distribution with Prob = 0.25",
     xlab = "x", ylab = "Mass",
     col = "blue")
# Add lines
segments(3, -1, 3, 0.1054688)
segments(-1, 0.1054688, 3, 0.1054688)
Probability Mass Function (PMF) of Geometric Distribution (0.25) in R

Probability Mass Function (PMF) of Geometric Distribution (0.25) in R

6 pgeom(): Cumulative Distribution Function for Geometric Distributions in R

Calculate the cumulative distribution at \(x = 3\), in the geometric distribution with \(\tt{probability\:of\:success}=0.4\). That is, \(P(X \le 3)\):

pgeom(3, 0.4)
[1] 0.8704
x = 0:12; y = pgeom(x, 0.4)
plot(x, y,
     xlim = c(0, 12), ylim = c(0, 1),
     main = "Cumulative Distribution Function of
Geometric Distribution with Prob = 0.4",
     xlab = "x", ylab = "Cumulative Distribution",
     col = "blue")
# Add lines
for(i in 1:12){
  segments(x[i], pgeom(x[i], 0.4),
           x[i] + 1, pgeom(x[i], 0.4),
           col = "blue")
}
segments(3, -1, 3, 0.8704)
segments(-1, 0.8704, 3, 0.8704)
Cumulative Distribution Function (CDF) of Geometric Distribution (0.4) in R

Cumulative Distribution Function (CDF) of Geometric Distribution (0.4) in R

For upper tail, at \(x = 3\), that is, \(P(X > 3) = 1 - P(X \le 3)\), set the "lower.tail" argument:

pgeom(3, 0.4, lower.tail = FALSE)
[1] 0.1296

7 qgeom(): Derive Quantile for Geometric Distributions in R

Derive the quantile for \(p = 0.85\), in the geometric distribution with \(\tt{probability\:of\:success}=0.5\). That is, the \(\tt{smallest}\) \(x\) such that, \(P(X\le x) \ge 0.85\):

qgeom(0.85, 0.5)
[1] 2
x = 0:8; y = pgeom(x, 0.5)
plot(x, y,
     xlim = c(0, 8), ylim = c(0, 1),
     main = "Cumulative Distribution Function of
Geometric Distribution with Size = 7 and Prob = 0.5",
     xlab = "x", ylab = "Cumulative Distribution",
     col = "blue")
# Add lines
for(i in 1:8){
  segments(x[i], pgeom(x[i], 0.5),
           x[i] + 1, pgeom(x[i], 0.5),
           col = "blue")
}
segments(2, -1, 2, 0.85)
segments(-1, 0.85, 2, 0.85)
Cumulative Distribution Function (CDF) of Geometric Distribution (0.5) in R

Cumulative Distribution Function (CDF) of Geometric Distribution (0.5) in R

For upper tail, for \(p = 0.15\), that is, the \(\tt{smallest}\) \(x\) such that, \(P(X > x) < 0.15\):

Note: \(P(X > x) = 1-P(X \le x)\).

qgeom(0.15, 0.5, lower.tail = FALSE)
[1] 2

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes