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,lambda = 20)
## [1] 0.08883532
dpois(30,lambda = 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
oneopcall = 240/10
oneopprob = dpois(oneopcall,lambda = 20)
tenopprob = oneopprob^10
cat("Probability ten operators can process 240 calls, assuming split equal: ",  tenopprob)
## Probability ten operators can process 240 calls, assuming split equal:  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
morethan100p = 1-ppois(100,lambda = 85)
cat("Probability more than 100 calls in an hour: ",  morethan100p)
## Probability more than 100 calls 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
lcg = function(a, c, m, rlength, seed){
  z = rep(0, rlength)
  z[1] = seed
  
  for(i in 1:(rlength-1)){
    z[i+1] = (a*z[i]+c) %% m
  }
  U = z/m
  return(list(z=z,U=U))
}
cat("Generator A: \n ")
## Generator A: 
## 
lcg(9,1,16,50,5)
## $z
##  [1]  5 14 15  8  9  2  3 12 13  6  7  0  1 10 11  4  5 14 15  8  9  2  3 12 13
## [26]  6  7  0  1 10 11  4  5 14 15  8  9  2  3 12 13  6  7  0  1 10 11  4  5 14
## 
## $U
##  [1] 0.3125 0.8750 0.9375 0.5000 0.5625 0.1250 0.1875 0.7500 0.8125 0.3750
## [11] 0.4375 0.0000 0.0625 0.6250 0.6875 0.2500 0.3125 0.8750 0.9375 0.5000
## [21] 0.5625 0.1250 0.1875 0.7500 0.8125 0.3750 0.4375 0.0000 0.0625 0.6250
## [31] 0.6875 0.2500 0.3125 0.8750 0.9375 0.5000 0.5625 0.1250 0.1875 0.7500
## [41] 0.8125 0.3750 0.4375 0.0000 0.0625 0.6250 0.6875 0.2500 0.3125 0.8750
cat("Comments: ", "Generator A has a period of 16, this matches the mod of 16. This means it has a full period.")
## Comments:  Generator A has a period of 16, this matches the mod of 16. This means it has a full period.
cat("Generator B: \n")
## Generator B:
lcg(7,3,32,50,10)
## $z
##  [1] 10  9  2 17 26 25 18  1 10  9  2 17 26 25 18  1 10  9  2 17 26 25 18  1 10
## [26]  9  2 17 26 25 18  1 10  9  2 17 26 25 18  1 10  9  2 17 26 25 18  1 10  9
## 
## $U
##  [1] 0.31250 0.28125 0.06250 0.53125 0.81250 0.78125 0.56250 0.03125 0.31250
## [10] 0.28125 0.06250 0.53125 0.81250 0.78125 0.56250 0.03125 0.31250 0.28125
## [19] 0.06250 0.53125 0.81250 0.78125 0.56250 0.03125 0.31250 0.28125 0.06250
## [28] 0.53125 0.81250 0.78125 0.56250 0.03125 0.31250 0.28125 0.06250 0.53125
## [37] 0.81250 0.78125 0.56250 0.03125 0.31250 0.28125 0.06250 0.53125 0.81250
## [46] 0.78125 0.56250 0.03125 0.31250 0.28125
cat("Comments: ", "Generator B has a period of 8, this doesn't matche the mod of 32. This means it doesn't have a full period.")
## Comments:  Generator B has a period of 8, this doesn't matche the mod of 32. This means it doesn't have a full period.

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

#Answer 
cat("Answer", " All three a, b, and Z0 are the parameters that affect the period of LCG. While a and b are more directly involved, Z0 is the seed and determines where in the order to begin")
## Answer  All three a, b, and Z0 are the parameters that affect the period of LCG. While a and b are more directly involved, Z0 is the seed and determines where in the order to begin

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
lcg = function(a, c, m, rlength, seed){
  z = rep(0, rlength)
  z[1] = seed
  
  for(i in 1:(rlength-1)){
    z[i+1] = (a*z[i]+c) %% m
  }
  U = z/m
  return(list(z=z,U=U))
}
genA = lcg(9,1,16,50,5)
ZTA = genA$z
Z1A = ZTA[1:19]
Z2A = ZTA[2:20]
plot(Z1A,Z2A,main="Scatter Plot of Gen A Zi values")

genB = lcg(7,3,32,50,10)
ZTB = genB$z
Z1B = ZTB[1:19]
Z2B = ZTB[2:20]
plot(Z1B,Z2B,main="Scatter Plot of Gen B Zi values")

cat("Generator's A values has a evident pattern, while Generator B's seems more random. Both as PRNGs, B seems to have a more random distribution.")
## Generator's A values has a evident pattern, while Generator B's seems more random. Both as PRNGs, B seems to have a more random distribution.

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

cat("The RNG shows effective randomness. There are no discernable patterns like seen in the LCG for generator A.")
## The RNG shows effective randomness. There are no discernable patterns like seen in the LCG for generator A.

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

#Answer
meanrn = mean(rn)
cat("Mean of Ui: ", mean(rn))
## Mean of Ui:  0.5373386

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

#Answer
genA = lcg(9,1,16,16,5)
genAdata = genA$U
hist(genAdata, probability = TRUE)

cat("Generator A demonstrates a more uniform shape/ distribution.")
## Generator A demonstrates a more uniform shape/ distribution.
genB = lcg(7,3,32,32,10)
genBdata = genB$U
hist(genBdata, probability = TRUE)

cat("Generator b demonstrates a more scattered shape with densities that differ .")
## Generator b demonstrates a more scattered shape with densities that differ .

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 (SUBMITTED IN SEPARATE FILE ON CANVAS)
# 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")

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

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
val = runif(10000)
alg = (-log(val))^(1/5)
hist(alg,main="Histogram of Values from Algorithm",prob=TRUE)

cat("Compared the the histograms of Generators A and B, this has a normal distrbution. Here, the farther from the mean, the less occurrance.")
## Compared the the histograms of Generators A and B, this has a normal distrbution. Here, the farther from the mean, the less occurrance.

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
sim_counts = function(){
  svalues = 0
  count = 0
  while(svalues<=1){
    count = count + 1
    svalues = svalues + runif(1)
  }
  return (count)
}
n = 10000
a = replicate(n, sim_counts())
meannumcounts = mean(a)
cat("Mean number of counts: ", meannumcounts)
## Mean number of counts:  2.7197
cat("\n This number is familiar but I'm not positive from where. Given this is a Monte Carlo Simulation which provides randomly generated numbers, each student's will have different numbers and therefore a different mean." )
## 
##  This number is familiar but I'm not positive from where. Given this is a Monte Carlo Simulation which provides randomly generated numbers, each student's will have different numbers and therefore a different mean.

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
pdfrs = function(x){
  return(10*x*(1-x))
}
ud = function(n){
  return(runif(n,0,1))
}
accrej = function(n){
  s = numeric(0)
  while(length(s)<n){
    x.star = ud(1)
    u = runif(1,0,1)
    if(u<pdfrs(x.star)){
      s = c(s,x.star)
    }
  }
  return(s)
}
ns = 10000
s = accrej(ns)

hist(s,breaks=50,main="Histogram of Samples",xlab="samples", ylab="density",probability=TRUE)
curve(pdfrs,from=0,to=1,add=TRUE,lwd=1) 

Quesion 1 (b)

#Answer