- #1
member 428835
Hi PF!
Take a deck of cards. 26 are red and 26 are black. You draw cards randomly without replacement: if the card is red you gain a dollar, if black you lose a dollar. What's the expected value?
My thought process was to look at simple cases and build to a 52 card deck. We know if ##r=0## expected payout is 0, and if ##b=0## expected payout is ##r##. Otherwise the following rule should govern the expected payout ##E(r,b)## of the process:
$$E(r,b) = \frac{r}{r+b}(1 + E(r-1,b) ) + \frac{b}{r+b}(-1 + E(r,b-1) )$$
Obviously we draw another card when ##E(r,b)>0##, else we stop. My question is how to program this? I've tried the following but am stumped as to what's next. Can you help?
Take a deck of cards. 26 are red and 26 are black. You draw cards randomly without replacement: if the card is red you gain a dollar, if black you lose a dollar. What's the expected value?
My thought process was to look at simple cases and build to a 52 card deck. We know if ##r=0## expected payout is 0, and if ##b=0## expected payout is ##r##. Otherwise the following rule should govern the expected payout ##E(r,b)## of the process:
$$E(r,b) = \frac{r}{r+b}(1 + E(r-1,b) ) + \frac{b}{r+b}(-1 + E(r,b-1) )$$
Obviously we draw another card when ##E(r,b)>0##, else we stop. My question is how to program this? I've tried the following but am stumped as to what's next. Can you help?
Python:
def cards(r, b):
def expVal(r, b):
if r == 0:
return 0
elif b == 0:
return r
else:
return r/(r + b) * (1 + expVal(r-1, b)) + b/(r + b) * (-1 + expVal(r, b-1))
expVal(r, b)
if __name__ == "__main__":
r = 26
b = 26
cards(r, b)