# Re: [R] Two envelopes problem

From: <markleeds_at_verizon.net>
Date: Mon, 25 Aug 2008 15:08:48 -0500 (CDT)

i haven't looked at your code and I'll try when I have time but, as you stated, that's an EXTREMELY famous problem that has tried to be posed
in a bayesian way and all sorts of other things have been done to try solve it. Note that if you change the utility function so that its log(X) rather than X
then it is seen that the expected values are the same and you don't get the 1.5X versus X weirdness. When someone showed me that, I gave up and just walked away from it by telling myself that it has something to do with utility and percentage weirdness , sort of like when something in the store is
marked down 50% and then up 50% but it doesn't get back to the original price. ( that's not right but we spent like a month talking about the problem and I got sick of it ).

Also, some have argued that the sample space is ill stated because once you see X, that doesn't tell you about the chances of other X because of the infinite sample space and that's not realistic. At the time, I looked around a lot but couldn't find better answers than those. You're brining back bad memories !!!!!

On Mon, Aug 25, 2008 at 3:40 PM, Mario wrote:

> 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_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help