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 .

\[ F(y)= P(Y\le y) = 1-P(Y< y) = 1-P(min(x_1,x_2,...x_n)>y) \]

\[ P(y)=1-P(x_1>y)*P(x_2>y)*...*P(x_n-1>y)*P(x_n>y) \]

\[ p(x_i>y)=1-\frac{y-1}{k-1} \]

\[ F(y)= P(Y=y) \\ = P(Y\le y) - P(Y\le y-1) \\ = 1-P(Y< y) \\ = 1-(1-\frac{y-1}{k-1})^{n} \\ = \frac{(k-y+1)^{n} - (k-y)^{n}}{k^{n}} \]


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..)

\[ P(X=k)=(1-p)^{k-1} \\ E[X]=\frac{1}{p} \\ Var[X]=\frac{1-p}{p^{2}} \]

# For a geometric distribution, where X is the number of trials up to and including the first success, then  P(X=k) = p * q^(k-1). The expected value = 1/p
# Convert years to months, or days. We'd need 8 year or 96 months. We'd need no failures through the first 8 years. We want the probability that the machine doesn't fail within the first 96 months. Since the expected value is 1/10, the probability of it not failing is 0.9, so we can model it as 0.9 ^ 8. We know this machine will fail every 120 months; 1/120 - (1/120)^96


#Probability of machine failure each year
p_fail <- 0.1
# Expected value 
ev_geom <- 1/p_fail
# Standard Deviation
sd_geom <- sqrt((1-p_fail)/(p_fail^2))

# Probability
prob_geom <- pgeom(8, p_fail, lower.tail = FALSE)

cat("The probability is: ", prob_geom)
## The probability is:  0.3874205
cat("The expected value is: ", ev_geom)
## The expected value is:  10
cat("The standard deviation is: ", sd_geom)
## The standard deviation is:  9.486833

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.

\[ P(X\le k)= e^{-\lambda x} \\ E[X]=\frac{1}{\lambda} \\ Var[x]=\frac{1}{\lambda^{2}} \]

# This is the same as the geometric distribution on a continuous space, so break years down into a finer state space. The Memoryless property holds here as well, we don't care what happened before. (Prob textbook, Pg. 206)
# Let T be an exponentially distributed random variable with parameter λ. If x>=0, then F(x) = P(T<= x) = 1-e^-λx.
# For P(X <=k) = 1-e^-k/mu where mu = 1/λ
# The probability is e^-λt * (λt)^n /n!.   

# Probability of machine failing
p_fail <- 0.1
# Expected value = λ
ev_exp <- 10
# Standard Deviation = λ 
sd_exp <- 10


# Probability
# This is the probability for the machine failing in 8 years or less, so we subtract it from 1 to get the prob of failing after 8 years.
prob_exp <- 1 -  pexp(8, 0.1)

cat("The probability is: ", prob_exp)
## The probability is:  0.449329
cat("The expected value is: ", ev_exp)
## The expected value is:  10
cat("The standard deviation is: ", sd_exp)
## The standard deviation is:  10

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)

\[ P(success)=(nCk)P^{n}(1-p)^{n-k} \\ E[X]=np \\ Var[X]=np(1-p) \]

# For a binomial distribution, a coin is tossed n times with a probability of heads being p, and q is 1-p, then P(X=k | n,p) = nCk * p^k * q^(n-k) where nCk = n!/(k! *(n-k)!) = dbinom(k,n,p).  Expected value = np. Var(X) = npq, and sd = var^0.5

# Since a binomial needs a specific number of trials, model 0 success within 8 trials (years), with prob of 0.1 as the chance of failure in a given year 

# Probability of machine failing
p_fail <- 0.1
n <- 8 #trials
k <- 0 #successes
p_fail <- 0.1

# Expected value 
ev_binom <- n*p_fail
# Standard Deviation
sd_binom <- sqrt(n*p_fail*(1-p_fail))


# Probability
prob_binom <- pbinom(0, size=8, prob=0.1)

cat("The probability is: ", prob_binom)
## The probability is:  0.4304672
cat("The expected value is: ", ev_binom)
## The expected value is:  0.8
cat("The standard deviation is: ", sd_binom)
## The standard deviation is:  0.8485281

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.

\[ P(X=x)=\frac{\lambda^{x}e^{-\lambda}}{x!} \\ E[X]=\lambda \\ Var[X]=\lambda \]

# On the average, there are λt occurrences in a time interval of length t. If this time interval is divided into n sub intervals, then using the Bernoulli trials interpretation, there should be np occurrences, so p = λt/n. P(X=k) = λ^k * e^-λ  / k!
# The mean = λ, and the variance = λ

# Poisson is used for occurences over some time interval, and we want 0 for the first 8 years. λ = 0.8 since the probability of each is 0.1


# Probability of machine failing
p_fail <- 0.1
# Expected value 
ev_poiss<- 0.8
# Standard Deviation
sd_poiss <- sqrt(0.8)


# Probability
prob_poiss <- ppois(0, lambda = 0.8)

cat("The probability is: ", prob_poiss)
## The probability is:  0.449329
cat("The expected value is: ", ev_poiss)
## The expected value is:  0.8
cat("The standard deviation is: ", sd_poiss)
## The standard deviation is:  0.8944272