<- rep(NA, 10)
output for(i in 1:5){
<- i
output[i]
}
6] output[
[1] NA
<- rep(NA, 10)
output for(i in 1:5){
<- i
output[i]
}
6] output[
[1] NA
<- rep(0, 10)
output for(i in 1:10){
<- i
output[i]
}
6] output[
[1] 6
<- rep(0, 10)
output for(i in 1:10){
<- i
output[i]
}
11] output[
[1] NA
<- rep(1, 10)
output for(i in 2:10){
<- i + output[i-1]
output[i]
}
5] output[
[1] 15
<- rep(1, 10)
output for(i in 2:10){
<- i + output[i+1]
output[i]
}
5] output[
[1] 6
<- 10
x <- function(x = 11){
test_fun return(x)
}test_fun()
[1] 11
x
[1] 10
<- 10
x <- function(y = 11){
test_fun return(x + 1)
}test_fun()
[1] 11
x
[1] 10
<- 10
x <- function(y = 11){
test_fun <- x + 1
x return(x + 1)
}test_fun()
[1] 12
x
[1] 10
<- 10
x <- function(x = 11){
test_fun <- x + 1
x return(x + 1)
}test_fun()
[1] 13
x
[1] 10
<- 10
x <- function(x = 11){
test_fun <- x + 1
x return(x + 1)
}<- test_fun(x)
x x
[1] 12
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\).
<- function(x){
my_sd <- length(x)
n 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):
= len(x)
n if(n == 1):
return(0)
else:
return(np.sqrt( 1.0/(n-1) * np.sum((x - np.mean(x))**2) ))
1,2,4])) my_sd(np.array([
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}\]
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).<- function(x, p=2){
p_norm 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))
1, 1, 1]) p_norm([
1.7320508075688772
1, 2, 3], 1) p_norm([
6.0
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<- list(mean, sd, list(c(0, 1, 2), function(x){return(x^(1/3))})) x
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.
set.seed(91)
<- 1000
nsim <- rep(NA, nsim)
results for(i in 1:nsim){
<- sample(c("red", "blue"), 3, replace=T)
hats <- c(sample(c("red", "blue"), 1), "pass", "pass")
guesses <- guesses[1] == hats[1]
results[i]
}
mean(results)
[1] 0.48
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.
<- 1000
nsim <- rep(NA, nsim)
results for(i in 1:nsim){
<- sample(c("red", "blue"), 3, replace=T)
hats <- rep(NA, 3)
guesses for(j in 1:3){
if(length(unique(hats[-j])) == 1){
<- ifelse(unique(hats[-j]) == "red", "blue", "red")
guesses[j] else {
} <- "pass"
guesses[j]
}
}
<- sum(guesses[guesses != "pass"] == hats[guesses != "pass"]) ==
results[i] length(guesses[guesses != "pass"])
}
mean(results)
[1] 0.751