Figure 3.3

Prisoner's Dilemma

In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib

In [2]:
%load http://www.astroml.org/_downloads/fig_contingency_table.py
In [3]:
"""
A 2x2 Contingency Table
-----------------------
Figure 3.3.

A contingency table showing p(T|D).
"""
# Author: Jake VanderPlas
# License: BSD
#   The figure produced by this code is published in the textbook
#   "Statistics, Data Mining, and Machine Learning in Astronomy" (2013)
#   For more information, see http://astroML.github.com
#   To report a bug or issue, use the following forum:
#    https://groups.google.com/forum/#!forum/astroml-general
from matplotlib import pyplot as plt

#----------------------------------------------------------------------
# This function adjusts matplotlib settings for a uniform feel in the textbook.
# Note that with usetex=True, fonts are rendered with LaTeX.  This may
# result in an error if LaTeX is not installed on your system.  In that case,
# you can set usetex to False.
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)

fig = plt.figure(figsize=(2, 2), facecolor='w')
ax = fig.add_axes((0, 0, 1, 1), xticks=[], yticks=[], frameon=False)

for i in [-1, 0, 1]:
    ax.plot([i, i], [-1, 1], '-k')
    ax.plot([-1, 1], [i, i], '-k')

kwds = dict(ha='center', va='center', size=11)

ax.text(-0.5, 1.15, '0', **kwds)
ax.text(0.5, 1.15, '1', **kwds)
ax.text(0, 1.25, 'T', **kwds)

ax.text(-1.15, 0.5, '0', **kwds)
ax.text(-1.15, -0.5, '1', **kwds)
ax.text(-1.25, 0, 'D', **kwds)

kwds['size'] = 14

ax.text(0.5, 0.5, '$\epsilon_{fP}$', **kwds)
ax.text(-0.5, 0.5, '$1 - \epsilon_{fP}$', **kwds)
ax.text(-0.5, -0.5, '$\epsilon_{fN}$', **kwds)
ax.text(0.5, -0.5, '$1 - \epsilon_{fN}$', **kwds)

ax.set_xlim(-1.5, 1.2)
ax.set_ylim(-1.2, 1.5)

plt.show()

Note that this figure was the subject of a Text Erratum. I scrutinized the figure and text and was not able to substantiate the erratum. What am I missing? I submitted an issue to the astroML erratum page.

The modified figure

This is a simple table-like figure. Let's modify it to look like the prototypical example of game theory, "the Prisoner's Dilemma".

In [25]:
"""
A 2x2 Contingency Table
-----------------------
Figure 3.3.

A contingency table showing p(T|D).
"""
# Author: Jake VanderPlas
# License: BSD
#   The figure produced by this code is published in the textbook
#   "Statistics, Data Mining, and Machine Learning in Astronomy" (2013)
#   For more information, see http://astroML.github.com
#   To report a bug or issue, use the following forum:
#    https://groups.google.com/forum/#!forum/astroml-general
from matplotlib import pyplot as plt

#----------------------------------------------------------------------
# This function adjusts matplotlib settings for a uniform feel in the textbook.
# Note that with usetex=True, fonts are rendered with LaTeX.  This may
# result in an error if LaTeX is not installed on your system.  In that case,
# you can set usetex to False.
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=16, usetex=True)

fig = plt.figure(figsize=(4, 4), facecolor='w')
ax = fig.add_axes((0, 0, 2, 2), xticks=[], yticks=[], frameon=False)

for i in [-2, 0, 2]:
    ax.plot([i, i], [-2, 2], '-k')
    ax.plot([-2, 2], [i, i], '-k')

kwds = dict(ha='center', va='center', size=20)

ax.text(-1.0, 2.3, 'Cooperate', **kwds)
ax.text(1.0, 2.3, 'Defect', **kwds)
ax.text(0, 2.5, 'Prisoner A', color='#ff0000',**kwds)

ax.text(-2.5, 1.0, 'Cooperate', **kwds)
ax.text(-2.5, -1.0, 'Defect', **kwds)
ax.text(-2.7, 0, 'Prisoner B', color='#0000ff', **kwds)

kwds['size'] = 20


ax.text(0.9, 1.0, '0', color='#ff0000', **kwds)
ax.text(1.0, 1.0, ',', color='#000000', **kwds)
ax.text(1.2, 1.0, '-20', color='#0000ff', **kwds)

#defect - defect
ax.text(0.85, -1.0, '-6', color='#ff0000', **kwds)
ax.text(1.0, -1.0, ',', color='#000000', **kwds)
ax.text(1.1, -1.0, '-6', color='#0000ff', **kwds)

ax.text(-1.15, 1.0, '-1', color='#ff0000', **kwds)
ax.text(-1.0, 1.0, ',', color='#000000', **kwds)
ax.text(-0.9, 1.0, '-1', color='#0000ff', **kwds)

ax.text(-1.2, -1.0, '-20', color='#ff0000', **kwds)
ax.text(-1.0, -1.0, ',', color='#000000', **kwds)
ax.text(-0.9, -1.0, '0', color='#0000ff', **kwds)

ax.set_xlim(-3, 2.4)
ax.set_ylim(-2.4, 3)

plt.show()

The figure shows the classic prisoner's dilemma game from game theory economics. The numbers represent jail sentences in years, represented as a negative number since years spent in jail are "lost". The key idea of the prisoner's dilemma is that the dominant strategy is always to defect. In other words, regardless of how your accomplice behaves, you will spend fewer years in prison if you defect than if you cooperate.

In []: