library(glue)
Bayesian inference is a statistical approach for updating beliefs about the probability of an event based on prior knowledge and new evidence.
Sensitivity shows how good a test is at finding sick people, specificity checks how good it is at finding healthy people, and prevalence is how often the disease is around. In Bayesian thinking, we use these to better guess the chance of having a sickness when the test says “yes” in a particular group.
sensitivity <- 0.96
specificity <- 0.98
prevalence <- 0.001
p_positive_given_disease <- sensitivity
p_positive_given_no_disease <- 1 - specificity
p_positive <- (p_positive_given_disease * prevalence) + (p_positive_given_no_disease * (1 - prevalence))
p_disease_given_positive <- (p_positive_given_disease * prevalence) / p_positive
message(glue("The probability of disease given a positive test is: {round(p_disease_given_positive,2)}"))
## The probability of disease given a positive test is: 0.05
In probability calculations, the code assesses the likelihood of a specific event happening.
This code finds the chance of having an inspection in 24 months by calculating the probability of not having an inspection each month.
p_inspection <- 0.05
n_months <- 24
p_result <- 1 - (1 - p_inspection)^n_months
message(glue("The probability for inspection in 24 months is: {round(p_result,2)}"))
## The probability for inspection in 24 months is: 0.71
Poisson Distribution models the number of events occurring in a fixed interval of time or space. It is used when events are rare and independent, providing probabilities for different numbers of occurrences based on the average rate.
dpois
: Probability of exactly num_arrivals
arrivals in one hour ppois
: Cumulative probability of up to
num_arrivals
arrivals in one hour
arrival_rate_per_hour <- 10
hours_in_one_hour <- 1
num_arrivals <- 5
p_pois <- dpois(num_arrivals, lambda = arrival_rate_per_hour * hours_in_one_hour)
cdf_pois <- ppois(num_arrivals, lambda = arrival_rate_per_hour * hours_in_one_hour)
message(glue("Probability of {num_arrivals} arrivals in 1 hour: {round(p_pois,2)}"))
## Probability of 5 arrivals in 1 hour: 0.04
message(glue("Cumulative probability of up to {num_arrivals} arrivals in 1 hour: {round(cdf_pois,3)}"))
## Cumulative probability of up to 5 arrivals in 1 hour: 0.067
Probability of selection considers the chance of choosing specific items from a group. It involves factors like the number of items, selection criteria, and the total population.
choose
: Function counts the ways to pick a specific
number of items from a group without caring about the order.
total_workers <- 30
total_nurses <- 15
total_non_nurses <- 15
trips_total <- 6
p_selection <- choose(total_nurses, 3) * choose(total_non_nurses, trips_total - 3) / choose(total_workers, trips_total)
message(glue("The probability of selecting exactly 3 nurses for the trips is: {round(p_selection,2)}"))
## The probability of selecting exactly 3 nurses for the trips is: 0.35
Probability of an event measures its likelihood. It’s a number
between 0 and 1, where 0 means impossible, and 1 means certain.
dpois
:Modeling event occurrences over time with Poisson
distribution, ideal for rare, independent events.
probability_per_hour <- 0.001
total_hours_per_year <- 1200
num_events <- 2
p_event <- dpois(num_events, lambda = probability_per_hour * total_hours_per_year)
message(glue("The probability of {num_events} events occurring in {total_hours_per_year} hours is: {round(p_event,2)}"))
## The probability of 2 events occurring in 1200 hours is: 0.22
Generator failure probability assesses time until next failure, often modeled by the exponential distribution—suitable for rare, independent events in a Poisson process.
rexp
: Model the time until the next failure, assuming an
exponential distribution.
failure_probability_per_hour <- 1 / 1000
hours_in_1000_hours <- 1000
time_until_failure <- rexp(1, rate = failure_probability_per_hour * hours_in_1000_hours)
message(glue("The time until the next failure is approximately {round(time_until_failure, 2)} hours."))
## The time until the next failure is approximately 1.58 hours.
Waiting time probability assesses the likelihood of events occurring within a specific time frame.
runif
: Generates a random waiting time between
lower_bound and upper_bound using a uniform distribution.
lower_bound <- 0
upper_bound <- 30
waiting_time <- runif(1, min = lower_bound, max = upper_bound)
message(glue("The waiting time is approximately {round(waiting_time,2)}"))
## The waiting time is approximately 20.61
Lifetime expectancy probability estimates the likelihood of living a
certain duration pexp
: Calculate the cumulative probability
of an exponential distribution up to a certain point. In this context,
it represents the probability of surviving up to 8 years.
expected_lifetime_mean <- 10
time_after_8_years <- 8
remaining_time <- 2
survival_prob_after_8_years <- pexp(time_after_8_years, rate = 1 / expected_lifetime_mean)
remaining_lifetime_expectancy <- remaining_time / (1 - survival_prob_after_8_years)
message(glue("The remaining lifetime expectancy after {time_after_8_years} years is approximately {round(remaining_lifetime_expectancy,2)}"))
## The remaining lifetime expectancy after 8 years is approximately 4.45