install.packages("ggplot2")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/patty/AppData/Local/R/win-library/4.3’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.3/ggplot2_3.4.3.zip'
Content type 'application/zip' length 3327885 bytes (3.2 MB)
downloaded 3.2 MB
package ‘ggplot2’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\patty\AppData\Local\Temp\RtmpUhyBuD\downloaded_packages
#Answer 1a
#parameters
n <- 24
p <- 0.9
#Value for random var
x <- 0:n
#prob density func
pdf_values <- dbinom(x, size = n, prob =p)
#cumulative distribution function
cdf_values <- pbinom(x, size = n, prob = p)
#df for plotting
data <- data.frame(x = x, PDF = pdf_values, CDF = cdf_values)
#plot the PDF
library(ggplot2)
ggplot(data, aes(x =x, y =PDF))+
geom_bar(stat = 'identity', fill = 'purple', alpha = 0.7) +
labs(title = 'Binomial Distribution PDF',
x = 'Number of correctly recieved bits',
y = 'Probability')
theme_minimal()
List of 97
$ line :List of 6
..$ colour : chr "black"
..$ linewidth : num 0.5
..$ linetype : num 1
..$ lineend : chr "butt"
..$ arrow : logi FALSE
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_line" "element"
$ rect :List of 5
..$ fill : chr "white"
..$ colour : chr "black"
..$ linewidth : num 0.5
..$ linetype : num 1
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_rect" "element"
$ text :List of 11
..$ family : chr ""
..$ face : chr "plain"
..$ colour : chr "black"
..$ size : num 11
..$ hjust : num 0.5
..$ vjust : num 0.5
..$ angle : num 0
..$ lineheight : num 0.9
..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : logi FALSE
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ title : NULL
$ aspect.ratio : NULL
$ axis.title : NULL
$ axis.title.x :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 1
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 2.75points 0points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.title.x.top :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 0
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 2.75points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.title.x.bottom : NULL
$ axis.title.y :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 1
..$ angle : num 90
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 2.75points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.title.y.left : NULL
$ axis.title.y.right :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 0
..$ angle : num -90
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 0points 2.75points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : chr "grey30"
..$ size : 'rel' num 0.8
..$ hjust : NULL
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.x :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 1
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 2.2points 0points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.x.top :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : num 0
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 2.2points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.x.bottom : NULL
$ axis.text.y :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 1
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 2.2points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.text.y.left : NULL
$ axis.text.y.right :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 0
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 0points 2.2points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ axis.ticks : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ axis.ticks.x : NULL
$ axis.ticks.x.top : NULL
$ axis.ticks.x.bottom : NULL
$ axis.ticks.y : NULL
$ axis.ticks.y.left : NULL
$ axis.ticks.y.right : NULL
$ axis.ticks.length : 'simpleUnit' num 2.75points
..- attr(*, "unit")= int 8
$ axis.ticks.length.x : NULL
$ axis.ticks.length.x.top : NULL
$ axis.ticks.length.x.bottom: NULL
$ axis.ticks.length.y : NULL
$ axis.ticks.length.y.left : NULL
$ axis.ticks.length.y.right : NULL
$ axis.line : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ axis.line.x : NULL
$ axis.line.x.top : NULL
$ axis.line.x.bottom : NULL
$ axis.line.y : NULL
$ axis.line.y.left : NULL
$ axis.line.y.right : NULL
$ legend.background : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ legend.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
..- attr(*, "unit")= int 8
$ legend.spacing : 'simpleUnit' num 11points
..- attr(*, "unit")= int 8
$ legend.spacing.x : NULL
$ legend.spacing.y : NULL
$ legend.key : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ legend.key.size : 'simpleUnit' num 1.2lines
..- attr(*, "unit")= int 3
$ legend.key.height : NULL
$ legend.key.width : NULL
$ legend.text :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : 'rel' num 0.8
..$ hjust : NULL
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ legend.text.align : NULL
$ legend.title :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 0
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ legend.title.align : NULL
$ legend.position : chr "right"
$ legend.direction : NULL
$ legend.justification : chr "center"
$ legend.box : NULL
$ legend.box.just : NULL
$ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
..- attr(*, "unit")= int 1
$ legend.box.background : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ legend.box.spacing : 'simpleUnit' num 11points
..- attr(*, "unit")= int 8
$ panel.background : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ panel.border : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ panel.spacing : 'simpleUnit' num 5.5points
..- attr(*, "unit")= int 8
$ panel.spacing.x : NULL
$ panel.spacing.y : NULL
$ panel.grid :List of 6
..$ colour : chr "grey92"
..$ linewidth : NULL
..$ linetype : NULL
..$ lineend : NULL
..$ arrow : logi FALSE
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_line" "element"
$ panel.grid.major : NULL
$ panel.grid.minor :List of 6
..$ colour : NULL
..$ linewidth : 'rel' num 0.5
..$ linetype : NULL
..$ lineend : NULL
..$ arrow : logi FALSE
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_line" "element"
$ panel.grid.major.x : NULL
$ panel.grid.major.y : NULL
$ panel.grid.minor.x : NULL
$ panel.grid.minor.y : NULL
$ panel.ontop : logi FALSE
$ plot.background : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ plot.title :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : 'rel' num 1.2
..$ hjust : num 0
..$ vjust : num 1
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 5.5points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ plot.title.position : chr "panel"
$ plot.subtitle :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : num 0
..$ vjust : num 1
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 0points 0points 5.5points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ plot.caption :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : 'rel' num 0.8
..$ hjust : num 1
..$ vjust : num 1
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 5.5points 0points 0points 0points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ plot.caption.position : chr "panel"
$ plot.tag :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : 'rel' num 1.2
..$ hjust : num 0.5
..$ vjust : num 0.5
..$ angle : NULL
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ plot.tag.position : chr "topleft"
$ plot.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
..- attr(*, "unit")= int 8
$ strip.background : list()
..- attr(*, "class")= chr [1:2] "element_blank" "element"
$ strip.background.x : NULL
$ strip.background.y : NULL
$ strip.clip : chr "inherit"
$ strip.placement : chr "inside"
$ strip.text :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : chr "grey10"
..$ size : 'rel' num 0.8
..$ hjust : NULL
..$ vjust : NULL
..$ angle : NULL
..$ lineheight : NULL
..$ margin : 'margin' num [1:4] 4.4points 4.4points 4.4points 4.4points
.. ..- attr(*, "unit")= int 8
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ strip.text.x : NULL
$ strip.text.x.bottom : NULL
$ strip.text.x.top : NULL
$ strip.text.y :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : NULL
..$ angle : num -90
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ strip.text.y.left :List of 11
..$ family : NULL
..$ face : NULL
..$ colour : NULL
..$ size : NULL
..$ hjust : NULL
..$ vjust : NULL
..$ angle : num 90
..$ lineheight : NULL
..$ margin : NULL
..$ debug : NULL
..$ inherit.blank: logi TRUE
..- attr(*, "class")= chr [1:2] "element_text" "element"
$ strip.text.y.right : NULL
$ strip.switch.pad.grid : 'simpleUnit' num 2.75points
..- attr(*, "unit")= int 8
$ strip.switch.pad.wrap : 'simpleUnit' num 2.75points
..- attr(*, "unit")= int 8
- attr(*, "class")= chr [1:2] "theme" "gg"
- attr(*, "complete")= logi TRUE
- attr(*, "validate")= logi TRUE
#plot the CDF
ggplot(data, aes(x = x, y = CDF))+
geom_step(color = "blue")+
labs(title = "Binomial Distribution CDF",
x = "Number of correctly recieved bits",
y = "Cumulative Probability") +
theme_minimal()
NA
NA
#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)
#param
n <- 24
p <- 0.9
#Calculate mean
mean <- n * p
#calculate std
std_dev <- sqrt(n * p *(1-p))
#print results
cat("Mean number of correctly recieved bits: ", mean, "\n")
Mean number of correctly recieved bits: 21.6
cat("Standard Deviation: ", std_dev, "\n")
Standard Deviation: 1.469694
#Answer 1c
#param
n < -24
[1] FALSE
p <- 0.1 #prob of single bit error
#calculate prob of 0, 1, 2, and 3 bit error
prob_less_4 <- sum(dbinom(0:3, size = n, prob = p))
#Prob of more than 3 bit error
prob_more_3 <- 1 - prob_less_4
cat("Probability of more than 3-bit error in the block of 24: ", prob_more_3, "\n")
Probability of more than 3-bit error in the block of 24: 0.2142622
#Answer 1d
#param
n <- 24
p <- 0.9
#Calculate the median
median_val <- floor(n * p + 1/2)
cat("Median of the distribution:", median_val, "\n")
Median of the distribution: 22
cat("The median can represent the middle value in the distribution when the data is arrranged in ascending order. The median of the distribution tells the number of correctly recieved bits at which there is an equal chance of having more or fewer correctly recieved bits.")
The median can represent the middle value in the distribution when the data is arrranged in ascending order. The median of the distribution tells the number of correctly recieved bits at which there is an equal chance of having more or fewer correctly recieved bits.
#Answer 1e
n <- 24
p <- 0.9
quantile_percent <- 0.60
quantile_val <- qbinom(quantile_percent, size = n, prob = p, lower.tail = TRUE)
cat("60th quantile of the distribution: ", quantile_val, "\n")
60th quantile of the distribution: 22
#Interpretation
cat("It represents the number of correctly recieved bits below which 60% of the data blocks fall. Based on the given parameters, 60% of the time, it can be expected to have this many or fewer correctly recieved bits in a block")
It represents the number of correctly recieved bits below which 60% of the data blocks fall. Based on the given parameters, 60% of the time, it can be expected to have this many or fewer correctly recieved bits in a block
#Answer 2a
#Param
lambda <- 20 #calls per hour
#number of call to calc probabilties
k1 <- 20
k2 <- 30
#probabilities
prob_20 <- dpois(k1, lambda)
prob_30 <- dpois(k2, lambda)
cat("Probability of an operator processing 20 calls in an hour:", prob_20, "\n")
Probability of an operator processing 20 calls in an hour: 0.08883532
cat("Probability of an operation processing 30 calls in an hour:", prob_30, "\n")
Probability of an operation processing 30 calls in an hour: 0.008343536
#Answer 2b
lambda <- 20
expected_calls <- 24 #calls split equally
prob_single_operator <- dpois(expected_calls, lambda)
cat("Probability of a single operator processing 24 calls:", prob_single_operator, "\n")
Probability of a single operator processing 24 calls: 0.05573456
#Answer 2c
lambda <- 85
k <- 100
prob_less_100 <- ppois(k, lambda)
prob_more_100 <- 1 - prob_less_100
cat("Probability of more than 100 calls in an hour:", prob_more_100, "\n")
Probability of more than 100 calls in an hour: 0.04934533
Generator A
#Answer 3a
#Initialize parameters Generator A
Z_A <- 5
period_A <- 0
Z_A_values <- c(Z_A)
u_A_values <- c(Z_A / 16)
#Simulate Generator A until number repeated
while(TRUE) {
Z_A <- (9 * Z_A + 1) %% 16
period_A <- period_A + 1
Z_A_values <- c(Z_A_values, Z_A)
u_A_values <- c(u_A_values, Z_A / 16)
if (Z_A %in% Z_A_values [-length(Z_A_values)]){
break
}
}
cat("Generator A Period:", period_A, "\n")
Generator A Period: 16
Generator B
#Initialize para Generator B
Z_B <- 10
period_B <- 0
Z_B_values <- c(Z_B)
u_B_values <- c(Z_B / 32)
#Simulate Generator B till number repeated
while (TRUE){
Z_B <- (7 * Z_B + 3) %% 32
period_B <- period_B + 1
Z_B_values <- c(Z_B_values, Z_B)
u_B_values <- c(u_B_values, Z_B / 32)
if (Z_B %in% Z_B_values[-length(Z_B_values)]){
break
}
}
cat("Generator B Period:", period_B, "\n")
Generator B Period: 8
#Answer
cat("Among the parameters of LCG, the multiplier (a) has the most significant effect on the period. The period of an LCG is determined by how many unique values it can generate before repeating. If a is carefully chosen with the modulus it can lead to a longer period to increase the range of possible values the geerator can produce. Howeverm, if is is poorly chosen, the period can be short and repeat numbers.")
Among the parameters of LCG, the multiplier (a) has the most significant effect on the period. The period of an LCG is determined by how many unique values it can generate before repeating. If a is carefully chosen with the modulus it can lead to a longer period to increase the range of possible values the geerator can produce. Howeverm, if is is poorly chosen, the period can be short and repeat numbers.
#Answer
#initialize parameters for generator A
Z_A <- 5
period_A <- 0
Z_A_values <- c(Z_A)
#Simulate Generator
while (period_A < 1000) {
Z_A <- (9 * Z_A + 1) %% 16
period_A <- period_A + 1
Z_A_values <- c(Z_A_values, Z_A)
if (Z_A %in% Z_A_values[-length(Z_A_values)]){
break
}
}
#Gen A Scatter Plot
plot(Z_A_values[-length(Z_A_values)], Z_A_values[-1],
main = "Generator A Scatter Plot",
xlab = "Z_i", ylab = "Z_(i+1) (i + 1)",
pch = 19, col = "navyblue"
)
NA
NA
NA
#Initialize parameters
Z_B <- 10
period_B <- 0
Z_B_values <- c(Z_B)
#Simulate Gen B
while (period_B < 1000){
Z_B <- (7 * Z_B + 3) %% 32
period_B <- period_B + 1
Z_B_values <- c(Z_B_values, Z_B)
if (Z_B %in% Z_B_values[-length(Z_B_values)]){
break
}
}
#Gen B Scatterplot
plot(Z_B_values[-length(Z_B_values)], Z_B_values[-1],
main = "Generator B Scatterplot",
xlab = "Z_i", ylab= "Z_(i+1) (i+1)",
pch = 19, col = "magenta")
cat("Generator A exhibits a clear pattern. The values appear to follow a deterministic sequence. There is no apparent randomness.")
Generator A exhibits a clear pattern. The values appear to follow a deterministic sequence. There is no apparent randomness.
cat("Generator B displays a noticeable pattern in its scatterplot. It is somewhat less regular than Generator A. There is still a degree of predicatability.")
Generator B displays a noticeable pattern in its scatterplot. It is somewhat less regular than Generator A. There is still a degree of predicatability.
cat("In both cases, scatterplots suggest that they do not possess the properties of true randomness. They show patterns that can be indicative of the limitations or flaws in their parameter choice.")
In both cases, scatterplots suggest that they do not possess the properties of true randomness. They show patterns that can be indicative of the limitations or flaws in their parameter choice.
#Answer
#Generate 100 random numbers
random_numbers <- runif(100)
#Scatter plot
plot(1:99, random_numbers[1:99],
main = "Random Number Plot",
xlab = "Index", ylab = "Random Value",
pch = 19, col="green")
cat("The random number generator shows no apparent pattern or structure. The points appear to be distributed randomly across the plot.
#There are no discernible trends or regularities in the distribution of the points.")
The random number generator shows no apparent pattern or structure. The points appear to be distributed randomly across the plot.
#There are no discernible trends or regularities in the distribution of the points.
#Answer
mean_UA <- mean(Z_A_values / 16)
cat("Mean value of U_i for Generator A:", mean_UA, "\n")
Mean value of U_i for Generator A: 0.4595588
mean_UB <- mean(Z_B_values / 32)
cat("Mean value of U_i for Generator B:", mean_UB, "\n")
Mean value of U_i for Generator B: 0.4097222
#Answer
#Gen A Histogram
hist(Z_A_values[-length(Z_A_values)] / 16, breaks = 20,
main = "Generator A Histogram",
xlab = " U_i Value", ylab = "Frequency", col = "blue")
NA
NA
NA
#Generator B Histogram
hist(Z_B_values[-length(Z_B_values)] / 32, breaks = 20,
main = "Generator B Histogram",
xlab = "U_i Value", ylab = "Frequency", col = "red")
cat("Complete Uniformity in these histograms is a positive characteristic, as it suggest that the generators are performing well in terms of producing uniformly distributed random values.")
Complete Uniformity in these histograms is a positive characteristic, as it suggest that the generators are performing well in terms of producing uniformly distributed random values.
\[ 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")
#Define parameters
lambda_param <- 2.0
k_param <- 1.5
#Generate random sample U from a uniform distribution [0,1]
U <- runif(1)
X <- lambda_param * (-log(1-U))^(1/k_param)
cat("Random Variable X:", X, "\n")
Random Variable X: 0.1124825
#Answer
lambda_param <- 1
k_param <- 5
U_values <- c(0.1124825 , 0.9622323 , 0.6541394 )
inverse_transformation <- function(U, lambda, k){
X <- lambda * (-log(1 - U))^(1/k)
return(X)
}
random_values <- inverse_transformation(U_values, lambda_param, k_param)
cat("Random Generated Values:", random_values, "\n")
Random Generated Values: 0.6536538 1.267876 1.01205
#Answer
#Parameters
lambda_param <- 1
k_param <- 5
num_samples <- 10000
set.seed(42)
U_values <- runif(num_samples)
X_values <- lambda_param * (-log(1 - U_values))^(1/k_param)
hist(X_values, breaks = 30,
main = "Generated Random Values Histogram",
xlab = "X Value",
ylab = "Frequency",
col = "pink")
cat("In question 4e, I talked about the uniformity of the histogram for Generator A. In this case the histogram represents a different distribution.This pattern indicated that values are concentrated in specific regions and alternate with lower frequency reginons.")
In question 4e, I talked about the uniformity of the histogram for Generator A. In this case the histogram represents a different distribution.This pattern indicated that values are concentrated in specific regions and alternate with lower frequency reginons.
#Answer
#number of trials
n <- 10000
#empty vector to store counts
counts <- numeric(n)
#Monte Carlo Sim
for (i in 1:n) {
sum <- 0
count <- 0
while (sum <= 1) {
u <- runif(1) #Generate a uniform number [0, 1]
sum <- sum + u
count <- count + 1
}
counts[i] <- count
}
#calc mean
mean_count <- mean(counts)
cat("Mean number of random numbers needed to exceed 1:", mean_count, "\n")
Mean number of random numbers needed to exceed 1: 2.733
cat("The numbers should look somewhat familiar because its closely related to the concept of the expected value or mean of a certain mathematical distribution.")
The numbers should look somewhat familiar because its closely related to the concept of the expected value or mean of a certain mathematical distribution.
cat("Every student may get a slightly different value due to the stochastic nature of random number generation.")
Every student may get a slightly different value due to the stochastic nature of random number generation.
#Answer
#Define the target pdf function f(x)
pdf <- function(x) {
return(10 * x * (1-x))
}
#Define proposal distribution g(x) uniform in 0,1
proposal_pdf <- function(x) {
return(1)
}
#Set the number of samples wanting to generate
num_samples <- 10000
#Initialize an empty vector
samples <- numeric(num_samples)
#Perform accept-reject
M <- 10 #Upper bound
count <- 0
while (count < num_samples) {
x <- runif(1)
u <- runif(1)
#acceptance condition
if (u <= pdf(x) / (M * proposal_pdf(x))) {
samples[count + 1] <- x
count <- count + 1
}
}
#Display the first few generated samples
head(samples)
[1] 0.6273986 0.2820369 0.5372046 0.7136449
[5] 0.5897641 0.3917895