1. (Bayesian). A new test for multinucleoside-resistant (MNR) human immunodeficiency virus type 1 (HIV-1) variants was recently developed. The test maintains 96% sensitivity, meaning that, for those with the disease, it will correctly report “positive” for 96% of them. The test is also 98% specific, meaning that, for those without the disease, 98% will be correctly reported as “negative.” MNR HIV-1 is considered to be rare (albeit emerging), with about a .1% or .001 prevalence rate. Given the prevalence rate, sensitivity, and specificity estimates, what is the probability that an individual who is reported as positive by the new test actually has the disease? If the median cost (consider this the best point estimate) is about $100,000 per positive case total and the test itself costs $1000 per administration, what is the total first-year cost for treating 100,000 individuals?

# 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

2. (Binomial). The probability of your organization receiving a Joint Commission inspection in any given month is .05. What is the probability that, after 24 months, you received exactly 2 inspections? What is the probability that, after 24 months, you received 2 or more inspections? What is the probability that your received fewer than 2 inspections? What is the expected number of inspections you should have received? What is the standard deviation?

# 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

3. (Poisson). You are modeling the family practice clinic and notice that patients arrive at a rate of 10 per hour. What is the probability that exactly 3 arrive in one hour? What is the probability that more than 10 arrive in one hour? How many would you expect to arrive in 8 hours? What is the standard deviation of the appropriate probability distribution? If there are three family practice providers that can see 24 templated patients each day, what is the percent utilization and what are your recommendations?

# 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

4. (Hypergeometric). Your subordinate with 30 supervisors was recently accused of favoring nurses. 15 of the subordinate’s workers are nurses and 15 are other than nurses. As evidence of malfeasance, the accuser stated that there were 6 company-paid trips to Disney World for which everyone was eligible. The supervisor sent 5 nurses and 1 non-nurse. If your subordinate acted innocently, what was the probability he/she would have selected five nurses for the trips? How many nurses would we have expected your subordinate to send? How many non-nurses would we have expected your subordinate to send?

# 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

5. (Geometric). The probability of being seriously injured in a car crash in an unspecified location is about .1% per hour. A driver is required to traverse this area for 1200 hours in the course of a year. What is the probability that the driver will be seriously injured during the course of the year? In the course of 15 months? What is the expected number of hours that a driver will drive before being seriously injured? Given that a driver has driven 1200 hours, what is the probability that he or she will be injured in the next 100 hours?

# 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

6. You are working in a hospital that is running off of a primary generator which fails about once in 1000 hours. What is the probability that the generator will fail more than twice in 1000 hours? What is the expected value?

# 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

7. A surgical patient arrives for surgery precisely at a given time. Based on previous analysis (or a lack of knowledge assumption), you know that the waiting time is uniformly distributed from 0 to 30 minutes. What is the probability that this patient will wait more than 10 minutes? If the patient has already waited 10 minutes, what is the probability that he/she will wait at least another 5 minutes prior to being seen? What is the expected waiting time?

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