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.
The table below shows the functions for Cauchy distributions 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\) |
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")
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")
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
[1] 0.1075371
[1] 0.1075371
Sample 200 observations from the Cauchy distribution with \(\tt{location} = 3\) and \(\tt{scale} = 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")
Calculate the density at \(x = 1.7\), in the Cauchy distribution with \(\tt{location} = 1\) and \(\tt{scale} = 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)
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)\):
[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)
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)
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:
[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)
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\):
[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)
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)
For upper tail, for \(p = 0.2\), that is, \(x\) such that, \(P(X \ge x)=0.2\):
[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)
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes