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)
z0 = c(5)
u0 = c()
while(TRUE) {
  z1 = (9 * z0[length(z0)] + 1) %% 16
  u1 = z0 / 16
  if(z1 %in% z0) {
    aper = length(z0)
    break
  }
  z0 = c(z0, z1)
  u0 = c(u0, u1)
}
cat("The period for generator A is", aper)
## The period for generator A is 16
set.seed(0)
z2 = c(10)
u2 = c()

while (TRUE) {
  z3 = (7 * z2[length(z2)] + 3) %% 32
  u3 = z3 / 32
  if(z3 %in% z2) {
    bper = length(z2)
    break
  }
  z2 = c(z2, z3)
  u2 = c(u2, u3)
}
cat("The period for generator B is", bper)
## The period for generator B is 8

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

#Answer 
cat("Answer", "Z0 definitely effects the period of LCG, this is the main difference between the periods of generators A and B shown above, the higher z0 is, the lower the period of the LCG.")
## Answer Z0 definitely effects the period of LCG, this is the main difference between the periods of generators A and B shown above, the higher z0 is, the lower the period of the LCG.

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(z0[-length(z0)], z0[-1], main = "Generator A")

plot(z2[-length(z2)], z2[-1], main = "Generator B")

cat("Generator A has more of a pattern to follow, with predictable outcomes, where as Generator B feels less predictable and more spread apart.")
## Generator A has more of a pattern to follow, with predictable outcomes, where as Generator B feels less predictable and more spread apart.

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)
rnumbers = runif(100)
plot(rnumbers[-100], rnumbers[-1])

cat("With 100 random numbers, it feels like pretty much every part of the diagram is covered with these random values, with little to no groups, clusters, or patterns.")
## With 100 random numbers, it feels like pretty much every part of the diagram is covered with these random values, with little to no groups, clusters, or patterns.

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

#Answer
meanUA = mean(u0)
cat("Mean of Ui for the period of generator A is", meanUA)
## Mean of Ui for the period of generator A is 0.5208333
meanUB = mean(u2)
cat("Mean of ui for the period of generator B is", meanUB)
## Mean of ui for the period of generator B is 0.4375

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

#Answer
hist(u0, main = "Generator A")

hist(u2, main = "Generator B")

cat("The density of both generators is very uniform and consistent across the whole way, with little changes on the y axis.")
## The density of both generators is very uniform and consistent across the whole way, with little changes on the y axis.

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 = "4A.png")

library(imager)
## Loading required package: magrittr
## 
## Attaching package: 'imager'
## The following object is masked from 'package:magrittr':
## 
##     add
## The following object is masked from 'package:IRdisplay':
## 
##     display
## The following objects are masked from 'package:stats':
## 
##     convolve, spectrum
## The following object is masked from 'package:graphics':
## 
##     frame
## The following object is masked from 'package:base':
## 
##     save.image
im = load.image("C:/Users/jctay/Pictures/Saved Pictures/4A.png")
plot(im)

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
ui1 = -1^5 * log10(1 - 0.231)
ui2 = -1^5 * log10(1 - 0.476)
ui3 = -1^5 * log10(1 - 0.763)
ui1
## [1] 0.1140737
ui2
## [1] 0.2806687
ui3
## [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", xlab = "Value", ylab = "Frequency", col = "blue")

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
times = 10000
values = numeric(times)
for (i in 1:times) {
  sum = 0
  count = 0
  while (sum <= 1) {
    sum = sum + runif(1)
    count = count + 1
  }
  values[i] = count
}
mnoc = mean(values)
cat("Mean number of counts", mnoc)
## 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
f = function(x) {
  return(10 * x * (1-x))
}
g = function(x) {
  return(1)
}
max = max(f(seq(0, 1, by = 0.001)) / g(seq(0, 1, by = 0.001)))
samples = 1000
for(i in 1:samples) {
  while(TRUE) {
    x = runif(1)
    y = runif(1)
    if(y <= f(x) / (max * g(x))) {
      samples[i] = x
      break
    }
  }
}

Quesion 1 (b)

#Answer
hist(samples, breaks = 30, main = "Generated Samples", xlab = "Value 1")