Exam 1 review solutions

Author

Ciaran Evans

output <- rep(NA, 10)
for(i in 1:5){
  output[i] <- i
}

output[6]
[1] NA
output <- rep(0, 10)
for(i in 1:10){
  output[i] <- i
}

output[6]
[1] 6
output <- rep(0, 10)
for(i in 1:10){
  output[i] <- i
}

output[11]
[1] NA
output <- rep(1, 10)
for(i in 2:10){
  output[i] <- i + output[i-1]
}

output[5]
[1] 15
output <- rep(1, 10)
for(i in 2:10){
  output[i] <- i + output[i+1]
}

output[5]
[1] 6
x <- 10
test_fun <- function(x = 11){
  return(x)
}
test_fun()
[1] 11
x
[1] 10
x <- 10
test_fun <- function(y = 11){
  return(x + 1)
}
test_fun()
[1] 11
x
[1] 10
x <- 10
test_fun <- function(y = 11){
  x <- x + 1
  return(x + 1)
}
test_fun()
[1] 12
x
[1] 10
x <- 10
test_fun <- function(x = 11){
  x <- x + 1
  return(x + 1)
}
test_fun()
[1] 13
x
[1] 10
x <- 10
test_fun <- function(x = 11){
  x <- x + 1
  return(x + 1)
}
x <- test_fun(x)
x
[1] 12

Practice with functions

The sample standard deviation of numbers \(x_1,...,x_n\) is given by

\[\widehat{\sigma}^2 = \frac{1}{n-1}\sum \limits_{i=1}^n (x_i - \bar{x})^2,\]

where \(\bar{x} = \frac{1}{n} \sum \limits_{i=1}^n x_i\).

my_sd <- function(x){
  n <- length(x)
  if(n == 1){
    return(0)
  } else {
    return(sqrt(1/(n-1) * sum((x - mean(x))^2)))
  }
}

# checking that it works
my_sd(c(1,2,4))
[1] 1.527525
sd(c(1,2,4))
[1] 1.527525
import numpy as np

def my_sd(x):
  n = len(x)
  if(n == 1):
    return(0)
  else:
    return(np.sqrt( 1.0/(n-1) * np.sum((x - np.mean(x))**2) ))

my_sd(np.array([1,2,4]))
1.5275252316519465

The \(\ell_p\) norm of a vector \(x = (x_1,...,x_k)\) is given by

\[||x||_p = \left( \sum \limits_{i=1}^k |x_i|^p \right)^{1/p}\]

  1. Write a function called p_norm in R, which takes two inputs: a vector x, and p, and returns \(\ell_p(x)\). Make p = 2 the default value (this corresponds to the usual Euclidean norm).
p_norm <- function(x, p=2){
  return((sum(abs(x)^p))^(1/p))
}

p_norm(c(1, 1, 1)) # = sqrt(3)
[1] 1.732051
p_norm(c(1, 2, 3), 1) # = 6
[1] 6
def p_norm(x, p=2):
  return(np.sum(np.abs(x)**p)**(1.0/p))

p_norm([1, 1, 1])
1.7320508075688772
p_norm([1, 2, 3], 1)
6.0

Practice with lists

  1. Create a list x in R such that:
  • x[[1]] returns the function mean
  • x[[2]] returns the function sd
  • x[[3]][[1]] returns the vector c(0, 1, 2)
  • x[[3]][[2]] returns an anonymous function which calculates the cube root of a vector
x <- list(mean, sd, list(c(0, 1, 2), function(x){return(x^(1/3))}))

Practice with probability simulations

Three players enter a room and a red or blue hat is placed on each person’s head. The color of each hat is determined by [an independent] coin toss (so, any combination of red and blue hats is possible). No communication of any sort is allowed, except for an initial strategy session before the game begins. Once they have had a chance to look at the other hats [but not their own], the players must simultaneously guess the color of their own hats or pass. The players win the game if at least one person guesses correctly, and no one guesses incorrectly.

  1. Here is one strategy: one player randomly guesses the color of their hat, while the other two players pass. Write a simulation to estimate the probability the players win the game (the true probability is 1/2).
set.seed(91)

nsim <- 1000
results <- rep(NA, nsim)
for(i in 1:nsim){
  hats <- sample(c("red", "blue"), 3, replace=T)
  guesses <- c(sample(c("red", "blue"), 1), "pass", "pass")
  results[i] <- guesses[1] == hats[1]
}

mean(results)
[1] 0.48
  1. Here is another strategy: if a player sees the same color on the other two hats, they guess the color they do not see. If a player sees different colors on the other two hats, they pass. For example: If players A, B, and C have hats red, blue, and blue respectively, then player A would guess red, player B would pass, and player C would pass. Write a simulation to estimate the probability the players win the game with this new strategy (the true probability is 3/4).

Note: For the exam, I am more interested in the logic of how you approach the simulation, than in your code syntax being perfect. Your code should be mostly correct, but a few minor errors isn’t an issue.

nsim <- 1000
results <- rep(NA, nsim)
for(i in 1:nsim){
  hats <- sample(c("red", "blue"), 3, replace=T)
  guesses <- rep(NA, 3)
  for(j in 1:3){
    if(length(unique(hats[-j])) == 1){
      guesses[j] <- ifelse(unique(hats[-j]) == "red", "blue", "red")
    } else {
      guesses[j] <- "pass"
    }
  }
  
  results[i] <- sum(guesses[guesses != "pass"] == hats[guesses != "pass"]) == 
    length(guesses[guesses != "pass"])
}

mean(results)
[1] 0.751