Here, we discuss Cauchy distribution functions in R, plots, parameter setting, random sampling, density, cumulative distribution and quantiles.

The Cauchy distribution with parameters \(\tt{location}=x_0\), and \(\tt{scale}=\gamma\) has probability density function (pdf) formula as:

\[\begin{align} f(x) & = \frac{1}{\pi\gamma \left[1 + \left(\frac{x - x_0}{\gamma}\right)^2\right]}\\ & = { 1 \over \pi \gamma } \left[ { \gamma^2 \over (x - x_0)^2 + \gamma^2 } \right],\end{align}\] for \(x\in\mathbb{R}\),

where \(x_0 \in \mathbb {R}\), \({\gamma>0}\), and \(\pi\approx 3.14159\).

The mean and variance are \(\tt{undefined}\).

The standard Cauchy distribution has \(\tt{location}=0\), and \(\tt{scale}=1\).

See also probability distributions and plots and charts.

1 Table of Cauchy Distribution Functions in R

The table below shows the functions for Cauchy distributions in R.

Table of Cauchy Distribution Functions in R
Function Usage
rcauchy(n, location=0, scale=1) Simulate a random sample with \(n\) observations
dcauchy(x, location=0, scale=1) Calculate the probability density at the point \(x\)
pcauchy(q, location=0, scale=1) Calculate the cumulative distribution at the point \(q\)
qcauchy(p, location=0, scale=1) Calculate the quantile value associated with \(p\)

2 Plot of Cauchy Distributions in R

Single distribution:

Below is a plot of the Cauchy distribution function with \(\tt{location}=2\) and \(\tt{scale}=1\).

x = seq(-6, 10, 1/1000); y = dcauchy(x, 2, 1)
plot(x, y, type = "l",
     xlim = c(-6, 10), ylim = c(0, max(y)),
     main = "Probability Density Function of Cauchy Distribution (2, 1)",
     xlab = "x", ylab = "Density",
     lwd = 2, col = "blue")
# Add line and legend
lines(c(2, 2), c(0, max(y)), col = "red")
legend("topleft", "loc. = 2, scale = 1",
       lwd = 2,
       col = "blue",
       bty = "n")
Probability Density Function (PDF) of a Cauchy Distribution in R

Probability Density Function (PDF) of a Cauchy Distribution in R

Multiple distributions:

Below is a plot of multiple Cauchy distribution functions in one graph.

x1 = seq(-5, 8, 1/1000); y1 = dcauchy(x1, 0, 0.5)
x2 = seq(-5, 8, 1/1000); y2 = dcauchy(x2, 0, 1)
x3 = seq(-5, 8, 1/1000); y3 = dcauchy(x3, 1, 2)
plot(x1, y1, type = "l",
     xlim = c(-5, 8), ylim = range(c(y1, y2, y3)),
     main = "Probability Density Functions of Cauchy Distributions",
     xlab = "x", ylab = "Density",
     lwd = 2, col = "blue")
points(x2, y2, type = "l", lwd = 2, col = "red")
points(x3, y3, type = "l", lwd = 2, col = "green")
# Add legend
legend("topright", c("location = 0, scale = 0.5",
                    "location = 0, scale = 1",
                    "location = 1, scale = 2"),
       lwd = c(2, 2, 2),
       col = c("blue", "red", "green"),
       bty = "n")
Probability Density Functions (PDFs) of Cauchy Distributions in R

Probability Density Functions (PDFs) of Cauchy Distributions in R

3 Examples for Setting Parameters for Cauchy Distributions in R

In the Cauchy distribution functions, parameters are pre-specified as \(x_0=0\) and \(\gamma=1\), hence they do not need to be specified, unless they are to be set to different values.

For example, for dcauchy(), the following are the same:

# The order of 0 and 1 matters here as the parameter names are not used.
# The first number 0 is location, and 1 is scale.
dcauchy(1.4); dcauchy(1.4, 0); dcauchy(1.4, 0, 1)
[1] 0.1075371
[1] 0.1075371
[1] 0.1075371
# Or:
dcauchy(1.4, location = 0, scale = 1)
[1] 0.1075371
# Or:
dcauchy(1.4, scale = 1, location = 0)
[1] 0.1075371

