Uniform (Speegle 4.5.1)

Situation: Uniform random variables can be either discrete or continuous, and describe a distribution where all outcomes are equally likely. Examples include

  • the result of a die roll
  • pseudo random number generator
  • round off error in measurements

pdf: \(f(x)=\frac{1}{b-a} \qquad a \leq x \leq b\)

Distributional Notation: \(X\sim Unif(a,b)\)

Mean and variance: \(E[X]=\frac{b+a}{2} \qquad Var(X)=\frac{(b-a)^{2}}{12}\)

R commands:

  • punif(x, a, b) to compute \(P(X\leq x)\) (the cdf)
  • runif(N, a, b) to randomly draw N samples from a \(X \sim Unuf(a,b)\) distribution.

Visualizing the shape of the distribution What happens to the distribution as you change a and b?

Example

Suppose a random variable \(X\) has a uniform distribution on the interval [-3,8], then the p.d.f. of \(X\) is

\[ f(x)=\Biggl\{ \begin{array}{cc} \frac{1}{b-a} & \mbox{ for } a < x < b \\ 0& {otherwise} \end{array} \]

Calculate P\((0\leq X\leq 4)\). Do this by hand, and confirm your answer using punif

\[ \int_{0}^{4} \frac{1}{11} dx = \frac{1}{11}x \Biggl|_{0}^{4} = \frac{4}{11} \]

punif(4, -3, 8) - punif(0, -3, 8) # 4/11
## [1] 0.3636364

Calculate the mean and standard deviation. Do this by hand, and confirm your answer using simulation.

\[ E(X) = \frac{b+a}{2} = \frac{8-3}{2} = 2.5 \\ SD(X) = \sqrt{\frac{(b-a)^2}{12}} = \sqrt{\frac{(8+3)^2}{12}} = 3.17 \]

x <- runif(10000, -3, 8)
mean(x)
## [1] 2.495369
sd(x)
## [1] 3.190014

You try it

Suppose that a random variable \(X\) has a uniform distribution on the interval [-4,10]. Write down the pdf of \(X\), find the mean, standard deviation and the value of \(P(-1 < X < 6)\) both theoretically and using simulation.

\[ f(x)=\Biggl\{ \begin{array}{cc} \frac{1}{14} & \mbox{ for } -4 < x < 10 \\ 0& {otherwise} \end{array} \]

\[ \begin{aligned} E(X) =& \frac{10-4}{2} = 3 \\ SD(X) =& \sqrt{\frac{(10+4)^2}{12}} = 4.04 \\ P(-1 < X < 6) =& \int_{-1}^{6} \frac{1}{14}dx = \frac{1}{2} \end{aligned} \]

x <- runif(10000, -4, 10)
mean(x)
## [1] 2.964397
sd(x)
## [1] 4.03015
mean(x < 6 & x > -1)
## [1] 0.4943

Exponential Random Variables (Speegle 4.5.2)

Situation: Exponential random variables measure the waiting time until the first event occurs in a Poisson process.

  • The waiting time until an electronic component fails could be exponential
  • The time between customers in a store.

Distributional Notation: Let \(X\sim Exp(\lambda)\) be an exponential random variable with rate \(\lambda\).

pdf: \(f(x) = \lambda e^{-\lambda y} \qquad y \geq 0\)

Mean and variance: \(E[X]=\frac{1}{\lambda} \qquad Var(X)=\frac{1}{\lambda^{2}}\)

R commands:

  • dunif(x, lambda) to compute \(P(X == x)\)
  • punif(x, lambda) to compute \(P(X\leq x)\) (the cdf)
  • runif(N, lambda) to randomly draw N samples from a \(X \sim Exp(\lambda)\) distribution.

Visualizing the shape of the distribution What happens to the distribution as you change lambda?

Example

Suppose the time to failure (in years) for a particular component is distributed as an exponential random variable with rate \(\lambda=1/5\). For better performance, the system has two components installed, and the system will work as long as either components installed, and the system will work as long as either component is functional. Assume the time to failure for the two components is independent. What is the probability that the system will fail before 10 years has passed?

  • Problem setup

Let \(X_{1}\) be the time until component 1 fails. Let \(X_{2}\) be the time until component 2 fails.

\[X_{1} \sim Exp(\frac{1}{5}), \qquad X_{2} \sim Exp(\frac{1}{5})\]

The system fails if \(X_{1}<10\) and \(X_{2}<10\). Since these are independent events,

\[P(system fail) = P(X_{1}<10)*P(X_{2}<10)\]

  • Theoretical Probability

\[ \int_{0}^{10}\frac{1}{5}e^{-\frac{1}{5}x_{1}} \, dx_{1} * \int_{0}^{10}\frac{1}{5}e^{-\frac{1}{5}x_{2}} \, dx_{2} \]

I don’t want to integrate that.

pexp(10, 1/5)^2
## [1] 0.7476451
  • Estimated Probability using Simulation
time.of.failure <- rexp(10000, 1/5)
mean(time.of.failure<10)^2
## [1] 0.7511689

You try it

Customers arrive at a teller’s window at a uniform rate 5 per hour. Let \(X\) be the length in minutes of time that the teller has to wait until they see their first customer after starting their shift.

Customers arriving is a poisson process with \(\lambda = 5\) per hour. So the wait time in minutes is \(Exp(5/60)\).

  1. Calculate the mean and standard deviation of the wait time in minutes using both theoretical formulas and simulation.

\[ E(X) = \frac{1}{\lambda} = \frac{60}{5} = 12 \\ SD(X) = \sqrt{\frac{1}{\lambda^2}} = \frac{60}{5} = 12 \]

wait.time <- rexp(10000, 5/60)
mean(wait.time)
## [1] 12.01216
sd(wait.time)
## [1] 11.93927
  1. Find the probability that the teller waits less than 10 minutes for their first customer.

Use both theoretical formulas (pexp)

pexp(10, 5/60)
## [1] 0.5654018

and simulation (rexp).

mean(wait.time < 10)
## [1] 0.5678