library(MASS)
Bayesian Formula:
\[P(A | B) = \frac{P(B | A) \cdot P(A)}{P(B)}\] where:
p_disease <- 0.001
sensitivity <- 0.96
specificity <- 0.98
false_pos_rate <- 1 - specificity
false_pos_rate
## [1] 0.02
p_positive <- sensitivity * p_disease + false_pos_rate * (1 - p_disease)
p_positive
## [1] 0.02094
p_disease_given_positive <- sensitivity * p_disease / p_positive
p_disease_given_positive
## [1] 0.04584527
cat("Probability of having the disease given a positive test result:",
round(p_disease_given_positive * 100, 2), "%\n")
## Probability of having the disease given a positive test result: 4.58 %
From the previous calculations using Bayes’ theorem, we know that the probability of having the disease given a positive test result is about 4.38%. So out of 100,000 individuals, we can expect about 4,380 to test positive for the disease.
total_cost <- 100000 * 4380
total_cost
## [1] 4.38e+08
Binomial Formula: \[P(X = k) = {n \choose k} p^k (1 - p)^{n - k}\] where:
The notation (n x) is equivalent to the notation P(X = x) in the standard binomial distribution formula. The mean and variance of the binomial distribution can be computed using this notation as:
months = 24
inspection_prob = .05
p_2 <- dbinom(2, 24, 0.05)
p_2_or_more <- 1 - pbinom(1, 24, 0.05)
p_2_or_more
## [1] 0.3391827
p_less_than_2 <- pbinom(1, 24, 0.05)
p_less_than_2
## [1] 0.6608173
expected <- 24 * 0.05
expected
## [1] 1.2
sd <- sqrt(24 * 0.05 * 0.95)
sd
## [1] 1.067708
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?
The Poisson distribution is the probability distribution of independent event occurrences in an interval. If λ is the mean occurrence per interval, then the probability of having x occurrences within a given interval is:
Poisson distribution formula:
\[P(X = k) = \frac{\lambda^k e^{-\lambda}}{k!}\]
lambda<- 10
K<- 3
prob_3 <- dpois(K, lambda)
prob_3
## [1] 0.007566655
ppois(9, lambda=10) # lower tail
## [1] 0.4579297
ppois(9, lambda=10, lower=FALSE) # upper tail
## [1] 0.5420703
t <- 8
expected_arrive <- lambda * t
expected_arrive
## [1] 80
\[SD = sqrt(\lambda)\]
sd_poisson <- sqrt(lambda)
sd_poisson
## [1] 3.162278
Percent utilization of 3 providers each seeing 24 patients per day
utilization <- 30 * 24 / (3 * 24)
utilization
## [1] 10
cat("Percent utilization of 3 providers each seeing 24 patients per day:", utilization * 100, "%\n")
## Percent utilization of 3 providers each seeing 24 patients per day: 1000 %
The probability mass function (PMF) for the hypergeometric distribution is:
P(X = k) = [K choose k] * [(N - K) choose (n - k)] / [N choose n]
where: * X is the random variable representing the number of objects of interest in the sample * k is a specific value of X * [a choose b] represents the binomial coefficient, which is the number of ways to choose b objects from a set of a objects
N <- 30 # Total population size
K <- 15 # Number of objects of interest
n <- 6 # Sample size
k <- 5 # Number of objects of interest in the sample
pmf <- choose(K, k) * choose(N-K, n-k) / choose(N, n)
pmf
## [1] 0.07586207
mean <- n * (K / N)
mean
## [1] 3
variance <- n * (K / N) * (1 - K / N) * ((N - n) / (N - 1))
variance
## [1] 1.241379
Geometric distribution Formula \[P(X = k) =(1 - p)^k-1 p\]
prob_injury = .001
hours = 1200
crash_1200 = pgeom(hours,prob_injury)
crash_1200
## [1] 0.6992876
crash_15_mths = pgeom(hours * (15/12),prob_injury)
crash_15_mths
## [1] 0.7772602
crash_next_100 = pgeom(100,prob_injury)
crash_next_100
## [1] 0.09611265
p_more_than_two_failures <- 1 - ppois(2, 1/1000 * 1000)
cat("Probability of more than two failures in 1000 hours:", p_more_than_two_failures, "\n")
## Probability of more than two failures in 1000 hours: 0.0803014
expected_value <- 1/1000 * 1000
cat("Expected value:", expected_value, "\n")
## Expected value: 1
# Define the minimum and maximum waiting times
a <- 0
b <- 30
p_wait_5_given_10 <- (1 - punif(15, min = 10, max = b)) / (1 - punif(10, min = a, max = b))
p_wait_5_given_10
## [1] 1.125
p_wait_more_than_10 <- 1 - punif(10, min = a, max = b)
p_wait_more_than_10
## [1] 0.6666667
expected_waiting_time <- (a + b) / 2
expected_waiting_time
## [1] 15
pdf_uniform <- dunif(0:30, min = a, max = b)
pdf_uniform
## [1] 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333
## [7] 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333
## [13] 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333
## [19] 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333
## [25] 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333 0.03333333
## [31] 0.03333333
we can use the properties of the exponential distribution. Specifically, if a random variable X follows an exponential distribution with parameter λ, then the expected value of X is 1/λ and the standard deviation is also 1/λ.
lambda <- 1/10
# Calculate the expected failure time
expected_failure_time <- 1/lambda
expected_failure_time
## [1] 10
sd_failure_time <- 1/lambda
sd_failure_time
## [1] 10
prob_fail_after_8_years <- exp(-lambda*8)
prob_fail_after_8_years
## [1] 0.449329
prob_fail_in_2_years_given_8_years <- exp(-lambda*2)
prob_fail_in_2_years_given_8_years
## [1] 0.8187308
https://www.youtube.com/watch?v=OByl4RJxnKA&t=678s https://www.3blue1brown.com/lessons/better-bayes https://www.youtube.com/watch?v=3PWKQiLK41M&t=1022s https://www.youtube.com/watch?v=m0o-585xwW0 https://www.youtube.com/watch?v=BCeFgnh6A1U https://r-coder.com/uniform-distribution-r/#The_punif_function https://r-coder.com/exponential-distribution-r/