Chapter 1

Week 1 lecture 1

  1. Generate a sample of \(n = 20\) \(N(1, 3^2)\) random variates, and without using mean(), var() or sd() write R functions to calculate the sample mean, \(\bar x\), sample variance, \(s^2\), and sample standard deviation, \(s\), where \[ \bar x = \dfrac{1}{n} \sum_{i = 1}^n x_i \text{ and } s^2 = \dfrac{1}{n - 1} \sum_{i = 1}^n (x_i - \bar x)^2. \] Note than sum() may be used.
Solution
n <- 20
y <- rnorm(n, 1, 3)

mean2 <- function(x) sum(x) / length(x)
mean2(y)
mean(y)

var2 <- function(x) sum((x - mean2(x))^2) / (length(x) - 1)
var2(y)
var(y)

sd2 <- function(x) sqrt(var2(x))
sd2(y)
sd(y)
  1. Consider computing \(\text{Pr}(Z \geq z) = 1 - \Phi(z)\) where \(Z \sim \text{Normal}(0, 1)\), or, for short, \(Z \sim N(0, 1)\). For \(z = 0, 0.5, 1, 1.5, 2, \ldots\) compute this in R in three different ways using the following three commands
pnorm(z, lower.tail = FALSE)
1 - pnorm(z)
pnorm(-z)

and find the lowest value of \(z\) for which the three don’t give the same answer.

Solution
pnorms <- function(z) {
cbind(
pnorm(z, lower.tail = FALSE),
1 - pnorm(z),
pnorm(-z)
)
}

vals <- seq(0, 10, by = .5)
pnorms(vals)
  1. The formula \(\text{Var}(Y) = \text{E}(Y^2) - [\text{E}(Y)]^2\) is sometimes called the ‘short-cut’ variance formula, i.e. a short-cut for \(\text{Var}(Y) = \text{E}[Y - \text{E}(Y)]^2\). Compare computing the biased version of \(\text{Var}(Y)\) using the two formulae above for the samples y1 and y2 below.
y1 <- 1:10
y2 <- y1 + 1e9
Solution
bvar1 <- function(x) {
  mean(x^2) - mean(x)^2
}

bvar2 <- function(x) {
  mean((x - mean(x))^2)
}

bvar1(y1)
bvar2(y1)

bvar1(y2)
bvar2(y2)