1. Let X1, X2, . . . , Xn be n mutually independent random variables, each of which is uniformly distributed on the integers from 1 to k. Let Y denote the minimum of the Xi’s. Find the distribution of Y.

# We need to create 2 functions here.
# First function returns the smallest random value between 1 - k
# Second function returns the chances of each possible smallest value 

# Function to find the smallest value among n random numbers
smallest_value <- function(n, k) {
  random_num <- sample(1:k, n, replace=TRUE)
  return(min(random_num))
}

# Function to calculate the chances of each possible smallest value in each trial
chances_of_smallest <- function(n, k, trials) {
  counts <- rep(0, k)
  
  for (i in 1:trials) {
    smallest <- smallest_value(n, k)
    counts[smallest] <- counts[smallest] + 1
  }
  
  probabilities <- counts / trials
  
  return(probabilities)
}

# Giving random values here
n <- 5
k <- 10
trials <- 10000

smallest_distribution <- chances_of_smallest(n, k, trials)

# The loop prints out the probability of smallest value being
for (i in 1:k) {
  cat(sprintf("%d: %f\n", i, smallest_distribution[i]))
}
## 1: 0.407400
## 2: 0.256200
## 3: 0.165500
## 4: 0.095800
## 5: 0.045200
## 6: 0.020000
## 7: 0.007400
## 8: 0.002300
## 9: 0.000100
## 10: 0.000100

2. Your organization owns a copier (future lawyers, etc.) or MRI (future doctors). This machine has a manufacturer’s expected lifetime of 10 years. This means that we expect one failure every ten years. (Include the probability statements and R Code for each part.).

a. What is the probability that the machine will fail after 8 years?. Provide also the expected value and standard deviation. Model as a geometric. (Hint: the probability is equivalent to not failing during the first 8 years..).

# Geometric Distribution: We are interested in the probability of the first success (failure)

p_monthly <- 1/120  # Monthly probability of failure
prob_geom_monthly <- 1 - pgeom(96, p_monthly)
exp_geom_monthly <- 1/p_monthly
sd_geom_monthly <- sqrt((1-p_monthly)/p_monthly^2)

prob_geom_monthly  # Probability of failure after 96 months
## [1] 0.4440935
exp_geom_monthly  # Expected value in months
## [1] 120
sd_geom_monthly  # Standard deviation in months
## [1] 119.499

b. What is the probability that the machine will fail after 8 years?. Provide also the expected value and standard deviation. Model as an exponential.

# Exponential Distribution: the time until the first success even (failure)

lambda_monthly <- 1/120  # Monthly rate of failure
prob_exp_monthly <- 1 - pexp(96, rate = lambda_monthly)
exp_exp_monthly <- 1/lambda_monthly
sd_exp_monthly <- 1/lambda_monthly

prob_exp_monthly  # Probability of failure after 96 months
## [1] 0.449329
exp_exp_monthly  # Expected value in months
## [1] 120
sd_exp_monthly  # Standard deviation in months
## [1] 120

c. What is the probability that the machine will fail after 8 years?. Provide also the expected value and standard deviation. Model as a binomial. (Hint: 0 success in 8 years).

# Binomial Distribution: for binomial we need to have fixed number of trials

n_months <- 96  # Number of months
p_monthly <- 1/120  # Monthly probability of failure
prob_binom_monthly <- 1 - dbinom(0, size = n_months, prob = p_monthly)
exp_binom_monthly <- n_months * p_monthly
sd_binom_monthly <- sqrt(n_months * p_monthly * (1-p_monthly))

prob_binom_monthly  # Probability of failure after 96 months
## [1] 0.5521747
exp_binom_monthly  # Expected value in months
## [1] 0.8
sd_binom_monthly  # Standard deviation in months
## [1] 0.8906926

d. What is the probability that the machine will fail after 8 years?. Provide also the expected value and standard deviation. Model as a Poisson.

# Poisson Distribution: models the number of events (failures) in fixed intervals of time,

lambda_96_months <- 96/120  # Rate of failure over 96 months
prob_poisson_monthly <- 1 - dpois(0, lambda = lambda_96_months)
exp_poisson_monthly <- lambda_96_months
sd_poisson_monthly <- sqrt(lambda_96_months)

prob_poisson_monthly  # Probability of failure after 96 months
## [1] 0.550671
exp_poisson_monthly  # Expected value in months
## [1] 0.8
sd_poisson_monthly  # Standard deviation in months
## [1] 0.8944272