#i)Compute 10 random trials if ps=c(0.3, 0.2, 0.2, 0.2, 0.1)
#Probability vector
ps <- c(0.3, 0.2, 0.2, 0.2, 0.1)
#Performing the 10 trials
trials_10 <- sample(1:5, size = 10, replace = TRUE, prob = ps)
print(trials_10)
## [1] 2 1 2 2 5 1 5 2 4 1
#ii)Compute 10,000 random trials and estimate probabilities
set.seed(200)
trials_10000 <- sample(1:5, size = 10000, replace = TRUE, prob = ps)
#Estimated probabilities
estimated_prob <- table(trials_10000) / 10000
print(estimated_prob)
## trials_10000
## 1 2 3 4 5
## 0.2970 0.1959 0.2003 0.2069 0.0999
#Compare with original probabilities ps
comparison <- data.frame(
Outcome = 1:5,
True_prob = ps,
Estimated_prob = as.numeric(estimated_prob)
)
print(comparison)
## Outcome True_prob Estimated_prob
## 1 1 0.3 0.2970
## 2 2 0.2 0.1959
## 3 3 0.2 0.2003
## 4 4 0.2 0.2069
## 5 5 0.1 0.0999
#iii)Expected gain of the game ##payouts: outcome 1 or 2 -> gain = -5, outcome 3 -> gain = -4 (entry fee 5, reward 1), outcome 4 -> gain = -3 (entry fee 5, reward 2), outcome 5 -> gain = 15 (entry fee 5, reward 20)
###E(G), G being her gain ####E(G) = P1(-5) + P2(-5) + P3(-4) + P4(-3) + P5(15) = 0.3(-5) + 0.2(-5) + 0.2(-4) + 0.2(-3) + 0.1(15) = -2.4
##Simulating 10000 games
set.seed(200)
trials <- sample (1:5, size = 10000, replace = TRUE, prob = ps)
#gain function
gain <- function(outcome) {
if (outcome == 1 || outcome == 2) return(-5)
if (outcome == 3) return(-4)
if (outcome == 4) return(-3)
if (outcome == 5) return(15)
}
gains <- sapply(trials,gain)
# Simulated expected gain
mean_gain <- mean(gains)
mean_gain
## [1] -2.3879
cat("Theoretical E(G):", -2.4, "\nsimulated mean gaain:", mean_gain, "\n")
## Theoretical E(G): -2.4
## simulated mean gaain: -2.3879
# Create the data frame with measurements y1 to y4
data <- data.frame(
y1 = c(47.8, 46.4, 46.3, 45.1, 47.6, 52.5, 51.2, 49.8, 48.1, 45.0,
51.2, 48.5, 52.1, 48.2, 49.6, 50.7, 47.2, 53.3, 46.2, 46.3),
y2 = c(48.8, 47.3, 46.8, 45.3, 48.5, 53.2, 53.0, 50.8, 50.8, 47.0,
51.4, 49.2, 52.8, 48.9, 50.4, 51.7, 47.7, 54.6, 47.5, 47.6),
y3 = c(49.0, 47.7, 47.8, 46.1, 48.9, 53.3, 54.3, 50.3, 52.3, 47.3,
51.6, 53.0, 53.7, 49.3, 51.2, 52.7, 48.4, 55.1, 48.1, 51.3),
y4 = c(49.7, 48.4, 48.5, 47.2, 49.3, 53.7, 54.5, 52.7, 54.4, 48.3,
51.9, 55.5, 55.0, 49.8, 51.8, 53.3, 49.5, 55.3, 48.4, 51.8)
)
# Sample mean vector
mean_vector <- colMeans(data)
print(mean_vector)
## y1 y2 y3 y4
## 48.655 49.665 50.570 51.450
# Covariance matrix
cov_matrix <- cov(data)
print(cov_matrix)
## y1 y2 y3 y4
## y1 6.329974 6.237289 5.777000 5.548158
## y2 6.237289 6.512921 6.142053 5.976053
## y3 5.777000 6.142053 6.918000 6.946316
## y4 5.548158 5.976053 6.946316 7.464737
# Determinant of S
det_S <- det(cov_matrix)
print(det_S)
## [1] 1.138019
# Trace of S (sum of diagonal elements)
trace_S <- sum(diag(cov_matrix))
print(trace_S)
## [1] 27.22563
z <- with(data, 3*y1 - 2*y2 + 4*y3 - y4)
# Expected value of z
E_z <- mean(z)
print(E_z)
## [1] 197.465
# Variance of z
var_z <- var(z)
print(var_z)
## [1] 101.7466
w <- with(data, y1 + 3*y2 - y3 + y4)
# Correlation between z and w
cor_zw <- cor(z, w)
print(cor_zw)
## [1] 0.9465789
# Define the new variables
z1 <- with(data, y1 + y2 + y3 + y4)
z2 <- with(data, 2*y1 - 3*y2 + y3 - 2*y4)
z3 <- with(data, -y1 - 2*y2 + y3 - 2*y4)
# Combine into new data frame
Z <- data.frame(z1, z2, z3)
# Sample mean vector of z1, z2, z3
mean_Z <- colMeans(Z)
print(mean_Z)
## z1 z2 z3
## 200.340 -104.015 -200.315
# Covariance matrix
cov_Z <- cov(Z)
print(cov_Z)
## z1 z2 z3
## z1 100.47937 -52.90726 -99.71621
## z2 -52.90726 31.66345 52.90292
## z3 -99.71621 52.90292 100.20134
# Correlation matrix
corr_Z <- cor(Z)
print(corr_Z)
## z1 z2 z3
## z1 1.0000000 -0.9379890 -0.9937807
## z2 -0.9379890 1.0000000 0.9392123
## z3 -0.9937807 0.9392123 1.0000000