 Sat 18 January 2020
 Analysis
 #Data Science, #Python, #Scipy, #Statistics
All this came about while researching gambling strategies during my first trip to Vegas. I didn't want to spend too much time on research. As an accountant who was versed in probability theory, I didn't see any point in playing knowing that the odds were stacked against me. But one does not go to Vegas and not gamble.
Roulette seemed a good option to try. Conceptually easy to understand. You didn't have to memorise hands like in cards and the strategies seemed simple enough. You could play a strategy as simple as tossing a coin that was very slightly weighted against you.
But, it seems that the odds of winning were not the only thing going against you. Let's face it you can't build a sprawling city in the middle of the desert with odds only slightly stacked against the punters. They have to be stacked a lot against the punters. This is where human nature comes in to play and the twin sides of the coin of fear and greed. Because in reality roulette has odds only slightly stacked against you if you play over a long period of time. But chances are your own nature will guarantee that you will finish well before this time.
Desperation after losing will make you take more risks. Feeling unstoppable on a winning streak will make you take more risks. And as in life, it's the accumulation of risks that gets you in the end.
This is exactly what happened to me. I came in with a wad of cash and a list of rules that I will not break. After quadrupling what I came in with, all the rules went out the window until I lost double of what I vowed was the absolute maximum I would play with. The emotional roller coaster and the realisation of the complete loss of control was worth the money.
Since then I have always wondered if I had followed the rules like a robot, completely algorithmically, what would have happened.
I now have the statistics and programming knowledge to run simulations to see how it would have played out.
The Disclaimer
At this point I should put in the disclaimer that this is not a recommendation. It's a bit of fun with code. I haven't tested this strategy nor do I have the testicular fortitude to do so. This is an exercise to see if a particular strategy works and if you can beat the system using it. Not time or resources to test this in the real world.
Also the real world is messy and this is a clean computer simulation that doesn't have variables for the mood of the player and how distracting that waitress is over there. What I mean is, all of this is not going to run as smoothly as the algorithm suggests. Assume that its just a bit of fun.
With that out of the way.
The Strategy
There is a glossary at the bottom of this post. I had to make up some words and give some explanations of theories that are useful to know.
The strategy had been around for a long time and was known as the Martingale strategy. And it goes like:
 Place a small bet with even odds (i.e. you have the same (similar) chance of winning as you do losing. This could either be on black or red, odds and evens, low or high (118, 2036).
 If you win keep the winnings and run the same bet again.
 If you lose, double the bet. If you keep losing double your bet until you win. At which point keep the winnings and the process resets with the initial small bet again.
Of course there are certain things you need to look out for.
You need to have a wallet (kitty) large enough to weather a series of losses. There is about a 0.5% chance of 7 consecutive losses but even if you started off with only 1% of your kitty as your first bet, you would be done at this point.
This series of losses is a lot more common that you'd think. So you need to pick a table with a high spread between the maximum and minimum bets. Ideally, the maximum bet should be 64 times the minimum bet.
Follow the rules. There should be no emotion. It is so tantalising to think that after a string of 7 losses I would definitely win the next one, it would just mean adding around a quarter of my original kitty, but this is exactly the point where you need to leave to fight another day.
If you don't want to read the technical bits, the analysis of what to do is summarised below. If you want to know how these were derived, then please proceed to the explanation.

It pays to leave after you have made 50% of your kitty rather than finish off the number of goes that you planned to do when you started. This is because the return evens off around the 50% of your kitty.

Your initial bet should be around 0.7% of what you have in your kitty. For example, if you bring in $140 to the table, your initial bet and the bet you reset to after a loss should be $1.

Unless you blow the bank, there is a good chance you will make 50% of your kitty if you continue playing. The kitty is large enough to weather and extraordinarily bad string of luck.

You should play for around 90 goes. That's about 7.5 hours of constant gambling or till you reach 50% of your kitty. These are the optimum values for these variables.
Here's the technical bits.
The Code
All the code is available on an iPython Notebook and we won't be going through the individual lines of code rather just have a summary and a discussion of some of the findings.
We first start with a Roulette wheel object which is just a list of equal possible outcomes and a function to returns a random outcome.
The player function represents a single player's visit to the casino. Also called a session. The player will have a kitty with some money in it. Each player will also have a fixed number of goes at the wheel. There is also a variable for a player to play until he makes a percentage of his kitty back. So the player will play till he either finishes his number of turns, blows his all the money in the kitty or, if set, makes a percentage of his kitty back.
There are functions to get summary data for a player and a group of players. The idea here is to get the statistics for a player running the strategy multiple times and to see if it is feasible on the long run.
The next is to do the analysis on the variables to see if they can be optimised. In order to compare variables each have been scaled
Observations
Kitty Size
You need to have a big enough buffer for when things go wrong and they will. This is a function of what your initial bet is and the amount in your kitty. Betting around 0.7% of your kitty per bet means that you can have 7 consecutive losses before blowing the bank. Although this is very unlikely, it does happen a few times in a large number of simulations. At this point the law of regression towards the mean will (sometimes) come to the rescue. But it's not the right way of looking at statistics.
The bigger the kitty the fewer the times you blow your account. But this also brings down the average winnings in the simulation as the amount of money you lose when you blow the bank increases. In terms of expected value, there seems to be a sweet spot around the $140 mark of kitty size for a starting bet of $1. This could be because at this kitty size you can withstand 7 consecutive losses. So if your kitty is \(x\), your starting bet should be \(x \times 0.007\).
Number of Goes
The number of goes are the number of times a player will bet during a session. Of course there is a physical limitation to the number of goes a player can have. If it takes 5 mins per spin, 100 spins can take a whole 8 hour night.
The graph below indicates that the more goes you have the better. The expected value seems to over the 100 mark so around 90 spins for practical purposes (7.5 hrs which is pretty hardcore) will be used for modelling.
The more you play, the more your wins go down (on average) this is because the house always wins and there is a small built in bias favouring the house in the game. The same applies to the number of times you blow your kitty. The more you play, the greater the chances. The strategy seems to work because even though this is the case, the average winnings value seems to go up.
Cashing Out Early
The variable Till is the percentage of your kitty you need to win back before you cash out. This could happen before you've reached your number of goes so you stop early. Some of the code has 999 as a value of till. This means the player always runs until broke or the number of goes is completed.
From the graph it looks like a good idea to cash out at 50% of the kitty. This will also save the number of goes.
Running with Optimised Numbers
It looks like the strategy works so using the optimised numbers we can run summary statistics on a group of simulations. We will look at the ev values here. Arbitrarily running a 1000 simulations for team sizes of 1000.
Assumptions and Stuff
A successful strategy is getting a positive outcome more times than a negative one when running the simulation. A strategy is deemed successful in this case if its expected value (ev) is positive and unsuccessful if it is negative. A strategy with a higher expected value is assumed to be better than one with a lower one.
Each of the variables are looked at in isolation and the best selected for expected value. The variables may not be the best together. An optimisation function can be created but this will take ages to compute based on what my PC is capable of.
Python will give you quasi random variables. This means unlike real world random numbers they are not statistically independent. You can do things like generate random numbers that fit a normal distribution. There are no such things as perfectly random variables in the real world. If you gather enough variables you will be able to see a bias. Real life roulette wheels may give you some groups of numbers more than others.
Glossary
Kitty  How much is in your wallet when you start.
Start  The starting bet. This is the bet that will be doubled after a loss or reset to at a win.
Blown  Ruin. You have blown your kitty.
Blowns  The number of people in the team that have blown their kitty.
Till  Short for "till what point do I continue playing". This is based on a fraction of the starting kitty. 0.5 means I will play till I make 50% of my starting kitty. With convention, 999 means I will keep playing the number of goes.
Player  How we model a single player's session at a casino. The player will have a fixed amount of money in his wallet (kitty) and will be playing the the strategy. He will play until he achieves a goal, the kitty runs out or for a fixed number of turns. This is also a session (at the casino).
Simulation  Many Players playing the same strategy. The statistics of the simulation will be used to decide how feasible the strategy is. Statistics will also be used to compare simulations with each other.
Expected Value  A calculation based on the averages of a simulation.
A positive ev indicates that on average running the strategy is a good idea. A negative ev indicates on average running the strategy is a bad idea.