#problem1:

vec <- c(runif(1, 0, 1), runif(1, 0, 1), runif(1, 0, 1), runif(1, 0, 1),runif(1, 0, 1))
datalist <- list(vec)
count <- 2 
while (count < 1000001)
{
    vec <- c(runif(1, 0, 1), runif(1, 0, 1), runif(1, 0, 1), runif(1, 0, 1),runif(1, 0, 1))
    datalist[[count]] <- vec
    count = count + 1
}
minvals = c()
for(i in 1:length(datalist))
{
  minvals[i] <-  datalist[[i]][which.min(datalist[[i]])]
}
hist(minvals)

# it shows exponential distribution of Y on the histogram 
#Problem2: 

#A.Geometric model:

# P = 1/10 = 0.10 –> p to fail in one year
# Expected Value = 1/p = 1/.10 = 10 expected number of years to see first failure
# Standard Deviation = sqrt((1-p)/p^2) = 9.49

# Calculating P(X>8) using geometric model

p <- 1/10
n <- 8
failure <- 1 - (1 - p)^(n - 1) * (p)

expected <- 1 / p

standard_deviation <- sqrt((1 - p) / (p^2))

cat("Probability of failure after 8 years:", failure, "\n")
## Probability of failure after 8 years: 0.9521703
cat("Expected value:", expected, "\n")
## Expected value: 10
p <- .10
round(sqrt((1-p)/p^2),2)
## [1] 9.49
# B. expential model

#lambda = 1 failure every ten years = 1/10 = .10 failure every year

lambda = .10

expected_value <- 1/lambda
expected_value
## [1] 10
standard_deviation <- sqrt(lambda^-2)
standard_deviation
## [1] 10
e <- exp(1)
round(e^(-.10*8),2)
## [1] 0.45
#C. Bonomial model

p <- 1 / 10

years <- 8

prob <- dbinom(0, size = years, prob = p)

expected <- years * p

standard_deviation <- sqrt(years * p * (1 - p))

cat("Probability of failure after 8 years:", prob, "\n")
## Probability of failure after 8 years: 0.4304672
cat("EV:", expected, "\n")
## EV: 0.8
cat("Sd:", standard_deviation, "\n")
## Sd: 0.8485281
#D.Poisson model

mean <- 10

years <- 8

lambda <- years / mean

pois_failure <- dpois(0, lambda = lambda)

expect <- lambda

standard_deviation <- sqrt(lambda)

cat("Probability of failure after 8 years:", pois_failure, "\n")
## Probability of failure after 8 years: 0.449329
cat("EV:", expect, "\n")
## EV: 0.8
cat("SD:", standard_deviation, "\n")
## SD: 0.8944272