## Q1 
# (c)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#paramaters for Ireland
lambda_I <- 1
p_I <- 0.95

num_sims <- 10000 #number of simulations

# Simulate the number of tries and successful scored by Ireland
tries <- rpois(num_sims, lambda_I)
successful_conversions <- rbinom(num_sims, tries, p_I)

# Calculate the total number of points 
points <- tries * 5 + successful_conversions * 2

# Plot the probability distribution
hist(points, breaks = seq(0, max(points) + 1, by = 1), prob = TRUE,
     col = "red", border = "black", xlim = c(0, max(points) + 1),
     main = "Probability Distribution of Points Scored by Ireland in a Game",
     xlab = "Points", ylab = "Probability")

#(d)

# number of simulations where Ireland score > 10 points
points_above_10 <- sum(points > 10)

# Calculate the probability
prob_above_10 <- points_above_10 / num_sims

cat("Probability that Ireland scores > 10 points:", prob_above_10, "\n")
## Probability that Ireland scores > 10 points: 0.2666
# (g) 
# Set parameters for Ireland
lambda_I <- 1  # mean number of tries for Ireland
p_I <- 0.95  # probability of successful conversion for Ireland

# Set parameters for France
lambda_F <- 2  # mean number of tries for France
p_F <- 0.7  # probability of successful conversion for France

# Number of simulations
num_simulations <- 10000

# Simulate the number of tries scored by both teams
tries_I <- rpois(num_simulations, lambda_I)
tries_F <- rpois(num_simulations, lambda_F)

# Simulate the number of successful conversions by both teams
successful_conv_I <- rbinom(num_simulations, tries_I, p_I)
successful_conv_F <- rbinom(num_simulations, tries_F, p_F)

# Calculate the total points for both teams
points_I <- tries_I * 5 + successful_conv_I * 2
points_F <- tries_F * 5 + successful_conv_F * 2

# Determine winner based on total points
ireland_win <- points_I > points_F

# Calculate the probability that Ireland win
probability_ireland_win <- sum(ireland_win) / num_simulations

# Print the result
cat("Probability that Ireland wins against France:", probability_ireland_win, "\n")
## Probability that Ireland wins against France: 0.2412
## Q2 (c) 
# Define matrix P
P <- matrix(c(0.6, 0.4, 0, 0,0.2,0.6,0.2,0,0.2,0,0.6,0.2,0,0,0.2,0.8), nrow = 4, ncol = 4)
P2 <- P %*% P
print(P2)
##      [,1] [,2] [,3] [,4]
## [1,] 0.44 0.28 0.24 0.04
## [2,] 0.48 0.44 0.08 0.00
## [3,] 0.08 0.24 0.40 0.28
## [4,] 0.00 0.04 0.28 0.68
P3 <- P2 %*% P
print(P3)
##       [,1]  [,2]  [,3]  [,4]
## [1,] 0.376 0.304 0.240 0.080
## [2,] 0.464 0.376 0.144 0.016
## [3,] 0.144 0.240 0.312 0.304
## [4,] 0.016 0.080 0.304 0.600