Extra Practice with R in Probability

Practice Questions

Part A: No one fails Stat 20

Suppose \(X \sim \text{Poisson}(6)\), where \(X\) represents the number of students who earn an A+ in Stat 20, and \(Y \sim \text{Poisson}(5)\) represents the number of students who earn an F in Stat 20. We can assume that these two quantities are independent, since the class is not curved. What is the probability that no one fails Stat 20?

Expected output is below, as a hint

[1] 0.006737947
Show answer
dpois(x = 0, lambda = 5)

Explanation: 1. Identify the parameter for the number of failures: \(Y \sim \text{Poisson}(\lambda = 5)\). 2. Set up the mathematical expression for no failures: \(P(Y = 0) = \frac{e^{-5} \cdot 5^0}{0!} = e^{-5}\). 3. Compute the value in R using the probability mass function dpois().


Part B: At least 10 students earn an A+

Using the same context where \(X \sim \text{Poisson}(6)\) is the number of students who earn an A+ in Stat 20, what is the probability that at least 10 students earn an A+?

Expected output is below, as a hint

[1] 0.08392402
Show answer
ppois(q = 9, lambda = 6, lower.tail = FALSE)
[1] 0.08392402

Explanation: 1. Use the complement rule to express “at least 10”: \(P(X \ge 10) = 1 - P(X \le 9) = 1 - \sum_{k=0}^{9} \frac{e^{-6} \cdot 6^k}{k!}\). 2. We use ppois() with lower.tail = FALSE and set q = 9 to calculate \(P(X > 9)\), which evaluates precisely to \(P(X \ge 10)\).


Part C: Roulette Wheel Successes

Suppose you are playing roulette in Las Vegas and you bet on red each time (an American roulette wheel has 18 red, 18 black, and 2 green slots). You play 50 times and bet on red every single time. Let \(X\) be the number of times you win in 50 plays. What is the distribution of \(X\) and the probability that you win at least 12 times?

Expected output is below, as a hint

[1] 0.9998088
Show answer
pbinom(q = 11, size = 50, prob = 18/38, lower.tail = FALSE)
[1] 0.9998088

Explanation: 1. Distribution and Parameters: Each play is an independent trial with a constant probability of success \(p = \frac{18}{18 + 18 + 2} = \frac{18}{38}\). With \(n = 50\) trials, \(X \sim \text{Binomial}\left(n = 50, p = \frac{18}{38}\right)\). 2. Probability of at least 12 wins: We find \(P(X \ge 12) = 1 - P(X \le 11)\). 3. In R, we evaluate this upper tail with lower.tail = FALSE starting above 11.


Part D: Hypergeometric Error Analysis

An analyst tried to use the Hypergeometric distribution to simulate drawing spades (\(\spadesuit\)) from a standard 52-card deck with the following line of code, but received an error and the code would not run. Run the code below to see the behavior, and determine what caused the error.

rhyper(m = 13, n = 39, k = 60, nn = 1)
[1] NA
Show answer

Correct Answer: The sample size k is larger than the total population size m + n.

In R’s rhyper(nn, m, n, k) function: * m is the number of successes in the population (13 spades). * n is the number of failures (39 non-spades).

The total population size is \(m + n = 13 + 39 = 52\) cards. Because the Hypergeometric distribution models sampling without replacement, you cannot draw a sample size of \(k = 60\) from a deck of only 52 cards. R throws an error because \(k > m + n\) is physically impossible.


Part E: Validating the rhyper Syntax

To fix the previous mistake, the simulation was altered to count the number of spades (\(\spadesuit\)) in a 5-card hand drawn from a standard deck using the code block below. This code runs without error, but is it correct?

rhyper(m = 13, n = 52, k = 5, nn = 1)
[1] 0
Show answer
rhyper(nn = 1, m = 13, n = 39, k = 5)
[1] 1

Explanation: The code runs because a sample size of (k = 5) is smaller than the population size specified in the arguments ((13 + 52 = 65)). However, it is conceptually incorrect.

In R, n represents the number of failures in the population, not the total population. By setting n = 52, the code models a deck containing 13 spades and 52 non-spades (a 65-card deck). To correctly simulate a standard 52-card deck, n should be set to 39 \((52 - 13\)).


Part F: Coin Tossing Simulation

Let \(X\) be the number of heads in 10 tosses of a fair coin. What is the distribution of \(X\)? Write code to simulate 100 values from this distribution and plot its empirical histogram.

Show answer
set.seed(20)
heads<-replicate(n=100, sum(rbinom(n=10,size=1,prob=0.5)))
data.frame(heads) |> 
  group_by(heads) |>
  summarize(prop = n() / 100) |>
  ggplot(aes(x = heads, y = prop)) +  # Removed factor() here
  geom_col() + 
  scale_x_continuous(breaks = 0:10, limits = c(-0.5, 10.5))

Explanation: Since each toss is an independent trial with a success probability \(p = 0.5\), the total number of heads in \(n = 10\) tosses follows a Binomial distribution: \(X \sim \text{Binomial}(n = 10, p = 0.5)\). We generate random draws using rbinom(n=10,size=1,prob=0.5). This will return a vector of 10 zeros or ones. The number of heads is then sum(rbinom(n=10,size=1,prob=0.5)). The replicate(n=100, sum(rbinom(n=10,size=1,prob=0.5)) does this 100 times giving you a vector of a 100 zeros and ones.