{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from scipy.special import gammaln\n", "\n", "outcomes = np.zeros((10001, 3), int)\n", "outcome_to_index = {'W': 0, 'B': 1, 'D': 2}\n", "for game, line in enumerate(open('data/chess_outcomes.txt')):\n", " outcome = line.strip()\n", " index = outcome_to_index[outcome]\n", " outcomes[game + 1][index] = 1\n", " \n", "counts_up_to = outcomes.cumsum(axis=0)\n", "\n", "def compute_prior(w, b):\n", " if 0 < w < 1 and \\\n", " 0 < b < 1 and \\\n", " w + b < 1:\n", " return 1. # the factor of 2 is taken care of by the Bayes denominator\n", " else:\n", " return 0.\n", "\n", "def compute_log_evidence_factor(w, b, W, B, D):\n", " return W * np.log(w) + B * np.log(b) + D * np.log(1 - w - b)\n", "\n", "def compute_log_denominator(W, B, D):\n", " return gammaln(W + 1) + gammaln(B + 1) + gammaln(D + 1) - gammaln(W + B + D + 3)\n", "\n", "def compute_p(w, b, W, B, D):\n", " prior = compute_prior(w, b)\n", " if prior == 0.:\n", " p = 0.\n", " else:\n", " log_evidence_factor = compute_log_evidence_factor(w, b, W, B, D)\n", " log_bayes_denominator = compute_log_denominator(W, B, D)\n", " p = np.exp(log_evidence_factor - log_bayes_denominator) * prior\n", " return p\n", "\n", "compute_p = np.vectorize(compute_p)\n", "\n", "n = 200 # Number of grid points on each axis\n", "w, w_step = np.linspace(0, 1., num=n, retstep=True)\n", "b, b_step = np.linspace(0, 1., num=n, retstep=True)\n", "ww, bb = np.meshgrid(w, b)\n", "\n", "def get_p(num_games):\n", " W, B, D = counts_up_to[num_games]\n", " p = compute_p(ww, bb, W, B, D)\n", " return p" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 121 }, { "cell_type": "code", "collapsed": false, "input": [ "ns = range(10) + range(10, 100, 10) + range(100, 1000, 100) + range(1000, 10001, 1000)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 123 }, { "cell_type": "code", "collapsed": false, "input": [ "%time ps = [get_p(n) for n in ns]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "CPU times: user 31.1 s, sys: 8 ms, total: 31.2 s\n", "Wall time: 31.2 s\n" ] } ], "prompt_number": 124 }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.pyplot as plt\n", "from matplotlib import animation\n", "from JSAnimation import IPython_display\n", "\n", "fig, ax = plt.subplots()\n", "\n", "im = ax.imshow(get_p(0), extent=[0, 1, 0, 1], origin='lower')\n", "ax.set_xlabel('w')\n", "ax.set_ylabel('b')\n", "fig.colorbar(im)\n", "\n", "def animate(i):\n", " im.set_array(ps[i])\n", " im.autoscale()\n", " ax.set_title('Posterior after {0:5d} games'.format(ns[i]))\n", " return [im]\n", "\n", "animation.FuncAnimation(fig, animate, frames=len(ns), interval=400)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " Once \n", " Loop \n", " Reflect \n", "
\n", "
\n", "\n", "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 126, "text": [ "" ] } ], "prompt_number": 126 } ], "metadata": {} } ] }