Part 1

# load the packages we will need
library(data.table)
library(ggplot2)
library(ggpubr)

# clean session
rm(list=ls())


# 1
die1 <- rep(1:4, 4)
die2 <- rep(1:4, each=4)
ans_1a <- data.table(die1, die2)

ans_1b <- data.table(ans_1a)
ans_1b[, X := die1 + die2]

ans_1c <- ans_1b[, sum(X<5)/.N]

if(ans_1b[X==2, .N]==1) {
  ans_1d <- "YES"
} else {
  ans_1d <- "NO"
}


# 2
x <- c(2, 3, 4, 5, 6)
fx <- c(.1, .3, .3, .2, .1)
d <- data.table(x, fx)

if(sum(fx) == 1) {
  ans_2a <- "YES"
} else {
  ans_2a <- "NO"
}
ans_2b <- d[, sum(x*fx)]
ans_2c <- d[x >= 4, sum(fx)]
ans_2d <- d[(x > 2) & (x <= 4), sum(fx)]


# 3
x <- c(0, 1, 2, 3)
fx <- c(0.3, 0.4, 0.2, 0.1)
d <- data.table(x, fx)

ans_3a <- d[x >= 2, sum(fx)]
ans_3b <- d[(x > 0) & (x <= 2), sum(fx)]
ans_3c <- d[, sum(x*fx)]
ans_3d <- d[, sum((x-ans_3c)^2*fx)]
ans_3e <- sqrt(ans_3d)


# 4 
ans_4a <- 0
ans_4b <- 0*0.64 + 1*0.32 + 2*0.04
ans_4c <- 'upper left'
ans_4d <- 'lower left'

Part 2

# load the packages we will need
library(data.table)
library(ggplot2)
library(ggpubr)

# clean session
rm(list=ls())

# 1
ans_1a <- TRUE
ans_1b <- FALSE
ans_1c <- TRUE
ans_1d <- FALSE

# 2
mu <- 15
sigma <- 2.74
ans_2a <- pnorm(17, mu, sigma, lower.tail=F)
ans_2b <- pnorm(14, mu, sigma, lower.tail=T)
ans_2c <- pnorm(20, mu, sigma, lower.tail=F)
ans_2d <- pnorm(13, mu, sigma, lower.tail=T)

n <- 30
p <- 0.5
ans_2e <- pbinom(17, n, p, lower.tail=F)
ans_2f <- pbinom(14, n, p, lower.tail=T)
ans_2g <- pbinom(20-1, n, p, lower.tail=F)
ans_2h <- pbinom(13-1, n, p, lower.tail=T)

# i
# Binomial(n, p) is well approximated by a Normal with mean
# np and variance np(1-p) when n is big enough (about 30)

# 3
ans_3a <- "IV"
ans_3b <- "VII"
ans_3c <- "VIII"
ans_3d <- "VI" 
ans_3e <- "I" 
ans_3f <- "III" 
ans_3g <- "II" 
ans_3h <- "V" 

# 4
d1 <- 5
d2 <- 100
n = 1000

mu_x <- (d2/(d2-2))
sig_x <- (2*(d2^2)*(d1+d2-2))/(d1*(d2-2)^2*(d2-4))

ans_4a <- "Skewed"
ans_4b <- "Decreases"
ans_4c <- "Decreases"
ans_4d <- mu_x
ans_4e <- sig_x / n