#Answer 1a
X = c(1:24)
binPDF = dbinom(X, 24, 0.9)
plot(X, binPDF, type = "h")
binCDF = pbinom(X, 24, 0.9)
plot(X, binCDF, 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)
mean = mean(binPDF)
cat("The mean number of correctly received bits ", mean)
## The mean number of correctly received bits 0.04166667
binX = var(rbinom(n = 24, size = 24, prob = 0.9))
stdev = (sqrt(binX))
cat("The standard deviation of the number of correctly received bits is ", stdev)
## The standard deviation of the number of correctly received bits is 1.585715
#Answer 1c
newmean = dbinom(x = 3, size = 24, prob = 0.9)
pbinom(3, 24, 0.9)
## [1] 1.498069e-18
#Answer 1d
qbinom(0.5, size = 24, prob = 0.9)
## [1] 22
#Answer 1e
quantile(24, probs = c(0.60))
## 60%
## 24
#Interpretation
cat("Interpretation", "the data will not exceed 24 bits being correctly received from the satellite 60% of the time.")
## Interpretation the data will not exceed 24 bits being correctly received from the satellite 60% of the time.
#Answer 2a
dpois(20, 20)
## [1] 0.08883532
dpois(30, 20)
## [1] 0.008343536
#Answer 2b
dpois(24, 20)
## [1] 0.05573456
#Answer 2c
1 - ppois(100, 85)
## [1] 0.04934533
#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
#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.
#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.
#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.
#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
#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.
\[ 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)
#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
#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")
#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
#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
}
}
}
#Answer
hist(samples, breaks = 30, main = "Generated Samples", xlab = "Value 1")