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?
# Probability of being HIV positive given a positive test result
P_A1_given_B <- 0.96  
# Probability of not being HIV positive given a positive test result
P_A2_given_B <- 0.02  
# Probability of being HIV positive
P_A1 <- 0.001
# Probability of not being HIV positive
P_A2 <- 0.999  

# Calculate P(A1 | B) using Bayes' Theorem
P_B <- (P_A1_given_B * P_A1) + (P_A2_given_B * P_A2)
P_A1_given_B <- (P_A1_given_B * P_A1) / P_B

# Print the result
cat("P(A1 | B):", P_A1_given_B, "\n")
## P(A1 | B): 0.04584527
#Variables
num_individuals <- 100000
prevalence <- 0.001
cost_per_positive <- 100000
cost_per_test <- 1000

# Calculations
total_cost_test_administration <- cost_per_test * num_individuals
total_cost_positive_cases <- cost_per_positive * (num_individuals * prevalence)
total_first_year_cost <- total_cost_test_administration + total_cost_positive_cases

cat("Total first-year cost:", total_first_year_cost, "\n")
## Total first-year cost: 1.1e+08
  1. (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?
# Probability of exactly 2 inspections 
prob_exact_2 <- dbinom(2, 24, 0.05)
cat("Probability of exactly 2 inspections after 24 months:", prob_exact_2, "\n")
## Probability of exactly 2 inspections after 24 months: 0.2232381
# Probability of 2 or more inspections 
prob_2_or_more <- 1 - pbinom(1, 24, 0.05)
cat("Probability of 2 or more inspections after 24 months:", prob_2_or_more, "\n")
## Probability of 2 or more inspections after 24 months: 0.3391827
# Probability of fewer than 2 inspections 
prob_fewer_than_2 <- pbinom(1, 24, 0.05)
cat("Probability of fewer than 2 inspections after 24 months:", prob_fewer_than_2, "\n")
## Probability of fewer than 2 inspections after 24 months: 0.6608173
# Expected number of inspections
expected_inspections <- 24 * 0.05
cat("Expected number of inspections:", expected_inspections, "\n")
## Expected number of inspections: 1.2
# Standard deviation
std_dev <- sqrt(24 * 0.05 * 0.95)
cat("Standard deviation:", std_dev, "\n\n")
## Standard deviation: 1.067708
  1. (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?
# Probability of exactly 3 arrivals in one hour
prob_exact_3 <- dpois(3, 10)
cat("Probability of exactly 3 arrivals in one hour:", prob_exact_3, "\n")
## Probability of exactly 3 arrivals in one hour: 0.007566655
# Probability of more than 10 arrivals in one hour
prob_more_than_10 <- 1 - ppois(10, 10)
cat("Probability of more than 10 arrivals in one hour:", prob_more_than_10, "\n")
## Probability of more than 10 arrivals in one hour: 0.4169602
# Expected arrivals in 8 hours
expected_arrivals <- 10 * 8
cat("Expected arrivals in 8 hours:", expected_arrivals, "\n")
## Expected arrivals in 8 hours: 80
# Standard deviation
std_dev_poisson <- sqrt(10)
cat("Standard deviation for Poisson distribution:", std_dev_poisson, "\n\n")
## Standard deviation for Poisson distribution: 3.162278
  1. (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?
# Probability of selecting 5 nurses for the trips
prob_5_nurses <- dhyper(5, 15, 15, 6)
cat("Probability of selecting 5 nurses for the trips:", prob_5_nurses, "\n")
## Probability of selecting 5 nurses for the trips: 0.07586207
# Expected number of nurses
expected_nurses <- 6 * 15 / 30
cat("Expected number of nurses:", expected_nurses, "\n")
## Expected number of nurses: 3
# Expected number of non-nurses
expected_non_nurses <- 6 - expected_nurses
cat("Expected number of non-nurses:", expected_non_nurses, "\n\n")
## Expected number of non-nurses: 3
  1. (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?
p_injury_per_hour <- 0.001
total_hours_year <- 1200
hours_15_months <- 1500
hours_next_100 <- 1300

# Probability of being injured during the course of the year

prob_injured_year <- 1 - pgeom(total_hours_year, p_injury_per_hour)
cat("Probability of being injured during the course of the year:", prob_injured_year, "\n")
## Probability of being injured during the course of the year: 0.3007124
# Probability of being injured during the course of 15 months

prob_injured_15_months <- 1 - pgeom(hours_15_months, p_injury_per_hour)
cat("Probability of being injured during the course of 15 months:", prob_injured_15_months, "\n")
## Probability of being injured during the course of 15 months: 0.2227398
# Expected number of hours before being seriously injured
expected_hours_injury <- 1 / p_injury_per_hour
cat("Expected number of hours before being seriously injured:", expected_hours_injury, "\n")
## Expected number of hours before being seriously injured: 1000
# Probability of being injured in the next 100 hours given not injured in the first 1200 hours

prob_injured_next_100 <- ((pgeom(hours_next_100, p_injury_per_hour) - pgeom(total_hours_year, p_injury_per_hour)) * (1 - pgeom(total_hours_year, p_injury_per_hour))) / (1 - pgeom(total_hours_year, p_injury_per_hour))

cat("Probability of being injured in the next 100 hours given not injured in the first 1200 hours:", prob_injured_next_100, "\n")
## Probability of being injured in the next 100 hours given not injured in the first 1200 hours: 0.02863018
  1. 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?
lambda <- 1

# Probability of more than two failures in 1000 hours
prob_more_than_2_failures <- 1 - ppois(2, lambda)
cat("Probability of more than two failures in 1000 hours:", prob_more_than_2_failures, "\n")
## Probability of more than two failures in 1000 hours: 0.0803014
# Expected value
expected_value <- lambda
cat("Expected value:", expected_value, "\n")
## Expected value: 1
  1. 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?
start_time <- 0
end_time <- 30
waiting_time_10_minutes <- 10
waiting_time_another_5_minutes <- 15

# Probability that the patient will wait more than 10 minutes
prob_wait_more_than_10 <- 1 - punif(waiting_time_10_minutes, start_time, end_time)
cat("Probability that the patient will wait more than 10 minutes:", prob_wait_more_than_10, "\n")
## Probability that the patient will wait more than 10 minutes: 0.6666667
# Probability that the patient will wait at least another 5 minutes given already waited 10 minutes
prob_wait_another_5_given_10 <- (1 - punif(waiting_time_another_5_minutes, waiting_time_10_minutes, end_time)) / (1 - punif(waiting_time_10_minutes, waiting_time_10_minutes, end_time))
cat("Probability that the patient will wait at least another 5 minutes given already waited 10 minutes:", prob_wait_another_5_given_10, "\n")
## Probability that the patient will wait at least another 5 minutes given already waited 10 minutes: 0.75
# Expected waiting time
expected_waiting_time <- 0.5 * (start_time + end_time)
cat("Expected waiting time:", expected_waiting_time, "\n")
## Expected waiting time: 15
  1. 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?
expected_lifetime <- 10
lambda <- 1 / expected_lifetime
time_after_8_years <- 8
time_next_2_years <- 2

# Expected failure time 

expected_failure_time <- 1 / lambda
cat("Expected failure time (mean):", expected_failure_time, "\n")
## Expected failure time (mean): 10
# Standard deviation

std_dev <- 1 / lambda
cat("Standard deviation:", std_dev, "\n")
## Standard deviation: 10
# Probability that the MRI will fail after 8 years

prob_fail_after_8_years <- 1 - pexp(time_after_8_years, lambda)
cat("Probability that the MRI will fail after 8 years:", prob_fail_after_8_years, "\n")
## Probability that the MRI will fail after 8 years: 0.449329
# Probability that it will fail in the next two years given 8 years have passed

prob_fail_next_2_given_8 <- ((pexp(time_after_8_years + time_next_2_years, lambda) - pexp(time_after_8_years, lambda)) * (1 - pexp(time_after_8_years, lambda))) / (1 - pexp(time_after_8_years, lambda))
cat("Probability that it will fail in the next two years given 8 years have passed:", prob_fail_next_2_given_8, "\n")
## Probability that it will fail in the next two years given 8 years have passed: 0.08144952