title: “R Program Assignment” author: “group 5” date: “2024-02-16” output: html_document ?publish
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{# Given mean value} lambda_a <- 4
prob_a <- exp(-lambda_a) cat(“Probability of no typos in a 300-page book:”, prob_a, “”)
prob_b <- 1 - ppois(5, lambda_a) cat(“Probability of more than 5 typos in a 300-page book:”, prob_b, “”)
prob_c <- exp(-lambda_a) cat(“Probability of no typos in a 157-page book:”, prob_c, “”)
simulated_no_typos <- sum(rpois(num_simulations, lambda_a) == 0) / num_simulations cat(“Simulated probability of no typos:”, simulated_no_typos, “”)
simulated_more_than_5_typos <- sum(rpois(num_simulations, lambda_a) > 5) / num_simulations cat(“Simulated probability of more than 5 typos:”, simulated_more_than_5_typos, “”)
```r
# Question 2 part 1
# Probability of more than seven accidents in May
# avg no of accidents =4
# denote a rv X for no of accidents to find p(X=7)
# Function to calculate Poisson probability
poisson_prob <- function(lambda, k) {
exp(-lambda) * (lambda^k) / factorial(k)
}
# (a) Probability of more than seven accidents in May
lambda <- 4
prob_more_than_7 <- 1 - sum(poisson_prob(lambda, 0:7))
# Question 2 part 2
# probability of not more than 3 accidens in May and June
# denote Y as the total no of accidents in May and June combined
# (b) Probability of no more than three accidents in May and June
prob_no_more_than_3 <- sum(poisson_prob(lambda, 0:3))^2
# Display results
cat("Probability of more than seven accidents in May:", prob_more_than_7, "\n")
## Probability of more than seven accidents in May: 0.05113362
cat("Probability of no more than three accidents in May and June:", prob_no_more_than_3, "\n")
## Probability of no more than three accidents in May and June: 0.1878963
# Question 3
# Function to calculate Poisson probability
poisson_prob <- function(lambda, k) {
exp(-lambda) * (lambda^k) / factorial(k)
}
# (a) Probability of zero calls in one minute
lambda_a <- 2
prob_zero_calls <- poisson_prob(lambda_a, 0)
# (b) Probability of less than five calls in one minute
prob_less_than_5_calls <- sum(poisson_prob(lambda_a, 0:4))
# (c) Probability of less than six calls in one hour
lambda_c <- 120
prob_less_than_6_calls <- sum(poisson_prob(lambda_c, 0:5))
# Display results
cat("Probability of zero calls in one minute:", prob_zero_calls, "\n")
## Probability of zero calls in one minute: 0.1353353
cat("Probability of less than five calls in one minute:", prob_less_than_5_calls, "\n")
## Probability of less than five calls in one minute: 0.947347
cat("Probability of less than six calls in one hour:", prob_less_than_6_calls, "\n")
## Probability of less than six calls in one hour: 1.658476e-44
## question 1, Normal distribution
# Given data
mean_income <- 25000
std_dev_income <- 10000
poverty_level <- 10000
middle_income_lower <- 20000
middle_income_upper <- 30000
# Calculate z-scores for poverty level and middle income range
z_poverty <- (poverty_level - mean_income) / std_dev_income
z_middle_lower <- (middle_income_lower - mean_income) / std_dev_income
z_middle_upper <- (middle_income_upper - mean_income) / std_dev_income
# Calculate probabilities using standard normal distribution
prob_poverty <- pnorm(z_poverty)
prob_middle <- pnorm(z_middle_upper) - pnorm(z_middle_lower)
# Convert probabilities to percentages
percentage_poverty <- prob_poverty * 100
percentage_middle <- prob_middle * 100
# Display results
cat("Percentage of population in poverty:", round(percentage_poverty, 2), "%\n")
## Percentage of population in poverty: 6.68 %
cat("Percentage benefiting from the new tax law (middle income):", round(percentage_middle, 2), "%\n")
## Percentage benefiting from the new tax law (middle income): 38.29 %
## question 5
# Given mean lifetime (average) in months
mean_lifetime <- 8
# (a) Probability between 3 and 12 months
prob_between_3_and_12 <- pexp(12, rate = 1/mean_lifetime) - pexp(3, rate = 1/mean_lifetime)
# (b) 95th percentile (quantile)
percentile_95 <- qexp(0.95, rate = 1/mean_lifetime)
# (c) Probability of lasting more than 25 months given 10 months already passed
prob_more_than_25_given_10 <- 1 - pexp(25, rate = 1/mean_lifetime, lower.tail = FALSE) / pexp(10, rate = 1/mean_lifetime)
# Display results
cat("Probability between 3 and 12 months:", round(prob_between_3_and_12, 4), "\n")
## Probability between 3 and 12 months: 0.4642
cat("95th percentile of the distribution (x):", round(percentile_95, 4), "\n")
## 95th percentile of the distribution (x): 23.9659
cat("Probability of lasting more than 25 months given 10 months already passed:", round(prob_more_than_25_given_10, 4), "\n")
## Probability of lasting more than 25 months given 10 months already passed: 0.9384
## question 6 Beta Probability distribution
# Given parameters
alpha <- 4
beta <- 2
# (a) Mean and Variance
mean_X <- alpha / (alpha + beta)
var_X <- (alpha * beta) / ((alpha + beta)^2 * (alpha + beta + 1))
cat("Mean of X:", round(mean_X, 4), "\n")
## Mean of X: 0.6667
cat("Variance of X:", round(var_X, 4), "\n")
## Variance of X: 0.0317
# (b) Probability of at least 80% computers sold
prob_at_least_80 <- 1 - pbeta(0.8, alpha, beta)
cat("Probability of at least 80% computers sold:", round(prob_at_least_80, 4), "\n")
## Probability of at least 80% computers sold: 0.2627