Project #6: Infinite Money in Roulette (Martingale Betting Strategy)
In this DISCOVERY Data Science Project, you will do real data science in less than an hour and you will earn this project's card to your collection when you fully complete this project! 🎉
Data Simulation: A Martingale Betting System
A "martingale" is a betting strategy where, after every loss, your next bet is made so that a win will always recover all of previous losses. In the game of Roulette, a wheel with 38 slots is spun and a ball is placed into the spinning wheel. As the spinning wheel slows down, the ball will land in one of the 38 slots with equal probability. Each of the slots are labeled with a number and a color.
When you place a bet on the color "red" or "black", the bet is "1:1" bet. A "1:1" bet means you are awarded with $1 for every $1 you bet if you win (plus you retain your original bet) or you lose your entire bet if you lose.
Applying the martingale betting strategy to the game of Roulette would involve the following betting strategy:
- Initially, you bet a small amount (ex: $1.00) that the ball will land on "red".
- A game of roulette is played. Based on the result of the game:
- If the ball lands on red, you won your bet! You made $1.00 and can repeat again.
- Each time you lose, you double your bet! You will initially lose the $1.00, but doubling your bet to $2.00 will make up for that initial loss when you win. (If you continue to lose, you'll bet $4.00 to make up for the $1 + $2 lost bets, then $8.00 to make up for the $1 + $2 + $4 lost bets, then $16, then $32, and so on.)
The following table outlines this strategy and your total eventual winnings:
| Scenario | Previous Losses | Winning Bet | Total Winnings |
|---|---|---|---|
| You bet red, and win | -- | $1.00 | +$1.00 |
| You win after losing once | -$1.00 | $2.00 | +$1.00 |
| You win after losing twice | -$3.00 | $4.00 | +$1.00 |
| You win after losing three times | -$7.00 | $8.00 | +$1.00 |
| You win after losing four times | -$15.00 | $16.00 | +$1.00 |
| You win after losing five times | -$31.00 | $32.00 | +$1.00 |
| You win after losing six times | -$63.00 | $64.00 | +$1.00 |
| You win after losing seven times | -$127.00 | $128.00 | +$1.00 |
| ... | ... | ... | +$1.00 |
In the table, you will see that every win will ALWAYS result in a total winnings of $1.00 -- even if you just lost 7 or more times in a row! Proponents of the martingale betting system will tell you that they have mathematically proven a way that you will make an infinite increasing amount of money so long as you play the game long enough!
In this project, we will explore this claim and use simulation to play Roulette using this martingale betting system. Let's nerd out! :)
Background Knowledge
To finish this project, we assume you already know:
- All topics covered in DISCOVERY Module 1: Basics of Data Science with Python (review the module here)
- Creating a Simple Simulation in Python (reviewing simulations in Python)
- Adding Conditionals to Simulations (reviewing conditionals in Python)
Let's get started! :)
Part 1: Simulation of a Martingale Betting Strategy in Roulette
Whenever we work with data from a simulation, the first thing to do is to build the simulation! To test the martingale betting strategy, let's simulate playing Roulette 1,000,000 times and we will start playing with $100,000 (that's a lot of money)!
Part 1.1: Simulating the number in Roulette Game
Unlike a simple simulation we've done before, we need a few Python variables that track our progress between individual games. There are two variables that we need to define before we start our simulation:
money = 100000($100,000), your starting moneycurrentBet = 1($1), your current bet size
Each observation in our simulation is one game of Roulette where you bet a currentBet amount of money on "red". The only event in a Roulette game is choosing where among the 38 possible spots that the ball lands: 00, 0, 1, 2, 3, ..., 35, or 36.
- Challenge #1: How do you want to represent
00? (There's no wrong answer here, but you must make sure to represent00in some way.) - Store the value of where the Roulette ball lands in the Python variable
number.
Create a DataFrame df that contains the data from a simulation of playing 1,000,000 games of Roulette, making sure to store the money, currentBet, and number variables for each game of Roulette. (At this stage, you are not updating the money or currentBet -- we want to make sure to get the game working first!)
⚙️ Test Case: Part 1.1: Simulating the `number` in Roulette Game
Part 1.2: Adding Each Number's color to the Simulation
In Roulette, every spot has both a number and a color. On a standard Roulette wheel in the United States, the following table shows the colors for each number, based on the order they appear on the wheel:
| Color | Numbers |
|---|---|
| red | 32, 19, 21, 25, 34, 27, 36, 30, 23, 5, 16, 1, 14, 9, 18, 7, 12, and 3 |
| black | 15, 4, 2, 17, 6, 13, 11, 8, 10, 24, 33, 20, 31, 22, 29, 28, 35, and 26 |
| green | 0, 00 |
In Python, we can check if a value is in a list of numbers with the in keyword. After you have your value for number, you can use the following code to set a value for color:
if number in [32, 19, 21, 25, 34, 27, 36, 30, 23, 5, 16, 1, 14, 9, 18, 7, 12, 3]:
color = "red"
elif number in [15, 4, 2, 17, 6, 13, 11, 8, 10, 24, 33, 20, 31, 22, 29, 28, 35, 26]:
color = "black"
else:
color = "green"Copy your simulation from the previous section into the cell below, and then extend it with the code above to include color to your simulation and DataFrame:
⚙️ Test Case: Part 1.2: Adding Each Number's `color` to the Simulation
Part 1.3: Adding result and nextBet to your Simulation
With the color of the number of each Roulette game stored in color, we can use the color variable to determine the result of the game of Roulette. There are two possible results of a game of Roulette for the purposes of our simulation:
If the color is "red", you've won! 🎉
- You have won the amount of money you bet, as stored in
currentBet! - To represent your winnings, in games where the
coloris"red", set the value ofresultto be equal to yourcurrentBet. - Additionally, set the value of
nextBetback to be $1.
If the color was NOT "red", you lost.
- You lost your bet. :(
- To represent you lost, in games where the
coloris NOT"red", set the value ofresultto be equal to the negative value of yourcurrentBet(ex:currentBet * -1). - Additionally, set the value of
nextBetto be double the value of yourcurrentBet(so your next bet can make up for all your previous losses).
Copy your simulation from the previous section into the cell below, and then extend it to add both result and nextBet to your simulation and DataFrame:
⚙️ Test Case: Part 1.3: Adding `result` and `nextBet` to your Simulation
Part 1.4: Complete your Simulation
The last step to complete your simulation is to update your money and bet size for the next day.
In your code AFTER you have recorded your simulation data for a game (AKA after making your dictionary but before appending your data), we need to update our global variables:
money: The value stored inmoneymust be updated by adding theresultof the Roulette game to the current value ofmoney. Sinceresultis negative when we lose money, adding a negative value will make ourmoneysmaller (and when ourresultis positive, ourmoneywill grow).currentBet: The value stored incurrentBetmust be updated to be the value stored innextBet.
These two tasks will complete the game and track the amount of money we've made -- copy your simulation from the previous section into the cell below and complete your simulation by finishing the final two steps outlined above! :)
⚙️ Test Case: Part 1.4: Complete your Simulation
Part 2: Analysis of Infinite Money
The following code creates a line plot using df.plot.line and uses money as the y-axis value to create a plot of our current money:
Part 2.1: Negative Money?
A casino requires you to have money to place a bet -- if your money ever went below $0, you couldn't make the bet. From your 1,000,000 games, create a DataFrame of all games with a negative starting balance -- store that as df_negativeBalance:
Part 2.2: Required Bankroll
A "bankroll" is the amount of money you have available to play at a casino.
The bankroll you would be required to have at the casino (on top of the original $100,000) would be the absolute value of the greatest amount of debt you were in during the simulation (which is the smallest value in money).
Store the "required bankroll" that you need in the variable bankroll_required:
(Note: Do not factor in your original $100,000 to the "required bankroll".)
⚙️ Test Case: Part 2: Analysis of Infinite Money
Part 3: Table Limits
If you are a multi-millionaire, the bankroll you need may not be an issue and -- after 1,000,000 games -- you have still won about ~$470,000 (you're expected to win about 18/38 times, each win winning $1). No casino could let you win like this!!
One feature of all games of Roulette is that there will always be a "table limit". A "table limit" is the maximum bet you can make on any one spin of Roulette. In our simulation, our table limit is $10,000.
Part 3.1: Updated Simulation with Table Limits
Return to Part 1.4 and copy your simulation and extend it to include table limits by doing the following:
- When setting the
currentBetnear the end of your for-loop, check if the value ofnextBetis greater than $10,000. - If
nextBetis larger than $10,000, setcurrentBetequal to $10,000 instead ofnextBet. - Otherwise, set
currentBetequal tonextBetas normal.
Create your simulation with table limits below:
⚙️ Test Case: Part 3.1: Updated Simulation with Table Limits
Part 3.2: Table Limits Visualization
Let's look at a data visualization of the simulation when table limits are put in place:
Part 3.3: Finding the Total Money Lost
Calculate the total money lost (as a positive amount of money lost) after playing all 1,000,000 simulated games when table limits are used. Store your total money lost in the variable totalLoss.
To calculate this, remember that your simulation maintains the Python variable money that stores the current amount of money you have. After playing the game with table limits, this value of money is negative since you are in debt.
- To calculate loss based on debt, your loss is the negation of the amount of debt you have (
-money). (Ex: The negation of -$500,000 is -(-$500,000) or "a loss of $500,000".) - In addition to the debt you racked up, you also loss all of your initial $100,000. Make sure to add that initial $100,000 to your calculation of
totalLoss.
⚙️ Test Case: Part 3.2: Analysis of Losses in Games with Table Limits
Part 3.4: Reflection
The martingale betting strategy is, in theory, not a completely crazy idea -- your initial data visualization showed you had a slow but steady increase in money when using the martingale system.
However, as soon as table limits are introduced, you are prevented from making massive bets to recover from streaks of bad luck, and the strategy completely falls apart and debt stacks up quickly!
Congratulations on writing quite a complex simulation!! 🎉
Validate and Complete This Project!
Congratulations on finishing this DISCOVERY Data Science Project! 🎉🎉
To validate your entire project, your entire code will run from top-to-bottom on this page and each test case will be validated one final time. If everything looks good, you'll earn the card for completing this project: