Exponential Distribution probabilities using R

Exponential Distribution probabilities using R

In this tutorial, you will learn about how to use dexp(), pexp(), qexp() and rexp() functions in R programming language to compute the individual probabilities, cumulative probabilities, quantiles and to generate random sample for Exponential distribution.

Before we discuss R functions for Exponential distribution, let us see what is Exponential distribution.

Exponential Distribution

Exponential distribution distribution is a continuous type probability distribution.

Exponential distribution is often used to model the lifetime of electric components. It is routinely used as a survival distribution in survival analysis and reliability analysis.

Let $X\sim Exp(\lambda)$. Then the probability distribution of $X$ is

$$ \begin{aligned} f(x)&= \begin{cases} \lambda e^{-\lambda x}, & x > 0;\lambda> 0; \\ 0, & Otherwise. \end{cases} \end{aligned} $$

where $\lambda$ is the scale parameter (also known as rate) of Exponential distribution.

Read more about the theory and results of Exponential distribution here.

Exponential probabilities using dexp() function in R

For continuous probability distribution, density is the value of the probability density function at $x$ (i.e., $f(x)$).

The syntax to compute the probability density function for Exponential distribution using R is

dexp(x,rate=1)

where

  • x : the value(s) of the variable and,
  • rate : rate parameter of exponential distribution.

Note: If you do not specify the rate, R assumes the default value rate=1 (which is a standard exponential distribution).

The dexp() function gives the density for given value(s) x and rate.

Numerical Problem for Exponential Distribution

To understand the four functions dexp(), pexp(), qexp() and rexp(), let us take the following numerical problem.

Exponential Distribution Example

The time (in hours) required to repair a machine is an exponential distributed random variable with paramter $\lambda=1/2$.

(a) Find the value of the density function at $x=2.5$.
(b) Plot the graph of Exponential probability distribution.
(c) Find the probability that a repair time takes at most 3 hours.
(d) Find the probability that a repair time exceeds 4 hours.
(e) Find the probability that a repair time takes between 2 to 4 hours.
(f) Plot the graph of cumulative Exponential probabilities.
(g) What is the value of $c$, if $P(X\leq c) \geq 0.50$?
(h) Simulate 1000 Exponential distributed random variables with $\lambda= 1/2$.

Let $X$ denote the time (in hours) required to repair a machine. Given that $X\sim Exp(\lambda=1/2)$.

Example 1: How to use dexp() function in R?

To find the value of the density function at $x=2.5$ we need to use dexp() function.

First let us define the given parameters as

# scale parameter
lambda <- 1/2

The probability density function of $X$ is

$$ \begin{aligned} f(x)&= \frac{1}{2} e^{-x/2},\\ &\quad\text{for } x \geq 0. \end{aligned} $$

For part (a), we need to find the density function at $x=2.5$. That is $f(2.5)$.

(a) The value of the density function at $x=2.5$ is

$$ \begin{aligned} f(2.5)&= \frac{1}{2}\times e^{-2.5/2}\\ &=2.5\times e^{-1.25}\\ &= 0.1432524 \end{aligned} $$

The above probability can be calculated using dexp(2.5,rate=0.5) function in R.

# Compute Exponential probability
result1 <- dexp(2.5,rate=lambda)
result1
[1] 0.1432524

Example 2 Visualize Exponential probability distribution

Using dexp() function we can compute Exponential distribution probabilities for given x and rate. To plot the probability density function of Exponential distribution, we need to create a sequence of x values and compute the corresponding probabilities.

# create a sequence of x values
x <- seq(0,5, by=0.02)
## Compute the Exponential pdf for each x
px<- dexp(x,rate=lambda)

(b) Visualizing Exponential Distribution with dexp() function and plot() function in R:

The probability density function of Exponential distribution with given 0.5 can be visualized using plot() function as follows:

## Plot the Exponential probability dist
plot(x,px,type="l",xlim=c(0,5),ylim=c(0,max(px)),
     lwd=3, col="darkred",ylab="f(x)")
title("PDF of Exponential (lambda = 1/2)")
PDF Exponential Dist
PDF Exponential Dist

Exponential cumulative probability using pexp() function in R

