## Laplace Distribution probabilities using R

In this tutorial, you will learn about how to use `dlaplace()`

, `plaplace()`

, `qlaplace()`

and `rlaplace()`

functions in R programming language to compute the individual probabilities, cumulative probabilities, quantiles and to generate random sample for Laplace distribution.

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

## Laplace Distribution

Laplace distribution distribution is a continuous type probability distribution.

Let $X\sim L(\mu,\lambda)$. Then the probability distribution of $X$ is

` $$ \begin{align*} f(x;\mu, \lambda)&= \begin{cases} \frac{\lambda}{2}e^{-\lambda|x-\mu|}, & -\infty < x< \infty; \\ & -\infty < \mu < \infty, \lambda >0; \\ 0, & Otherwise. \end{cases} \end{align*} $$ `

where $\mu$ is the location parameter and $\lambda$ is the scale parameter of Laplace distribution.

The distribution function of Laplace distribution is

` $$ \begin{align*} F(x) &= \begin{cases} \frac{1}{2}e^{\lambda(x-\mu)}, & x< \mu; \\ &\\ 1-\frac{1}{2}e^{-\lambda(x-\mu)}, & x\geq \mu; \end{cases} \end{align*} $$ `

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

## Laplace probabilities using `dlaplace()`

function in R

The Laplace distribution is not available in `base`

package. It is available in `VGAM`

(Vector Generalized Linear and Additive Models) package.

First we need to install the package using `install.packages(VGAM)`

, if it is not installed. Then to use the functions from `VGAM`

package we need to load it using `library(VGAM)`

command.

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 Laplace distribution using R is

`dlaplace(x,location, scale)`

where

`x`

: the value(s) of the variable and,`location`

: location parameter of Laplace distribution,`scale`

: scale parameter of Laplace distribution.

The `dlaplace()`

function gives the density for given value(s) `x`

, `location`

and `scale`

.

**Note:** If you do not specify the `location`

and `scale`

, R assumes the default value `location=0`

and `scale=1`

(which is a standard Laplace distribution).

## Numerical Problem for Laplace Distribution

To understand the four functions `dlaplace()`

, `plaplace()`

, `qlaplace()`

and `rlaplace()`

, let us take the following numerical problem.

### Laplace Distribution Example

Let $X$ be a random variable with Laplace distribution with parameter $\mu=5$ and $\lambda=2$. That is $X\sim L(5,2)$ distribution.

(a) Find the value of the density function at $x=3.5$.

(b) Plot the graph of Laplace probability distribution.

(c) Find the probability that, $X$ is less than or equal to 6.

(d) Find the probability that, $X$ is greater than 3.5.

(e) Find the probability that, $X$ is less than 10 but greater than 6.

(f) Plot the graph of cumulative Laplace probabilities.

(g) What is the value of $c$, if $P(X\leq c) \geq 0.75$?

(h) Simulate 1000 Laplace distributed random variables with $\mu= 5$ and $\lambda = 2$.

Given that $X\sim L(\mu=5, \lambda=2)$.

### Example 1: How to use `dlaplace()`

function in R?

To find the value of the density function at $x=0.35$ we need to use `dlaplace()`

function.

```
library(VGAM)
# parameter 1 location
mu <- 5
# parameter 2 scale
lambda <- 2
```

The probability density function of $X$ is

` $$ \begin{aligned} f(x)&= \frac{2}{2}e^{-2|x-5|},\\ &\quad\text{for } -\infty \leq x \leq \infty. \end{aligned} $$ `

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

First I will show you how to calculate the value of the density function for given value of $x$. Then I will show you how to compute the same using `dlaplace()`

function in R.

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

` $$ \begin{aligned} f(3.5)&= \frac{2}{2}e^{-2|3.5-5|}\\ &= 1\times e^{-3}\\ &= 0.1180916 \end{aligned} $$ `

The above probability can be calculated using `dlaplace(3.5,5,2)`

function in R.

```
# Compute Laplace probability
result1 <- dlaplace(3.5,mu,lambda)
result1
```

`[1] 0.1180916`

### Example 2 Visualize Laplace probability distribution

Using `dlaplace()`

function we can compute Laplace distribution probabilities for given `x`

, `location`

and `scale`

. To plot the probability density function of Laplace distribution, we need to create a sequence of `x`

values and compute the corresponding probabilities.

