Exercise 2.8: Flight Delays

For a certain airline, 30% of flights depart in the morning, 30% depart in the afternoon, and 40% depart in the evening. Overall, 15% of flights are delayed. Of the delayed flights, 40% are morning flights, 50% are afternoon flights, and 10% are evening flights.

Part A: Mine’s Morning Flight

Let \(M\) represent a morning flight and \(D\) represent a delayed flight.

# Given probabilities
p_morning <- 0.30
p_delayed <- 0.15
p_morning_given_delayed <- 0.40

# Joint probability of morning and delayed
p_morning_and_delayed <-
  p_morning_given_delayed * p_delayed

# Probability of delay given a morning flight
p_delayed_given_morning <-
  p_morning_and_delayed / p_morning

p_morning_and_delayed
## [1] 0.06
p_delayed_given_morning
## [1] 0.2

The joint probability that a flight is both a morning flight and delayed is:

\[ P(M \cap D) = P(M \mid D)P(D) = 0.40(0.15) = 0.06. \]

Therefore:

\[ P(D \mid M) = \frac{P(M \cap D)}{P(M)} = \frac{0.06}{0.30} = 0.20. \]

Thus, Mine’s morning flight has a 20% probability of being delayed.

Part B: Alicia’s Non-Delayed Flight

Alicia’s flight is not delayed. We want to calculate:

\[ P(M \mid D^c). \]

# Probability that a flight is not delayed
p_not_delayed <- 1 - p_delayed

# Probability that a flight is morning and not delayed
p_morning_and_not_delayed <-
  p_morning - p_morning_and_delayed

# Probability of morning given that the flight is not delayed
p_morning_given_not_delayed <-
  p_morning_and_not_delayed / p_not_delayed

p_not_delayed
## [1] 0.85
p_morning_and_not_delayed
## [1] 0.24
p_morning_given_not_delayed
## [1] 0.2823529

The probability that a flight is not delayed is:

\[ P(D^c)=1-0.15=0.85. \]

The probability that a flight is both a morning flight and not delayed is:

\[ P(M \cap D^c)=0.30-0.06=0.24. \]

Therefore:

\[ P(M \mid D^c) = \frac{P(M \cap D^c)}{P(D^c)} = \frac{0.24}{0.85} = 0.2824. \]

Thus, given that Alicia’s flight is not delayed, the probability that she is on a morning flight is approximately 28.24%.

Numerical Results

flight_results <- data.frame(
  Measurement = c(
    "P(Not delayed)",
    "P(Morning and delayed)",
    "P(Morning and not delayed)",
    "P(Morning | Not delayed)"
  ),
  Probability = c(
    p_not_delayed,
    p_morning_and_delayed,
    p_morning_and_not_delayed,
    p_morning_given_not_delayed
  )
)

flight_results$Percentage <-
  round(flight_results$Probability * 100, 2)

flight_results
##                  Measurement Probability Percentage
## 1             P(Not delayed)   0.8500000      85.00
## 2     P(Morning and delayed)   0.0600000       6.00
## 3 P(Morning and not delayed)   0.2400000      24.00
## 4   P(Morning | Not delayed)   0.2823529      28.24

Graphical Output

morning_probabilities <- c(
  "All flights" = p_morning,
  "Delayed flights" = p_morning_given_delayed,
  "Not-delayed flights" = p_morning_given_not_delayed
)

bar_positions <- barplot(
  morning_probabilities * 100,
  col = c("steelblue", "tomato", "seagreen"),
  ylim = c(0, 50),
  main = "Probability of a Morning Flight",
  xlab = "Flight group",
  ylab = "Probability (%)"
)

text(
  x = bar_positions,
  y = morning_probabilities * 100,
  labels = paste0(
    round(morning_probabilities * 100, 2),
    "%"
  ),
  pos = 3
)

The graph shows that morning flights account for 30% of all flights and 40% of delayed flights. Morning flights are disproportionately represented among delayed flights. Once we know that Alicia’s flight was not delayed, the probability that it was a morning flight decreases from 30% to approximately 28.24%.

Exercise 2.14: Late Bus

Li Qiang surveyed 20 commuters about the probability, denoted by \(\pi\), that the 8:30 a.m. bus would be late. Three commuters estimated \(\pi=0.15\), three estimated \(\pi=0.25\), eight estimated \(\pi=0.50\), three estimated \(\pi=0.75\), and three estimated \(\pi=0.85\).