The syntax to compute the cumulative probability distribution function (CDF) for Exponential distribution using R is

pexp(q, rate=1)

where

  • q : the value(s) of the variable,
  • rate : scale parameter of exponential distribution.

Using this function one can calculate the cumulative distribution function of Exponential distribution for given value(s) of q (value of the variable x), rate.

Example 3: How to use pexp() function in R?

In the above example, for part (c), we need to find the probability $P(X\leq 3)$.

(c) The probability that a repair time takes at most 3 hours is

$$ \begin{aligned} P(X\leq 3) &=\int_0^{3} f(x)\; dx. \end{aligned} $$

## Compute cumulative Exponential probability
result2 <- pexp(3,rate=lambda)
result2
[1] 0.7768698

Example 4: How to use pexp() function in R?

In the above example, for part (d), we need to find the probability $P(X \geq 4)$.

To calculate the probability that a random variable $X$ is greater than a given number, one can use the option lower.tail=FALSE in pexp() function.

Above probability can be calculated easily using pexp() function with argument lower.tail=FALSE as

$P(X \geq 4) =\int_{4}^\infty f(x)\; dx$= pexp(4,rate=lambda,lower.tail=FALSE)

or by using complementary event as

$P(X \geq 4) = 1- P(X\leq 4)$= 1- pexp(4,rate=lambda)

# compute cumulative Exponential probabilities
# with lower.tail False
pexp(4,rate=lambda,lower.tail=FALSE)
[1] 0.1353353

(d) The probability that a repair time exceeds 4 hours is

$$ \begin{aligned} P(X\geq 4) &=\int_{4}^\infty f(x)\; dx\\ &=0.1353353. \end{aligned} $$

# Using complementary event
1-pexp(4,rate=lambda)
[1] 0.1353353

Example 5: How to use pexp() function in R?

One can also use pexp() function to calculate the probability that the random variable $X$ is between two values.

(e) The probability that a repair time takes between 2 to 4 hours can be written as $P(2 < X < 4)$.

$$ \begin{aligned} P(2 < X < 4) &= P(X< 4) -P(X < 2)\\ &= 0.8646647 - 0.6321206\\ &= 0.2325442 \end{aligned} $$

The above probability can be calculated using pexp() function as follows:

a <- pexp(4,rate=lambda)
b <- pexp(2,rate=lambda)
result3 <- a - b
result3
[1] 0.2325442

Example 6: Visualize the cumulative Exponential probability distribution

Using pexp() function we can compute Exponential cumulative probabilities (CDF) for given x and rate. To plot the CDF of Exponential distribution, we need to create a sequence of x values and compute the corresponding cumulative probabilities.

# create a sequence of x values
x <- seq(0,5, by=0.02)
## Compute the Exponential pdf for each x
Fx <- pexp(x,rate=lambda)

(f) Visualizing Exponential Distribution with pexp() function and plot() function in R:

The cumulative probability distribution of Exponential distribution with given x and rate can be visualized using plot() function as follows:

## Plot the Exponential  probability dist
plot(x,Fx,type="l",xlim=c(0,5),ylim=c(0,1),
     lwd=3, col="darkred",ylab="F(x)")
title("CDF of Exp (lambda= 1/2)")
CDF Exponential Dist
CDF Exponential Dist

Exponential Distribution Quantiles using qexp() in R

The syntax to compute the quantiles of Exponential distribution using R is

qexp(p,rate=1)

where

  • p : the value(s) of the probabilities,
  • rate =1 : scale parameter of exponential distribution.

The function qexp(p,rate=1) gives $100*p^{th}$ quantile of Exponential distribution for given value of p, and rate.

The $p^{th}$ quantile is the smallest value of Exponential random variable $X$ such that $P(X\leq x) \geq p$.

It is the inverse of pexp() function. That is, inverse cumulative probability distribution function for Exponential distribution.

Example 7: How to use qexp() function in R?

In part (g), we need to find the value of $c$ such a that $P(X\leq c) \geq 0.50$. That is we need to find the $50^{th}$ quantile of given Exponential distribution.

lambda <- 1/2
prob <- 0.50
# compute the quantile for Exponential  dist
qexp(0.50,rate=lambda)
[1] 1.386294

