#Answer 1a
X = c(1:24)
X
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
PDFbin = dbinom(X, 24, 0.9)
PDFbin
## [1] 2.160000e-22 2.235600e-20 1.475496e-18 6.971719e-17 2.509819e-15
## [6] 7.152983e-14 1.655405e-12 3.165961e-11 5.065538e-10 6.838477e-09
## [11] 7.833164e-08 7.637335e-07 6.344863e-06 4.486725e-05 2.692035e-04
## [16] 1.362843e-03 5.772039e-03 2.020214e-02 5.741660e-02 1.291874e-01
## [21] 2.214640e-01 2.717968e-01 2.127105e-01 7.976644e-02
plot(X,PDFbin, type="h")
cdfbin = pbinom(X,24, 0.9)
cdfbin
## [1] 2.170000e-22 2.257300e-20 1.498069e-18 7.121525e-17 2.581034e-15
## [6] 7.411087e-14 1.729516e-12 3.338913e-11 5.399430e-10 7.378420e-09
## [11] 8.571006e-08 8.494436e-07 7.194307e-06 5.206155e-05 3.212650e-04
## [16] 1.684108e-03 7.456147e-03 2.765828e-02 8.507489e-02 2.142622e-01
## [21] 4.357263e-01 7.075230e-01 9.202336e-01 1.000000e+00
plot(X, cdfbin, 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(PDFbin)
cat("Mean number of correctly received bits ", mean )
## Mean number of correctly received bits 0.04166667
varbin <- var(rbinom(n = 24, size = 24, prob = .9))
stddev = (sqrt(varbin))
cat("Standard deviation of number of correctly received bits ", stddev )
## Standard deviation of number of correctly received bits 1.44463
#Answer 1c
mean <-dbinom(x = 3, size = 24, prob = 0.9)
mean
## [1] 1.475496e-18
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(.60))
## 60%
## 24
#Interpretation
cat("Interpretation", "60% of the time the data wille not exceed 24 bits of correctly revieved bits from the satelite. ")
## Interpretation 60% of the time the data wille not exceed 24 bits of correctly revieved bits from the satelite.
#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) # Set seed
Z_A <- c(5) # Initial value Z[0]
U_A <- c() # Store U[i] values
while (TRUE) {
Z_A_new <- (9 * Z_A[length(Z_A)] + 1) %% 16
U_A_new <- Z_A_new / 16
if (Z_A_new %in% Z_A) {
period_A <- length(Z_A)
break
}
Z_A <- c(Z_A, Z_A_new)
U_A <- c(U_A, U_A_new)
}
cat("Generator A repeats after:", period_A, "numbers \n")
## Generator A repeats after: 16 numbers
set.seed(0) # set seed
Z_B <- c(10) # Initial value Z[0]
U_B <- c() # Store U[i] values
while (TRUE) {
Z_B_new <- (7 * Z_B[length(Z_B)] + 3) %% 32
U_B_new <- Z_B_new / 32
if (Z_B_new %in% Z_B) {
period_B <- length(Z_B)
break
}
Z_B <- c(Z_B, Z_B_new)
U_B <- c(U_B, U_B_new)
}
cat("Generator B repeats after:", period_B, " numbers \n")
## Generator B repeats after: 8 numbers
#Answer
cat("Answer", "The m value or the modulus value effect the period of the LCG and that is how all the Z[i] values are found in the calculation. A large m makes the U[i] values very dense")
## Answer The m value or the modulus value effect the period of the LCG and that is how all the Z[i] values are found in the calculation. A large m makes the U[i] values very dense
#Answer
plot(Z_A[-length(Z_A)], Z_A[-1], main = "Plot A")
plot(Z_B[-length(Z_B)], Z_B[-1], main = "Plot B")
cat("Generator B seems to be a little more random and have a greater spread then generator A that seems to generate values in more of a consistent patter \n")
## Generator B seems to be a little more random and have a greater spread then generator A that seems to generate values in more of a consistent patter
#Answer
set.seed(0)
randomnums <- runif(100)
plot(randomnums[-100], randomnums[-1])
cat("This distribution seems to be relatively uniform and fairly random as there are different points of data spread out all over the scatterplot and there is not any noticeable patterns of data clusters in the scatter plot really.")
## This distribution seems to be relatively uniform and fairly random as there are different points of data spread out all over the scatterplot and there is not any noticeable patterns of data clusters in the scatter plot really.
#Answer
meanUA <- mean(U_A)
meanUB <- mean(U_B)
cat("Mean of U[i] for A:", meanUA, "\n")
## Mean of U[i] for A: 0.4791667
cat("Mean of U[i] for B:", meanUB)
## Mean of U[i] for B: 0.4375
#Answer
hist(U_A, main = "Density Histogram of A", xlab = "", ylab = "")
hist(U_B, main = "Density Histogram of B", xlab = "", ylab = "")
\[ 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 = "4a301.png")
## Warning in getOption("jupyter.base_display_func")(data, metadata): IRdisplay can
## only be used from the IPython R kernel and R magic.
#attempted in r will attatch image
knitr::include_graphics("4a301.png")
#Answer
U1 = -1^5 * log10(1 - 0.231)
U2 = -1^5 * log10(1 - 0.476)
U3 = -1^5 * log10(1 - 0.763)
U1
## [1] 0.1140737
U2
## [1] 0.2806687
U3
## [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 of the Exponential",
xlab = "Value", ylab = "Frequency", col = "red")
#Answer
sims <- 10000
values <- numeric(sims)
#loop for sim
for (i in 1:sims) {
sum_numbers <- 0
count <- 0
while (sum_numbers <= 1) {
sum_numbers <- sum_numbers + runif(1) # Generate a random number between 0 and 1
count <- count + 1
}
values[i] <- count
}
#mean counts
mean_count <- mean(values)
cat("Mean number of counts:", mean_count, "\n")
## Mean number of counts: 2.7401
#Answer
# Define the target PDF function
f <- function(x) {
return(10 * x * (1 - x))
}
# Define the envelope function (g(x)) that is greater than or equal to f(x)
g <- function(x) {
return(1) # You can choose a simple constant envelope function
}
# Find the maximum ratio of f(x) / g(x) within the range [0, 1]
M <- max(f(seq(0, 1, by = 0.001)) / g(seq(0, 1, by = 0.001)))
# Number of samples to generate
samples <- 1000
# Initialize an empty vector to store the samples
samples <- numeric(samples)
# Generate samples using accept-reject algorithm
for (i in 1:samples) {
while (TRUE) {
x <- runif(1) # Generate a random value from [0, 1]
u <- runif(1) # Generate a random value from [0, 1]
if (u <= f(x) / (M * g(x))) {
samples[i] <- x
break
}
}
}
## Warning in 1:samples: numerical expression has 1000 elements: only the first
## used
#Answer
# Plot
hist(samples, breaks = 30, main = "Histogram of Generated Samples", xlab = "x")