Question2. The Public Service Answering Point (PSAP) in San Francisco employs \(19\) operators in \(8\)-hour shifts to process \(911\) calls. There are at least \(5\) operators always answering calls. The number of calls processed per operator can be modeled with a Poisson random variable with rate \(\lambda_0 =20\) calls per hour.

Quesion 2 (a) What is the probability an operator can process 20 calls in an hour? Repeat for 30 calls in an hour?

#Answer 2a
rate <- 20

x_20 <- 20
prob_20 <- dpois(x_20, rate)

x_30 <- 30
prob_30 <- dpois(x_30, rate)

cat("Probability pf processing 20 calls in an hour: ", prob_20, "\n")
## Probability pf processing 20 calls in an hour:  0.08883532
cat("Probability of processing 30 calls in an hour: ", prob_30, "\n")
## Probability of processing 30 calls in an hour:  0.008343536

Quesion 2 (b) Given that 240 calls occurred in an hour, what is the probability that the ten operators can process them all assuming they are split equally among the operators?

#Answer 2b
x_24 <- 24
single_op <- dpois(x_24, rate)
cat("Probability that a single operator can process 24 calls: ", single_op, "\n")
## Probability that a single operator can process 24 calls:  0.05573456
overall <- single_op^10
cat("Probability for ten operators to process 24 calls: ", overall, "\n")
## Probability for ten operators to process 24 calls:  2.892317e-13

Quesion 2 (c) Now, If the aggregate call rate per hour \(\lambda_c\) , is measured by \(\lambda_c = 85\) calls per hour, what is the probability of more than \(100\) calls in an hour

#Answer 2c
aggregate_rate <- 85
call <- 100
fewer <- ppois(call, aggregate_rate)
more_than <- 1 - fewer
cat("Probability of more than 100 call in an hour: ", more_than, "\n")
## Probability of more than 100 call in an hour:  0.04934533

Question 3. For the two random number generator below A and B(don’t forget to add your R code)

- [A] \(Z_i = (9Z_{i-1} + 1) \mod 16\) with \(Z_0 = 5\).

- [B] \(Z_i = (7Z_{i-1} + 3) \mod 32\) with \(Z_0 = 10\),

Quesion 3 (a) Compute \(Z_i\) and \(U_i\) for values of \(i\) until a number is repeated, what is the period of both the generator? Provide your comments about the period of both RGN

#Answer 3a
z0_a <- 5
a_a <- 9
c_a <- 1
m_a <- 16

zi_a <- z0_a
ui_a <- NULL
period_a <- 0
visited_a <- c()

while (zi_a %in% visited_a == FALSE) {
    visited_a <- c(visited_a, zi_a)
    ui_a <- c(ui_a, zi_a / m_a)
    zi_a <- (a_a * zi_a + c_a) %% m_a
    period_a <- period_a + 1
}
cat("Generator A Period: ", period_a, "\n")
## Generator A Period:  16
z0_b <- 10
a_b <- 7
c_b <- 3
m_b <- 32

zi_b <- z0_b
ui_b <- NULL
period_b <- 0
visited_b <- c()

while (zi_b %in% visited_b == FALSE) {
    visited_b <- c(visited_b, zi_b)
    ui_b <- c(ui_b, zi_b / m_b)
    zi_b <- (a_b * zi_b + c_b) %% m_b
    period_b <- period_b + 1
}
cat("Generator B Period: ", period_b, "\n")
## Generator B Period:  8

Quesion 3 (b) Which of these parameters effect the period of LCG – \(a\), \(b\), \(Z_0\)

#Answer 
cat("Answer: ", "a affects the period of LGC because it is part of the formula. Z0 does not direstly affect the period of an LGC since it's not part of the equation.")
## Answer:  a affects the period of LGC because it is part of the formula. Z0 does not direstly affect the period of an LGC since it's not part of the equation.

Quesion 3 (c) For both generators plot a scatter diagram of the Zi values 1 apart. What are your observations from these plots? What do you think about the property of randomness of these generators?

#Answer
scatter_a <- NULL

