Exponential Distribution in R Programming

In R programming, the exponential distribution can be used to model the time between events that occur randomly and independently at a constant rate. The probability density function (PDF) of the exponential distribution is:

f(x) = λe^(-λx)

where λ is the rate parameter and x is the time variable.

To generate random numbers from the exponential distribution, you can use the rexp() function in R. The syntax for the rexp() function is:

rexp(n, rate)

where n is the number of random numbers to generate and rate is the rate parameter λ.

So, the Exponential distribution is a continuous probability distribution used to model the time between events that occur independently at a constant rate. In R programming language, you can use the “rexp” function to generate random numbers from the Exponential distribution and the “pexp”, “dexp”, and “qexp” functions to calculate the probability density function (PDF), cumulative distribution function (CDF), and quantile function of the Exponential distribution, respectively.

Here’s an example of how to use these functions in R:

 

# generate 10 random numbers from Exponential distribution with rate 
# parameter lambda = 0.5
x <- rexp(10, rate = 0.5)
x

# calculate the PDF of Exponential distribution 
# with lambda = 0.5 at x = 1

dexp(1, rate = 0.5)

# calculate the CDF of Exponential distribution 
# with lambda = 0.5 at x = 1
pexp(1, rate = 0.5)

# calculate the quantile of Exponential distribution 
# with lambda = 0.5at probability level 0.75
qexp(0.75, rate = 0.5)

 

In the above example, we generated 10 random numbers from the Exponential distribution with a rate parameter of 0.5 using the “rexp” function. We then calculated the PDF, CDF, and quantile of the Exponential distribution at x = 1 and a probability level of 0.75 using the “dexp”, “pexp”, and “qexp” functions, respectively. Note that the “rate” parameter specifies the rate parameter lambda of the Exponential distribution.

Example – 2

In R Programming, the exponential distribution can be generated using the rexp() function, which takes two arguments: n, the number of random values to generate, and rate, the rate parameter of the distribution.

# Generate 1000 random values from an
# exponential distribution with rate = 0.5

set.seed(123) # for reproducibility
values <- rexp(n = 1000, rate = 0.5)

# Plot a histogram of the generated values
hist(
values,
freq = FALSE,
main = "Exponential Distribution",
xlab = "Values",
ylab = "Density"
)

# Add the theoretical density curve to the plot
curve(dexp(x, rate = 0.5),
add = TRUE,
col = "red",
lwd = 2)

This will produce a plot that shows the histogram of the generated values and the theoretical density curve of the exponential distribution with the specified rate parameter.

Note that the exponential distribution is commonly used to model the time between events that occur at a constant rate, such as the time between arrivals at a service center or the time between equipment failures.

Example – 3

Now, you know to generate random numbers from the exponential distribution, you can use the rexp() function in R. The syntax for the rexp() function is:

rexp(n, rate)

where n is the number of random numbers to generate and rate is the rate parameter λ.

For example, to generate 10 random numbers from an exponential distribution with a rate parameter of 0.5, you can use the following code:

set.seed(123)
random_numbers <- rexp(10, 0.5)
random_numbers

To plot the probability density function (PDF) of the exponential distribution, you can use the dexp() function. The syntax for the dexp() function is:

dexp(x, rate)

where x is the time variable and rate is the rate parameter λ.

For example, to plot the PDF of an exponential distribution with a rate parameter of 0.5, you can use the following code:

x <- seq(0, 5, length.out=100)
pdf <- dexp(x, 0.5)
plot(x, pdf, type="l", ylab="PDF")

This will produce a plot of the PDF of the exponential distribution, which will look like a decreasing exponential curve.

Chi-Square Distribution in R

How to Plot a Log Normal Distribution in R