You are currently browsing the category archive for the ‘probability’ category.

The Barnsley Fern: Mathematical Art

This pattern of a fern pictured above was generated by a simple iterative program designed by mathematician Michael Barnsely.  I downloaded the Python code from the excellent Tutorialspoint and then modified it slightly to run on  What we are seeing is the result of 40,000 individual points – each plotted according to a simple algorithm.  The algorithm is as follows:

Transformation 1: (0.85 probability of occurrence)

xi+1 = 0.85xi +0.04yi

yi+1= -0.04xi+0.85yi+1.6

Transformation 2: (0.07 probability of occurrence)

xi+1 = 0.2xi -0.26yi

yi+1= 0.23xi+0.22yi+1.6

Transformation 3: (0.07 probability of occurrence)

xi+1 = -0.15xi -0.28yi

yi+1= 0.26xi+0.24yi+0.44

Transformation 4: (0.01 probability of occurrence)

xi+1 = 0

yi+1= 0.16yi

So, I start with (0,0) and then use a random number generator to decide which transformation to use.  I can run a generator from 1-100 and assign 1-85 for transformation 1, 86-92 to transformation 2, 93-99 for transformation 3 and 100 for transformation 4.  Say I generate the number 36 – therefore I will apply transformation 1.

xi+1 = 0.85(0)+0.04(0)

yi+1= -0.04(0)+0.85(0)+1.6

and my new coordinate is (0,1.6).  I mark this on my graph.

I then repeat this process – say this time I generate the number 90.  This tells me to do transformation 2.  So:

xi+1 = 0.2(0) -0.26(1.6)

yi+1= 0.23(0)+0.22(1.6)+1.6

and my new coordinate is (-0.416, 1.952).  I mark this on my graph and carry on again.  The graph above was generated with 40,000 iterations – let’s see how it develops over time:

1000 iterations:

10,000 iterations:

100,000 iterations:


500,000 iterations:

If we want to understand what is happening here we can think of each transformation as responsible for a different part of our fern.  Transformation 1 is most likely and therefore this fills in the smaller leaflets. Transformations 2 and 3 fill in the bottom left and right leaflet (respectively) and transformation 4 fills in the stem.

It’s quite amazing to think that a simple computer program can create what looks like art – or indeed that is can replicate what we see in nature so well.  This fern is an example of a self-similar pattern – i.e one which will look the same at different scales.  You could zoom into a detailed picture and see the same patterns repeating.  You might want to explore the idea of fractals in delving into this topic in more detail.

Changing the iterations

We can explore what happens when we change the iterations very slightly.

Christmas tree

Crazy spiral

Modern art


You can modify the code to run this here.  Have a go!


Screen Shot 2020-06-05 at 10.14.13 PM

Martingale II and Currency Trading

We can use computer coding to explore game strategies and also to help understand the underlying probability distribution functions.   Let’s start with a simple game where we toss a coin 4 times, stake 1 counter each toss and always call heads.  This would give us a binomial distribution with 4 trials and the probability of success fixed as 1/2.

Tossing a coin 4 time [simple strategy]

Screen Shot 2020-06-05 at 9.27.47 PM

For example the only way of losing 4 counters is a 4 coin streak of T,T,T,T.  The probability of this happening is 1/16.  We can see from this distribution that the most likely outcome is 0 (i.e no profit and no loss).  If we work out the expected value, E(X) by multiplying profit/loss by frequencies and summing the result we get E(X) = 0.  Therefore this is a fair game (we expect to neither make a profit nor a loss).

Tossing a coin 4 time [Martingale strategy]

Screen Shot 2020-06-05 at 9.34.19 PM

This is a more complicated strategy which goes as follows:

1) You stake 1 counter on heads.
b) if you lose you stake 2 counters on heads
c) if you lose you stake 4 counters on heads
d) if you lose you stake 8 counters on heads.

If you win, the your next stake is always to go back to staking 1 counter.

For example for the sequence: H,H,T,T 

First you bet 1 counter on heads.  You win 1 counter
Next you bet 1 counter on heads.  You win 1 counter
Next you bet 1 counters on heads.  You lose 1 counter
Next you bet 2 counters on heads.  You lose 2 counters

[overall loss is 1 counter]

For example for the sequence: T,T,T,H 

First you bet 1 counter on heads.  You lose 1 counter
Next you bet 2 counter on heads.  You lose 2 counters
Next you bet 4 counters on heads.  You lose 4 counter
Next you bet 8 counters on heads.  You win 8 counters

[overall profit is 1 counter]

This leads to the following probabilities:

Once again we will have E(X) = 0, but a very different distribution to the simple 4 coin toss.  We can see we have an 11/16 chance of making a profit after 4 coins – but the small chance of catastrophic loss (15 counters) means that the overall expectation is still zero.

Iterated Martingale:

Here we can do a computer simulation.  This is the scenario this time:

Screen Shot 2020-06-05 at 10.01.23 PM

We start with 100 counters, we toss a coin for a maximum of 3 times. We then define a completed round as when we get to a shaded box.  We then repeat this process through 999 rounds, and model what happens. Here I used a Python program to simulate a player using this strategy.

Screen Shot 2020-06-05 at 10.06.15 PM

We can see that we have periods of linear growth followed by steep falls – which is a very familiar pattern across many investment types.  We can see that the initial starting 100 counters was built up to around 120 at the peak, but was closer to just 40 when we finished the simulation.

Let’s do another simulation to see what happens this time:

Screen Shot 2020-06-05 at 10.14.13 PM

Here we can see that the 2nd player was actually performing significantly worse after around 600 rounds, but this time ended up with a finishing total of around 130 counters.

Changing the multiplier

We can also see what happens when rather than doubling stakes on losses we follow some other multiple.  For example we might choose to multiply our stake by 5.  This leads to much greater volatility as we can see below:

Multiplier x5

Screen Shot 2020-06-06 at 6.18.05 AM

Here we have 2 very different outcomes for 2 players using the same model.  Player 1 (in blue) may believe they have found a sure-fire method of making huge profits, but player 2 (green) went bankrupt after around 600 rounds.

Multiplier x1.11

Screen Shot 2020-06-06 at 6.28.07 AM

Here we can see that if the multiplier is close to 1 we have much less volatility (as you would expect because your maximum losses per round are much smaller).

Screen Shot 2020-06-06 at 5.20.07 PM

We can run the simulation across 5000 rounds – and here we can see that we have big winning and losing streaks, but that over the long run the account value oscillates around the starting value of 100 counters.

Forex charts

We can see similar graphs when we look at forex (currency exchange) charts.  For example:

Screen Shot 2020-06-06 at 6.42.53 AM

In this graph (from here) we plot the exchange between US dollar and Thai Baht.  We can see the same sort of graph movements – with run of gains and losses leading to a similar jagged shape.  This is not surprising as forex trades can also be thought of in terms of 2 binary outcomes like tossing a coin, and indeed huge amounts of forex trading is done through computer programs, some of which do use the Martingale system as a basis.

The effect of commission on the model

Screen Shot 2020-06-06 at 7.16.35 PM

So, to finish off we can modify our system slightly so that we try to replicate forex trading.  We will follow the same model as before, but this time we have to pay a very small commission for every trade we make.  This now gives us:

E(X) = -0.000175. (0.0001 counters commission per trade)

E(X) = -0.00035. (0.0002 counters commission per trade)

Even though E(X) is very slightly negative, it means that in the long run we would expect to lose money.    With the 0.0002 counters commission we would expect to lose around 20 counters over 50,000 rounds.  The simulation graph above was run with 0.0002 counters commission –  and in this case it led to bankruptcy before 3000 rounds.

Computer code

Screen Shot 2020-06-06 at 6.53.33 AM

