#Answer 1a
n <- 24
p <- .9
val <- 0:n
pdf <- dbinom(val, size = n, prob = p, log = FALSE)
cdf <- pbinom(val, size = n, prob = p, log = FALSE)
plot(val,pdf,main = "Binomial Distribution PDF", xlab = "Number of Received Bits", ylab = "Probability", type = "h", col = "red")
#Used type = 's' because the notes described the distribution as a stepwise function.
plot(val,cdf,main = "Binomial Distribution CDF", xlab = "Number of Received Bits", ylab = "Probability", type = "s", col = "red")
#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)
#Formulas for a binomial Distribution
mean <- n * p
stdev <- sqrt(n*p*(1-p))
cat("Mean # of bits: ", mean, "\n")
## Mean # of bits: 21.6
cat("Standard Deviation: ", stdev, "\n")
## Standard Deviation: 1.469694
#Answer 1c
prob_0_to_3_errors <- pbinom(20,24,.9)
cat("The Probability of more than 3-bit errors in the block of 24: ", prob_0_to_3_errors, "\n")
## The Probability of more than 3-bit errors in the block of 24: 0.2142622
#Answer 1d
median <- qbinom(0.5, size = n, prob = p)
cat("Median:", median, "\n")
## Median: 22
cat("Interpretation: The median represents the middle of a dataset. So, in this case, 22 represents the average # of bits received correctly.")
## Interpretation: The median represents the middle of a dataset. So, in this case, 22 represents the average # of bits received correctly.
#Answer 1e
quantile <- qbinom(0.6, size = n, prob = p)
#Interpretation
cat("60th quantile: ", quantile, "\n")
## 60th quantile: 22
cat("Interpretation: ", "The quantile represents a point in the distribution where 60% of the data is less than or equal to it.")
## Interpretation: The quantile represents a point in the distribution where 60% of the data is less than or equal to it.
#Answer 2a
lambda <- 20
prob_20 <- dpois(20, lambda)
prob_30 <- dpois(30, lambda)
cat("Probability for processing 20 calls in an hour: ", prob_20, "\n")
## Probability for processing 20 calls in an hour: 0.08883532
cat("Probability for processing 30 calls in an hour: ", prob_30, "\n")
## Probability for processing 30 calls in an hour: 0.008343536
#Answer 2b
# 240 / 10 = 24 calls per operator and have to assume all are processed.
greater_than_23 = 1 -ppois(23,20)
greater_than_23
## [1] 0.2125072
cat("Probability that 10 operators can process 240 calls: ", greater_than_23, "\n")
## Probability that 10 operators can process 240 calls: 0.2125072
#Answer 2c
lambda <- 85
calls <- 100
prob <- 1 - ppois(calls, lambda)
cat("Probability for more than 100 calls per hour: ", prob, "\n")
## Probability for more than 100 calls per hour: 0.04934533
#Answer 3a
Z_i <- 5
period_a <- 0
seq_a <- c(Z_i)
while(TRUE)
{
Z_i = ((9*Z_i + 1)%% 16)
period_a <- period_a + 1
if(Z_i %in% seq_a)
{
break
}
seq_a <- c(seq_a, Z_i)
}
Z_j <- 10
period_b <- 0
seq_b <- c(Z_j)
while(TRUE)
{
Z_j = ((7*Z_j + 3)%% 32)
period_b <- period_b + 1
if(Z_j %in% seq_b)
{
break
}
seq_b <- c(seq_b, Z_j)
}
#cat("Sequence [A]:", seq_a, "\n")
cat("Period [A]:", period_a, "\n")
## Period [A]: 16
#cat("Sequence [B]:", seq_b, "\n")
cat("Period [B]:", period_b, "\n")
## Period [B]: 8
#Answer
cat("The parameters that effect the period of LCG seem to be a and b.")
## The parameters that effect the period of LCG seem to be a and b.
#Answer
#Generator A
plot(seq_a[-length(seq_a)], seq_a[-1], xlab = "Z_i", ylab = "Z_i+1" )
#Generator B
plot(seq_b[-length(seq_b)], seq_b[-1], xlab = "Z_i", ylab = "Z_i+1" )
cat("Observations: The scatter diagrams seem to be very diverse. The relation between each point proves to be very random as the points are scattered across the plot. However, scatter diagram A has a slight pattern as the points are placed diagonally. On the other hand, generator B gives off no distinct pattern. ")
## Observations: The scatter diagrams seem to be very diverse. The relation between each point proves to be very random as the points are scattered across the plot. However, scatter diagram A has a slight pattern as the points are placed diagonally. On the other hand, generator B gives off no distinct pattern.
#Answer
random_num <- runif(100)
plot(random_num[-100], random_num[-1], col = "red", xlab = "Random # i", ylab = "Random Number for i + 1", main = "Scatter Diagram for Default Random Number Generator")
cat("Observations: There seems to be a uniform distribution, however, there is not a specific pattern. This supports the belief of truly random selections, as the data seems to be independent of each other and randomly scattered.")
## Observations: There seems to be a uniform distribution, however, there is not a specific pattern. This supports the belief of truly random selections, as the data seems to be independent of each other and randomly scattered.
#Answer
mean_a <- mean(seq_a / 16)
mean_b <- mean(seq_b/ 32)
cat("Mean for [A]: ", mean_a, "\n")
## Mean for [A]: 0.46875
cat("Mean for [B]: ", mean_b, "\n")
## Mean for [B]: 0.421875
#Answer
#Generator A
hist(seq_a[-1] / 16, main = "Density Plot for [A]",
xlab = "Value", ylab = "Density", col = "blue")
#Generator B
hist(seq_b[-1] / 32, main = "Density Plot for [B]",xlab = "Value", ylab = "Density", col = "red")
cat("Observations: For the first density plot, it proves to be very uniform. On the other hand, the second density plot seems to have density around 1, however, at some ranges there is a spike to 2. ")
## Observations: For the first density plot, it proves to be very uniform. On the other hand, the second density plot seems to have density around 1, however, at some ranges there is a spike to 2.
\[ 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_jpeg(file = "Question4a.jpg")
## Warning in getOption("jupyter.base_display_func")(data, metadata): IRdisplay
## can only be used from the IPython R kernel and R magic.
#Answer
lambda <- 1
k <- 5
# 0.3125, 0.875, 0.9375 first 3 values.
U <- c(0.3125, 0.875, 0.9375)
X <- (-log(lambda-U))^(1/k)
print(X)
## [1] 0.8217415 1.1576822 1.2262446
#Answer
generated_val <- runif(10000)
Xi <- (-log(lambda-generated_val))^(1/k)
hist(Xi, main = "Inverse Transform Diagram", freq = FALSE, col ="green")
cat("Observations: The histogram here tends to show a slightly skewed distribution. There is a hint of normal/symmetric distribution, however, it can be argued that there is a slight skewness. Comparing this to 3f, the graphs are very different as one proves to be uniform and the other has skewness (unimodal).")
## Observations: The histogram here tends to show a slightly skewed distribution. There is a hint of normal/symmetric distribution, however, it can be argued that there is a slight skewness. Comparing this to 3f, the graphs are very different as one proves to be uniform and the other has skewness (unimodal).
#Answer
n <- 10000
#Initializes an open vector
vec <- rep(0,n)
range <- 1:n
sum <- 0
count <- 0
for (i in range) {
# Creates an infinite loop without the need of while to have condition.
repeat{
condition = runif(1)
sum = condition + sum
count = count + 1
if (sum > 1)
{
break
}
}
vec[i] <- count
sum <- 0
count <- 0
}
mean_of_count <- mean(vec)
cat("Mean of the number of counts: ", mean_of_count, "\n")
## Mean of the number of counts: 2.7324
cat("Interpretation: I noticed that the value hovers around 2.7 which is quite similar to the number e. Every iteration is different due to the random choice of values.")
## Interpretation: I noticed that the value hovers around 2.7 which is quite similar to the number e. Every iteration is different due to the random choice of values.
#Answer
#TAKEN FROM SLIDES ON CANVAS**********
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 <- 2
vals <- accept_reject(f,c,g,rg,10000)
hist(vals,breaks = 30, freq = FALSE, main = "Sample vs True Density")
xs <- seq(0,1, len = 1000)
lines(xs, dbeta(xs,2,2), col = "blue", lwd = 2)
#Answer