Question 1.1

set.seed(123)
n <- 10
data_exp_10 <- rexp(n, rate = 1)
hist(data_exp_10, probability = TRUE, main = "Histogram of Exp(1) for n = 10", 
     xlab = "Value", col = "skyblue", border = "white")
lines(density(data_exp_10), col = "red", lwd = 2)

# Question 1.2

set.seed(123)
n <- 100000
data_exp_100k <- rexp(n, rate = 1)
hist(data_exp_100k, probability = TRUE, main = "Histogram of Exp(1) for n = 100k", 
     xlab = "Value", col = "skyblue", border = "white")
lines(density(data_exp_100k), col = "red", lwd = 2)

Compare Density plot from 1.1 and 1.2:

  • The two plots compare the histogram of samples drawn from an exponential distribution Exp(1) for 2 different sample sizes n=10 and n=100,000. Here is the compare
  • Histogram shape : with n = 10 there is irregular and noisy, with n=100,000 the shape is smooth and well-fitted
  • The red density curve(True probability density function) does not match the histogram closely with n=10, but with n=100,00, the curve match perfectly.

Question 2.1

set.seed(123)
n<- 100000
data <- rpois(n,1)
barplot(table(data)/n, col = "blue", main = "Poison(1)", xlab = "k", ylab = "Probability")

k_val <- 0:max(data)
pois_density <- dpois(k_val, 1)
lines(k_val + 0.5, pois_density, col = "red", lwd = 2)

* Comment: The shape of the PMF for Poisson(1) is skewed to the right, and most of the probability mass is concentrated around k=0 and k=1. The distribution is highly skewed due to the small rate parameter λ=1.

Question 2.2

set.seed(123)
n<- 100000
data <- rpois(n,5)
barplot_height <- barplot(table(data)/n, col = "blue", main = "Poison(5)", xlab = "k", ylab = "Probability")


k_val <- 0:max(data)
pois_density <- dpois(k_val, 5)
lines(barplot_height, pois_density, col = "red", lwd = 2)

Question 2.3

# Set seed and parameters
set.seed(123)
n <- 100000
data1 <- rpois(n, 15)

observed_freq <- table(factor(data1, levels = 0:max(data1))) / n

barplot_heights <- barplot(observed_freq,
                           col = "blue",
                           main = "Poisson(15)",
                           xlab = "k",
                           ylab = "Probability")

# Calculate theoretical probabilities
k_value <- 0:max(data1)
pois_density <- dpois(k_value, 15)

# Add density line
lines(barplot_heights, pois_density, col = "red", lwd = 2)

Question 3.1

set.seed(123)

norm_data_10 <- rnorm(10, mean = 1, sd = sqrt(2))
hist(norm_data_10, probability = TRUE, main = "Histogram of N(1,2), n = 10", xlab = "Value", col = "blue")

# add curve
curve(dnorm(x, mean = 1, sd = sqrt(2)), add = TRUE, col = "red", lwd = 2)

norm_data_100k <- rnorm(100000, mean = 1, sd = sqrt(2))
hist(norm_data_100k, probability = TRUE, main = "Histogram of N(1,2), n = 100k", xlab = "Value", col = "blue")

# add curve
curve(dnorm(x, mean = 1, sd = sqrt(2)), add = TRUE, col = "red", lwd = 2)