# Sensitivity (true positive rate)
s <- 0.96
# Specificity (true negative rate)
t <- 0.98
# Prevalence rate
p <- 0.001
# Probability of testing positive given the disease (true positive)
a <- s
# Probability of testing negative given no disease (true negative)
b <- t
# Probability of having the disease given a positive test (using Bayes' theorem)
c <- (a * p) / ((a * p) + ((1 - t) * (1 - p)))
# Total first-year cost for treating 100,000 individuals
cost <- 100000 * (c * 100000 + (1 - c) * 0)
cat("Probability that an individual who tests positive actually has the disease:", c, "\n")
## Probability that an individual who tests positive actually has the disease: 0.04584527
cat("Total first-year cost for treating 100,000 individuals:", cost, "\n")
## Total first-year cost for treating 100,000 individuals: 458452722
# Number of trials
n <- 24
# Probability of success
p <- 0.05
# Number of inspections
k <- 2
# Probability mass function of receiving exactly 2 inspections in 24 months
pmf <- dbinom(k, size = n, prob = p)
# Cumulative distribution function of receiving 2 or more inspections in 24 months
cdf_2_or_more <- pbinom(k - 1, size = n, prob = p, lower.tail = FALSE)
# Probability of receiving fewer than 2 inspections in 24 months
cdf_fewer_than_2 <- pbinom(k - 1, size = n, prob = p)
# Expected number of inspections
expected <- n * p
# Standard deviation
sd <- sqrt(n * p * (1 - p))
cat("Probability of receiving exactly 2 inspections in 24 months:", pmf, "\n")
## Probability of receiving exactly 2 inspections in 24 months: 0.2232381
cat("Probability of receiving 2 or more inspections in 24 months:", cdf_2_or_more, "\n")
## Probability of receiving 2 or more inspections in 24 months: 0.3391827
cat("Probability of receiving fewer than 2 inspections in 24 months:", cdf_fewer_than_2, "\n")
## Probability of receiving fewer than 2 inspections in 24 months: 0.6608173
cat("Expected number of inspections in 24 months:", expected, "\n")
## Expected number of inspections in 24 months: 1.2
cat("Standard deviation of the number of inspections in 24 months:", sd, "\n")
## Standard deviation of the number of inspections in 24 months: 1.067708
# Rate of arrivals per hour
l <- 10
# Number of arrivals
k <- 3
# Probability mass function of exactly 3 arrivals in one hour
pmf_3 <- dpois(k, l)
# Cumulative distribution function of more than 10 arrivals in one hour
cdf_more_than_10 <- 1 - ppois(10, l)
# Expected number of arrivals in 8 hours
expected_8_hours <- l * 8
# Standard deviation of the number of arrivals per hour
sd_arrivals <- sqrt(l)
cat("Probability of exactly 3 arrivals in one hour:", pmf_3, "\n")
## Probability of exactly 3 arrivals in one hour: 0.007566655
cat("Probability of more than 10 arrivals in one hour:", cdf_more_than_10, "\n")
## Probability of more than 10 arrivals in one hour: 0.4169602
cat("Expected number of arrivals in 8 hours:", expected_8_hours, "\n")
## Expected number of arrivals in 8 hours: 80
cat("Standard deviation of the number of arrivals per hour:", sd_arrivals, "\n")
## Standard deviation of the number of arrivals per hour: 3.162278
# Total number of supervisors
total_supervisors <- 30
# Number of nurses among the supervisors
nurses <- 15
# Number of non-nurses among the supervisors
non_nurses <- total_supervisors - nurses
# Total number of trips
total_trips <- 6
# Number of nurses sent for the trips
nurses_sent <- 5
# Number of non-nurses sent for the trips
non_nurses_sent <- total_trips - nurses_sent
# Probability of selecting exactly 5 nurses for the trips
prob_5_nurses <- dhyper(x = nurses_sent, m = nurses, n = non_nurses, k = total_trips)
# Expected number of nurses selected for the trips
expected_nurses <- (nurses / total_supervisors) * total_trips
# Expected number of non-nurses selected for the trips
expected_non_nurses <- (non_nurses / total_supervisors) * total_trips
cat("Probability of selecting exactly 5 nurses for the trips:", prob_5_nurses, "\n")
## Probability of selecting exactly 5 nurses for the trips: 0.07586207
cat("Expected number of nurses selected for the trips:", expected_nurses, "\n")
## Expected number of nurses selected for the trips: 3
cat("Expected number of non-nurses selected for the trips:", expected_non_nurses, "\n")
## Expected number of non-nurses selected for the trips: 3
# Probability of being seriously injured per hour
p <- 0.001
# Number of hours in a year
h_year <- 1200
# Number of hours in 15 months
h_15_months <- 15 * 30 * 24
# 1. Probability of being seriously injured during the year
prob_injured_year <- 1 - (1 - p) ^ h_year
# 2. Probability of being seriously injured during 15 months
prob_injured_15_months <- 1 - (1 - p) ^ h_15_months
# 3. Expected number of hours until serious injury
expected_hours_until_injury <- 1 / p
# Probability of being injured in the next 100 hours given 1200 hours already driven
prob_injured_next_100_hours <- 1 - (1 - p) ^ 100
cat("Probability of being seriously injured during the year:", prob_injured_year, "\n")
## Probability of being seriously injured during the year: 0.6989866
cat("Probability of being seriously injured during 15 months:", prob_injured_15_months, "\n")
## Probability of being seriously injured during 15 months: 0.9999797
cat("Expected number of hours until serious injury:", expected_hours_until_injury, "\n")
## Expected number of hours until serious injury: 1000
cat("Probability of being injured in the next 100 hours given 1200 hours already driven:", prob_injured_next_100_hours, "\n")
## Probability of being injured in the next 100 hours given 1200 hours already driven: 0.09520785
# Probability of failure per hour
p <- 1 / 1000
# Number of hours
h <- 1000
# Probability of the generator failing more than twice in 1000 hours
prob_fail_more_than_twice <- 1 - pgeom(2, prob = p)
# Expected number of failures
expected_failures <- 1 / p
cat("Probability of failing more than twice in 1000 hours:", prob_fail_more_than_twice, "\n")
## Probability of failing more than twice in 1000 hours: 0.997003
cat("Expected number of failures:", expected_failures, "\n")
## Expected number of failures: 1000
min_time <- 0 # Minimum waiting time in minutes
max_time <- 30 # Maximum waiting time in minutes
range_time <- max_time - min_time # Total waiting time range
# Probability that the patient will wait more than 10 minutes
prob_wait_more_than_10 <- (max_time - 10) / range_time
# If the patient has already waited 10 minutes, probability of waiting at least another 5 minutes
prob_wait_at_least_another_5 <- (max_time - 10 - 5) / range_time
# Expected waiting time
expected_waiting_time <- (max_time + min_time) / 2
cat("Probability of waiting more than 10 minutes:", prob_wait_more_than_10, "\n")
## Probability of waiting more than 10 minutes: 0.6666667
cat("Probability of waiting at least another 5 minutes given 10 minutes have already passed:", prob_wait_at_least_another_5, "\n")
## Probability of waiting at least another 5 minutes given 10 minutes have already passed: 0.5
cat("Expected waiting time:", expected_waiting_time, "minutes\n")
## Expected waiting time: 15 minutes
##8. Your hospital owns an old MRI, which has a manufacturer’s lifetime of about 10 years (expected value). Based on previous studies, we know that the failure of most MRIs obeys an exponential distribution. What is the expected failure time? What is the standard deviation? What is the probability that your MRI will fail after 8 years? Now assume that you have owned the machine for 8 years. Given that you already owned the machine 8 years, what is the probability that it will fail in the next two years?
# Manufacturer's lifetime of the MRI
E <- 10
# Exponential distribution parameter for the MRI failure
l <- 1 / E
# 1. Expected failure time
E_failure <- 1 / l
# 2. Standard deviation of the exponential distribution
sd_exp <- 1 / l
# 3. Probability of MRI failure after 8 years
p_failure_8_years <- pexp(8, rate = l, lower.tail = FALSE)
# 4. Probability of MRI failure in the next two years given it has been owned for 8 years
p_failure_next_2_years <- pexp(2, rate = l)
cat("Expected failure time:", E_failure, "years\n")
## Expected failure time: 10 years
cat("Standard deviation of the exponential distribution:", sd_exp, "years\n")
## Standard deviation of the exponential distribution: 10 years
cat("Probability of MRI failure after 8 years:", p_failure_8_years, "\n")
## Probability of MRI failure after 8 years: 0.449329
cat("Probability of MRI failure in the next two years given it has been owned for 8 years:", p_failure_next_2_years, "\n")
## Probability of MRI failure in the next two years given it has been owned for 8 years: 0.1812692