#Answer 1a
cbits = 0:30
plot(cbits,dbinom(cbits,size=24,prob=.9),type='h')
plot(cbits,pbinom(cbits,size=24,prob=.9),type='s')
#Answer 1b
#(make sure that what you print make sense to me) - for example to print mean you can use cat command once you done with the calculation and have value in the variable
#variable_name=10
#cat("Mean number of correctly received bits ", variable_name)
meanbits = .9*24
cat("Mean number of correctly received bits ", meanbits)
## Mean number of correctly received bits 21.6
sdvbits = sqrt(meanbits*(1-.9))
cat("Standard Deviation of correctly received bits ", sdvbits)
## Standard Deviation of correctly received bits 1.469694
#Answer 1c
morethanthree = 1-pbinom(20, size=24, prob=.9)
cat("Probability of more than 3 bit errors: ", morethanthree)
## Probability of more than 3 bit errors: 0.7857378
#Answer 1d
qbinom(.5,size=24,prob=.9)
## [1] 22
#Interpretation
cat("Interpretation:", "22 divides the data in half.Roughly 50% of all our cases are less than 22 bits while the other 50% is above 22 bits. ")
## Interpretation: 22 divides the data in half.Roughly 50% of all our cases are less than 22 bits while the other 50% is above 22 bits.
#Answer 1e
qbinom(.6, size=24, prob=.9)
## [1] 22
#Interpretation
cat("Interpretation:", "Out of a size of 24, the 22 correct received bits are in the 60th quantile out of all the correct bits. 60% of the time, 22 correct bits are to be had.")
## Interpretation: Out of a size of 24, the 22 correct received bits are in the 60th quantile out of all the correct bits. 60% of the time, 22 correct bits are to be had.
#Answer 2a
dpois(20,lambda = 20)
## [1] 0.08883532
dpois(30,lambda = 20)
## [1] 0.008343536
#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
#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
#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.
#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
#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.
#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.
#Answer
meanrn = mean(rn)
cat("Mean of Ui: ", mean(rn))
## Mean of Ui: 0.5373386
#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 .
\[ 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")
#Answer
#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.
#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.
#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)
#Answer