#Answer 1a
n <- 24
p <- 0.9
x<- 0:24
pdf <- dbinom(x, size = n, prob = p)
cdf <- pbinom(x, size = n, prob = p)
plot(x, pdf, type = "h", lwd = 3, col = "blue", main = "Probability Density Function", xlab = "Number of Correct Bits", ylab = "Prpbability")
grid()
plot(x, cdf, type = "s", lwd = 3, col = "red", main = "Cumulative Distribution Function", xlab = "Number of Correct Bits", ylab = "Cumulative Probability")
grid()
#Answer 1b
mean_value <- n * p
std_dev <- sqrt(n * p * (1 - p))
cat("Mean: ", mean_value, "\n")
## Mean: 21.6
cat("Standard Deviation: ", std_dev)
## Standard Deviation: 1.469694
#Answer 1c
e <- 0.1
prob_3_or_fewer <- pbinom(3, size = n, prob = e)
prob_more_than_3 <- 1 - prob_3_or_fewer
cat("Probabilty of more than 3-bit errors: ", prob_more_than_3)
## Probabilty of more than 3-bit errors: 0.2142622
#Answer 1d
m = n * p
cat("Median: ", m, "\n")
## Median: 21.6
cat("Interpretation: ", "the median represents the middle value of the distribution. Meaning if we order the possible outcomes in ascending order, the median would be the value right in the middle, or the average between the two middle values. Additonally, it measures the central tendency, which means approximately half of the possible outcomes will be less than the median, 21.6, and half of the outcomes will be greater than the median.")
## Interpretation: the median represents the middle value of the distribution. Meaning if we order the possible outcomes in ascending order, the median would be the value right in the middle, or the average between the two middle values. Additonally, it measures the central tendency, which means approximately half of the possible outcomes will be less than the median, 21.6, and half of the outcomes will be greater than the median.
#Answer 1e
quantile_60 <- qbinom(0.6, size = n, prob = p)
cat("60th Quantile: ", quantile_60, "\n")
## 60th Quantile: 22
#Interpretation
cat("Interpretation: ", "The 60th quantile shows the value below which 60% of the trials are expected to have a resule lower or equal to it. Meaning, approximately 60% of the time, we would observe a number correctly received bits lower than or equal to the calculated quantile value. Since for distribution is right skewed, the 60% quantile would probability observe the tail.")
## Interpretation: The 60th quantile shows the value below which 60% of the trials are expected to have a resule lower or equal to it. Meaning, approximately 60% of the time, we would observe a number correctly received bits lower than or equal to the calculated quantile value. Since for distribution is right skewed, the 60% quantile would probability observe the tail.
#Answer 2a
rate <- 20
x_20 <- 20
prob_20 <- dpois(x_20, rate)
x_30 <- 30
prob_30 <- dpois(x_30, rate)
cat("Probability pf processing 20 calls in an hour: ", prob_20, "\n")
## Probability pf processing 20 calls in an hour: 0.08883532
cat("Probability of processing 30 calls in an hour: ", prob_30, "\n")
## Probability of processing 30 calls in an hour: 0.008343536
#Answer 2b
x_24 <- 24
single_op <- dpois(x_24, rate)
cat("Probability that a single operator can process 24 calls: ", single_op, "\n")
## Probability that a single operator can process 24 calls: 0.05573456
overall <- single_op^10
cat("Probability for ten operators to process 24 calls: ", overall, "\n")
## Probability for ten operators to process 24 calls: 2.892317e-13
#Answer 2c
aggregate_rate <- 85
call <- 100
fewer <- ppois(call, aggregate_rate)
more_than <- 1 - fewer
cat("Probability of more than 100 call in an hour: ", more_than, "\n")
## Probability of more than 100 call in an hour: 0.04934533
#Answer 3a
z0_a <- 5
a_a <- 9
c_a <- 1
m_a <- 16
zi_a <- z0_a
ui_a <- NULL
period_a <- 0
visited_a <- c()
while (zi_a %in% visited_a == FALSE) {
visited_a <- c(visited_a, zi_a)
ui_a <- c(ui_a, zi_a / m_a)
zi_a <- (a_a * zi_a + c_a) %% m_a
period_a <- period_a + 1
}
cat("Generator A Period: ", period_a, "\n")
## Generator A Period: 16
z0_b <- 10
a_b <- 7
c_b <- 3
m_b <- 32
zi_b <- z0_b
ui_b <- NULL
period_b <- 0
visited_b <- c()
while (zi_b %in% visited_b == FALSE) {
visited_b <- c(visited_b, zi_b)
ui_b <- c(ui_b, zi_b / m_b)
zi_b <- (a_b * zi_b + c_b) %% m_b
period_b <- period_b + 1
}
cat("Generator B Period: ", period_b, "\n")
## Generator B Period: 8
#Answer
cat("Answer: ", "a affects the period of LGC because it is part of the formula. Z0 does not direstly affect the period of an LGC since it's not part of the equation.")
## Answer: a affects the period of LGC because it is part of the formula. Z0 does not direstly affect the period of an LGC since it's not part of the equation.
#Answer
scatter_a <- NULL
for (i in 1:500){
zi_a <- (a_a * zi_a + c_a) %% m_a
scatter_a <- c(scatter_a, zi_a)
}
plot(1:500, scatter_a, xlab = "Index (i)", ylab = "Zi Values", main = "Scatter Diagram for Generator A")
scatter_b <- NULL
for (i in 1:500){
zi_b <- (a_b * zi_b + c_b) %% m_b
scatter_b <- c(scatter_b, zi_b)
}
plot(1:500, scatter_b, xlab = "Index (i)", ylab = "Zi Values", main = "Scatter Diagram for Generator B")
cat("Observation for Generator A: ", "generator A exhibits a specific pattern in the scatter diagram, where theres two points next to each other in a specific pattern. There is a clear predictable relationship between the Zi values, meaning it is non-random. ", "\n", "\n")
## Observation for Generator A: generator A exhibits a specific pattern in the scatter diagram, where theres two points next to each other in a specific pattern. There is a clear predictable relationship between the Zi values, meaning it is non-random.
##
cat("Observation for Generator B: ", "generator B also exhibits a specific pattern in the scatter diagram, where it starts to form a horiztonal line, a consistent space between each line it formed and between the points itself. Therefore it is also non-random.", "\n")
## Observation for Generator B: generator B also exhibits a specific pattern in the scatter diagram, where it starts to form a horiztonal line, a consistent space between each line it formed and between the points itself. Therefore it is also non-random.
#Answer
random_num <- runif(100)
indices <- 1:100
plot(indices, random_num, xlab = "Index (i)", ylab = "Random Numbers", main = "Scatter Diagram of 100 Random Numbers")
cat("Observation: ", "the points are scattered all acrossed the diagram, there seem to exhibit no visible pattern. It is unpredictable, therefore it is random.", "\n")
## Observation: the points are scattered all acrossed the diagram, there seem to exhibit no visible pattern. It is unpredictable, therefore it is random.
#Answer
mean_ui_a <- sum(ui_a) / length(ui_a)
mean_ui_b <- sum(ui_b) / length(ui_b)
cat("Mean value of Ui for Generator A: ", mean_ui_a, "\n")
## Mean value of Ui for Generator A: 0.46875
cat("Mean value of Ui for Generator B: ", mean_ui_b, "\n")
## Mean value of Ui for Generator B: 0.421875
#Answer
library(ggplot2)
ggplot(data = data.frame(ui = ui_a)) + geom_histogram(aes(x = ui), fill = "beige", color = "black", bins = 20) + labs(x = "Ui (Genertaor A)", y = "Frequency") + ggtitle("Histogram of Generator A") + theme_minimal()
ggplot(data = data.frame(ui = ui_b)) + geom_histogram(aes(x = ui), fill = "pink", color = "black", bins = 20) + labs(x = "Ui (Generator B)", y = "Frequency") + ggtitle("Histogram of Generator B") + theme_minimal()
cat("Observations: ", "The histogram for Generator A exhibits some uniformity. The values are roughly evenly distributed suggesting that the distribution may be uniform. The histogram for Generator B shows a relatively more uniform distribution. The values appear to be more evenly distributed across the range, with no prominent peaks or valleys.")
## Observations: The histogram for Generator A exhibits some uniformity. The values are roughly evenly distributed suggesting that the distribution may be uniform. The histogram for Generator B shows a relatively more uniform distribution. The values appear to be more evenly distributed across the range, with no prominent peaks or valleys.
\[ 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 = "example.png")
lambda <- 1.5
k <- 2
num_samples <- 500
random_variables <- rep(NA, num_samples)
for (i in 1:num_samples) {
u <- runif(1)
x <- lambda * (-log(1 - u))^(1/k)
random_variables[i] <- x
}
hist(random_variables, breaks = 30, prob = TRUE, main = "Histogram of Generated Random Variables", xlab = "Random Variable Values")
#Answer
lambda <- 1
k <- 5
ui_values <- c(0.9028747897, 0.7229011791, 0.3323187047)
random_variable_values <- rep(NA, length(ui_values))
for (i in 1:length(ui_values)) {
random_variable_values[i] <- lambda * (-log(1 - ui_values[i]))^(1/k)
}
cat("Generated random variable values: ")
## Generated random variable values:
print(random_variable_values)
## [1] 1.1845041 1.0511656 0.8341887
#Answer
lambda <- 1
k <- 5
num_samples <- 10000
random_variables <- rep(NA, num_samples)
for (i in 1:num_samples) {
u <- runif(1)
x <- lambda * (-log(1 - u))^(1/k)
random_variables[i] <- x
}
hist(random_variables, breaks = 30, prob = TRUE, main = "Histogram of Generated Random Variables", xlab = "Random Variable Values")
cat("Observation: ", "the distribution for this histogram is right skewed with the distribution in 4a is more left skewed.", "\n")
## Observation: the distribution for this histogram is right skewed with the distribution in 4a is more left skewed.
#Answer
n_simulations <- 10000
counts <- numeric(n_simulations)
for (i in 1:n_simulations) {
sum <- 0
count <- 0
while (sum <= 1) {
sum <- sum + runif(1)
count <- count + 1
}
counts[i] <- count
}
mean_counts <- mean(counts)
cat("Mean of the number of counts:", mean_counts, "\n")
## Mean of the number of counts: 2.7253
cat("Observation: ", "the mean number of counts is similar to Euler's number. Every student might get slighte=ly different values because the simulation involves random numbers and thue the outcome will be different for every stimulation. Therefore it causes the mean number of counts to change slightly.", "\n")
## Observation: the mean number of counts is similar to Euler's number. Every student might get slighte=ly different values because the simulation involves random numbers and thue the outcome will be different for every stimulation. Therefore it causes the mean number of counts to change slightly.
#Answer
num_samples <- 1000
samples <- numeric(num_samples)
target_distribution <- function(x) {
return(10 * x * (1 - x))
}
for (i in 1:num_samples) {
repeat {
x <- runif(1)
u <- runif(1)
if (u <= target_distribution(x)) {
samples[i] <- x
break
}
}
}
hist(samples, breaks = 30, prob = TRUE, main = "Histogram of Generated Samples", xlab = "Sample Values")