for (i in 1:500){
  zi_a <- (a_a * zi_a + c_a) %% m_a
  scatter_a <- c(scatter_a, zi_a)
}
plot(1:500, scatter_a, xlab = "Index (i)", ylab = "Zi Values", main = "Scatter Diagram for Generator A")

scatter_b <- NULL

for (i in 1:500){
  zi_b <- (a_b * zi_b + c_b) %% m_b
  scatter_b <- c(scatter_b, zi_b)
}
plot(1:500, scatter_b, xlab = "Index (i)", ylab = "Zi Values", main = "Scatter Diagram for Generator B")

cat("Observation for Generator A: ", "generator A exhibits a specific pattern in the scatter diagram, where theres two points next to each other in a specific pattern. There is a clear predictable relationship between the Zi values, meaning it is non-random. ", "\n", "\n")
## Observation for Generator A:  generator A exhibits a specific pattern in the scatter diagram, where theres two points next to each other in a specific pattern. There is a clear predictable relationship between the Zi values, meaning it is non-random.  
## 
cat("Observation for Generator B: ", "generator B also exhibits a specific pattern in the scatter diagram, where it starts to form a horiztonal line, a consistent space between each line it formed and between the points itself. Therefore it is also non-random.", "\n")
## Observation for Generator B:  generator B also exhibits a specific pattern in the scatter diagram, where it starts to form a horiztonal line, a consistent space between each line it formed and between the points itself. Therefore it is also non-random.

Quesion 3 (d) Randomness of default random generator of R – Run runif command to generate 100 random numbers and plot the scatter diagram of these numbers ( values 1 apart) and discuss your observations about randomness of this random generator.

#Answer
random_num <- runif(100)
indices <- 1:100
plot(indices, random_num, xlab = "Index (i)", ylab = "Random Numbers", main = "Scatter Diagram of 100 Random Numbers")

cat("Observation: ", "the points are scattered all acrossed the diagram, there seem to exhibit no visible pattern. It is unpredictable, therefore it is random.", "\n")
## Observation:  the points are scattered all acrossed the diagram, there seem to exhibit no visible pattern. It is unpredictable, therefore it is random.

Quesion 3 (e) Compute the mean value of \(U_i\) across the period

#Answer
mean_ui_a <- sum(ui_a) / length(ui_a)
mean_ui_b <- sum(ui_b) / length(ui_b)
cat("Mean value of Ui for Generator A: ", mean_ui_a, "\n")
## Mean value of Ui for Generator A:  0.46875
cat("Mean value of Ui for Generator B: ", mean_ui_b, "\n")
## Mean value of Ui for Generator B:  0.421875

Quesion 3 (f) By providing a plot of density (histogram) discuss the uniformity of both of the generators

#Answer
library(ggplot2)
ggplot(data = data.frame(ui = ui_a)) + geom_histogram(aes(x = ui), fill = "beige", color = "black", bins = 20) + labs(x = "Ui (Genertaor A)", y = "Frequency") + ggtitle("Histogram of Generator A") + theme_minimal()

ggplot(data = data.frame(ui = ui_b)) + geom_histogram(aes(x = ui), fill = "pink", color = "black", bins = 20) + labs(x = "Ui (Generator B)", y = "Frequency") + ggtitle("Histogram of Generator B") + theme_minimal()

cat("Observations: ", "The histogram for Generator A exhibits some uniformity. The values are roughly evenly distributed suggesting that the distribution may be uniform. The histogram for Generator B shows a relatively more uniform distribution. The values appear to be more evenly distributed across the range, with no prominent peaks or valleys.")
## Observations:  The histogram for Generator A exhibits some uniformity. The values are roughly evenly distributed suggesting that the distribution may be uniform. The histogram for Generator B shows a relatively more uniform distribution. The values appear to be more evenly distributed across the range, with no prominent peaks or valleys.

Question 4. Using the inverse transform method

Question 4.(a) Develop an algorithm for the random variable with cumulative distribution function F(x) below.

\[ F(x) = 1 - e^{-(x/\lambda)^k} \]

where \(x \geq 0\), \(\lambda \geq 0\), and \(k \geq 0\).

# Answer 4a
# This might be hard to print using R - you can write the step in your notebook and then include image here / or you can include as seperate file when upload