4 rcauchy(): Random Sampling from Cauchy Distributions in R

Sample 200 observations from the Cauchy distribution with \(\tt{location} = 3\) and \(\tt{scale} = 0.5\):

rcauchy(200, 3, 0.5)
set.seed(1000) # Line allows replication (use any number).
sample = rcauchy(200, 3, 0.5)
hist(sample,
     main = "Histogram of 200 Observations from Cauchy Distribution
with Location = 3 and Scale = 0.5",
     xlab = "x",
     col = "deepskyblue", border = "white")
Histogram of Cauchy Distribution (3, 0.5) Random Sample in R

Histogram of Cauchy Distribution (3, 0.5) Random Sample in R

5 dcauchy(): Probability Density Function for Cauchy Distributions in R

Calculate the density at \(x = 1.7\), in the Cauchy distribution with \(\tt{location} = 1\) and \(\tt{scale} = 0.5\):

dcauchy(1.7, 1, 0.5)
[1] 0.2150742
x = seq(-4, 6, 1/1000); y = dcauchy(x, 1, 0.5)
plot(x, y, type = "l",
     xlim = c(-4, 6), ylim = c(0, max(y)),
     main = "Probability Density Function of Cauchy Distribution
with Location = 1 and Scale = 0.5",
     xlab = "x", ylab = "Density",
     lwd = 2, col = "blue")
# Add lines
segments(1.7, -1, 1.7, 0.2150742)
segments(-5, 0.2150742, 1.7, 0.2150742)
Probability Density Function (PDF) of Cauchy Distribution (1, 0.5) in R

Probability Density Function (PDF) of Cauchy Distribution (1, 0.5) in R

6 pcauchy(): Cumulative Distribution Function for Cauchy Distributions in R

Calculate the cumulative distribution at \(x = 1.6\), in the Cauchy distribution with \(\tt{location} = 0.5\) and \(\tt{scale} = 0.4\). That is, \(P(X \le 1.6)\):

pcauchy(1.6, 0.5, 0.4)
[1] 0.8889827
x = seq(-5.5, 6.5, 1/1000); y = pcauchy(x, 0.5, 0.4)
plot(x, y, type = "l",
     xlim = c(-5.5, 6.5), ylim = c(0,1),
     main = "Cumulative Distribution Function of Cauchy Distribution
with Location = 0.5 and Scale = 0.4",
     xlab = "x", ylab = "Cumulative Distribution",
     lwd = 2, col = "blue")
# Add lines
segments(1.6, -1, 1.6, 0.8889827)
segments(-6, 0.8889827, 1.6, 0.8889827)
Cumulative Distribution Function (CDF) of Cauchy Distribution (0.5, 0.4) in R

Cumulative Distribution Function (CDF) of Cauchy Distribution (0.5, 0.4) in R

x = seq(-5.5, 6.5, 1/1000); y = dcauchy(x, 0.5, 0.4)
plot(x, y, type = "l",
     xlim = c(-5.5, 6.5), ylim = c(0, max(y)),
     main = "Probability Density Function of Cauchy Distribution
with Location = 0.5 and Scale = 0.4",
     xlab = "x", ylab = "Density",
     lwd = 2, col = "blue")
# Add shaded region and legend
point = 1.6
polygon(x = c(x[x <= point], point),
        y = c(y[x <= point], 0),
        col = "limegreen")
legend("topright", c("Area = 0.8889827"),
       fill = c("limegreen"),
       inset = 0.01)
Shaded Probability Density Function (PDF) of Cauchy Distribution (0.5, 0.4) in R

Shaded Probability Density Function (PDF) of Cauchy Distribution (0.5, 0.4) in R

For upper tail, at \(x = 1.6\), that is, \(P(X \ge 1.6) = 1 - P(X \le 1.6)\), set the "lower.tail" argument:

pcauchy(1.6, 0.5, 0.4, lower.tail = FALSE)
[1] 0.1110173
x = seq(-5.5, 6.5, 1/1000); y = dcauchy(x, 0.5, 0.4)
plot(x, y, type = "l",
     xlim = c(-5.5, 6.5), ylim = c(0, max(y)),
     main = "Shaded Upper Region: Probability Density Function of
Cauchy Distribution with Location = 0.5 and Scale = 0.4",
     xlab = "x", ylab = "Density",
     lwd = 2, col = "blue")
# Add shaded region and legend
point = 1.6
polygon(x = c(point, x[x >= point]),
        y = c(0, y[x >= point]),
        col = "limegreen")
legend("topright", c("Area = 0.1110173"),
       fill = c("limegreen"),
       inset = 0.01)
Shaded Upper Region: Probability Density Function (PDF) of Cauchy Distribution (0.5, 0.4) in R

Shaded Upper Region: Probability Density Function (PDF) of Cauchy Distribution (0.5, 0.4) in R

7 qcauchy(): Derive Quantile for Cauchy Distributions in R

Derive the quantile for \(p = 0.8\), in the Cauchy distribution with \(\tt{location} = 5\) and \(\tt{scale} = 1\). That is, \(x\) such that, \(P(X \le x)=0.8\):

qcauchy(0.8, 5, 1)
[1] 6.376382
x = seq(-5, 15, 1/1000); y = pcauchy(x, 5, 1)
plot(x, y, type = "l",
     xlim = c(-5, 15), ylim = c(0,1),
     main = "Cumulative Distribution Function of Cauchy Distribution
with Location = 5 and Scale = 1",
     xlab = "x", ylab = "Cumulative Distribution",
     lwd = 2, col = "blue")
# Add lines
segments(6.376382, -1, 6.376382, 0.8)
segments(-6, 0.8, 6.376382, 0.8)
Cumulative Distribution Function (CDF) of Cauchy Distribution (5, 1) in R

Cumulative Distribution Function (CDF) of Cauchy Distribution (5, 1) in R

x = seq(-5, 15, 1/1000); y = dcauchy(x, 5, 1)
plot(x, y, type = "l",
     xlim = c(-5, 15), ylim = c(0, max(y)),
     main = "Probability Density Function of Cauchy Distribution
with Location = 5 and Scale = 1",
     xlab = "x", ylab = "Density",
     lwd = 2, col = "blue")
# Add shaded region and legend
point = 6.376382
polygon(x = c(x[x <= point], point),
        y = c(y[x <= point], 0),
        col = "limegreen")
legend("topright", c("Area = 0.8"),
       fill = c("limegreen"),
       inset = 0.01)
Shaded Probability Density Function (PDF) of Cauchy Distribution (5, 1) in R

Shaded Probability Density Function (PDF) of Cauchy Distribution (5, 1) in R

For upper tail, for \(p = 0.2\), that is, \(x\) such that, \(P(X \ge x)=0.2\):

qcauchy(0.2, 5, 1, lower.tail = FALSE)
[1] 6.376382
x = seq(-5, 15, 1/1000); y = dcauchy(x, 5, 1)
plot(x, y, type = "l",
     xlim = c(-5, 15), ylim = c(0, max(y)),
     main = "Shaded Upper Region: Probability Density Function of
Cauchy Distribution with Location = 5 and Scale = 1",
     xlab = "x", ylab = "Density",
     lwd = 2, col = "blue")
# Add shaded region and legend
point = 6.376382
polygon(x = c(point, x[x >= point]),
        y = c(0, y[x >= point]),
        col = "limegreen")
legend("topright", c("Area = 0.2"),
       fill = c("limegreen"),
       inset = 0.01)
Shaded Upper Region: Probability Density Function (PDF) of Cauchy Distribution (5, 1) in R

Shaded Upper Region: Probability Density Function (PDF) of Cauchy Distribution (5, 1) in R

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes