Skip to content

Instantly share code, notes, and snippets.

@drvinceknight
Created February 7, 2015 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drvinceknight/d9af5f1bc6b24b9033d2 to your computer and use it in GitHub Desktop.
Save drvinceknight/d9af5f1bc6b24b9033d2 to your computer and use it in GitHub Desktop.
"""
Quick script and data to analyse game played in class against a random strategy
The variable 'student_data' is a list composed of lists of strategies recorded by
players in class against a computer player a series of 3 mixed strategies:
- (.2, .8)
- (.9, .1)
- (1/3, 2/3)
The game played is a modification of the matching pennies game:
[[2, -2], [-2, 2]]
[[-1, 1], [1, -1]]
"""
from __future__ import division
import matplotlib.pyplot as plt
def analyse_game(student_data, computer_data):
for rnd in range(3): # Plot the mixed strategies.
x = sum(([sum([s == 'h' for s in row]) / 6 for row in [row[rnd]
for row in student_data]])) / len(student_data)
plt.figure()
ind = [1, 2]
width = .5
fig, ax = plt.subplots()
cax = ax.bar(ind, [x, 1 - x], width)
ax.set_xticks([i + width / 2 for i in ind])
ax.set_xticklabels(['$x$', '$1-x$'])
x1, x2, y1, y2 = plt.axis()
plt.axis((x1, x2, 0, 1))
plt.savefig('%sstrategiesvbestresponse.png' % rnd)
print x
scores = []
for row in [row[rnd] for row in student_data]:
score = 0
for j, s in enumerate(row):
if s == computer_data[rnd][j] == 'h':
score += -2
if s == computer_data[rnd][j] == 't':
score += -1
if s == 'h' and computer_data[rnd][j] == 't':
score += 1
if s == 't' and computer_data[rnd][j] == 'h':
score += 2
scores.append(score)
print sum(scores) / len(scores)
plt.figure()
plt.hist(scores)
plt.savefig('%sscore_histogram.png' % rnd)
if __name__ == '__main__':
student_data = [['hhthth', 'tttttt', 'hhhhth'],
['hhhhhh', 'tttttt', 'hththt'],
['hhhhhh', 'ththtt', 'ttthtt'],
['hhhhhh', 'tttttt', 'hhhhhh'],
['hhhhhh', 'tttttt', 'hhhhhh'],
['hhhthh', 'tttttt', 'hththh'],
['hhhthh', 'tttttt', 'hththh'],
['hhhhth', 'tttttt', 'htthht'],
['hthhhh', 'tttttt', 'hthhht'],
['tthhhh', 'tttttt', 'ttthtt'],
['hthhtt', 'httttt', 'tthtth'],
['hhtthh', 'tttttt', 'hthhht'],
['hhhhtt', 'tttttt', 'tthhtt'],
['hhtttt', 'tttttt', 'hhhtth'],
['hhhhhh', 'ttttht', 'hthttt'],
['hhhhhh', 'ttttht', 'hthttt'],
['hhhhhh', 'tttttt', 'hthhth'],
['hhhtth', 'ttttth', 'thhhth'],
['hhhtth', 'ttttth', 'thhhth'],
['hhhthh', 'ttttth', 'htthth'],
['hhhhhh', 'tttttt', 'hhhhht'],
['hhhhtt', 'ttttth', 'hhthth'],
['htthht', 'tthttt', 'thhhtt'],
['hhhhth', 'tttttt', 'hthhth'],
['hhhhtt', 'tttttt', 'hhhhhh'],
['hhttht', 'tttttt', 'hhtttt'],
['httttt', 'tttttt', 'hhhtth'],
['httttt', 'tttttt', 'hhhtth'],
['hhthht', 'tttttt', 'hhthth'],
['hhhtth', 'tttttt', 'thhthh'],
['thhthh', 'tttttt', 'hhttht'],
['hhhtht', 'tttttt', 'hhhtth'],
['hhhhhh', 'tttttt', 'hhhtth'],
['hhhhhh', 'tttttt', 'thhhht'],
['hhhthh', 'tttttt', 'hhhhhh'],
['hhhhtt', 'tttttt', 'hhhhth'],
['hhhhhh', 'tttttt', 'htthtt'],
['hhthhh', 'tttttt', 'hhhhtt'],
['hhthtt', 'tttttt', 'hhtttt'],
['hhhhhh', 'tttttt', 'hhhhhh'],
['thhttt', 'ttttht', 'hhhttt'],
['thhhht', 'httttt', 'httttt'],
['hhhtht', 'tttttt', 'hhhttt'],
['hhhttt', 'tttttt', 'ttttth'],
['hhhtth', 'tttttt', 'hthhth'],
['hhtthh', 'tttttt', 'htthth'],
['thttht', 'tttttt', 'ttthht'],
['hhthhh', 'tthttt', 'hthhhh'],
['thhhht', 'tttttt', 'thhhth'],
['thttth', 'tttttt', 'htttth'],
['thhhht', 'tttttt', 'hhhhhh'],
['hhthht', 'ttthth', 'hhhtth'],
['thhtth', 'tttttt', 'hhhhhh'],
['hhhhhh', 'tttttt', 'hththt'],
['hhhtht', 'tttttt', 'thtthh'],
['hhthhh', 'tttttt', 'tthhtt'],
['hhhtht', 'tthttt', 'hhhhtt'],
['hthhtt', 'ttthtt', 'hhhtth'],
['hhhhhh', 'tttttt', 'hhhhhh'],
['hhhthh', 'tttttt', 'hhthhh'],
['hhhhhh', 'tttttt', 'hhhhhh'],
['hhthht', 'tthtth', 'hhthht'],
['thhhht', 'ttttth', 'hhttht'],
['htthtt', 'tthttt', 'ttttth']
]
computer_data = ['ttttth', 'hhhhhh', 'httthh']
analyse_game(student_data, computer_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment