1.

a.

set.seed(1)
n <- 100

i.

Xi <- runif(n, min = -10, max = 10)
hist(Xi, xlab = "Xi", freq = FALSE)

ii.

(Not graded)

alpha <- 1
beta <- 1
Xi <- rbeta(n, shape1 = alpha, shape2 = beta)
hist(Xi, xlab = "Xi", freq = FALSE)

iii.

For any \(X \sim Gamma (\alpha, \beta)\), where \(\alpha\) is the shape parameter and \(\beta\) is the rate parameter, the mean and variance are \[ E (X) = \frac{\alpha}{\beta} , \qquad Var (X) = \frac{\alpha}{\beta^2} . \] Then, we have \[ \alpha = \frac{\left(E (X) \right)^2}{Var (X)} , \qquad \beta = \frac{E (X)}{Var (X)} . \] In this question, we have mean \(0.5\) and variance \(1\). So, \[ \alpha = \frac{0.5 ^ 2}{1} = 0.25 , \qquad \beta = \frac{0.5}{1} = 0.5 . \]

alpha <- 0.25
beta <- 0.5
Xi <- rgamma(n, shape = alpha, rate = beta)

### Quick check
### Close when n is large enough
mean(Xi)
[1] 0.5346663
var(Xi)
[1] 0.8240147
hist(Xi, xlab = "Xi", freq = FALSE)

iv.

(Not graded)

p <- 0.5
n.binom <- 4
Xi <- rbinom(n, size = n.binom, prob = p)
hist(Xi, xlab = "Xi", freq = FALSE, breaks = seq(0 - 0.25, n.binom + 0.25, 0.5))

v.

Note that rgeom is trying to model the number of failures in a sequence of Bernoulli trials before success occurs. So, to get the number of trials, we need to add \(1\) to the number of failures generated.

p <- 0.5
p.geom <- 0.2
### Number of trials ni
n.binom <- rgeom(n, prob = p.geom) + 1

Xi <- rbinom(n, size = n.binom, prob = p)
hist(Xi, xlab = "Xi", freq = FALSE, breaks = seq(0 - 0.25, max(Xi) + 0.25, 0.5))

b.

set.seed(2)
n <- 30
df <- 2
X <- rchisq(n, df = df)

i.

### Histogram
hist(X, xlab = "X", freq = FALSE)

### Overlay the density
plot.range <- seq(min(X) - 1, max(X) + 1, length.out = 1000)
lines(plot.range, dchisq(plot.range, df = df))

ii.

(Not graded)

### Plot empirical cdf
Fn <- ecdf(X)
plot(Fn, xlab = "X")

### Overlay true cdf
lines(plot.range, pchisq(plot.range, df = df), col = 2, lty = 2)

### Add legend
legend("bottomright", c("Empirical cdf", "True cdf"), col = 1:2, lty = 1:2)

iii.

probs <- seq(0.1, 0.9, 0.1)

### Empirical deciles
edec <- quantile(X, probs = probs)

### True deciles
dec <- qchisq(probs, df = df)

### Scatter plot
plot(dec, edec, xlab = "True deciles", ylab = "Empirical deciles")