The world of statistics is teeming with different probability distributions, each one possessing its own unique set of properties and applications. Among these, the geometric distribution holds a special place due to its relevance in describing events that embody a “first success” in a sequence of independent Bernoulli trials. It is a discrete probability distribution that finds applications across diverse fields such as reliability engineering, survival analysis, and queuing theory.

In the context of the R programming language, the geometric distribution is used extensively for modeling and simulating such “first success” scenarios. This article aims to delve deeper into the concept of the geometric distribution, its properties, and its applications in the R programming language.

## Understanding the Geometric Distribution

Before we jump into the code, let’s build a solid understanding of the geometric distribution. As mentioned earlier, it models the number of trials needed to get the first success in repeated independent Bernoulli trials.

A Bernoulli trial is an experiment that results in a success with probability ‘p’ and failure with probability ‘1-p’. These trials are assumed to be independent, i.e., the outcome of one trial does not influence the outcome of another.

The probability mass function (PMF) of the geometric distribution is given by:

P(X=x) = (1-p)^(x-1) * p

where:

- ‘X’ is a random variable denoting the number of trials until the first success.
- ‘p’ is the probability of success on each trial.
- ‘x’ is the number of trials. It can be any positive integer.

The mean (expected value) and variance of a geometrically distributed random variable are 1/p and (1-p)/p^2, respectively.

## Geometric Distribution in R

R provides in-built functions for working with the geometric distribution. There are four main functions associated with this distribution:

- dgeom(x, prob): This function calculates the PMF, i.e., the probability that the first success will happen on the x-th trial.
- pgeom(q, prob): This function calculates the cumulative distribution function (CDF), i.e., the probability that the first success will happen on or before the q-th trial.
- qgeom(p, prob): This is the quantile function, the inverse of the CDF. It returns the minimum number of trials needed to achieve the first success with at least ‘p’ probability.
- rgeom(n, prob): This function generates ‘n’ random numbers following the geometric distribution.

Let’s now see these functions in action.

### dgeom Function

The `dgeom`

function calculates the height of the probability density (mass) function at a given quantile `x`

. It is given by:

`dgeom(x, prob, log = FALSE)`

The `log`

argument, if set to `TRUE`

, returns the log-density. Let’s generate the PMF for a geometric distribution with a success probability of 0.2:

```
x <- 0:10
prob <- 0.2
dgeom_x <- dgeom(x, prob)
plot(x, dgeom_x, type = "h", main = "PMF of Geometric Distribution",
xlab = "Number of Trials", ylab = "Probability", las = 1, col = "blue")
```

### pgeom Function

The `pgeom`

function calculates the cumulative distribution function for the geometric distribution. It sums up the probabilities from 0 to the given quantile `q`

. The syntax is:

`pgeom(q, prob, lower.tail = TRUE, log.p = FALSE)`

The `lower.tail`

and `log.p`

arguments, if set to `TRUE`

, return the lower tail (default) and the log probability. Let’s plot the CDF for a geometric distribution with a success probability of 0.2:

```
x <- 0:10
prob <- 0.2
pgeom_x <- pgeom(x, prob)
plot(x, pgeom_x, type = "s", main = "CDF of Geometric Distribution",
xlab = "Number of Trials", ylab = "Cumulative Probability", las = 1, col = "blue")
```

### qgeom Function

The `qgeom`

function returns the quantile function, which is the inverse of the CDF. Given a probability `p`

, it returns the smallest value of `q`

such that `pgeom(q, prob) >= p`

. The syntax is:

`qgeom(p, prob, lower.tail = TRUE, log.p = FALSE)`

For instance, let’s find the 90th percentile of a geometric distribution with a success probability of 0.2:

```
prob <- 0.2
p <- 0.9
qgeom_90 <- qgeom(p, prob)
print(qgeom_90)
```

### rgeom Function

The `rgeom`

function generates random variates from a geometric distribution. The syntax is:

`rgeom(n, prob)`

For example, to generate a sample of 100 random variates from a geometric distribution with a success probability of 0.2, use:

```
n <- 100
prob <- 0.2
rgeom_sample <- rgeom(n, prob)
hist(rgeom_sample, main = "Random Variates from Geometric Distribution",
xlab = "Number of Trials", ylab = "Frequency", col = "blue")
```

## Conclusion

The geometric distribution is an essential component of many statistical models and experiments, and R provides a suite of functions to work with this distribution effectively. Whether you’re generating random variables, computing probabilities, or visualizing distributions, understanding how to use these functions can greatly enhance your data analysis capabilities. With the knowledge of geometric distribution functions in R, you can now tackle a wide variety of problems that involve waiting times and sequences of independent trials.