#Answer 1a
plot(dbinom(1:24, 24, .9),
type='h',
main="Binomial Distribution",
ylab="Probability",
xlab="Num of Correctly Received Bits")
plot(pbinom(1:24, 24, .9),
type='s',
main="Cumulative Distribution",
ylab="Probability",
xlab="Num of Correctly Received Bits")
#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)
mean = 24*0.9
cat("Mean Number of Correctly Received Bits", mean)
## Mean Number of Correctly Received Bits 21.6
SD = sqrt(24*0.9*(1-0.9))
cat("Standard Deviation of Correctly Received Bits", SD)
## Standard Deviation of Correctly Received Bits 1.469694
#Answer 1c
partc = pbinom(20, 24, 0.9)
cat("Probability of more than 3-bit errors", partc)
## Probability of more than 3-bit errors 0.2142622
#Answer 1d
median = qbinom(0.5, 24, 0.9, lower.tail=TRUE)
cat("Median of distribution", median)
## Median of distribution 22
cat("Interpretation", "Half of the data points are less than or equal to 22 (correct bits), and half of the data points are greater than or equal to 22(correct bits).")
## Interpretation Half of the data points are less than or equal to 22 (correct bits), and half of the data points are greater than or equal to 22(correct bits).
#Answer 1e
ans = qbinom(0.6, 24, 0.9)
cat("60th quantile of distribution", ans)
## 60th quantile of distribution 22
#Interpretation
cat("Interpretation", "60 percent of the data falls below or at 22(correct bits)")
## Interpretation 60 percent of the data falls below or at 22(correct bits)
#Answer 2a
ans = dpois(20, 20)
cat("Probability an operator can process 20 calls in an hour", ans)
## Probability an operator can process 20 calls in an hour 0.08883532
ans2 = dpois(30, 20)
cat("Probability an operator can process 30 calls in an hour", ans2)
## Probability an operator can process 30 calls in an hour 0.008343536
#Answer 2b
ans3 = (dpois(24, 20))^10
cat("Probability that the ten operators can process all calls", ans3)
## Probability that the ten operators can process all calls 2.892317e-13
#Answer 2c
ans4 = 1-ppois(100, 85)
cat("Probability of more than 100 calls in an hour", ans4)
## Probability of 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))
}
#A
A = lcg(9, 1, 16, 17, 5)
cat("Zi for A", A$z)
## Zi for A 5 14 15 8 9 2 3 12 13 6 7 0 1 10 11 4 5
cat("Ui for A", A$U)
## Ui for A 0.3125 0.875 0.9375 0.5 0.5625 0.125 0.1875 0.75 0.8125 0.375 0.4375 0 0.0625 0.625 0.6875 0.25 0.3125
cat("period of A", "16")
## period of A 16
#B
B = lcg(7, 3, 32, 9, 10)
cat("Zi for B", B$z)
## Zi for B 10 9 2 17 26 25 18 1 10
cat("Ui for B", B$U)
## Ui for B 0.3125 0.28125 0.0625 0.53125 0.8125 0.78125 0.5625 0.03125 0.3125
cat("period of B", "8")
## period of B 8
#Answer
cat("Answer", "a(the multiplier) certainly affects the period of an LCG and b(the increment) can as well, but a affects it much more")
## Answer a(the multiplier) certainly affects the period of an LCG and b(the increment) can as well, but a affects it much more
#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))
}
A = lcg(9, 1, 16, 30, 5)
Za = A$z
AZ1 = Za[1:29]
AZ2 = Za[2:30]
plot(AZ1, AZ2, main = "Scatter Plot of Generator A Zi Values", xlab="Zi of Generate A")
axis(1, at=1:30)
cat("Observation of A", "Generator A does not seem to properly generate random numbers as there seems to be a pattern to its generated numbers.")
## Observation of A Generator A does not seem to properly generate random numbers as there seems to be a pattern to its generated numbers.
B = lcg(7, 3, 32, 30, 10)
Zb = B$z
BZ1 = Zb[1:29]
BZ2 = Zb[2:30]
plot(BZ1, BZ2, main = "Scatter Plot of Generator B Zi Values", xlab="Zi of Generator B")
axis(1, at=1:30)
cat("Observation of B", "Generate B looks to be better at generating random numbers than A as the values are more sporatic, but some in some areas we can still see a slight pattern, so it might not be the best option to generate random numbers.")
## Observation of B Generate B looks to be better at generating random numbers than A as the values are more sporatic, but some in some areas we can still see a slight pattern, so it might not be the best option to generate random numbers.
#Answer
numbers = runif(100)
plot(numbers)
axis(1, at = 1:100)
cat("Observation", "This generator looks to be good at generating random numbers as I do not see any pattern between the values. It is much better at generating random numbers than Generators A and B.")
## Observation This generator looks to be good at generating random numbers as I do not see any pattern between the values. It is much better at generating random numbers than Generators A and B.
#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))
}
#A
A = lcg(9, 1, 16, 20, 5)
UvalsA = A$U
UvalsA = UvalsA[1:16]
sumA = sum(UvalsA)
avgA = sumA/16
cat("mean value of Ui of A across the period", avgA)
## mean value of Ui of A across the period 0.46875
#B
B = lcg(7, 3, 32, 20, 10)
UvalsB = B$U
UvalsB = UvalsB[1:8]
sumB = sum(UvalsB)
avgB = sumB/8
cat("mean value of Ui of B across the period", avgB)
## mean value of Ui of B across the period 0.421875
#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))
}
#A
A = lcg(9, 1, 16, 5000, 5)
Ha = A$U
hist(Ha, main = "Histogram of A", xlab = "Ui of A")
#B
B = lcg(7, 3, 32, 5000, 10)
Hb = B$U
hist(Hb, main = "Histogram of B", xlab = "Ui of B")
#observation
cat("Observation", "The histogram of A is uniform in most areas, but there are a few values where there are no frequencies at all, so we can't say the distribution is actually uniform. The histogram of B is not uniform either and is more sporatic, as some values have high frequencies and many values have no frequencies.")
## Observation The histogram of A is uniform in most areas, but there are a few values where there are no frequencies at all, so we can't say the distribution is actually uniform. The histogram of B is not uniform either and is more sporatic, as some values have high frequencies and many values have no frequencies.
\[ 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
#using lambda = 1
lambda = 1
#using k = 5
k = 5
#generate Ui from U[0,1] RNG
u = runif(3, 0, 1)
#set seed for runif
set.seed(0)
#solve for x from the inverse transform function
x = ((-log(1-u))^(1/k))*lambda
#random values generated
cat("random values generated:", x)
## random values generated: 1.112257 0.9457742 0.9280298
#Answer
set.seed(0)
lambda = 1
k = 5
#same Ui from part a because set to same seed
u = runif(3, 0, 1)
x = ((-log(1-u))^(1/k))*lambda
cat("3 values from the random variable", x)
## 3 values from the random variable 1.178172 0.790447 0.8581587
#Answer
set.seed(0)
lambda = 1
k = 5
u = runif(10000, 0, 1)
x = ((-log(1-u))^(1/k))*lambda
hist(x, freq=FALSE, main = "Histogram of Density")
cat("Insights", "This histogram exhibits a normal curve, while the scatter plot from question 3 part d is uniformly distributed. This generator does not generate uniform random values, while the other one (runif) used in 3d does.")
## Insights This histogram exhibits a normal curve, while the scatter plot from question 3 part d is uniformly distributed. This generator does not generate uniform random values, while the other one (runif) used in 3d does.
#Answer
set.seed(0)
n=10000
count = 0
total_count = 0
sum = 0
for(i in 1:n){
while(sum<=1){
sum = sum + runif(1)
count = count + 1
}
total_count = total_count + count
count = 0
sum = 0
}
mean = total_count/n
cat("Mean of Number of Counts", mean)
## Mean of Number of Counts 2.7245
cat("Other Questions", "This number is close to Euler's Number. Every student might get slightly different values of means since we could all choose different seeds, so the number of uniform random numbers that must be added together to get a sum greater than 1 could vary between trials in the different seeds.")
## Other Questions This number is close to Euler's Number. Every student might get slightly different values of means since we could all choose different seeds, so the number of uniform random numbers that must be added together to get a sum greater than 1 could vary between trials in the different seeds.
#Answer
accept.reject = function(f, c, g, rg, n){
n.accepts = 0
result.sample = rep(NA, n)
while(n.accepts<n){
x.star = rg(1)
u = runif(1, 0, 1)
if(u<f(x.star)/(c*g(x.star))){
n.accepts = n.accepts+1
result.sample[n.accepts]=x.star
}
}
result.sample
}
f = function(x)10*x*(1-x)
g = function(x)x/x
rg = function(n)runif(n,0,1)
c = 3 #f(0.5) = 2.5 --> 3
vals = accept.reject(f, c, g, rg, 10000)
hist(vals, xlab = "Values", breaks=30, freq=FALSE, main="Sample vs True Density of this Acceptance-Rejection Algorithm")