How poker machines vacuum up your money

Poker machines are unique in the gambling world. They are the only form of gambling that has been designed and crafted for the purpose of making money, and where there is absolutely no chance of influencing the outcome.

Tom Cummings, Poker machine maths, 27 May 2011

The article, linked above, on poker machines is an excellent insight into the mathematics of poker machine gaming and how, despite a guaranteed return-to-player percentage which is high, a punter that plays long enough ends up with nothing. The vital point that Tom Cummings makes is this:

It’s common knowledge that poker machines have a return-to-player percentage of anywhere between 85 per cent and 90 per cent, depending on where you live and what kind of establishment you’re playing in. For the sake of the story, let’s assume that Gladys’ poker machine was set to 90 per cent. That means that over a long period of time, the machine will return 90 per cent of money gambled to players, and keep 10 per cent as profit.

But wait, I hear you cry. Gladys didn’t lose 10 per cent; she lost it all! Well, no… not according to poker machine mathematics. The rule is that the poker machine has to return 90 per cent of money gambled… not money inserted. And there’s a huge difference.

The way it works, mathematically, is, I think, very interesting. Take the example used in the article of a 2-cent gaming machine, at $1 per play, with guaranteed return of 90%. When you load that dollar in the slot, sometimes you win, and sometimes you lose, but the long-term average dictates that at each “turn” the dollar is losing 10% of its value. $1 turns into 90c, turns into 81c, turns into 72.9c, … , turns into 2c at which point the game ends.

More generally, at “turn” n, an amount A1 initially fed into a slot machine with return R is worth:


or solving for n:

n=\frac{\displaystyle ln(A_{n}) - ln(A_{1})}{\displaystyle ln(R)} + 1

What does this mean? It means that it takes, on average

n = [ ln(0.02) – ln(1.00) / ln(0.90) ] + 1 = 38

iterations to turn $1.00 into 2 cents on a 90% return poker machine. More importantly, the total amount gambled isn’t $1.00, it’s actually (because you’re re-investing your winnings and following your losses until the game ends): $1.00 + 90c + 81c + 72.9c + … + 2c. Or more generally,

A_{1}\sum_{1}^{n} R^{i-1}

which you’ll remember converges to:

A_{1} \frac{\displaystyle 1-R^{n-1}}{\displaystyle 1-R}

So a single dollar coin will generate, on average (on a 2-cent, 90% return machine):

[ 1-0.9(38-1)] / [ 1-0.90 ] = $9.80 in total bets.

At $1.00 per game, at 10 games per minute, that’s about 1 minute of play for every dollar put in, or 5 hours to burn through Gladys’ $300.

Those who defend poker machines often point to the high rate of return as one of the reasons that pokies are just “good, clean fun” for most people. The reality is that every poker machine can meet this “rate of return” requirement while still leaving the gambler broke. That’s poker machine mathematics.


Further reading:

ABC Hungry Beast, The Beast File: Pokies


Love triangles


My wife is a big fan of the TV medical drama, Grey’s Anatomy.  I don’t care much for the show myself, but by all accounts it’s a quality production and a hit with audiences.  Besides, it makes my wife happy.  And a happy wife equals a happy life.  As far as I can tell, from my limited exposure, Grey’s Anatomy centres its drama around a series of love triangles.  No, I’m not talking about ménages à trois, so stop that sniggering.  I’m talking about that other kind of messy relationship structure.  You know… boy1 loves girl1 but girl1 secretly pines for boy2 who is married to girl2 but is unfaithful with girl3…  And so on, ad nauseam.

Undoubtedly this makes for great TV.  But I was thinking about it from a statistical/probability point of view.  I wondered just how long could the writers of Grey’s Anatomy keep these shenanigans up?  Exactly how many love triangles are possible?

Let’s assume for simplicity that old characters don’t leave and new characters aren’t introduced.  That is, consider a closed system of m males and f females.  Utilising the combination formula there are m fC2 combinations of male-female-female love triangles and f mC2 combinations of female-male-male love triangles.  And for the sake of completeness there are mC3 male-male-male combinations and fC3 female-female-female combinations.  Not that there’s anything wrong with that.

Looking at the photo above, I count five male doctors and four female doctors.  Assuming that the writers of Grey’s Anatomy create one episode per love triangle combination then that line up of cast can generate:

= m fC2 + f mC2 + mC3 + fC3

= 5 4C2 + 4 5C2 + 5C3 + 4C3

= 84 unique love triangles.

Interestingly, this figure is remarkably close to the 93 episodes actually created so far.  Looking at the statistics, Grey’s Anatomy has simply exhausted itself and there will be no Season 6.

My wife will be devastated!

The Stock Market: Beautiful One Day, Perfect The Next (Part 2)

Several weeks ago I used simple conditional probability to formulate an optimal investment strategy:

…put your money into the market only if the last month was up.  If the prior month was down then keep/take your money out.  Lather.  Rinse.  Repeat.  In theory this strategy should work about 65% of the time.  If, on the other hand, you put your money into the market when the previous month was down (or sideways) you’ll have just 49% chance of making a profit.
The Stock Market: Beautiful One Day, Perfect The Next

The purpose of the analysis was not, and is not, for anyone to use as financial advice (which I am not qualified to give).  It was for academic interest only.  And this remains the case.  However, I wanted to measure how the theory would “work” in the “real world”.

I decided to test my theory over a timeframe covering the last five years.  This has been an interesting period on the Australian Stock Exchange (ASX) to say the least.  Several years of a huge bull run completely wiped out in 12 months by the Global Financial Crisis.  I wondered how my investment strategy would hold out against that kind of shenanigans.  Using monthly average ASX All Ordinaries adjusted close indexes (AORD) I invested a theoretical $10,000 in the market at the beginning of January 2004.  Strategy 1 is my optimal strategy: put money into the market only if the previous month was up.  If the previous month was sideways or down then pull your capital out.  Strategy 2 is the control: capital remains in the market.  For example, the first six months of capital growth using Strategy 1 and Strategy 2 would look like this:

(adj close)
Monthly Δ Strategy 1
Strategy 2
Start $10,000 $10,000
Jan 2004 3283.6 -0.7% $9,932 $9,932
Feb 2004 3372.5 2.7% $9,932 $10,201
Mar 2004 3416.4 1.3% $10,062 $10,334
Apr 2004 3407.7 -0.3% $10,036 $10,308
May 2004 3456.9 1.4% $10,036 $10,456
June 2004 3530.3 2.1% $10,249 $10,678

Between January 2004 and October 2008 there were 58 months in total.  For 39 of those months the ASX All Ords went up, and the average percentage movement of these “up” months was +2.6%.  In the remaining 19 months the index went sideways or down, and the average percentage movement of these “flat/down” months was -4.1%.  This has obviously been skewed by the market meltdown over the last year or so and the last two months in particular.  We’ve seen some horror months on our Stock Market lately.  For example the AORD fell fell a massive 11.3% in January 2008 (looking back this was the death of the coal mine canary), 11.2% in September 2008 and then fell 14.0% in October 2008.

But what if you had used my investment strategy over the last five years?

Well the bad news is that Strategy 1 quite significantly underperformed Strategy 2 during the bull run.  A few relatively minor flat/down months here and there meant that capital was pulled out and very healthy returns would be missed during the subsequent month.  But the good news is that Strategy 1 protected the investor against the financial meltdown.  By October 2008 the initial $10,000 invested using Strategy 1 in January 2004 was worth $14,508 (average growth of 8.0% p.a. compounded) vs. $12,047 (3.9%) for Strategy 2.  So the really good news is that Strategy 1 outperformed the market by more than double.

Woo hoo!

Enough hubris.  Of course things are a lot more complicated in the real “real world”.  For a start, history is no guarantee of future performance.  Strategy 1 might look good on the spreadsheet but carries more risk.  Also, the success of Strategy 1 over Strategy 2 assumed no brokerage fees.  And it took no account at all of dividend payments or relative tax consequences.  Under the weight of these considerations Strategy 1 doesn’t look quite so “optimal” after all.

But an interesting result nonetheless.



Your shipment of FAIL has arrived


I was reading “Steve Waddington’s Network Notes” today where he describes how two electrical components failed on him at the same time, and wonders what the chances were of it happening.

It is highly unlikely that two electrically isolated components would fail at exactly the same time. Since both are rated at an MTBF of 90,000 hours, the chance of them both failing in any given hour, after less than 10,000 hours of operation, would have to be in the region of one in one billion.

It got me wondering… what were the chances?

“MTBF” is “Mean Time Between Failures”.  It is the reciprocal of the failure rate, λ, and follows an exponential failure distribution.  This distribution is asymmetrical, so it is not true to say that the MTBF represents the point at which the probability of failure equals 50%.  However, an exponential distribution does make probability calculations relatively easy.

P(component fails at exactly 10000 hrs | MTBF=90000 hrs) = λe−λx

= 1/90000 * exp (-1/9)

= 0.00000994

or about one chance in 100,577.

That’s the chance of one component failing.  The chance of the two components failing at once is therefore one in 100,5772 or more than one in 10 billion.  No wonder Steve ruled out “just bad luck”.  I’d be very suspicious too.  It will be interesting to see if his theory of a dodgy part proves to be correct.

Monte Carlo: The method, not the delicious biscuit

Bernard: It’s all waffle!  Nobody is prepared to admit that wine doesn’t have a taste.
Manny: But you can’t taste anything.  You smoke eighty bajillion cigarettes a day.  What’s that you’re eating?
Bernard: It’s some sort of delicious biscuit.
Manny: It’s a coaster!