```
# create a sequence of x values
x <- seq(-5,15, by=0.02)
## Compute the Laplace pdf for each x
px<-dlaplace(x,mu,lambda)
```

(b) Visualizing Laplace Distribution with `dlaplace()`

function and `plot()`

function in R:

The probability density function of Laplace distribution with given 5 and 2 can be visualized using `plot()`

function as follows:

```
## Plot the Laplace probability dist
plot(x,px,type="l",xlim=c(-5,15),ylim=c(0,max(px)),
lwd=3, col="darkred",ylab="f(x)",
main=expression(paste("PDF of L(",
mu,"=5, ",lambda,"=2)")))
```

## Laplace cumulative probability using `plaplace()`

function in R

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

`plaplace(q,location, scale)`

where

`q`

: the value(s) of the variable,`location`

: first parameter of Laplace distribution,`scale`

: second parameter of Laplace distribution.

Using this function one can calculate the cumulative distribution function of Laplace distribution for given value(s) of `q`

(value of the variable `x`

), `location`

and `scale`

.

### Example 3: How to use `plaplace()`

function in R?

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

(c) The probability that, $X$ is less than or equal to 6 is

` $$ \begin{aligned} P(X \leq 6) &=F(6)\\ &=1-\frac{1}{2}e^{\dfrac{-(6-5)}{2}}\\ &\qquad (\because 6 > \mu)\\ &=1-\frac{1}{2}e^{\dfrac{-(6-5)}{2}}\\ &= 1-0.3033\\ &= 0.6967 \end{aligned} $$ `

```
## Compute cumulative Laplace probability
result2 <- plaplace(6,mu,lambda)
result2
```

`[1] 0.6967347`

### Example 4: How to use `plaplace()`

function in R?

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

(d) The probability that $X$ is greater than $3.5$ is

` $$ \begin{aligned} P(X > 3.5) &=1-P(X < 3.5)\\ &=1-F(3.5)\\ &=1-\frac{1}{2}e^{\dfrac{-(3.5-5)}{2}}\\ &\qquad (\because 3.5 < \mu)\\ &=1-\frac{1}{2}e^{\dfrac{(3.5-5)}{2}}\\ &= 1-0.2362\\ &= 0.7638 \end{aligned} $$ `

To calculate the probability that a random variable $X$ is greater than a given number one can use the option `lower.tail=FALSE`

in `plaplace()`

function.

Above probability can be calculated easily using `plaplace()`

function with argument `lower.tail=FALSE`

as

$P(X > 3.5) =$ `plaplace(3.5,mu,lambda,lower.tail=FALSE)`

or by using complementary event as

$P(X > 3.5) = 1- P(X\leq 3.5)$= 1- `plaplace(3.5,mu,lambda)`

```
# compute cumulative Laplace probabilities
# with lower.tail False
plaplace(3.5,mu,lambda,lower.tail=FALSE)
```

`[1] 0.7638167`

```
# Using complementary event
1-plaplace(3.5,mu,lambda)
```

`[1] 0.7638167`

### Example 5: How to use `plaplace()`

function in R?

One can also use `plaplace()`

function to calculate the probability that the random variable $X$ is between two values.

(e) The probability that $X$ is between $6$ and $10$ is

` $$ \begin{aligned} P(6 \leq X \leq 10)&=P(X\leq 10)-P(X\leq 6)\\ &=F(10) -F(6)\\ &=\bigg(1-\frac{1}{2}e^{\dfrac{-(10-5)}{2}}\bigg)-\bigg(1-\frac{1}{2}e^{\dfrac{-(6-5)}{2}}\bigg)\\ &=\frac{1}{2}e^{\dfrac{-(6-5)}{2}}-\frac{1}{2}e^{\dfrac{-(10-5)}{2}}\\ &= 0.3033-0.041\\ &=0.2623 \end{aligned} $$ `

The above probability can be calculated using `plaplace()`

function as follows:

```
a <- plaplace(10,mu,lambda)
b <- plaplace(6,mu,lambda)
result3 <- a - b
result3
```

`[1] 0.2622228`

### Example 6: Visualize the cumulative Laplace probability distribution

Using `plaplace()`

function we can compute Laplace cumulative probabilities (CDF) for given `x`

, `location`

and `scale`

. To plot the CDF of Laplace distribution, we need to create a sequence of `x`

values and compute the corresponding cumulative probabilities.

```
# create a sequence of x values
x <- seq(-5,15, by=0.02)
## Compute the Laplace pdf for each x
Fx <- plaplace(x,mu,lambda)
```

(f) Visualizing Laplace Distribution with `plaplace()`

function and `plot()`

function in R:

The cumulative probability distribution of Laplace distribution with given `x`

, `location`

and `scale`

can be visualized using `plot()`

function as follows:

```
## Plot the Laplace probability dist
plot(x,Fx,type="l",xlim=c(-5,15),ylim=c(0,1),
lwd=3, col="darkred",ylab="F(x)",
main=expression(paste("Distribution function of L(",
mu,"=5, ",lambda,"=2)")))
```

## Laplace Distribution Quantiles using `qlaplace()`

in R

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

`qlaplace(p,location,scale)`

where

`p`

: the value(s) of the probabilities,`location`

: first parameter of Laplace distribution,`scale`

: second parameter of Laplace distribution.

The function `qlaplace(p,location,scale)`

gives $100*p^{th}$ quantile of Laplace distribution for given value of `p`

, `location`

and `scale`

.

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

It is the inverse of `plaplace()`

function. That is, inverse cumulative probability distribution function for Laplace distribution.

### Example 7: How to use `qlaplace()`

function in R?

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

```
mu <- 5
lambda <- 2
prob <- 0.75
```

```
# compute the quantile for Laplace dist
qlaplace(0.75,mu, lambda)
```

`[1] 6.386294`

The $75^{th}$ percentile of given Laplace distribution is 6.3862944.

### Visualize the quantiles of Beta Distribution

The quantiles of Beta distribution with given `p`

, `location`

and `scale`

can be visualized using `plot()`

function as follows:

```
p <- seq(0,1,by=0.02)
qx <- qlaplace(p,mu,lambda)
# Plot the quantiles of Laplace dist
plot(p,qx,type="l",lwd=2,col="darkred",
ylab="quantiles",
main=expression(paste("Quantiles of L(",
mu,"=5, ",lambda,"=2)")))
```

## Simulating Laplace random variable using `rlaplace()`

function in R

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

`rlaplace(n,location,scale)`

where,

`n`

: the sample observations,`location`

: first parameter of Laplace distribution,`scale`

: second parameter of Laplace distribution.

The function `rlaplace(n,location,scale)`

generates `n`

random numbers from Laplace distribution with given `location`

and `scale`

.

### Example 8: How to use `rlaplace()`

function in R?

In part (h), we need to generate 1000 random numbers from Laplace distribution with given $location = 5$ and $scale=2$.

(h) We can use `rlaplace(1000,mu,lambda)`

function to generate random numbers from Laplace distribution.

```
## initialize sample size to generate
n <- 1000
# Simulate 1000 values From Laplace dist
x_sim <- rlaplace(n,mu,lambda)
```

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

```
## Plot the simulated data
plot(density(x_sim),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated data from L(",
mu,"=5, ",lambda,"=2)")))
```

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

```
# Simulate 1000 values From Laplace dist
x_sim_2 <- rlaplace(n,mu,lambda)
```

```
## Plot the simulated data
plot(density(x_sim_2),xlab="Simulated x",ylab="density",
lwd=5,col="blue",
main=expression(paste("Simulated data from L(",
mu,"=5, ",lambda,"=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 Laplace dist
x_sim_3 <- rlaplace(n,mu,lambda)
```

```
## Plot the simulated data
plot(density(x_sim_3),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated data from L(",
mu,"=5, ",lambda,"=2)")))
```

```
set.seed(1457)
# Simulate 1000 values From Laplace dist
x_sim_4 <- rlaplace(n,mu,lambda)
```

```
## Plot the simulated data
plot(density(x_sim_4),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated data from L(",
mu,"=5, ",lambda,"=2)")))
```

Since we have used `set.seed(1457)`

function, R will generate the same set of Laplace distributed random numbers.

```
hist(x_sim_4,breaks = 30,col="red4",
main=expression(paste("Histogram L(",
mu,"=5, ",lambda,"=2)")))
```

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

Exponential distribution in R

Normal distribution in R

Log-normal distribution in R

Gamma distribution in R

Beta distribution in R

Cauchy 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 Laplace distribution in R programming. You also learned about how to simulate a Laplace 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 Laplace Distribution using R and your thought on this article.