cases=matrix(c(c(10,5),c(10,20)),nrow=2,ncol=2,byrow=FALSE) number_of_envelope_pairs=10000 resulting_envelope_value_C=rep(0,number_of_envelope_pairs) #Case_C for(i in 1:number_of_envelope_pairs){ my_envelope_pair=cases[,rbinom(1,1,0.5)+1] after_switch=my_envelope_pair[2] #this chooses "the other one than 10" resulting_envelope_value_C[i]=after_switch } case_C_gain=mean(resulting_envelope_value_C-10) case_C_gain #can see it's 2.5
#Something that sounds like Case_A
my_envelopes=rep(0,number_of_envelope_pairs)
my_envelopes_switch_gain=my_envelopes
for(i in 1:number_of_envelope_pairs){
case_index=rbinom(1,1,0.5)+1
envelope_index=rbinom(1,1,0.5)+1
my_envelopes[i]=cases[envelope_index, case_index]
my_envelopes_switch_gain[i]=cases[-envelope_index, case_index]-my_envelopes[i]
}
mean(my_envelopes_switch_gain) #approx 0, this is if you don't open the envelope
#illustrating the conditioning, NB this is not the same as conditioning on
#the pair being (5,10) or the pair being (10,20)
#this is the gain given the chosen envelope is 10 and the
#other envelope is known to be 5 or 20.
mean(my_envelopes_switch_gain[my_envelopes==10])
#this gives you approx 2.5 as we saw in case C.
#Thing which is actually Case_A
my_envelopes=rep(0,number_of_envelope_pairs)
my_envelopes_switch_gain=my_envelopes
for(i in 1:number_of_envelope_pairs){
what_case_am_i_in_given_i_have_10=rbinom(1,1,0.5)+1
#random assignment of case, 10 provides no knowledge of case
five_ten_switch_gain_given_random_envelope=0
#conditioning on pair being 5,10, gain is known
ten_twenty_switch_gain_given_random_envelope=0
#conditioning on pair being 10,20, gain is known
case_gains=c(five_ten_switch_gain_given_random_envelope,
ten_twenty_switch_gain_given_random_envelope)
my_envelopes_switch_gain[i]=case_gains[what_case_am_i_in_given_i_have_10]
}
mean(my_envelopes_switch_gain)#this is just 0
two.envelopes <- function(){
x <- (runif(1))
x <- (abs(as.numeric(format(round(x, 3)))))*10
#randomly selects a number
#limits the number of decimal places x can have and muiltples x by 10 to simluate realistic dollar values.
p <- c(x,2*x)
A <- sample(p, 1, replace=F)
#creates a vector with x and 2x then randomly selects one for A.
if (A == x) {
B <- 2*x
} else {
(B <- x)
}
return(c(A,B))
}
two.envelopes <- function(){
x <- (rnorm(1))
x <- (abs(as.numeric(format(round(x, 3)))))*10
#randomly selects a number
#limits the number of decimal places x can have and muiltples x by 10 to simluate realistic dollar values.
p <- c(x,2*x)
A <- sample(p, 1, replace=F)
#creates a vector with x and 2x then randomly selects one for A.
if (A == x) {
B <- 2*x
} else {
(B <- x)
}
return(c(A))
}
#sets the value for B based on: if A = x then B = 2x or if A = 2x then B = x
g <- replicate(10000, two.envelopes())
two.envelopes.s <- function(){
x <- (rnorm(1))
x <- (abs(as.numeric(format(round(x, 3)))))*10
#randomly selects a number
#limits the number of decimal places x can have and muiltples x by 10 to simluate realistic dollar values.
p <- c(x,2*x)
A <- sample(p, 1, replace=F)
#creates a vector with x and 2x then randomly selects one for A.
if (A == x) {
B <- 2*x
} else {
(B <- x)
}
return(c(B))
}
#sets the value for B based on: if A = x then B = 2x or if A = 2x then B = x
g.s <- replicate(10000, two.envelopes())
library(ggplot2)
plot(g)
plot(g.s)
ggplot() + aes(g)+ geom_histogram(binwidth=10, colour="black", fill="white")
ggplot() + aes(g.s)+ geom_histogram(binwidth=10, colour="black", fill="white")
ks.test(g, g.s)
#K-S test results
p-value will be approximate in the presence of ties
Two-sample Kolmogorov-Smirnov test
data: g and g.s
D = 0.0077, p-value = 0.9283
alternative hypothesis: two-sided
two.envelopes <- function(){
x <- (rnorm(1))
x <- (abs(as.numeric(format(round(x, 3)))))*10
#randomly selects a number
#limits the number of decimal places x can have and muiltples x by 10 to simluate realistic dollar values.
p <- c(x,2*x)
A <- sample(p, 1, replace=F)
#creates a vector with x and 2x then randomly selects one for A.
if (A == x) {
B <- 2*x
} else {
(B <- x)
}
return(c(A,B))
}
#sets the value for B based on: if A = x then B = 2x or if A = 2x then B = x
g <- t(replicate(100, two.envelopes()))
head(g)
#results
[,1] [,2]
[1,] 5.23 10.46
[2,] 5.48 10.96
[3,] 25.60 12.80
[4,] 6.17 12.34
[5,] 3.88 7.76
[6,] 7.59 15.18
two.envelopes <- function(){
x <- (runif(1))
x <- (abs(as.numeric(format(round(x, 3)))))*10
#randomly selects a number
#limits the number of decimal places x can have and muiltples x by 10 to simluate realistic dollar values.
p <- c(x,2*x)
A <- sample(p, 1, replace=F)
#creates a vector with x and 2x then randomly selects one for A.
if (A == x) {
B <- 2*x
} else {
(B <- x)
}
return(c(A,B))
}
#sets the value for B based on: if A = x then B = 2x or if A = 2x then B = x
g <- t(replicate(100, two.envelopes()))
head(g)
#results
[,1] [,2]
[1,] 27.20 13.60
[2,] 28.72 14.36
[3,] 12.49 24.98
[4,] 12.95 25.90
[5,] 61.18 30.59
[6,] 2.66 1.33
two.envelopes<- function(){
x <- (rcauchy(1, location = 0, scale = 1))
x <- (abs(as.numeric(format(round(x, 3)))))*10
#randomly selects a number
#limits the number of decimal places x can have and muiltples x by 10 to simluate realistic dollar values.
p <- c(x,2*x)
A <- sample(p, 1, replace=F)
#creates a vector with x and 2x then randomly selects one for A.
if (A == x) {
B <- 2*x
} else {
(B <- x)
}
return(c(A,B))
}
#sets the value for B based on: if A = x then B = 2x or if A = 2x then B = x
g <- t(replicate(100, two.envelopes()))
head(g)
[,1] [,2]
[1,] 10.22 20.44
[2,] 24.54 12.27
[3,] 2.05 4.10
[4,] 8.96 4.48
[5,] 15.44 7.72
[6,] 13.74 27.48
two.envelopes <- function(){
x <- (sample(1:100, 1))
#randomly selects a number
p <- c(x,2*x)
A <- sample(p, 1, replace=F)
#creates a vector with x and 2x then randomly selects one for A.
if (A == x) {
B <- 2*x
} else {
(B <- x)
}
return(c(A,B))
}
#sets the value for B based on: if A = x then B = 2x or if A = 2x then B = x
g <- t(replicate(100, two.envelopes()))
head(g)
#results
[,1] [,2]
[1,] 27 54
[2,] 136 68
[3,] 33 66
[4,] 14 28
[5,] 30 60
[6,] 57 114
Get involved in philosophical discussions about knowledge, truth, language, consciousness, science, politics, religion, logic and mathematics, art, history, and lots more. No ads, no clutter, and very little agreement — just fascinating conversations.