Serving the Quantitative Finance Community

 
User avatar
Junior08
Topic Author
Posts: 0
Joined: May 13th, 2009, 11:38 am

Jelly beans

May 13th, 2009, 1:19 pm

Got this at a junior analyst position at a fund.You have a bag of jelly beans in both of your pockets. Each bag contains 4 jelly beans. You eat one jelly bean at a time, each time reaching into a random bag... eventually, you reach into a bag and it's empty. What is the expected number of jelly beans remaining in the other bag. Part two... what is each bag contains 100 beans?
Last edited by Junior08 on May 12th, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
MCarreira
Posts: 64
Joined: January 1st, 1970, 12:00 am

Jelly beans

May 13th, 2009, 3:43 pm

I have:f[n_] := Sum[((Binomial[n - 1 + k, k] + Binomial[n - 1 + k, n - 1])/2^(n - 1 + k) (1/2)) (n - k), {k, 0, n - 1}];f[4]=35/16=2.1875N[f[100]]=11.27
 
User avatar
FalsePositive
Posts: 4
Joined: March 10th, 2009, 1:12 am

Jelly beans

May 13th, 2009, 4:51 pm

QuoteOriginally posted by: MCarreiraI have:f[n_] := Sum[((Binomial[n - 1 + k, k] + Binomial[n - 1 + k, n - 1])/2^(n - 1 + k) (1/2)) (n - k), {k, 0, n - 1}];f[4]=35/16=2.1875N[f[100]]=11.27Do you mean this?I'll appreciate if you can explain where each term comes from. Most of us in this forum are not expert in probability..
 
User avatar
MCarreira
Posts: 64
Joined: January 1st, 1970, 12:00 am

Jelly beans

May 13th, 2009, 5:11 pm

Visualize a Pascal Triangle.You can reach state RRRR with probability Binomial[4,0]/2^4, in which case you'll have 4 JB on your left pocket.You can reach state LLLL with probability Binomial[4,0]/2^4, in which case you'll have 4 JB on your right pocket.You can think of reaching each of these states as reaching the previous state (Binomial[3,0]/2^3 or Binomial[3,3]/2^3) and having a probability of 1/2 to reach the desired state.That means that you can define the probability of reaching state {{R,4},{L,m}} as the probability of reaching state {{R,3},{L,m}} times (1/2), as you don't need to consider the transition {{R,4},{L,m-1}} => {{R,4},{L,m}}.You then add those probabilities (at each extra round you'll "prune" the triangle) weighted by the number of remaining JBs.I hope it makes sense.
 
User avatar
everforget
Posts: 0
Joined: October 29th, 2007, 11:16 pm

Jelly beans

May 13th, 2009, 5:26 pm

Assume you have K beans left, then you have picked totally x = ( n - k ) + n + 1 times. so you need to get the prob for toss a coin x times, get exact n heads( tails ) for first x - 1 try and get heads( tails ) again in last toss. Rember k = 0 will be a special case, where the last toss could be either heads or tails. the below fomula and answer are amiss. Should be much smaller.
Last edited by everforget on May 12th, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
everforget
Posts: 0
Joined: October 29th, 2007, 11:16 pm

Jelly beans

May 13th, 2009, 5:37 pm

MCarrira, your solution should be right if you not times 1/2 directly but considering the special case which both reach empty state.
 
User avatar
MCarreira
Posts: 64
Joined: January 1st, 1970, 12:00 am

Jelly beans

May 13th, 2009, 6:11 pm

Looking at only the probabilities for each "round":h[n_] := Table[((Binomial[n - 1 + k, k] + Binomial[n - 1 + k, n - 1])/2^(n - 1 + k) (1/2)), {k, 0, n - 1}];h[4] = {1/8, 1/4, 5/16, 5/16}1/8 is equal to 1/16 (RRRR) + 1/16 (LLLL)1/4 is equal to 4/16 (RRRL) * 1/2 + 4/16 (RLLL) * 1/25/16 is equal to ( 4/16 (RRRL) * 1/2 + 4/16 (RLLL) * 1/2 ) * 1/2 + ( 6/16 (RRLL) * ( 1/4 + 1/4 ) )So the probabilities seem ok to me, and h[4].{4,3,2,1} = 35/16
 
User avatar
karakfa
Posts: 0
Joined: May 25th, 2002, 5:05 pm

Jelly beans

May 13th, 2009, 8:59 pm

35/16 is correct. everforget: Both can't reach to zero, when one reaches the process ends.
 
User avatar
FalsePositive
Posts: 4
Joined: March 10th, 2009, 1:12 am

Jelly beans

May 14th, 2009, 1:07 am

