Cauchy Distribution Probabilities Using R

Cauchy Distribution probabilities using R

In this tutorial, you will learn about how to use dcauchy(), pcauchy(), qcauchy() and rcauchy() functions in R programming language to compute the individual probabilities, cumulative probabilities, quantiles and to generate random sample for Cauchy distribution.

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

Cauchy Distribution

Cauchy distribution distribution is a continuous type probability distribution.

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

$$ \begin{aligned} f(x)&= \begin{cases} \frac{\lambda}{\pi}\cdot \frac{1}{\lambda^2+(x-\mu)^2}, & -\infty < x< \infty; \\ & -\infty < \mu< \infty, \lambda>0;\\ 0, & Otherwise. \end{cases} \end{aligned} $$

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

The distribution function of Cauchy distribution is

$$ \begin{equation*} F(x) =\frac{1}{\pi}\tan^{-1}\bigg(\frac{x-\mu}{\lambda}\bigg) + \frac{1}{2}. \end{equation*} $$

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

Cauchy probabilities using dcauchy() 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 Cauchy distribution using R is

dcauchy(x,location, scale)

where

  • x : the value(s) of the variable and,
  • location : location parameter of Cauchy distribution,
  • scale : scale parameter of Cauchy distribution.

The dcauchy() 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 Cauchy distribution).

Numerical Problem for Cauchy Distribution

To understand the four functions dcauchy(), pcauchy(), qcauchy() and rcauchy(), let us take the following numerical problem.

Cauchy Distribution Example

Let random variable $X$ follows a Cauchy distribution with $\mu=2$ and $\lambda=4$.

(a) Find the value of the density function at $x=2.5$.
(b) Plot the graph of Cauchy probability distribution.
(c) Find the probability that, $X$ is less than 3.
(d) Find the probability that, $X$ is greater than 4.
(e) Find the probability that, $X$ is less than 1 but greater than 3.5.
(f) Plot the graph of cumulative Cauchy probabilities.
(g) What is the value of $c$, if $P(X\leq c) \geq 0.80$?
(h) Simulate 1000 Cauchy distributed random variables with $\mu= 2$ and $\lambda = 4$.

Let $X$ denote the daily proportion of major automobile accidents across the United States. Given that $X\sim C(\mu=2, \lambda=4)$.

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

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

Let $X\sim C(2,4)$.

First let us define the given parameters as

# parameter 1 location
mu <- 2
# parameter 2 scale
lambda <- 4

The probability density function of $X$ is

$$ \begin{aligned} f(x)&= \frac{4}{\pi}\cdot \frac{1}{4^2+(x-2)^2},\\ &\quad\text{for } -\infty \leq x \leq \infty. \end{aligned} $$

For part (a), we need to find the density function at $x=2.5$. That is $f(2.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 dcauchy() function in R.

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

$$ \begin{aligned} f(2.5)&=\frac{4}{\pi}\cdot \frac{1}{4^2+(2.5-2)^2}\\ &=\frac{4}{\pi}\cdot \frac{1}{4^2+(2.5-2)^2} &= 0.0783532 \end{aligned} $$

The above probability can be calculated using dcauchy(2.5,2,4) function in R.

# Compute Cauchy  probability
result1 <- dcauchy(2.5,mu,lambda)
result1
[1] 0.0783532

Example 2 Visualize Cauchy probability distribution

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

# create a sequence of x values
x <- seq(-10,14, by=0.02)
## Compute the Cauchy pdf for each x
px<-dcauchy(x,mu,lambda)

(b) Visualizing Cauchy Distribution with dcauchy() function and plot() function in R:

The probability density function of Cauchy distribution with given 2 and 4 can be visualized using plot() function as follows:

## Plot the Cauchy  probability dist
plot(x,px,type="l",xlim=c(-10,14),ylim=c(0,max(px)),
     lwd=3, col="darkred",ylab="f(x)",
     main=expression(paste("PDF of C(",
        mu,"=2, ",lambda,"=4)")))
abline(v=c(2),lty=2,col="gray")
density function of cauchy distribution
density function of cauchy distribution

Cauchy cumulative probability using pcauchy() function in R

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

pcauchy(q,location, scale)

where

  • q : the value(s) of the variable,
  • location : first parameter of Cauchy distribution,
  • scale : second parameter of Cauchy distribution.

Using this function one can calculate the cumulative distribution function of Cauchy distribution for given value(s) of q (value of the variable x), location and scale.

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

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

(c) The probability that $X$ is less than $3$ is

$$ \begin{aligned} P(X \leq 3) &=F(3)\\ &=0.5+\frac{1}{\pi} tan^{-1}\big(\frac{3-2}{4}\big)\\ &=0.5 + \frac{1}{3.1416}tan^{-1}\big(0.25\big)\\ &=0.5 + \frac{1}{3.1416}(0.245)\\ &= 0.578 \end{aligned} $$

## Compute cumulative Cauchy  probability
result2 <- pcauchy(3,mu,lambda)
result2
[1] 0.5779791

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

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

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

$$ \begin{aligned} P(X > 4) &=1- P(X < 4)\\ &= 1- F(4)\\ &=1-\bigg(0.5+\frac{1}{\pi} tan^{-1}\big(\frac{4-2}{4}\big)\bigg)\\ &=0.5 - \frac{1}{3.1416}tan^{-1}\big(0.5\big)\\ &=0.5 - \frac{1}{3.1416}(0.4636)\\ &= 0.3524 \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 pcauchy() function.

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

$P(X \geq 4)=$ pcauchy(4,mu,lambda,lower.tail=FALSE)

or by using complementary event as

$P(X \geq 4) = 1- P(X\leq 4)$= 1- pcauchy(1,mu,lambda)

# compute cumulative Cauchy  probabilities
# with lower.tail False
pcauchy(4,mu,lambda,lower.tail=FALSE)
[1] 0.3524164
# Using complementary event
1-pcauchy(4,mu,lambda)
[1] 0.3524164

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

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

(e) The probability that $X$ is between $1$ and $3.5$ is

$$ \begin{aligned} P(1 \leq X \leq 3.5)&=P(X\leq 3.5)-P(X\leq 1)\\ &=F(3.5) -F(1)\\ &=\bigg[0.5+\frac{1}{\pi} tan^{-1}\big(\frac{3.5-2}{4}\big)\bigg]-\bigg[0.5+\frac{1}{\pi} tan^{-1}\big(\frac{1-2}{4}\big)\bigg]\\ &=\frac{1}{\pi} tan^{-1}\big(0.375\big)-\frac{1}{\pi} tan^{-1}\big(-0.25\big)\\ &=\frac{1}{3.1416}(0.3588)-\frac{1}{3.1416}(-0.245)\\ &=0.1922 \end{aligned} $$

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

a <- pcauchy(3.5,mu,lambda)
b <- pcauchy(1,mu,lambda)
result3 <- a - b
result3
[1] 0.1921794

Example 6: Visualize the cumulative Cauchy probability distribution

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

# create a sequence of x values
x <- seq(-10,14, by=0.02)
## Compute the Cauchy pdf for each x
Fx <- pcauchy(x,mu,lambda)

(f) Visualizing Cauchy Distribution with pcauchy() function and plot() function in R:

The cumulative probability distribution of Cauchy distribution with given x, location and scale can be visualized using plot() function as follows:

## Plot the Cauchy  probability dist
plot(x,Fx,type="l",xlim=c(-10,14),ylim=c(0,1),
     lwd=3, col="darkred",ylab="F(x)",
     main=expression(paste("Distribution function of C(",
        mu,"=2, ",lambda,"=4)")))
cumulative density function of cauchy distribution
cumulative density function of cauchy distribution

Cauchy Distribution Quantiles using qcauchy() in R

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

qcauchy(p,location,scale)

where

  • p : the value(s) of the probabilities,
  • location : first parameter of Cauchy distribution,
  • scale : second parameter of Cauchy distribution.

The function qcauchy(p,location,scale) gives $100*p^{th}$ quantile of Cauchy distribution for given value of p, location and scale.

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

It is the inverse of pcauchy() function. That is, inverse cumulative probability distribution function for Cauchy distribution.

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

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

mu <- 2
lambda <- 4
prob <- 0.80
# compute the quantile for Cauchy  dist
qcauchy(0.80,mu, lambda)
[1] 7.505528

The $85^{th}$ percentile of given Cauchy distribution is 9.850442.

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 <- qcauchy(p,mu,lambda)
# Plot the quantiles of Cauchy  dist
plot(p,qx,type="l",lwd=2,col="darkred",
     ylab="quantiles",
     main=expression(paste("Quantiles of C(",
        mu,"=2, ",lambda,"=4)")))
quantiles of cauchy distribution
quantiles of cauchy distribution

Simulating Cauchy random variable using rcauchy() function in R

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

rcauchy(n,location,scale)

where,

  • n : the sample observations,
  • location : first parameter of Cauchy distribution,
  • scale : second parameter of Cauchy distribution.

The function rcauchy(n,location,scale) generates n random numbers from Cauchy distribution with given location and scale.

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

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

(h) We can use rcauchy(1000,mu,lambda) function to generate random numbers from Cauchy distribution.

# location parameter
mu <- 2
# scale parameter
lambda <- 4
## initialize sample size to generate
n <- 10000
# Simulate 10000 values From Cauchy  dist
x_sim <- rcauchy(n,mu,lambda)
summary(x_sim)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
-2427.697    -2.085     1.984     1.600     6.045  3567.315 

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

## Plot the simulated data
plot(density(x_sim),xlab="Simulated x",ylab="density",
     xlim=c(-200,200),lwd=5,col="darkred",
     main=expression(paste("Simulated Data from C(",
        mu,"=2, ",lambda,"=4)")))
random sample from cauchy distribution
random sample from cauchy distribution

The histogram of simulated random variables from Cauchy distribution is as follows:

hist(x_sim,breaks = 1000,xlim=c(-200,200),col="red4",
     main=expression(paste("Histogram C(",
        mu,"=2, ",lambda,"=4)")))
Histogram of random sample from cauchy distribution
Histogram of random sample from cauchy distribution

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
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 Cauchy distribution in R programming. You also learned about how to simulate a Cauchy 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 Cauchy 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