# Check if the IRdisplay package is already installed
if (!require(IRdisplay, quietly = TRUE)) {
  # If not installed, install it
  install.packages("IRdisplay")
  
  # Load the IRdisplay library
  library(IRdisplay)
} else {
  # If already installed, just load the library
  library(IRdisplay)
}


# Embed an image
#display_png(file = "example.png")

lambda <- 1.5 
k <- 2       
num_samples <- 500

random_variables <- rep(NA, num_samples)
for (i in 1:num_samples) {
  u <- runif(1)  
  x <- lambda * (-log(1 - u))^(1/k)  
  random_variables[i] <- x
}

hist(random_variables, breaks = 30, prob = TRUE, main = "Histogram of Generated Random Variables", xlab = "Random Variable Values")

Quesion 4 (b) Use the first three Ui values from part (a) and generator [A] of previous problem to create 3 values from the random variable when, \(\lambda = 1\), \(𝑘 = 5\)

#Answer
lambda <- 1
k <- 5

ui_values <- c(0.9028747897, 0.7229011791, 0.3323187047)

random_variable_values <- rep(NA, length(ui_values))

for (i in 1:length(ui_values)) {
  random_variable_values[i] <- lambda * (-log(1 - ui_values[i]))^(1/k)
}

cat("Generated random variable values: ")
## Generated random variable values:
print(random_variable_values)
## [1] 1.1845041 1.0511656 0.8341887

Quesion 4 (c) sing R generate 10,000 values from your algorithm when = 1 , 𝑘 = 5 and plot a histogram of the density. Discuss what insights you obtain when you look at the plot generated here vs the plot you generated in question 3 (e)

#Answer
lambda <- 1
k <- 5

num_samples <- 10000

random_variables <- rep(NA, num_samples)
for (i in 1:num_samples) {
  u <- runif(1) 
  x <- lambda * (-log(1 - u))^(1/k) 
  random_variables[i] <- x
}

hist(random_variables, breaks = 30, prob = TRUE, main = "Histogram of Generated Random Variables", xlab = "Random Variable Values")

cat("Observation: ", "the distribution for this histogram is right skewed with the distribution in 4a is more left skewed.", "\n")
## Observation:  the distribution for this histogram is right skewed with the distribution in 4a is more left skewed.

Question 5. Develop a Monte Carlo simulation in R that counts the number of uniform [0,1] random numbers that must be summed to get a sum greater than 1. Run a single simulation with n = 10,000 times and find the mean of the number of counts. Do you think this number looks somewhat familiar. Every student might get slightly different value any ideas why?

#Answer
n_simulations <- 10000

counts <- numeric(n_simulations)

for (i in 1:n_simulations) {
  sum <- 0
  count <- 0
  while (sum <= 1) {
    sum <- sum + runif(1) 
    count <- count + 1
  }
  counts[i] <- count
}

mean_counts <- mean(counts)

cat("Mean of the number of counts:", mean_counts, "\n")
## Mean of the number of counts: 2.7253
cat("Observation: ", "the mean number of counts is similar to Euler's number. Every student might get slighte=ly different values because the simulation involves random numbers and thue the outcome will be different for every stimulation. Therefore it causes the mean number of counts to change slightly.", "\n")
## Observation:  the mean number of counts is similar to Euler's number. Every student might get slighte=ly different values because the simulation involves random numbers and thue the outcome will be different for every stimulation. Therefore it causes the mean number of counts to change slightly.

Question 6. For our specific random variable, we know that its PDF is defined by the function \(f(x) = 10x(1-x)\). Utilize the accept-reject algorithm to draw samples from this distribution. Take a look at Lecture 10 slide 8 and 9

#Answer
num_samples <- 1000

samples <- numeric(num_samples)

target_distribution <- function(x) {
  return(10 * x * (1 - x))
}

for (i in 1:num_samples) {
  repeat {
    x <- runif(1)  
    u <- runif(1)  

    if (u <= target_distribution(x)) {
      samples[i] <- x
      break  
    }
  }
}

hist(samples, breaks = 30, prob = TRUE, main = "Histogram of Generated Samples", xlab = "Sample Values")