QuoteOriginally posted by: MCarreiraVisualize a Pascal Triangle.You can reach state RRRR with probability Binomial[4,0]/2^4, in which case you'll have 4 JB on your left pocket.You can reach state LLLL with probability Binomial[4,0]/2^4, in which case you'll have 4 JB on your right pocket.You can think of reaching each of these states as reaching the previous state (Binomial[3,0]/2^3 or Binomial[3,3]/2^3) and having a probability of 1/2 to reach the desired state.That means that you can define the probability of reaching state {{R,4},{L,m}} as the probability of reaching state {{R,3},{L,m}} times (1/2), as you don't need to consider the transition {{R,4},{L,m-1}} => {{R,4},{L,m}}.You then add those probabilities (at each extra round you'll Thank you for the nice explanation. I thought about it again and came up with a simpler solution:Assuming the right pocket is the one which gets emptied first we can have the following possible sequences with the corresponding probabilities:P(RRR) R : C(3,0) * (1/2)^3P(RRRL) R : C(4,1) * (1/2)^4P(RRRLL) R : C(5,2) * (1/2)^5P(RRRLLL) R : C(6,3) * (1/2)^6Here P means all the possible permutations. Therefore E = (4 * 1 / 8) + (3 * 4 / 16) + (2 * 10 / 32) + (1 * 20 / 64) = 2.1875which is the same as your result. Hence the formula for n jelly beans in each pocket can be written as
Last edited by FalsePositive on May 17th, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
tarunvirsingh
Posts: 0
Joined: July 19th, 2008, 3:36 am

Jelly beans

May 14th, 2009, 4:28 am

I think that we need to replace n by (n+1) in our calculations. This is because we do not have to find “the expected number of JB left when one bag gets empty”, rather, we have to find the “ expected number of JB left when we reach again into an empty bag” (in other words, we have to find the number of JB left when one bag has got (-1) beans left in it.)
 
User avatar
Junior08
Topic Author
Posts: 0
Joined: May 13th, 2009, 11:38 am

Jelly beans

May 14th, 2009, 7:13 am

QuoteOriginally posted by: tarunvirsinghI think that we need to replace n by (n+1) in our calculations. This is because we do not have to find “the expected number of JB left when one bag gets empty”, rather, we have to find the “ expected number of JB left when we reach again into an empty bag” (in other words, we have to find the number of JB left when one bag has got (-1) beans left in it.)Yeah, it's reaching into an empty bag. So for the first part of the problem, RRRRR is a possible sequence, leaving 4 beans in the other bag. RRRRLLLLL is also a possible sequence, leaving 0 beans.
Last edited by Junior08 on May 13th, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
everforget
Posts: 0
Joined: October 29th, 2007, 11:16 pm

Jelly beans

May 14th, 2009, 7:15 am

Why can't both reach 0? Did you read the puzzle carefully? A simple MC will prove the number should be around 1.4, not 35 / 16, which hugely underestimate the prob for 0 jb left and in turn increased the expectation.
 
User avatar
quantyst
Posts: 0
Joined: June 4th, 2008, 5:08 am

Jelly beans

May 14th, 2009, 8:38 am

QuoteOriginally posted by: Junior08Got this at a junior analyst position at a fund.You have a bag of jelly beans in both of your pockets. Each bag contains 4 jelly beans. You eat one jelly bean at a time, each time reaching into a random bag... eventually, you reach into a bag and it's empty. What is the expected number of jelly beans remaining in the other bag. Part two... what is each bag contains 100 beans?Solution:Let's generalize the problem.Assume that in the beginning the left pocket has m beans, and the right pocket has n beans. And let E(m,n) denote the expected number of beans in the non-empty pocket when one of the pockets becomes empty.Clearly E(m,n)=E(n,m), E(m,0)=m, and E(0,n)=n.On the first pick of a bean, either the left pocket is decreased by one bean with probability 1/2 or the right pocket is decreased by one bean with probability 1/2. ThusE(m,n)=(1/2)E(m-1,n)+(1/2)E(m,n-1).All we need to do now is to solve the above recursion.Or, we can iterate it until we get the required result.
 
User avatar
everforget
Posts: 0
Joined: October 29th, 2007, 11:16 pm

Jelly beans

May 14th, 2009, 9:48 am

Again, the method is good but you misunderstood the puzzle. E( 0, n ) is not n.
 
User avatar
Traden4Alpha
Posts: 3300
Joined: September 20th, 2002, 8:30 pm

Jelly beans

May 14th, 2009, 10:29 am

QuoteOriginally posted by: everforgetAgain, the method is good but you misunderstood the puzzle. E( 0, n ) is not n.Good point! The halting condition isn't "when the pocket is empty," it's when you next reach into the pocket and find it empty. This adds one more iteration to the Pascal triangle (Pascal's Square?) and makes (0,0) reachable.E(0,n) = n - 1 + (1/2)^n