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.
The table below shows the functions for geometric distributions 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\) |
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")
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")}
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:
[1] 0.21
[1] 0.21
Sample 700 observations from the geometric distribution with \(\tt{probability\:of\:success}=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")
Calculate the mass at \(x = 3\), in the geometric distribution with \(\tt{probability\:of\:success}=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)
Calculate the cumulative distribution at \(x = 3\), in the geometric distribution with \(\tt{probability\:of\:success}=0.4\). That is, \(P(X \le 3)\):
[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)
For upper tail, at \(x = 3\), that is, \(P(X > 3) = 1 - P(X \le 3)\), set the "lower.tail" argument:
[1] 0.1296
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\):
[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)
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)\).
[1] 2
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes