Here, we discuss continuous uniform distribution functions in R, plots, parameter setting, random sampling, density, cumulative distribution and quantiles.
The continuous uniform distribution with parameters \(\tt{min}=a\), and \(\tt{max}=b\) has probability density function (pdf) formula as:
\[f(x)=\begin{cases} \frac{1}{b - a} & \mathrm{for}\ a \le x \le b, \\[8pt] 0 & \mathrm{for}\ x<a\ \mathrm{or}\ x>b \end{cases},\] where \(-\infty < a < b < \infty\).
The mean is \(\frac{1}{2}(a+b)\), and the variance is \(\frac{1}{12}(b-a)^2\).
See also probability distributions and plots and charts.
The table below shows the functions for continuous uniform distributions in R.
Function | Usage |
runif(n, min=0, max=1) | Simulate a random sample with \(n\) observations |
dunif(x, min=0, max=1) | Calculate the probability density at the point \(x\) |
punif(q, min=0, max=1) | Calculate the cumulative distribution at the point \(q\) |
qunif(p, min=0, max=1) | Calculate the quantile value associated with \(p\) |
Below is a plot of the continuous uniform distribution function with \(\min=0\) and \(\max=1\).
x = seq(0, 1); y = dunif(x, 0, 1)
plot(x, y, type = "l",
xlim = c(-1, 2), ylim = c(0, 1.2),
main = "Probability Density Function of
Continuous Uniform Distribution (0, 1)",
xlab = "x", ylab = "Density",
lwd = 4, col = "blue")
# Add lines
segments(-1, 0, 0, 0, col = "blue")
segments(0, 0, 0, 1, lty = "dashed", col = "blue")
segments(1, 1, 1, 0, lty = "dashed", col = "blue")
segments(1, 0, 2, 0, col = "blue")
In the continuous uniform distribution functions, parameters are pre-specified as \(\min=0\) and \(\max=1\), hence they do not need to be specified, unless they are to be set to different values.
For example, for punif()
, 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 min, and 1 is max.
punif(0.4); punif(0.4, 0); punif(0.4, 0, 1)
[1] 0.4
[1] 0.4
[1] 0.4
[1] 0.4
[1] 0.4
Sample 500 observations from the continuous uniform distribution with \(\min = 2\) and \(\max = 4\):
set.seed(1234) # Line allows replication (use any number).
sample = runif(500, 2, 4)
hist(sample,
main = "Histogram of 500 Observations from
Continuous Uniform Distribution with Min = 2 and Max = 4",
xlab = "x",
col = "deepskyblue", border = "white")
Calculate the density at \(x = 1.5\), in the continuous uniform distribution with \(\min = 1\) and \(\max = 3\):
[1] 0.5
x = seq(1, 3); y = dunif(x, 1, 3)
plot(x, y, type = "l",
xlim = c(0, 4), ylim = c(0, 0.6),
main = "Probability Density Function of
Continuous Uniform Distribution with Min = 1 and Max = 3",
xlab = "x", ylab = "Density",
lwd = 4, col = "blue")
# Add lines
segments(0, 0, 1, 0, col = "blue")
segments(1, 0, 1, 0.5, lty = "dashed", col = "blue")
segments(3, 0.5, 3, 0, lty = "dashed", col = "blue")
segments(3, 0, 4, 0, col = "blue")
segments(1.5, -1, 1.5, 0.5, lty = "dashed")
segments(-1, 0.5, 1.5, 0.5, lty = "dashed")
Calculate the cumulative distribution at \(x = 3.5\), in the continuous uniform distribution with \(\min = 2\) and \(\max = 6\). That is, \(P(X \le 3.5)\):
[1] 0.375
x = seq(2, 6); y = punif(x, 2, 6)
plot(x, y, type = "l",
xlim = c(1, 7), ylim = c(0,1),
main = "Cumulative Distribution Function of
Continuous Uniform Distribution with Min = 2 and Max = 6",
xlab = "x", ylab = "Cumulative Distribution",
lwd = 4, col = "blue")
# Add lines
segments(1, 0, 2, 0, col = "blue")
segments(6, 1, 7, 1, col = "blue")
segments(3.5, -1, 3.5, 0.375, lty = "dashed")
segments(-1, 0.375, 3.5, 0.375, lty = "dashed")
x = seq(2, 6); y = dunif(x, 2, 6)
plot(x, y, type = "l",
xlim = c(1, 7), ylim = c(0, 0.3),
main = "Probability Density Function of
Continuous Uniform Distribution with Min = 2 and Max = 6",
xlab = "x", ylab = "Density",
lwd = 4, col = "blue")
# Add lines
segments(1, 0, 2, 0, col = "blue")
segments(2, 0, 2, 0.25, lty = "dashed", col = "blue")
segments(6, 0.25, 6, 0, lty = "dashed", col = "blue")
segments(6, 0, 7, 0, col = "blue")
# Add shaded region and legend
polygon(x = c(2, 2, 3.5, 3.5),
y = c(0, 0.25, 0.25, 0),
col = "limegreen")
legend("topright", c("Area = 0.375"),
fill = c("limegreen"),
inset = 0.01)
For upper tail, at \(x = 3.5\), that is, \(P(X \ge 3.5) = 1 - P(X \le 3.5)\), set the "lower.tail" argument:
[1] 0.625
x = seq(2, 6); y = dunif(x, 2, 6)
plot(x, y, type = "l",
xlim = c(1, 7), ylim = c(0, 0.3),
main = "Shaded Upper Region: Probability Density Function of
Continuous Uniform Distribution with Min = 2 and Max = 6",
xlab = "x", ylab = "Density",
lwd = 4, col = "blue")
# Add lines
segments(1, 0, 2, 0, col = "blue")
segments(2, 0, 2, 0.25, lty = "dashed", col = "blue")
segments(6, 0.25, 6, 0, lty = "dashed", col = "blue")
segments(6, 0, 7, 0, col = "blue")
# Add shaded region and legend
polygon(x = c(3.5, 3.5, 6, 6),
y = c(0, 0.25, 0.25, 0),
col = "limegreen")
legend("topright", c("Area = 0.625"),
fill = c("limegreen"),
inset = 0.01)
Derive the quantile for \(p = 0.6\), in the continuous uniform distribution with \(\min = 0\) and \(\max = 2\). That is, \(x\) such that, \(P(X \le x)=0.6\):
[1] 1.2
x = seq(0, 2); y = punif(x, 0, 2)
plot(x, y, type = "l",
xlim = c(-1, 3), ylim = c(0,1),
main = "Cumulative Distribution Function of
Continuous Uniform Distribution with Min = 0 and Max = 2",
xlab = "x", ylab = "Cumulative Distribution",
lwd = 4, col = "blue")
# Add lines
segments(-1, 0, 0, 0, col = "blue")
segments(2, 1, 3, 1, col = "blue")
segments(1.2, -1, 1.2, 0.6, lty = "dashed")
segments(-2, 0.6, 1.2, 0.6, lty = "dashed")
x = seq(0, 2); y = dunif(x, 0, 2)
plot(x, y, type = "l",
xlim = c(-1, 3), ylim = c(0, 0.6),
main = "Probability Density Function of
Continuous Uniform Distribution with Min = 0 and Max = 2",
xlab = "x", ylab = "Density",
lwd = 4, col = "blue")
# Add lines
segments(-1, 0, 0, 0, col = "blue")
segments(0, 0, 0, 0.5, lty = "dashed", col = "blue")
segments(2, 0.5, 2, 0, lty = "dashed", col = "blue")
segments(2, 0, 3, 0, col = "blue")
# Add shaded region and legend
polygon(x = c(0, 0, 1.2, 1.2),
y = c(0, 0.5, 0.5, 0),
col = "limegreen")
legend("topright", c("Area = 0.6"),
fill = c("limegreen"),
inset = 0.01)
For upper tail, for \(p = 0.4\), that is, \(x\) such that, \(P(X \ge x)=0.4\):
[1] 1.2
x = seq(0, 2); y = dunif(x, 0, 2)
plot(x, y, type = "l",
xlim = c(-1, 3), ylim = c(0, 0.6),
main = "Shaded Upper Region: Probability Density Function of
Continuous Uniform Distribution with Min = 0 and Max = 2",
xlab = "x", ylab = "Density",
lwd = 4, col = "blue")
# Add lines
segments(-1, 0, 0, 0, col = "blue")
segments(0, 0, 0, 0.5, lty = "dashed", col = "blue")
segments(2, 0.5, 2, 0, lty = "dashed", col = "blue")
segments(2, 0, 3, 0, col = "blue")
# Add shaded region and legend
polygon(x = c(1.2, 1.2, 2, 2),
y = c(0.5, 0, 0, 0.5),
col = "limegreen")
legend("topright", c("Area = 0.4"),
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