The Python code above can be used to generate data which can then be copied into Desmos.  The above code simulates 1 player playing 999 rounds, starting with 100 counters, with a multiplier of 5.   If you know a little bit about coding you can try and play with this yourselves!

Screen Shot 2020-09-12 at 1.36.32 PM

I’ve also just added a version of this code onto repl.  You can run this code – and also generate the graph direct (click on the graph png after running).  It creates some beautiful images like that shown above.


The Martingale system

The Martingale system was first used in France in 1700s gambling halls and remains used today in some trading strategies.  I’ll look at some of the mathematical ideas behind this and why it has remained popular over several centuries despite having a long term expected return of zero.

The scenario

You go to a fair ground and play a simple heads-or-tails game.  The probability of heads is 1/2 and tails is also 1/2.  You place a stake of counters on heads.  If you guess correctly you win that number of counters.  If you lose, you double your stake of counters and then the coin is tossed again.  Every time you lose you double up your stake of counters and stop when you finally win.

Infinitely deep pockets model:

You can see that in the example above we always have a 0.5 chance of getting heads on the first go, which gives a profit of 1 counter.  But we also have a 0.5 chance of a profit of 1 counter as long as we keep doubling up our stake, and as long as we do indeed eventually throw heads.  In the example here you can see that the string of losing throws don’t matter [when we win is arbitrary, we could win on the 2nd, 3rd, 4th etc throw].  By doubling up, when you do finally win you wipe out your cumulative losses and end up with a 1 counter profit.

This leads to something of a paradoxical situation, despite only having a 1/2 chance of guessing heads we end up with an expected value of 1 counter profit for every 1 counter that we initially stake in this system.

So what’s happening?  This will always work but it requires that you have access to infinitely deep pockets (to keep your infinite number of counters) and also the assumption that if you keep throwing long enough you will indeed finally get a head (i.e you don’t throw an infinite number of tails!)

Finite pockets model:

Real life intrudes on the infinite pockets model – because in reality there will be a limit to how many counters you have which means you will need to bail out after a given number of tosses.  Even if the probability of this string of tails is very small, the losses if it does occur will be catastrophic –  and so the expected value for this system is still 0.

Finite pockets model capped at 4 tosses:

In the example above we only have a 1/16 chance of losing – but when we do we lose 15 counters.  This gives an expected value of:

Finite pockets model capped at n tosses:

If we start with a 1 counter stake then we can represent the pattern we can see above for E(X) as follows:

Here we use the fact that the losses from n throws are the sum of the first (n-1) powers of 2. We can then notice that both of these are geometric series, and use the relevant formula to give:

Therefore the expected value for the finite pockets model is indeed always still 0.

So why does this system remain popular?

So, given that the real world version of this has an expected value of 0, why has it retained popularity over the past few centuries?  Well, the system will on average return constant linear growth – up until a catastrophic loss.  Let’s say you have 100,000 counters and stake 1 counter initially.  You can afford a total of 16 consecutive losses.  The probability of this is only:

but when you do lose, you’ll lose a total of:

So, the system creates a model that mimics linear growth, but really the small risk of catastrophic loss means that the system still has E(X) = 0.  In the short term you would expect to see the following very simple linear relationship for profit:

With 100,000 counters and a base trading stake of 1 counter, if you made 1000 initial 1 counter trades a day you would expect a return of 1000 counters a day (i.e 1% return on your total counters per day).  However the longer you continue this strategy the more likely you are to see a run of 16 tails – and see all your counters wiped out.

Computer model

I wrote a short Python code to give an idea as to what is happening. Here I started 9 people off with 1000 counters each.  They have a loss limit of 10 consecutive losses.  They made starting stakes of 1 counter each time, and then I recorded how long before they made a loss of 10 tosses in a row.

For anyone interested in the code here it is:


The program returned the following results.  The first number is the number of starting trades until they tossed 10 tails in a row.  The second number was their new account value (given that they had started with 1000 counters, every previous trade had increased their account by 1 counter and that they had then just lost 1023 counters).

1338, 1315
1159, 1136
243, 220
1676, 1653
432, 409
1023, 1000
976, 953
990, 967
60, 37

This was then plotted on Desmos. The red line is the trajectory their accounts were following before their loss.  The horizontal dotted line is at y = 1000 which represents the initial account value.  As you can see 6 people are now on or below their initial starting account value.  You can also see that all these new account values are themselves on a line parallel to the red line but translated vertically down.

From this very simple simulation, we can see that on average a person was left with 884 counters following hitting 10 tails.  i.e below initial starting account.  Running this again with 99 players gave an average of 869.

999 players

I ran this again with 999 players – counting what their account value would be after their first loss.  All players started with 1000 counters.  The results were:

31 players bankrupt: 3%

385 players left with less than half their account value (less than 500): 39%

600 players with less than their original account value (less than 1000): 60%

51 players at least tripled their account (more than 3000): 5%

The top player ended up with 6903 counters after their first loss.

The average account this time was above starting value (1044.68).  You can see clearly that the median is below 1000 – but that a small number of very lucky players at the top end skewed the mean above 1000.

Second iteration

I then ran the simulation again – with players continuing with their current stake.  This would have been slightly off because my model allowed players who were bankrupt from the first round to carry on [in effect being loaned 1 counter to start again].  Nevertheless it now gave:

264 players bankrupt: 26%

453 players left with less than half their account value (less than 500): 45%

573 players with less than their original account value (less than 1000): 57%

95 players at least tripled their account (more than 3000): 10%

The top player ended up with 9583 counters after their second loss.

We can see a dramatic rise in bankruptcies – now over a quarter of all players.  This would suggest the long term trend is towards a majority of players being bankrupted, though the lucky few at the top end may be able to escape this fate.

Website Stats


IB HL Paper 3 Practice Questions (120 page pdf)

IB HL Paper 3 Practice Questions 

Seventeen full investigation questions – each one designed to last around 1 hour, and totaling around 40 pages and 600 marks worth of content.  There is also a fully typed up mark scheme.  Together this is around 120 pages of content.

Available to download here.

IB Maths Exploration Guide

IB Maths Exploration Guide

A comprehensive 63 page pdf guide to help you get excellent marks on your maths investigation. Includes:

  1. Investigation essentials,
  2. Marking criteria guidance,
  3. 70 hand picked interesting topics
  4. Useful websites for use in the exploration,
  5. A student checklist for top marks
  6. Avoiding common student mistakes
  7. A selection of detailed exploration ideas
  8. Advice on using Geogebra, Desmos and Tracker.

Available to download here.

Modelling Guide

IB Exploration Modelling Guide 

A 50 page pdf guide full of advice to help with modelling explorations – focusing in on non-calculator methods in order to show good understanding.

Modelling Guide includes:

Linear regression and log linearization, quadratic regression and cubic regression, exponential and trigonometric regression, comprehensive technology guide for using Desmos and Tracker.

Available to download here.

Statistics Guide

IB Exploration Statistics Guide

A 55 page pdf guide full of advice to help with modelling explorations – focusing in on non-calculator methods in order to show good understanding.

Statistics Guide includes: Pearson’s Product investigation, Chi Squared investigation, Binomial distribution investigation, t-test investigation, sampling techniques, normal distribution investigation and how to effectively use Desmos to represent data.

Available to download here.

IB Revision Notes

IB Revision Notes

Full revision notes for SL Analysis (60 pages), HL Analysis (112 pages) and SL Applications (53 pages).  Beautifully written by an experienced IB Mathematics teacher, and of an exceptionally high quality.  Fully updated for the new syllabus.  A must for all Analysis and Applications students!

Available to download here.

Recent Posts

Follow IB Maths Resources from British International School Phuket on