– “Black Books” TV Series

Not the smoothest of segues, I admit, but I felt an urge to write about Monte Carlo methods.  This got me thinking about biscuits, which reminded me of that scene from the classic UK comedy series, Black Books, and, well, here we are…

The application of Monte Carlo methods is a very useful tool in the field of statistics.  The Monte Carlo method isn’t actually one particular method.  It instead describes any method involving the application of repeated sets of random numbers, within a set of pre-defined constraints, to produce an estimate which could not easily be derived analytically.  Monte Carlo methods have become especially easy to apply with the evolution of faster and more powerful computers.

As an example, imagine you have three datasets bound by the following constraints:

Dataset 1: n=7; min=2; median=12; max=18

Dataset 2: n=11; min=3; median=6; max=22

Dataset 3: n=9; min=1; median=8; max=16

dataset 1 dataset 2 dataset 3
obs 01
2 3 1
obs 02 a e m
obs 03 b f n
obs 04 12 g o
obs 05 c h 8
obs 06 d 6 p
obs 07 18 i q
obs 08 j r
obs 09 k 16
obs 10 l
obs 11 22

Knowing nothing else about these data could you make a guess at the mean and standard deviation of the three sets combined?  Yes you could, because you’ve been reading my blog and you know that this is a situation where Monte Carlo methods could be employed.  It would involve using a computer to replace letters a to r with randomly generated figures that fall within the required constraints.  For example, a and b must lie between the minimum of 2 and the median of 12.  Similarly p, q, r must lie between 8 and 16.  And so on.

One possible randomly generated dataset could look like the table below.  In this example I simply used the randbetween function in a spreadsheet to replace a to r with (pseudo) random numbers.

dataset 1 dataset 2 dataset 3
obs 01
2 3 1
obs 02 6 3 4
obs 03 11 3 5
obs 04 12 4 6
obs 05 15 5 8
obs 06 17 6 9
obs 07 18 12 11
obs 08 16 15
obs 09 17 16
obs 10 19
obs 11 22

This particular iteration has a mean of 9.9 and a standard deviation of 6.2 for the three sets combined.  Replacing a to r again with another set of randomly generated figures might result in a different combined mean and standard deviation.  Out of interest I ran just three iterations resulting in simulated means of 9.9, 10.2 and 10.5 as well as simulated standard deviations of 6.2, 6.3 and 6.1.  The idea behind the Monte Carlo method is you would run it over and over again, perhaps thousands or even millions of iterations, aggregating the results until you felt that you were converging toward a common outcome.  In the simple example described above it would probably be enough to look at the mode of several hundred simulated means and standard deviations.

So that’s Monte Carlo methods in a nutshell.  A very general concept, but simple and powerful in its application.

OK, now I’m hungry.  Anyone have a good biscuit recipe?

Russian Roulette: An Optimal Strategy

OK now we’ve got ourselves a game
– Mike (Robert De Nero), The Deer Hunter

You may have seen the 1978 classic (and Academy Awards winning) Vietnam War film, The Deer Hunter.  It features a particularly harrowing scene involving two prisoners of war, Mike (played by Robert De Nero) and Nick (played by Christopher Walken), being forced by their captors into playing Russian Roulette.  In the scene Mike significantly ups the ante of the “game” by having three bullets placed in the barrel, rather than the more traditional single round.  Crucially, after Mike survives his turn the barrel is not re-spun before the pistol is handed over to Nick.  I won’t spoil the ending, but it is often a topic of debate among viewers of this scene whether Nick’s chances of survival were improved or reduced by not spinning the barrel again.

In a game of Russian Roulette, assuming that the bullets are placed in sequence, and given that Player 1 survives the first turn, is the optimal strategy to spin again or not spin again?

Good question.  The answer, it seems, depends on how many bullets.

The Deer Hunter scene featured three bullets, giving Mike even odds of surviving his turn.  What were Nick’s chances?  Before answering that question let’s consider, for the sake of completeness, all the combinations of one through to six bullets placed in sequence.  What are the chances of surviving a second turn either with re-spin or without re-spin?

The table below represents the different combinations (combn. 1 to 6) of having 1 to 6 bullets placed in sequence in the barrel of a pistol.  Imagine the circular barrel of a pistol with its 6 chambers laid out flat.  “X” denotes a bullet.  “O” denotes an empty chamber.  Also imagine that each chamber has a number.  So for example combination OOXXXO means that chambers 1, 2, and 6 are empty and chambers 3, 4 & 5 have bullets in them.  The chamber numbers are simply arbitrary.  What is important is that Chamber “1” represents the live chamber that exists on Player 1’s turn.  Chamber 2 represents the live chamber that exists on Player 2’s turn.  OOXXXO represents both Player 1 and Player 2 surviving their respective turns.

