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
dpois(20,20)
## [1] 0.08883532
dpois(30,20)
## [1] 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
dpois(24,20)
## [1] 0.05573456

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
1 - ppois(100,85)
## [1] 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
set.seed(0)  # Set seed
Z_A <- c(5)  # Initial value Z[0] 
U_A <- c()   # Store U[i] values 

while (TRUE) {
  Z_A_new <- (9 * Z_A[length(Z_A)] + 1) %% 16
  U_A_new <- Z_A_new / 16
  if (Z_A_new %in% Z_A) {
    period_A <- length(Z_A)
    break
  }
  Z_A <- c(Z_A, Z_A_new)
  U_A <- c(U_A, U_A_new)
}

cat("Generator A repeats after:", period_A, "numbers \n")
## Generator A repeats after: 16 numbers
set.seed(0)  # set seed
Z_B <- c(10) # Initial value Z[0] 
U_B <- c()   # Store U[i] values

while (TRUE) {
  Z_B_new <- (7 * Z_B[length(Z_B)] + 3) %% 32
  U_B_new <- Z_B_new / 32
  if (Z_B_new %in% Z_B) {
    period_B <- length(Z_B)
    break
  }
  Z_B <- c(Z_B, Z_B_new)
  U_B <- c(U_B, U_B_new)
}

cat("Generator B repeats after:", period_B, " numbers \n")
## Generator B repeats after: 8  numbers

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

#Answer 
cat("Answer", "The m value or the modulus value effect the period of the LCG and that is how all the Z[i] values are found in the calculation. A large m makes the U[i] values very dense")
## Answer The m value or the modulus value effect the period of the LCG and that is how all the Z[i] values are found in the calculation. A large m makes the U[i] values very dense

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
plot(Z_A[-length(Z_A)], Z_A[-1], main = "Plot A")

plot(Z_B[-length(Z_B)], Z_B[-1], main = "Plot B")

cat("Generator B  seems to be a little more random and have a greater spread then generator A that seems to generate values in more of a consistent patter \n")
## Generator B  seems to be a little more random and have a greater spread then generator A that seems to generate values in more of a consistent patter

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
set.seed(0)
randomnums <- runif(100)
plot(randomnums[-100], randomnums[-1])

cat("This distribution seems to be relatively uniform and fairly random as there are different points of data spread out all over the scatterplot and there is not any noticeable patterns of data clusters in the scatter plot really.")
## This distribution seems to be relatively uniform and fairly random as there are different points of data spread out all over the scatterplot and there is not any noticeable patterns of data clusters in the scatter plot really.

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

#Answer
meanUA <- mean(U_A)
meanUB <- mean(U_B)
cat("Mean of U[i] for A:", meanUA, "\n")
## Mean of U[i] for A: 0.4791667
cat("Mean of U[i] for B:", meanUB)
## Mean of U[i] for B: 0.4375

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

#Answer
hist(U_A, main = "Density Histogram of A", xlab = "", ylab = "")

hist(U_B, main = "Density Histogram of B", xlab = "", ylab = "")

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 = "4a301.png")
## Warning in getOption("jupyter.base_display_func")(data, metadata): IRdisplay can
## only be used from the IPython R kernel and R magic.
#attempted in r will attatch image
knitr::include_graphics("4a301.png")

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
U1 = -1^5 * log10(1 - 0.231)

U2 = -1^5 * log10(1 - 0.476) 

U3 = -1^5 * log10(1 - 0.763) 
U1
## [1] 0.1140737
U2
## [1] 0.2806687
U3
## [1] 0.6252517

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
set.seed(123)
lambda <- 1
k <- 5
n <- 10000
values <- rexp(n, rate = lambda^k)

hist(values, breaks = 30, main = "Density of the Exponential",
     xlab = "Value", ylab = "Frequency", col = "red")

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
sims <- 10000

values <- numeric(sims)
#loop for sim
for (i in 1:sims) {
  sum_numbers <- 0
  count <- 0
   while (sum_numbers <= 1) {
    sum_numbers <- sum_numbers + runif(1)  # Generate a random number between 0 and 1
    count <- count + 1
   }
  values[i] <- count
}

#mean counts
mean_count <- mean(values)
cat("Mean number of counts:", mean_count, "\n")
## Mean number of counts: 2.7401

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
# Define the target PDF function
f <- function(x) {
  return(10 * x * (1 - x))
}

# Define the envelope function (g(x)) that is greater than or equal to f(x)
g <- function(x) {
  return(1)  # You can choose a simple constant envelope function
}

# Find the maximum ratio of f(x) / g(x) within the range [0, 1]
M <- max(f(seq(0, 1, by = 0.001)) / g(seq(0, 1, by = 0.001)))

# Number of samples to generate
samples <- 1000

# Initialize an empty vector to store the samples
samples <- numeric(samples)

# Generate samples using accept-reject algorithm
for (i in 1:samples) {
  while (TRUE) {
    x <- runif(1)  # Generate a random value from [0, 1]
    u <- runif(1)  # Generate a random value from [0, 1]
    if (u <= f(x) / (M * g(x))) {
      samples[i] <- x
      break
    }
  }
}
## Warning in 1:samples: numerical expression has 1000 elements: only the first
## used

Quesion 1 (b)

#Answer
# Plot 
hist(samples, breaks = 30, main = "Histogram of Generated Samples", xlab = "x")