In this handout, I use ChatGPT 4.0 and the KeyMate.AI Search plugin to synthesize the information from the textbook, give examples, and create R code. All prompts are exact, but some of the output may be modified for correctness.
Synthesize the information in this website. Include all relevant formulas and definitions. https://www.econometrics-with-r.org/2.1-random-variables-and-probability-distributions.html
Expected Value (Mean) of a Discrete Random Variable: \[ E(X) = \sum_{i} x_i \times P(X = x_i) \]
Variance of a Discrete Random Variable: \[ Var(X) = E(X^2) - [E(X)]^2 \]
Expected Value (Mean) of a Continuous Random Variable: \[ E(X) = \int_{-\infty}^{\infty} x \times f(x) \, dx \]
Variance of a Continuous Random Variable: \[ Var(X) = \int_{-\infty}^{\infty} x^2 \times f(x) \, dx - [E(X)]^2 \]
Please give examples of calculating expected value and variance using these formulas.
Example: Suppose we roll a fair six-sided die. Let \(X\) be the number that shows up on the die.
Probability Mass Function (PMF): \[ P(X = x_i) = \frac{1}{6} \] for \(x_i\) in {1, 2, 3, 4, 5, 6}.
Expected Value (Mean): \[ E(X) = \sum_{i} x_i \times P(X = x_i) \] \[ E(X) = 1(\frac{1}{6}) + 2(\frac{1}{6}) + 3(\frac{1}{6}) + 4(\frac{1}{6}) + 5(\frac{1}{6}) + 6(\frac{1}{6}) \] \[ E(X) = \frac{21}{6} = 3.5 \]
Variance: \[ Var(X) = E(X^2) - [E(X)]^2 \] First, find \(E(X^2)\): \[ E(X^2) = \sum_{i} x_i^2 \times P(X = x_i) \] \[ E(X^2) = 1^2(\frac{1}{6}) + 2^2(\frac{1}{6}) + 3^2(\frac{1}{6}) + 4^2(\frac{1}{6}) + 5^2(\frac{1}{6}) + 6^2(\frac{1}{6}) \] \[ E(X^2) = \frac{91}{6} \] Now, plug into the variance formula: \[ Var(X) = \frac{91}{6} - (3.5)^2 \] \[ Var(X) = \frac{91}{6} - 12.25 = 2.9167 \]
Example: Let \(X\) be a random variable representing the time (in hours) it takes for a task to be completed, where \(X\) has a uniform distribution between 1 and 3 hours.
Probability Density Function (PDF): \[ f(x) = \frac{1}{3-1} = 0.5 \] for 1 ≤ \(x\) ≤ 3 and \(f(x) = 0\) otherwise.
Expected Value (Mean): \[ E(X) = \int_{1}^{3} x \times f(x) \, dx \] \[ E(X) = \int_{1}^{3} x(0.5) \, dx \] \[ E(X) = 0.5 \times \frac{x^2}{2} \Big|_1^3 \] \[ E(X) = 2 \]
Variance: \[ Var(X) = \int_{1}^{3} x^2 \times f(x) \, dx - [E(X)]^2 \] First, find \(E(X^2)\): \[ E(X^2) = \int_{1}^{3} x^2(0.5) \, dx \] \[ E(X^2) = 0.5 \times \frac{x^3}{3} \Big|_1^3 \] \[ E(X^2) = \frac{26}{3} \] Now, plug into the variance formula: \[ Var(X) = \frac{26}{3} - 2^2 \] \[ Var(X) = \frac{26}{3} - 4 = \frac{14}{3} = 4.6667 \]
Create examples of how to use R to plot the PDF and CDF functions.
For the six-sided die:
Probability Mass Function (PMF):
# PMF for a fair six-sided die
x <- c(1, 2, 3, 4, 5, 6)
pmf <- rep(1/6, 6)
# Plot PMF
barplot(pmf, names.arg=x, ylim=c(0, 1/6), main="PMF of a Fair Die", xlab="Die Face", ylab="Probability")
Cumulative Distribution Function (CDF):
# CDF for a fair six-sided die
cdf <- cumsum(pmf)
# Plot CDF
plot(x, cdf, type="s", ylim=c(0, 1), main="CDF of a Fair Die", xlab="Die Face", ylab="Cumulative Probability")
For the task completion time between 1 and 3 hours:
Probability Density Function (PDF):
# PDF for a uniform distribution between 1 and 3
x <- seq(1, 3, by=0.01)
pdf <- rep(0.5, length(x))
# Plot PDF
plot(x, pdf, type="l", ylim=c(0, 0.5), main="PDF of Task Completion Time", xlab="Time (hours)", ylab="Density")
Cumulative Distribution Function (CDF):
# CDF for a uniform distribution between 1 and 3
cdf <- pnorm(x, mean=2, sd=sqrt(4.6667/3))
# Plot CDF
plot(x, cdf, type="l", ylim=c(0, 1), main="CDF of Task Completion Time", xlab="Time (hours)", ylab="Cumulative Probability")
Note: In the CDF plot for the continuous random variable, I used the
pnorm function to generate the CDF values. This is because
the uniform distribution between 1 and 3 can be approximated using a
normal distribution with mean 2 and variance 4.6667/3. However, for a
precise uniform distribution, you’d use the punif
function.
You can run these scripts in R or RStudio to visualize the PDF and CDF plots for the given examples.