Random Variables - Normal Continuous
Ideas in code
Useful functions
Uniform\((a, b)\)
dunif
computes the density \(f(x)\) of \(X\) where \(f(x) = \displaystyle \frac{1}{b-a}\), for \(a < x < b\).
- Arguments:
x
: the value of \(x\) in \(f(x)\)min
: the parameter \(a\), the lower end point of the interval for \(X\). The default value ismin = 0
max
: the parameter \(b\), the upper end point of the interval for \(X\). The default value ismax = 1
punif
computes the cdf \(F(x) = P(X \le x)\) of \(X\).
- Arguments:
q
: the value of \(x\) in \(F(x)\)min
: the parameter \(a\), the lower end point of the interval for \(X\). The default value ismin = 0
max
: the parameter \(b\), the upper end point of the interval for \(X\). The default value ismax = 1
runif
generates random numbers from the \(Unif(a,b)\) distribution.
- Arguments:
n
: the size of the sample we wantmin
: the parameter \(a\), the lower end point of the interval for \(X\). The default value ismin = 0
max
: the parameter \(b\), the upper end point of the interval for \(X\). The default value ismax = 1
Exercises
- Suppose a random variable \(X\) follows a uniform distribution between 0 and 10. Use
dunif
to find the probability density of \(X\) at \(x = 5\)
- A machine part is produced with a length that varies uniformly between 20 cm and 30 cm. Use
dunif
to calculate the probability density for a part that measures exactly 25 cm.
- A student arrives at the night bus stop without checking the bus times. The bus arrives once every 15 minutes. Assuming the waiting time is uniformly distributed between 0 and 15 minutes, use
punif
to calculate the probability that the customer waits 10 minutes or less.
- The temperature on a specific day is uniformly distributed between 10°F and 30°F. What is the probability that the temperature will be more than 18°F?
- The time it takes for your teacher to return work to you follows a uniform distribution between 2 and 8 days. Generate and plot the wait time for 100 assignments.
Normal\((\mu, \sigma^2)\)
dnorm
computes the density \(f(x)\) of \(X \sim N(\mu,\sigma^2)\)
- Arguments:
x
: the value of \(x\) in \(f(x)\)mean
: the parameter \(\mu\), the mean of the distribution. The default value ismean = 0
sd
: the parameter \(\sigma\), the sd of the distribution. The default value issd = 1
pnorm
computes the cdf \(F(x) = P(X \le x)\) of \(X\).
- Arguments:
q
: the value of \(x\) in \(F(x)\)mean
: the parameter \(\mu\), the mean of the distribution. The default value ismean = 0
sd
: the parameter \(\sigma\), the sd of the distribution. The default value issd = 1
rnorm
generates random numbers from the Normal\((\mu, \sigma^2)\) distribution.
- Arguments:
n
: the size of the sample we wantmean
: the parameter \(\mu\), the mean of the distribution. The default value ismean = 0
sd
: the parameter \(\sigma\), the sd of the distribution. The default value issd = 1
1. Understanding the Normal Distribution using dnorm()
- Plot the density function of a normal distribution with a mean of 5 and a standard deviation of 2. How does changing the standard deviation to 1 affect the plot? How does changing the mean to 7 change the plot?
- Calculate the height of the normal density curve at
x = 6
for a normal distribution with mean = 5 and standard deviation = 3.
- What is the probability that a normally distributed random variable with a mean of 100 and standard deviation of 15 is less than 120?
- Suppose the heights of a population follow a normal distribution with a mean of 170 cm and a standard deviation of 10 cm. What proportion of people are taller than 180 cm? What proportion of people are taller than my sister, who is 192 cm (6 foot 3)? What about who are taller than you? What about who are shorter than you?
- Calculate the probability that a value is between 60 and 80 for a normal distribution with mean = 70 and sd = 10.
3. Generating Random Samples using rnorm()
Generate random samples from an normal distribution with mean 0 and sd 20.
Example
Let’s verify the empirical rule for the standard normal random variable:
Note that (for example) \(P(-1 \le X \le 1) = F(1) - F(-1)\):
The argument prob
in the function sample()
We have seen the function sample()
, but so far, have only used it when we were sampling uniformly at random. That is, all the values are equally likely. We can sample according to a weighted probability, though, by putting in a vector of probabilities. Let’s look at the example of net gain while betting on red on a roulette spin. Recall that if we bet a dollar on red, then our net gain is \(+1\) with a probability of \(\displaystyle \frac{18}{38}\) and \(-1\) with a probability of \(\displaystyle \frac{20}{38}\).
- Exercise: Update the code below to look at the probability of landing on a green in roulette (*N.B. There are only 2 green in roulette)
Here is a simulation showing the Central Limit Theorem at work, with the empirical distribution becoming gradually more bell-shaped. Net gain is the sum of \(n\) draws with replacement from the vector gain
defined above using the prob_gain
vector.