## ThermoPoker Dot Viz: using pygame animations to understand iterated games

I’m revisiting some older research of mine, so that I can talk a little bit about some data visualization I did along the way. If you frequent TriZPUG or the SplatSpace, you might have seen my original presentation, but In Case You Missed It…

You might remember a while back I got interested in researching the statistical thermodynamics of poker tournaments. To briefly recap, I was treating the distribution of chips amongst players as a probability distribution, which meant that I could use the concept of entropy to describe the distribution. Entropy in thermodynamic systems is associated with how ‘spread out’ the energy is in that system: A hot cup of coffee in a cold room has low entropy while warm coffee in a warm room has high entropy. In a statistical system like a poker table, entropy measures how evenly distributed the chips are between the players. When the players start the tournament with equal amounts, the entropy is at a maximum. When one player wins all the chips, the entropy is at a minimum. Already things are interesting – entropy in this statistical system must decrease with time, in stark contrast with the second law of thermodynamics. And we haven’t even looked at what happens between those two points!

Poker entropy

To better understand the behavior of tournaments, I needed a way to play them and replay them, to turn them into something other than tables of names and numbers. The first representation worked well at illustrating the distribution, but failed to capture the dynamics; except in catastrophic rearrangements, it was not always obvious how the chips moved around from hand to hand.

What’s going on is, I’ve whimsically renamed the players for anonymity, and then represented the size of their stack with a circle. Each hand is then represented by a transaction in which chips flow from one or more players to a single winner, with chip flow represented by black lines whose size is representative of the magnitude of flow. I find this hypnotic.

If you don’t care about coding, feel free to skip down….

How exactly did I put this together?

Zeroeth, we have to get our tools together.

```import pickle, sys    #file IO utilities
import pygame    #pygame library
from pygame.locals import *    #more pygame stuff
from math import sin, cos, pi, sqrt    #math tools```

First, there is a great deal of tedious regular expression slicing and dicing that you have to do to convert a tournament history file into usable data; I’ll be merciful and skip that. So I’ve finally bundled up the data in a couple of files.

## Thermodynamics and Poker

There is a companion article which discusses this project’s role in decentralized community and citizen science at ArkFab.

You can find the current paper at Scribd here or download it here.

A while back, I got the idea to investigate how the entropy of a poker tournament evolves with time. In thermodynamics, entropy is a measure of how ‘spread out’ energy is amongst the states available to it. When the energy in a system is concentrated in one place (like a hot cup of coffee in a cold room), the entropy of the system is low. When the energy is spread out (a few hours later, both the room and the coffee are the same temperature) the entropy of the system is high.  Although originally defined for distributions of physical energy, entropy can be defined more generally to study arbitrary distributions – for example the distribution of capital, in the form of chips, between players in a poker tournament.

Just by looking at the formal structure of the game, you can tell some things about how entropy behaves. For example, it is formally required that entropy falls to zero with time. On the one hand, this is a fancy way of saying, ‘one person will eventually win the tournament’; on the other hand, it is interesting to consider that this is the exact opposite of what happens in the physical, thermodynamic world. The entropy of a closed thermodynamic system necessarily increases with time: hot coffee in a cold room will cool down, but warm coffee in a warm room will never heat up. However, the entropy of a closed poker table necessarily decreases. It has a second law of thermodynamics that runs in the opposite direction from ours.

Beyond a bottom-up analytical approach, I wanted to see how real-life tournaments behave. Although online gaming has generated a wealth of data, accessing the data is difficult, and I could find only one other paper which investigated the phenomenon. This was ‘Universal statistical properties of poker tournaments’ by Clement Sire (Sire 2007). The author notes that most of the game-theoretic work on poker has been on largely restricted to optimal betting strategies in head-to-head tournaments. Sire builds a relatively simple model of player behavior: a player bets according to a simple evaluation of their hand and the table, and goes all-in if their hand is evaluated to be above a certain quality. This model predicts that tournaments will have certain statistical properties; this prediction is born out in real-life tournaments.

I was only able to get two suitable datasets, so it’s hard to draw solid conclusions about what is going on. However,  there are interesting observations to be made. Here’s a visualisation of one tournament:

Tournament fortunes and entropy for one set of data. The top graph shows the holdings of each player; the bottom graph shows the entropy of the tournament as a function of time (green). The red lines are the upper and lower bounds for the tournament; this is a function of the number of players, which is in turn a function of time.

For one thing, the entropy remains close to its theoretical maximum value, generally ~90% of the absolute maximum. In the tournament pictured, entropy appears to increase to a maximum, and then slowly decline, before the loss of a player abruptly changes the distribution of chips (the sudden changes in the stair-step of the max/min entropy.)  Furthermore, when the tournament entropy is normalized by its maximum entropy, there is a significant upwards trend (p = 0.012). Over the course of the tournament, the entropy increases towards its theoretical maximum. Additionally, it is interesting to me that, in between the losses of players, entropy appears to increase, reach a maximum, and then decrease again before collapsing. (It’s more clear in this image) I interpret this as the redistribution of the winnings of the leaving player (eg, of cyan to black and then to the rest of the table in hands 1-25) followed by a concentration of chips which eventually pushes a player out (yellow vs. the rest, hands 25-40).

However, none of these observations held in the second tournament. One possibility is that, because the second tournament was faster paced, players were eliminated much faster, and these frequent perturbations are obscuring the pattern. On the other hand, it’s entirely possible that the first tournament was a fluke. The only way to resolve this question is with more data!

One reason I am interested in this question has to do with a series of papers written by Arto Annilla from the University of Helsinki. He’s shown that protein folding, genomics, abiogenesis, ecological succession – pretty much every aspect of nature – is not merely constrained by the second law of thermodynamics, but a direct consequence of it. Most relevant to this project, I think, is his analysis of economies and ecosystems. The ultimate goal of each, he argues, is not only to increase entropy to a maximum, but to do so as fast as possible. To the extent that poker tournaments can be thought of as a toy model of an economy, they may provide empirical insights into thermoeconomics. Of course, we’ve already seen that tournament entropy is formally constrained to decrease with time, though it would be interesting to see the behavior of a tournament which is not driven by a rising minimum bet, as these are. The first tournament may show an upward trend after perturbation from quasiequilibrium conditions, and the relative entropy may show a tournament-scale increase. (Or, it may not. Argh! Why oh why must n=2?!) Continue reading