How Do You Model This Coin Flip Bet?


The bet is this: we flip a coin (actually, Derek is the one making the bet). If you win the flip, you get twenty dollars. If you lose the flip, you lose only ten dollars. Got it? Would you take the bet? If you watch the video above you will find that most people would NOT take this bet. No one wanted to lose ten dollars. But Derek Muller (from Veritasium) claims that it’s worth considering – especially if you run the bet multiple times. So, what are the chances that you would lose money if you flipped the coin ten times? What about 100 times? Let’s find out.


Let me go ahead and say that there is indeed a way to calculate the probability of losing money on ten flips. But I don’t want to do it that way. I want to model this with a quick python program. Now I’m going to show you how to do that.


Modeling Coin Flips in Python


The first thing we will need is a random picker. In python, you will need to import the random module (not any random module, but THE random module). Actually, we only need one function from that module, the choice function. Check out this super simple program. Oh, if you don’t have python installed – try this.


 Randombet py Users Rjallain Projects Python Randombet py


When you run this code, you might get something like this:


Python 2 7 6 Shell


It’s that simple. Run it more than once just to convince yourself that it is indeed generating random “flips”. Even though this is cool, it’s not that useful. Let’s jump to something better. Here is a program that flips the coin ten times. If it is “heads” the player gets 20 dollars. If it’s tails, they lose 10 dollars. How much money do they have at the end of the game?


 Randombet py Users Rjallain Projects Python Randombet py


If you aren’t familiar with python, let me say something about loops. First, this isn’t the most efficient loop. There are cooler ways to flip this coin ten times. However, I like this way since it seems to make sense to many people. The basic idea is to have this counter variable (n) that goes up by one each time the loop is run. The loop does this until the value of N is greater than the desired number of loops (N). In python, anything that is tab-indented is part of the loop. When you outdent, it ends the loop. I know that seems weird, but that’s the way python works.


Go ahead. Change N to 100 and re run it. Change it to 1000. Python doesn’t really care how big that number is.


More Complicated and More Useful Code


Really I don’t want to flip the coin 100 times. I want to run the bet 100 times where I flip the coin 100 times. Or maybe I want to run the 100-flip bet 1,000 times. How do you do that? The key is to define a function. Let me just show you the code and then I will give a quick explanation.


 Randombet py Users Rjallain Projects Python Randombet py


First, there is the function. I defined the function “flip” at the beginning of the program. The function takes in three parameters (the number of flips, the amount you lose and the amount you win). Inside the function is essentially the same as our first looping program. The only thing added is the “return(total)”. In the rest of the program, if you put a line that says flip(10,-10,20) it would give you the amount of money you win after 10 flips.


There are a couple of other things to point out. First, this:


 Randombet py Users Rjallain Projects Python Randombet py


Numpy is another python module. We need this module in order to easily calculate the average of the amount of money that was won. That leads to the second thing that I need to mention – a list. Look at this line.


 Randombet py Users Rjallain Projects Python Randombet py


The variable “money” starts as an empty list. Every time I run the 100 flips, I am going to add the result to this list. To do that, I would just say money = money + [stuff] where stuff is the amount won. Now, after 1000 runs of 100 flips, I have a list that has 1000 elements in it. Since I imported the numpy module, I can just find the average money value with “mean(money)”. That’s not too bad. Right?


But did I every have 100 flips where I lost money? Here I added another variable called “lost”. When I flip the coin 100 times, I get the amount of money won. If this value is less than 0 dollars, then I add 1 to this lost counter. At the end, I print it. Of course, out of 1000 bets I probably won’t lose. Run it yourself and see what happens.


Derek claims that the average amount you win is $500 – do you get something close to that? What if you change the win and lose amounts to -10 and 15 dollars? What happens then? Maybe you should try that.


Making a Histogram


Finding the average amount of money won (or lost) is cool and fun – but it doesn’t tell the whole story. Really, humans don’t want just the average. They want to see the whole distribution of the data. Sure, you could print out all 1,000 results of the betting but that wouldn’t be easy to read.


The best way to represent this data is with a histogram (btw, Plotly’s introduction to histograms is simply the best). If you need a tutorial on histograms with python and Plotly, check this out.


See. Doesn’t that look nice? Here is the code that I used to make this program (you will need your own plotly login and key though).


Ok, clearly there is going to be homework.



  • Create a plot that shows the average amount of money won vs. the amount you could win. So, on the horizontal axis should be the amount of money won per flip (start at 10 dollars and assume you lose 10 dollars) and on the vertical axis should be the average amount won after 100 flips.

  • Create a plot of the average number of loses vs. the number of flips (use win 20 and lose 10).

  • Derek claims the risk of losing money is 1 out of 2,300. Run the 100 flip bet 10,000 times and see how many times you would lose money.


You won’t learn how to make histograms and numerical calculations unless you practice. Oh, and as Derek says – this is really a lesson about life. You need to take some risks to make some gains.



No comments:

Post a Comment