combn. 1 combn. 2 combn. 3 combn. 4 combn. 5 combn. 6
1 bullet
2 bullets
3 bullets
4 bullets
5 bullets
6 bullets

Consider the first, single bullet scenario.  We discount combination 1 as this would result in the death of Player 1 and the game would end.  Given that Player 1 survives their turn then the bullet has to be in either chamber 2, 3, 4, 5 or 6.  So without a re-spin Player 2 now has a 1 in 5 (20.0%) shot (sorry, couldn’t resist) of killing themselves.  This compares to “only” a 1 in 6 (16.7%) chance of dying with a re-spin.  Conclusion?  In a single bullet game of Russian Roulette it is (very slightly) better to re-spin.

But what about multiple bullets?

In the two bullet scenario we discount combinations 1 and 6.  So the chance of dying without a re-spin reduces to 1 in 4 (25.0%) vs. 2 in 6 (33.3%) with a re-spin.  In a two bullet game of Russian Roulette it is actually better not to re-spin.

And so on…

3 bullet (Deer Hunter) scenario:  Chance of dying with no re-spin=1 in 3 (33.3%) vs. spin=3 in 6 (50%).  Significantly better to not re-spin.

4 bullets?  No re-spin=1 in 2 (50%) vs. re-spin=4 in 6 (66.7%).  Don’t re-spin.

5 bullets?  No re-spin=1 in 1 (100%) vs. re-spin=5 in 6 (83.3%).  Definitely re-spin!

6 bullets!  Umm… I don’t think it counts as Russian Roulette with 6 bullets.  Obviously Player 1 is guaranteed to die on the first turn…

Next time you’re watching The Deer Hunter with your mates (which I highly recommend as it’s an awesome film) tell them confidently that Nick did the right thing by not re-spinning the barrel of the pistol.

Excuse me… but are you a terrorist?

The other day I came across this article over on Wired: Data-Mining for Terrorists Not ‘Feasible,’ DHS-Funded Study Finds.

The (United States) government should not be building predictive data-mining programs systems that attempt to figure out who among millions is a terrorist, a privacy and terrorism commission funded by Homeland Security reported Tuesday. The commission found that the technology would not work and the inevitable mistakes would be un-American.

“Even in well-managed programs, such tools are likely to return significant rates of false positives, especially if the tools are highly automated.”

I haven’t read the full report, but Wired’s article piqued my interest.  Of course the issue of privacy is a very important one.  But I was particularly interested from a probability/ statistical point of view in the reference to “significant rates of false positives”.  It is a seemingly paradoxical outcome of a particular law of probability known as Bayes’ Law that the rarer the event for which we are testing, despite the accuracy of the test, the greater the proportion of false positives.

Let me explain by way of example.

Let us assume that the United States’ Department of Homeland Security (DHS) has developed an uber “predictive data-mining program” that is 99.9% sensitive.  That is, if you are a terrorist, the algorithm will correctly identify you as one 99.9% of the time.  The bad guys have only a 1 in 1000 shot at slipping under the radar.  However, the chance that any person picked at random in the population is actually a terrorist would have to be fantastically slim.  Say for argument’s sake that the rate of terrorists in the population is 1 in 15million.  That’s a probability of just 0.00000667%.  As I’ll show below it is this tiny probability that, conversely, brings the DHS testing unstuck.

Let “T” be the event of being a terrorist and “N” be the complementary event of not being a terrorist.  Similarly, let “+” be the event of the DHS algorithm flagging you as a terrorist and “-” be the complementary event of not being identified.  Expressed as probabilities we have the following:

P(you are a terrorist) = P(T) = 0.0000000667

P(you are not a terrorist) = P(N) = 1-P(T) = 0.9999999333

P(identified as a terrorist, given that you are a terrorist) = P(+|T) = 0.999

P(identified as a terrorist, given that you are not a terrorist) = P(+|N) = 1-P(+|T) = 0.001

So far everything looks good for the DHS.  The chances of a terrorist living in the population is very, very small and the chances of their algorithm working on a terrorist is very, very high.  However, Bayes had other ideas when he began toying with conditional probabilities.  Using Bayes’ Law the probability of a false positive can be calculated as:

P(you are not a terrorist, given that you were identified as one)

= P(N|+)

= P(+|N) x P(N) / P(+)

= [ P(+|N) x P(N) ] / [ P(+|T)xP(T) + P(+|N)xP(N) ]

= [ 0.001 x 0.9999999333 ] / [ 0.999 x 0.0000000667 + 0.001 x 0.9999999333 ]

= 0.9999333711 (i.e. more than 99.9%)

In other words, even if the accuracy of the DHS program was extremely high, all it would really do is falsely accuse everyone except your grandma and the neighbour’s cat as a terrorist.

And there would be some suspicions about the cat.