-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Mario
Sent: Monday, August 25, 2008 1:41 PM
To: r-help@r-project.org
Subject: [R] Two envelopes problem
A friend of mine came to me with the two envelopes problem, I
hadn't heard of this problem before and it goes like this:
someone puts an amount `x' in an envelope and an amount `2x'
in another. You choose one envelope randomly, you open it,
and there are inside, say £10. Now, should you keep the £10
or swap envelopes and keep whatever is inside the other
envelope? I told my friend that swapping is irrelevant since
your expected earnings are 1.5x whether you swap or not. He
said that you should swap, since if you have £10 in your
hands, then there's a 50% chance of the other envelope having
£20 and 5% chance of it having £5, so your expected earnings
are £12.5 which is more than £10 justifying the swap. I told
my friend that he was talking non-sense. I then proceeded to
write a simple R script (below) to simulate random money in
the envelopes and it convinced me that the expected earnings
are simply
1.5 * E(x) where E(x) is the expected value of x, a random
variable whose distribution can be set arbitrarily. I later
found out that this is quite an old and well understood
problem, so I got back to my friend to explain to him why he
was wrong, and then he insisted that in the definition of the
problem he specifically said that you happened to have £10
and no other values, so is still better to swap. I thought
that it would be simply to prove in my simulation that from
those instances in which £10 happened to be the value seen in
the first envelope, then the expected value in the second
envelope would still be £10. I run the simulation and
surprisingly, I'm getting a very slight edge when I swap,
contrary to my intuition. I think something in my code might
be wrong. I have attached it below for whoever wants to play
with it. I'd be grateful for any feedback.
# Envelopes simulation:
#
# There are two envelopes, one has certain amount of money
`x', and the other an # amount `r*x', where `r' is a positive
constant (usaully r=2 or r=0.5).
You are
# allowed to choose one of the envelopes and open it. After
you know the amount # of money inside the envelope you are
given two options: keep the money from # the current envelope
or switch envelopes and keep the money from the second #
envelope. What's the best strategy? To switch or not to switch?
#
# Naive explanation: imagine r=2, then you should switch
since there is a 50% # chance for the other envelope having
2x and 50% of it having x/2, then your # expected earnings
are E = 0.5*2x + 0.5x/2 = 1.25x, since 1.25x > x you # should
switch! But, is this explanation right?
#
# August 2008, Mario dos Reis
# Function to generate the envelopes and their money # r:
constant, so that x is the amount of money in one envelop and
r*x is the
# amount of money in the second envelope
# rdist: a random distribution for the amount x # n: number
of envelope pairs to generate # ...: additional parameters
for the random distribution # The function returns a 2xn
matrix containing the (randomized) pairs # of envelopes
generateenv <- function (r, rdist, n, ...) {
env <- matrix(0, ncol=2, nrow=n)
env[,1] <- rdist(n, ...) # first envelope has `x'
env[,2] <- r*env[,1] # second envelope has `r*x'
# randomize de envelopes, so we don't know which one from
# the pair has `x' or `r*x'
i <- as.logical(rbinom(n, 1, 0.5))
renv <- env
renv[i,1] <- env[i,2]
renv[i,2] <- env[i,1]
return(renv) # return the randomized envelopes }
# example, `x' follows an exponential distribution with E(x)
= 10 # we do one million simulations n=1e6) env <-
generateenv(r=2, rexp, n=1e6, rate=1/10)
mean(env[,1]) # you keep the randomly assigned first envelope
mean(env[,2]) # you always switch and keep the second
# example, `x' follows a gamma distributin, r=0.5 env <-
generateenv(r=.5, rgamma, n=1e6, shape=1, rate=1/20)
mean(env[,1]) # you keep the randomly assigned first envelope
mean(env[,2]) # you always switch and keep the second
# example, a positive 'normal' distribution # First write
your won function:
rposnorm <- function (n, ...)
{
return(abs(rnorm(n, ...)))
}
env <- generateenv(r=2, rposnorm, n=1e6, mean=20, sd=10)
mean(env[,1]) # you keep the randomly assigned first envelope
mean(env[,2]) # you always switch and keep the second
# example, exponential approximated as an integer rintexp <-
function(n, ...) return (ceiling(rexp(n, ...))) # we use
ceiling as we don't want zeroes env <- generateenv(r=2,
rintexp, n=1e6, rate=1/10)
mean(env[,1]) # you keep the randomly assigned first envelope
mean(env[,2]) # you always switch and keep the second i10 <-
which(env[,1]==10)
mean(env[i10,1]) # Exactly 10
mean(env[i10,2]) # ~ 10.58 - 10.69 after several trials
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.