
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
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.