Ergodicity, Animated

Sid Shanker -

Last year, I wrote a blog post explaining the concept of Ergodicity. I thought it would be helpful in understanding this to make some animations!

To recap: we are playing a game where on each turn, we flip a fair coin, and if the coin lands heads, we win 55% of our bet, and if it lands tails, we lose 45% of our bet.

The fascinating takeaway here is that if a large number of people play this game a fixed number of times, say 20, the average outcome will be positive, which makes sense, since the expected value of playing this game is positive. However, if any individual plays this game long enough, they will lose almost all of their money.

A note on terminology: When I refer to the “current value”, I refer to the amount of money each player holds. For instance, at the start, the value is 100, and after one iteration, if the coin lands head, the value will be 155.

With a group

In this example, we have 40 players play the game 20 times. Notice that each time you run this, the average value for all the players at the end will usually be above 100. Notice, however, that the way things tend to shake out, there will be one or two players who make a huge amount of money and that most lose almost everything.

Iterations: 0 Average Value: 100

The Individual Case

In this example, when you click "Play", you will begin playing the game. Notice that while at any given point in time, you might have made a lot of money, after a long time, the "current value" you hold will eventually converge to 0.

Iterations: 0 Current Value: 100


It’s pretty counterintuitive that playing a game like this, in which each turn has a positive expected value, would in the long run eventually result in ruin. Hopefully these animations help show visually that very different outcomes occur in this game when a many people play the game a small number of times (ensemble-averaging) and when a single individual plays the game many times (time-averaging). Expected value is a nuanced concept!

If you’re curious to learn more, check out my my original post on the subject, where I try to explain it more rigorously.


I made this using plotly.js, an awesome & simple charting library, and some very hacky Javascript. If you’re interested in being able to configure more about these animations (say the number of iterations, or players), let me know!

Sid Shanker <sid.p.shanker at>