Part A: Prior Model

The prior probability assigned to each possible value of \(\pi\) equals the proportion of commuters who selected that value.

# Possible probabilities of the bus being late
pi_values <- c(0.15, 0.25, 0.50, 0.75, 0.85)

# Number of commuters selecting each probability
commuters <- c(3, 3, 8, 3, 3)

# Convert the counts into prior probabilities
prior <- commuters / sum(commuters)

prior_model <- data.frame(
  Pi = pi_values,
  Commuters = commuters,
  Prior_Probability = prior
)

prior_model
##     Pi Commuters Prior_Probability
## 1 0.15         3              0.15
## 2 0.25         3              0.15
## 3 0.50         8              0.40
## 4 0.75         3              0.15
## 5 0.85         3              0.15
# Calculate the prior mean
prior_mean <- sum(pi_values * prior)

prior_mean
## [1] 0.5

The prior model is:

Possible value of \(\pi\) Prior probability
0.15 0.15
0.25 0.15
0.50 0.40
0.75 0.15
0.85 0.15

The prior model places its greatest probability, 0.40, on \(\pi=0.50\).

The prior mean is:

\[ E(\pi)=\sum \pi P(\pi)=0.50. \]

Therefore, before collecting her own data, Li Qiang’s expected probability that the bus would be late was 50%.

Part B: Posterior Model

Li Qiang observed the bus for 13 days, and it was late 3 times. A binomial likelihood is used to measure how well each possible value of \(\pi\) explains the observed data.

# Observed bus data
days <- 13
late_days <- 3

# Binomial likelihood for each possible value of pi
likelihood <- dbinom(
  x = late_days,
  size = days,
  prob = pi_values
)

# Multiply each prior probability by its likelihood
unnormalized_posterior <- prior * likelihood

# Normalize so the posterior probabilities add up to 1
posterior <- unnormalized_posterior /
  sum(unnormalized_posterior)

# Create the results table
posterior_results <- data.frame(
  Pi = pi_values,
  Prior = round(prior, 4),
  Likelihood = round(likelihood, 6),
  Posterior = round(posterior, 4)
)

posterior_results
##     Pi Prior Likelihood Posterior
## 1 0.15  0.15   0.190033    0.3553
## 2 0.25  0.15   0.251651    0.4705
## 3 0.50  0.40   0.034912    0.1740
## 4 0.75  0.15   0.000115    0.0002
## 5 0.85  0.15   0.000001    0.0000
# Calculate the posterior mode
posterior_mode <- pi_values[which.max(posterior)]

# Calculate the posterior mean
posterior_mean <- sum(pi_values * posterior)

posterior_mode
## [1] 0.25
posterior_mean
## [1] 0.2580942

The posterior probabilities are approximately:

Possible value of \(\pi\) Posterior probability
0.15 0.3553
0.25 0.4705
0.50 0.1740
0.75 0.0002
0.85 0.0000

The posterior model assigns its greatest probability, approximately 0.4705, to \(\pi=0.25\).

The posterior mean is:

\[ E(\pi \mid Y=3)=0.2581. \]

Therefore, after observing the bus, Li Qiang estimates that the probability of the bus being late is approximately 25.81%.

Part C: Comparison of Prior and Posterior Models

comparison <- rbind(
  Prior = prior,
  Posterior = posterior
)

barplot(
  comparison,
  beside = TRUE,
  names.arg = pi_values,
  col = c("steelblue", "orange"),
  ylim = c(0, 0.55),
  main = "Prior and Posterior Models for Bus Lateness",
  xlab = "Possible values of pi",
  ylab = "Probability",
  legend.text = c("Prior", "Posterior"),
  args.legend = list(x = "topright")
)

Before observing the bus, the prior model placed the greatest probability on \(\pi=0.50\), and the prior mean was 50%.

However, the bus was late on only 3 of the 13 observed days:

\[ \frac{3}{13}=0.2308. \]

The observed data shifted the posterior model toward lower values of \(\pi\). The posterior mode is 0.25, and the posterior mean decreased from 50% to approximately 25.81%. Posterior probabilities for \(\pi=0.75\) and \(\pi=0.85\) are nearly zero.

Li Qiang therefore learned that the bus is probably more reliable than the surveyed commuters initially believed. Based on the posterior model, the estimated probability that the bus will be late is approximately 25.81%.