tinytex::install_tinytex(force = TRUE)
set.seed(65)

### 1.
# a.
p_X_7 <- choose(15, 7) * (0.5)^7 * (0.5)^(15-7)
cat("P(X = 7):", p_X_7, "\n")
## P(X = 7): 0.1963806
# b.
num_repetitions <- 100000
coin_tosses <- matrix(sample(c(0, 1), 15 * num_repetitions, replace = TRUE), nrow = num_repetitions)
num_heads <- rowSums(coin_tosses)
rel_freq_7_heads <- cumsum(num_heads == 7) / seq_len(num_repetitions)
plot(rel_freq_7_heads, type = "l", xlab = "Number of Repetitions", ylab = "Relative Frequency", main = "Evolution of Relative Frequency for X = 7")

# c.
# The frequentist interpretation of probability suggests that in the long run, the relative frequency of an event will converge to its theoretical probability as the number of repetitions increases. In the context of this experiment, this means that as we conduct more and more trials of flipping a fair coin 15 times, the proportion of times we observe exactly 7 heads should approach the theoretical probability of P(X=7).By plotting the evolution of the relative frequency of obtaining exactly 7 heads in 15 coin tosses, we can observe how the observed relative frequency converges towards the theoretical probability calculated using the binomial distribution formula. This convergence supports the frequentist interpretation of probability, indicating that empirical observations tend to align with theoretical expectations as the number of repetitions increases.Therefore, the frequentist interpretation of probability is validated by the observed behavior of the relative frequency in our experiment, confirming the convergence towards the theoretical probability of obtaining 7 heads in 15 coin tosses.

# d. 
E_X <- 15 * 0.5
observed_average <- mean(num_heads)
cat("E(X):", E_X, "\n")
## E(X): 7.5
cat("Observed Average:", observed_average, "\n")
## Observed Average: 7.49902
# Observing the plot, we can see that as the number of repetitions increases, the observed average tends to stabilize around the expected value of 7.5. This behavior confirms the law of large numbers, indicating that as the sample size increases, the sample mean approaches the population mean. Therefore, based on the observed behavior in the plot and the comparison to the theoretical expected value, we can conclude that our observations align with the law of large numbers for this experiment.

# e.
plot(cumsum(num_heads) / seq_len(num_repetitions), type = "l", xlab = "Number of Repetitions", ylab = "Observed Average", main = "Law of Large Numbers for Coin Tosses")
abline(h = E_X, col = "red")

### 2.
n <- 100000

standard_normal <- rnorm(n)

sample_means_normal <- cumsum(standard_normal) / seq_len(n)
true_mean_normal <- 0  
plot(sample_means_normal, type = "l", xlab = "Number of Samples", ylab = "Sample Mean", main = "Law of Large Numbers for Standard Normal Distribution")
abline(h = true_mean_normal, col = "red")

exponential <- rexp(n, rate = 5)

sample_means_exponential <- cumsum(exponential) / seq_len(n)
true_mean_exponential <- 1/5  
plot(sample_means_exponential, type = "l", xlab = "Number of Samples", ylab = "Sample Mean", main = "Law of Large Numbers for Exponential Distribution (lambda = 5)")
abline(h = true_mean_exponential, col = "red")

### 3.

num_repetitions_binomial <- 10000
X_sim <- rbinom(num_repetitions_binomial, 21, 3/5)
Y_sim <- rbinom(num_repetitions_binomial, 19, 3/5)

sums <- X_sim + Y_sim

hist(sums, breaks = seq(0, 40, by = 1), freq = FALSE, main = "Sum of X and Y (Binomial Distributions)", xlab = "Sum", ylab = "Density")

# The sample means of both the standard normal distribution and the exponential distribution appear to converge towards their respective population means as the number of samples increases, validating the law of large numbers. The histogram of the sum of X and Y from binomial distributions displays a distribution that supports the sum X + Y following a binomial distribution Bin(40, 3/5).

### 4.
# a. 
n <- 1000000
X <- runif(n, -1, 1)
g_X <- sqrt(1 - X^2)
pi_estimate <- mean(g_X) * 2
cat("Estimated Value of π:", pi_estimate, "\n")
## Estimated Value of π: 1.570806
# b. 
f <- function(t) sqrt(1 + t^2 + t^4)
integral_estimate <- mean(f(runif(n, 0, 2)))
cat("Estimated Value of Integral:", integral_estimate, "\n")
## Estimated Value of Integral: 2.095965
### 5. 
num_repetitions_binomial <- 10000
X_sim <- rbinom(num_repetitions_binomial, 21, 3/5)
Y_sim <- rbinom(num_repetitions_binomial, 19, 3/5)

# Draw relative frequency histogram
hist(X_sim + Y_sim, breaks = seq(0, 40, by = 1), freq = FALSE, main = "Sum of X and Y", xlab = "Sum", ylab = "Density")

# The highest density occurs when the sum of X and Y is between 20 and 30. This observation suggests that the most common outcomes fall within this range. It does not align with the expectation of the sum being centered around 24. Therefore, the outcome does not strongly support the claim that X + Y follows a Binomial distribution with parameters (40, 3/5).