The $50^{th}$ percentile of given Exponential distribution is 1.3862944.

Visualize the quantiles of exponential Distribution

The quantiles of exponential distribution with given p and rate=lambda can be visualized using plot() function as follows:

p <- seq(0,1,by=0.02)
qx <- qexp(p,rate=lambda)
# Plot the Quantiles of Exponential  dist
plot(p,qx,type="l",lwd=2,col="darkred",
     ylab="quantiles",
main="Quantiles of Exponential(lambda=1/2)")
Quantiles Exponential Dist
Quantiles Exponential Dist

Simulating Exponential random variable using rexp() function in R

The general R function to generate random numbers from Exponential distribution is

rexp(n,rate=1)

where,

  • n : the sample observations,
  • rate : scale parameter of exponential distribution.

The function rexp(n,rate) generates n random numbers from Exponential distribution with given rate.

Example 8: How to use rexp() function in R?

In part (h), we need to generate 1000 random numbers from Exponential distribution with given $rate = 0.5$.

(h) We can use rexp(1000,rate) function to generate random numbers from Exponential distribution.

## initialize sample size to generate
n <- 1000
# Simulate 1000 values From Exponential  dist
x_sim <- rexp(n,rate=lambda)

The below graphs shows the density of the simulated random variables from Exponential Distribution.

## Plot the simulated data
plot(density(x_sim),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Exponential(lambda=1/2) dist")
Random Sample Exponential Dist
Random Sample Exponential Dist

If you use same function again, R will generate another set of random numbers from $Exp(0.5)$.

# Simulate 1000 values From Exponential  dist
x_sim_2 <- rexp(n,rate=lambda)
## Plot the simulated data
plot(density(x_sim_2),xlab="Simulated x",ylab="density",
     lwd=5,col="blue",
     main="Simulated data from Exp(lambda=1/2) dist")
Random Sample Exponential Dist 2
Random Sample Exponential Dist 2

For the simulation purpose to reproduce same set of random numbers, one can use set.seed() function.

# set seed for reproducibility
set.seed(1457)
# Simulate 1000 values From Exponential  dist
x_sim_3 <- rexp(n,rate=lambda)
## Plot the simulated data
plot(density(x_sim_3),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Exp(lambda=1/2) dist")
Random Sample Exponential Dist 3
Random Sample Exponential Dist 3
set.seed(1457)
# Simulate 1000 values From Exponential  dist
x_sim_4 <- rexp(n,rate=lambda)
## Plot the simulated data
plot(density(x_sim_4),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Exp(lambda=1/2) dist")
Random Sample Exponential Dist 3
Random Sample Exponential Dist 3

Since we have used set.seed(1457) function, R will generate the same set of Exponential distributed random numbers.

hist(x_sim_4,breaks = 30)
Histogram Exponential Dist
Histogram Exponential Dist

To learn more about other discrete and continuous probability distributions using R, go through the following tutorials:

Discrete Distributions Using R

Binomial distribution in R
Poisson distribution in R
Geometric distribution in R
Negative Binomial distribution in R
Hypergeometric distribution in R

Continuous Distributions Using R

Uniform distribution in R
Normal distribution in R
Log-Normal distribution in R
Beta distribution in R
Gamma distribution in R
Cauchy distribution in R
Laplace distribution in R
Logistic distribution in R
Weibull distribution in R

Endnote

In this tutorial, you learned about how to compute the probabilities, cumulative probabilities and quantiles of Exponential distribution in R programming. You also learned about how to simulate a Exponential distribution using R programming.

To learn more about R code for discrete and continuous probability distributions, please refer to the following tutorials:

Probability Distributions using R

Let me know in the comments below, if you have any questions on Exponential Distribution using R and your thought on this article.

VRCBuzz co-founder and passionate about making every day the greatest day of life. Raju is nerd at heart with a background in Statistics. Raju looks after overseeing day to day operations as well as focusing on strategic planning and growth of VRCBuzz products and services. Raju has more than 25 years of experience in Teaching fields. He gain energy by helping people to reach their goal and motivate to align to their passion. Raju holds a Ph.D. degree in Statistics. Raju loves to spend his leisure time on reading and implementing AI and machine learning concepts using statistical models.

Leave a Comment