speice.io/blog/2016-05-15-the-unfair-casino/_notebook.ipynb

720 lines
179 KiB
Plaintext
Raw Normal View History

2024-11-03 19:16:00 -05:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or, how to get thrown out of a casino because you're a mathematician.\n",
"\n",
"---\n",
"\n",
"In the ongoing eternal cycle of mathematicians asking generally useless questions about probability, I dreamt up another one. The scenario is as follows:\n",
"\n",
"**You're playing a game with two die, and you do not get to see what the outcome of the die are on each roll. All you get to see is their sum. Given an arbitrarily long list of the sum of two rolls, can you determine if one or both die are loaded, and what those loadings are?**\n",
"\n",
"# Proving we can detect cheating\n",
"\n",
"My first question is simply, is this possible? There's a lot of trivial cases that make it obvious that there's cheating going on. But there are some edge cases that might give us more difficulty. First though, let's get a picture of what the fair distribution looks like. In principle, we can only detect cheating if the distribution of the fair die differs from the distribution of the loaded die."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEKCAYAAAA8QgPpAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHVRJREFUeJzt3X20XXV95/H3hyBPEhB1yK2JkCANBgtq1PiA1KMoD1pD\nOrOKWFtEGGeVYKE6y1WCayZ3OmsUnDI+tIOjgjGwQBrwgTDFACkcK1MwKNCgQUyrCUkglyIUH7CQ\nSz7zx943HC65ybk795x97zmf11pnZe/f2Wd/v+esm/M9v99vP8g2ERER47VX3QlERMTUlAISERGV\npIBEREQlKSAREVFJCkhERFSSAhIREZWkgERfkPRlSRdM8D7nSPrFBO5vR46STpD0swnc99sk3TdR\n+4sAUM4DialE0gbgUGAYEGBgru2tExznbOCLwJNl078ATeBTtv+pwr7+yPbbx/GaE4Av2z5iPLHK\n104DtgGzbT843tdHtCs9kJhqDLzH9kG2p5f/7lHxKL9wd+bvbR8EHAy8k+JL+fuSjhpvCIq8d5XD\nRP5f3G28iImQAhJ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ltS9un\nJd0v6V5JX5d0UMtzSyStL58/saV9vqS1kn4i6bOdzDkiItrT6R7IMuCkUW03A6+y/RpgPbAEQNLR\nwGnAPOAU4NLyHugAXwDOtj0XmCtp9D4jIqLLOlpAbN8OPD6qbbXt7eXqncCscnkhcI3tYdsbKIrL\nAkkDwHTbd5XbXQEs6mTeERGxe3XPgZwF3FguzwQ2tTy3pWybCWxuad9ctkVERI1quye6pE8A22x/\nbaL3PTg4uGO50WjQaDQ
"text/plain": [
"<matplotlib.figure.Figure at 0x2a460450e80>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"fair_1 = np.random.randint(1, 7, 10000)\n",
"fair_2 = np.random.randint(1, 7, 10000)\n",
"\n",
"pd.Series(fair_1 + fair_2).plot(kind='hist', bins=11);\n",
"plt.title('Fair Distribution');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This distribution makes sense: there are many ways to make a 7 (the most frequent observed value) and very few ways to make a 12 or 2; an important symmetry. As a special note, you can notice that the sum of two fair dice is a discrete case of the [Triangle Distribution][1], which is itself a special case of the [Irwin-Hall Distribution][2].\n",
"\n",
"# The Edge Cases\n",
"\n",
"Given that we understand how the results of two fair dice are distributed, let's see some of the interesting edge cases that come up. This will give us assurance that when a casino is cheating, it is detectable (given sufficient data). To make this as hard as possible, we will think of scenarios where the expected value of the sum of loaded dice is the same as the expected value of the sum of fair dice.\n",
"\n",
"### Edge Case 1\n",
"What happens when one die is biased low, and one die is biased high? That is, where:\n",
"\n",
"\\begin{align}\n",
"\\begin{array}{cc}\n",
"D_1 = \\left\\{\n",
"\\begin{array}{lr}\n",
"1 & w.p. 1/3\\\\\n",
"2 & w.p. 1/3\\\\\n",
"3 & w.p. 1/12\\\\\n",
"4 & w.p. 1/12\\\\\n",
"5 & w.p. 1/12\\\\\n",
"6 & w.p. 1/12\n",
"\\end{array}\n",
"\\right. &\n",
"D_2 = \\left\\{\n",
"\\begin{array}{lr}\n",
"1 & w.p. 1/12\\\\\n",
"2 & w.p. 1/12\\\\\n",
"3 & w.p. 1/12\\\\\n",
"4 & w.p. 1/12\\\\\n",
"5 & w.p. 1/3\\\\\n",
"6 & w.p. 1/3\n",
"\\end{array}\n",
"\\right. \\\\\n",
"\\mathbb{E}[D_1] = 2.5 & \\mathbb{E}[D_2] = 4.5\n",
"\\end{array}\\\\\n",
"\\mathbb{E}[D_1 + D_2] = 7 = \\mathbb{E}[D_{fair} + D_{fair}]\n",
"\\end{align}\n",
"\n",
"[1]: https://en.wikipedia.org/wiki/Triangular_distribution\n",
"[2]: https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEMCAYAAADqG+D0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAG2RJREFUeJzt3X20XXV95/H3ByJP8lB8gKsJTz6EBlrEIPGBihetgG15\n0I4RdRURdLkUBWtnRsJ0hminU+laiFgLqz5hYMAIKAothZAFV8ZpNRRJQZKBVA0QMBcfEEVYmJDP\n/LF/lxxubpJzd7LPvvecz2uts7LP7+y9f9+zc8/5nt/D3lu2iYiImKwd2g4gIiKmpySQiIioJQkk\nIiJqSQKJiIhakkAiIqKWJJCIiKglCSQiImpJAomIiFqSQGK7k/RjSW/cwus/kHR0D+O5VNInt/D6\nFuOdqvrpOE8m1un6/9WPZrQdQAwe27/XdgyDYDod5+kUa2yUBBLPIul1wF8BTwHXALsAvw/cZfuS\nNmPrJznO0Q/ShRXPYvtfgPXAZ2x/2fbFtj8IvEPS8CR2NU/SPZJ+LulLknYae6GzC0LSxyX9h6Rf\nlW6Mkzt3Ul5fU15fKemYUv4iSddIekTSDyV9pGObV0q6Q9JjkhZTfTl3RdIcSbdKelTS3Z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0naq2ObBZJWSVop\n6diO8rmS7pJ0n6TPNBl3RERsXdMtkPXAx2wfCrwWOFPS7wLnAEttHwzcAiwAkHQIMB+YA7wFuFiS\nyr4uAc6wPRuYLem4hmOPiIgtaDSB2F5re3lZfhxYCcwCTgIWldUWASeX5ROBxbbX214NrALmSRoC\n9rB9e1nvso5tIiKiBT0
"text/plain": [
"<matplotlib.figure.Figure at 0x2a460ad01d0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def unfair_die(p_vals, n):\n",
" x = np.random.multinomial(1, p_vals, n)\n",
" return x.nonzero()[1] + 1\n",
"\n",
"d1 = [1/3, 1/3, 1/12, 1/12, 1/12, 1/12]\n",
"d2 = [1/12, 1/12, 1/12, 1/12, 1/3, 1/3]\n",
"\n",
"x1 = unfair_die(d1, 10000)\n",
"x2 = unfair_die(d2, 10000)\n",
"\n",
"pd.Series(x1 + x2).plot(kind='hist', bins=11);\n",
"plt.title('$D_1$ biased low, $D_2$ biased high');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that while the 7 value remains the most likely (as expected), the distribution is not so nicely shaped any more. \n",
"\n",
"**Edge Case 2:** When one die is loaded low, and one is loaded high, we've seen how we can detect them. How about when two die are loaded both low and high? That is, we have the following distribution:\n",
"\n",
"\\begin{align}\n",
"\\begin{array}{cc}\n",
"D_1 = \\left\\{\n",
"\\begin{array}{lr}\n",
"1 & w.p. 1/3\\\\\n",
"2 & w.p. 1/12\\\\\n",
"3 & w.p. 1/12\\\\\n",
"4 & w.p. 1/12\\\\\n",
"5 & w.p. 1/12\\\\\n",
"6 & w.p. 1/3\n",
"\\end{array}\n",
"\\right. &\n",
"D_2 = \\left\\{\n",
"\\begin{array}{lr}\n",
"1 & w.p. 1/3\\\\\n",
"2 & w.p. 1/12\\\\\n",
"3 & w.p. 1/12\\\\\n",
"4 & w.p. 1/12\\\\\n",
"5 & w.p. 1/12\\\\\n",
"6 & w.p. 1/3\n",
"\\end{array}\n",
"\\right. \\\\\n",
"\\mathbb{E}[D_1] = 3.5 & \\mathbb{E}[D_2] = 3.5\n",
"\\end{array}\\\\\n",
"\\mathbb{E}[D_1 + D_2] = 7 = \\mathbb{E}[D_{fair} + D_{fair}]\n",
"\\end{align}\n",
"\n",
"We can see even that the expected value of each individual die is the same as the fair die! However, the distribution (if we are doing this correctly) should still be skewed:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEMCAYAAADqG+D0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGuhJREFUeJzt3X2UXXV97/H3ByIC8lAskmCigNUg4ANGDSrLdkB50Cqg\nvSLiKuLTcikCVXsvhNpL7JPStRTQXlxVEQMXmgv4BJZCYOHUZXslEUGQ5EJaDZBABhVEKQgJ+dw/\n9m/gOJmHM3uyz54583mtdVb2+Z398N1nJvM5v9/eZ2/ZJiIiYrK2a7uAiIiYmRIgERFRSwIkIiJq\nSYBEREQtCZCIiKglARIREbUkQCIiopYESMw4ki6S9Fc92M7PJB0+zus/kfSHTdfRsb2e7Hev9et+\nzQYJkIiabL/E9vfarmPYRIHXxfKnSFol6beSvrota5sKSSdIWi3pEUlrJR3adk1RmdN2ATH9SXod\n8NfA48CVwI7AS4HbbH+xzdrGMhNrngY2UL1nRwE7tVwLAJKOAD4NHG97laS9264pnpYeSEzI9r8D\nm4HzbH/V9gW2Pwy8U9JAN+uQdIak/5D06zL0c9yI138m6ROSfizpIUn/JGmH8torJN0s6WFJy6nC\noPGai8WS7pD0S0kXDtfUUfPhXe7fGZLWl9fXSDqstO8t6UpJD0j6T0mndizT9X5Luhh4PnB12caf\nS3qxpO+W9/N2SW+d4D37lu2rgAcnelPG29/xfpaT3S9gKfBXtleVGu+3ff9E9UVvJEBiQpKeARwC\nfH/ESz8Ajtt6iVH9B3Co7d2ATwH/W9LcEfO8AzgS2A94OXBy2fY3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0x2a461035a20>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"d1 = [1/3, 1/12, 1/12, 1/12, 1/12, 1/3]\n",
"d2 = d1\n",
"\n",
"x1 = unfair_die(d1, 10000)\n",
"x2 = unfair_die(d2, 10000)\n",
"\n",
"pd.Series(x1 + x2).plot(kind='hist', bins=11)\n",
"plt.title(\"$D_1$ and $D_2$ biased to 1 and 6\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a very un-subtle way, we have of course made the values 2 and 12 far more likely.\n",
"\n",
"# Detection Conclusion\n",
"\n",
"There are some trivial examples of cheating that are easy to detect: whenever the expected value of the sum of two fair dice deviates from the expected value for the sum of two fair dice, we can immediately conclude that there is cheating at stake.\n",
"\n",
"The interesting edge cases occur when the expected value of the sum of loaded dice matches the expected value of the sum of fair dice. Considering the above examples (and a couple more I ran through in developing this), we have seen that in every circumstance having two unfair dice leads to a distribution of results different from the fair results.\n",
"\n",
"We can thus finally state: **just by looking at the distribution of results from this game, we can immediately conclude whether there is cheating.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simulated Annealing\n",
"\n",
"What we really would like to do though, is see if there is any way to determine how exactly the dice are loaded. This is significantly more complicated, but we can borrow some algorithms from Machine Learning to figure out exactly how to perform this process. I'm using the Simulated Annealing algorithm, and I discuss why this works and why I chose it over some of the alternatives in the [justification](#Justification-of-Simulated-Annealing). If you don't care about how I set up the model and just want to see the code, check out [the actual code](#The-actual-code).\n",
"\n",
"[Simulated Annealing][3] is a variation of the [Metropolis-Hastings Algorithm][4], but the important thing for us is: Simulated Annealing allows us to quickly optimize high-dimensional problems. But what exactly are we trying to optimize? Ideally, we want a function that can tell us whether one distribution for the dice better explains the results than another distribution. This is known as the **likelihood** function.\n",
"\n",
"## Deriving the Likelihood function\n",
"\n",
"To derive our likelihood function, we want to know: **what is the probability of seeing a specific result given those hidden parameters?** This is actually a surprisingly difficult problem. While we can do a lot of calculations by hand, we need a more general solution since we will be working with very some interesting die distributions.\n",
"\n",
"We first note that the sum of two dice can take on 11 different values - 2 through 12. This implies that each individual sum follows a [Categorical distribution](https://en.wikipedia.org/wiki/Categorical_distribution). That is:\n",
"\n",
"\\begin{align}\n",
"\\mathcal{L(x)} = \\left\\{\n",
"\\begin{array}{lr}\n",
"p_2 & x = 2\\\\\n",
"p_3 & x = 3\\\\\n",
"\\ldots & \\\\\n",
"p_{11} & x = 11\\\\\n",
"p_{12} & x = 12\n",
"\\end{array}\n",
"\\right.\n",
"\\end{align}\n",
"\n",
"Where each $p_i$ is the probability of seeing that specific result. However, we need to calculate what each probability is! I'll save you the details, but [this author](http://math.stackexchange.com/a/1646360/320784) explains how to do it.\n",
"\n",
"Now, we would like to know the likelihood of our entire data-set. This is trivial:\n",
"\n",
"\\begin{align}\n",
"\\mathcal{L(\\mathbf{X})} &= \\prod_{i=1}^n L(x)\n",
"\\end{align}\n",
"\n",
"However, it's typically much easier to work with the $\\log(\\mathcal{L})$ function instead. This is critically important from a computational perspective: when you multiply so many small numbers together (i.e. the product of $L(x)$ terms) the computer suffers from rounding error; if we don't control for this, we will find that no matter the distributions we choose for each die, the \"likelihood\" will be close to zero because the computer is not precise enough.\n",
"\n",
"\\begin{align}\n",
"\\log(\\mathcal{L}) &= \\sum_{i=1}^n \\log(L)\n",
"\\end{align}\n",
"\n",
"## The process of Simulated Annealing\n",
"\n",
"The means by which we optimize our likelihood function is the simulated annealing algorithm. The way it works is as follows:\n",
"\n",
"1. Start with a random guess for the parameters we are trying to optimize. In our case we are trying to guess the distribution of two dice, and so we \"optimize\" until we have a distribution that matches the data.\n",
"\n",
"2. For each iteration of the algorithm:\n",
"\n",
" 1. Generate a new \"proposed\" set of parameters based on the current parameters -\n",
" i.e. slightly modify the current parameters to get a new set of parameters.\n",
" 2. Calculate the value of $\\log(\\mathcal{L})$ for each set of parameters. If the function value for the\n",
" proposed parameter set is higher than for the current, automatically switch to the new parameter set\n",
" and continue the next iteration.\n",
" 3. Given the new parameter set performs worse, determine a probability of switching to the new parameter set anyways: $\\mathcal{P}(p_{current}, p_{proposed})$\n",
" 4. Switch to the new parameter set with probability $\\mathcal{P}$. If you fail to switch, begin the next iteration.\n",
" \n",
"3. The algorithm is complete after we fail to make a transition $n$ times in a row.\n",
"\n",
"If everything goes according to plan, we will have a value that is close to the true distribution of each die.\n",
"\n",
"# The actual code\n",
"\n",
"We start by defining the score function. This will tell us how well the proposed die densities actually explain the results.\n",
"\n",
"[3]:https://en.wikipedia.org/wiki/Simulated_annealing\n",
"[4]:https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy import polynomial\n",
"\n",
"def density_coef(d1_density, d2_density):\n",
" # Calculating the probabilities of each outcome was taken\n",
" # from this author: http://math.stackexchange.com/a/1710392/320784\n",
" d1_p = polynomial.Polynomial(d1_density)\n",
" d2_p = polynomial.Polynomial(d2_density)\n",
" coefs = (d1_p * d2_p).coef\n",
" return coefs\n",
"\n",
"def score(x, d1_density, d2_density):\n",
" # We've now got the probabilities of each event, but we need\n",
" # to shift the array a bit so we can use the x values to actually\n",
" # index into it. This will allow us to do all the calculations\n",
" # incredibly quickly\n",
" coefs = density_coef(d1_density, d2_density)\n",
" coefs = np.hstack((0, 0, coefs))\n",
" return np.log(coefs[x]).sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Afterward, we need to write something to permute the proposal densities. We make random modifications, and eventually the best one survives."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def permute(d1_density, d2_density):\n",
" # To ensure we have legitimate densities, we will randomly\n",
" # increase one die face probability by `change`,\n",
" # and decrease one by `change`.\n",
" # This means there are something less than (1/`change`)^12 possibilities\n",
" # we are trying to search over.\n",
" change = .01\n",
" \n",
" d1_index1, d1_index2 = np.random.randint(0, 6, 2)\n",
" d2_index1, d2_index2 = np.random.randint(0, 6, 2)\n",
" \n",
" # Also make sure to copy. I've had some weird aliasing issues\n",
" # in the past that made everything blow up.\n",
" new_d1 = np.float64(np.copy(d1_density))\n",
" new_d2 = np.float64(np.copy(d2_density))\n",
" \n",
" # While this doesn't account for the possibility that some\n",
" # values go negative, in practice this never happens\n",
" new_d1[d1_index1] += change\n",
" new_d1[d1_index2] -= change\n",
" new_d2[d2_index1] += change\n",
" new_d2[d2_index2] -= change\n",
" \n",
" return new_d1, new_d2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we've got the main algorithm code to do. This is what brings all the pieces together."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def optimize(data, conv_count=10, max_iter=1e4):\n",
" switch_failures = 0\n",
" iter_count = 0\n",
" \n",
" # Start with guessing fair dice\n",
" cur_d1 = np.repeat(1/6, 6)\n",
" cur_d2 = np.repeat(1/6, 6)\n",
" cur_score = score(data, cur_d1, cur_d2)\n",
" \n",
" # Keep track of our best guesses - may not be\n",
" # what we end with\n",
" max_score = cur_score\n",
" max_d1 = cur_d1\n",
" max_d2 = cur_d2\n",
" \n",
" # Optimization stops when we have failed to switch `conv_count`\n",
" # times (presumably because we have a great guess), or we reach\n",
" # the maximum number of iterations.\n",
" while switch_failures < conv_count and iter_count < max_iter:\n",
" iter_count += 1\n",
" if iter_count % (max_iter / 10) == 0:\n",
" print('Iteration: {}; Current score (higher is better): {}'.format(\n",
" iter_count, cur_score))\n",
" \n",
" new_d1, new_d2 = permute(cur_d1, cur_d2)\n",
" new_score = score(data, new_d1, new_d2)\n",
" \n",
" if new_score > max_score:\n",
" max_score = new_score\n",
" max_d1 = new_d1\n",
" max_d2 = new_d2\n",
" \n",
" if new_score > cur_score:\n",
" # If the new permutation beats the old one,\n",
" # automatically select it.\n",
" cur_score = new_score\n",
" cur_d1 = new_d1\n",
" cur_d2 = new_d2\n",
" switch_failures = 0\n",
" else:\n",
" # We didn't beat the current score, but allow\n",
" # for possibly switching anyways.\n",
" accept_prob = np.exp(new_score - cur_score)\n",
" coin_toss = np.random.rand()\n",
" if coin_toss < accept_prob:\n",
" # We randomly switch to the new distribution\n",
" cur_score = new_score\n",
" cur_d1 = new_d1\n",
" cur_d2 = new_d2\n",
" switch_failures = 0\n",
" else:\n",
" switch_failures += 1\n",
" \n",
" # Return both our best guess, and the ending guess\n",
" return max_d1, max_d2, cur_d1, cur_d2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now we have finished the hard work!\n",
"\n",
"# Catching the Casino\n",
"\n",
"Let's go through a couple of scenarios and see if we can catch the casino cheating with some loaded dice. **In every scenario we start with an assumption of fair dice**, and then try our hand to figure out what the *actual* distribution was.\n",
"\n",
"## Attempt 1\n",
"\n",
"\n",
"The casino is using two dice that are both biased low. How well can we recover the distribution?"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration: 1000; Current score (higher is better): -22147.004400281654\n",
"Simulated Annealing time: 0.30s\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl0AAAJZCAYAAACTE4A9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XucZHV95//XGyYYL4iocSYOAopyjYawm5HERDtho4Ab\nhk2iAd31grvLL17gF10XMBcmySaRuCq6hLisSMCgBDWJZIOKrvZGY+SiQYwMMEYZhwHacPGC/iRc\nPr8/zhmsKaqnq7urT1V3v56PRz2m6pzv+Z5P1VR9+lPf8z2nUlVIkiRpae027gAkSZJWA4suSZKk\nDlh0SZIkdcCiS5IkqQMWXZIkSR2w6JIkSeqARZeWTJKXJfmbEfZ3Q5LntPd/L8l7Rtj3byY5d1T9\nSRq9JN9Jsv8u1v9Jkt9Y5D6el2TbIvu4PMl/WEwfPX39TJLNPY+/luTnR9F3298/JnnuqPrTrsXr\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nanlFxCbDT9T2q6o6q+jzN\nt8SHtBOrf7aqLmjb3d9eJ0mSlkxVfYbm0HKvX6OZd3N/22bHGXQbgUva/HQzzeGaDe0k5D3bw1TQ\nXLtt6BNLtCAertKKNkzRtZ6dT8vtP3V+V54K3JHmKsFfSHJekkfON0hJGoEDgecm+VyaK4b/q3Z5\nf47b3i5bz85nrM4n92kBquokDy1qJVvqifRraOZ5/XFVHUFzTY/Tl3ifkjTIGmDvqjqS5ozUD8zR\nXpJGas0QbbbTXDhzh32Y/Zop/W6hmRS54zT+DwKnDWqYxGFlaZWpqlFe32ku24C/aPd7dZIHkjyB\n2XPcdna+HtKsuc/8Ja1O881hw4x0XQ08Pcl+7XVTTqD5weDZPBRAVc0A2/KDHws+Crh+tg2ramJu\nZ5555thjMJ7lHZPx7Pr
"text/plain": [
"<matplotlib.figure.Figure at 0x2a46294cf98>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"def simulate_casino(d1_dist, d2_dist, n=10000):\n",
" d1_vals = unfair_die(d1_dist, n)\n",
" d2_vals = unfair_die(d2_dist, n)\n",
" \n",
" start = time.perf_counter()\n",
" max_d1, max_d2, final_d1, final_d2 = optimize(d1_vals + d2_vals)\n",
" end = time.perf_counter()\n",
" print(\"Simulated Annealing time: {:.02f}s\".format(end - start))\n",
" \n",
" coef_range = np.arange(2, 13) - .5\n",
" plt.subplot(221)\n",
" plt.bar(coef_range, density_coef(d1_dist, d2_dist), width=1)\n",
" plt.title('True Distribution')\n",
" \n",
" plt.subplot(222)\n",
" plt.hist(d1_vals + d2_vals, bins=11)\n",
" plt.title('Empirical Distribution')\n",
" \n",
" plt.subplot(223)\n",
" plt.bar(coef_range, density_coef(max_d1, max_d2), width=1)\n",
" plt.title('Recovered Distribution')\n",
" \n",
" plt.gcf().set_size_inches(10, 10)\n",
"\n",
"\n",
"simulate_casino([2/9, 2/9, 2/9, 1/9, 1/9, 1/9],\n",
" [2/9, 2/9, 2/9, 1/9, 1/9, 1/9])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attempt 2\n",
"\n",
"The casino now uses dice that are both biased towards 1 and 6."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Simulated Annealing time: 0.08s\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl0AAAJZCAYAAACTE4A9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XucZFdd7/3Pd8gBDIQQhMzIhCRyCxdF4NEhHhAbc4QB\nHpl4QRM4ouFy8ggRHvF4ElCcwSvRI0aMQaMBCQYigpB45DIgtofICRmQJEAmZLhkyHUgFwIBwUnm\nd/7Yu5Oapme6urt6VXXP5/161aur9l577VXV1au/tfbau1JVSJIkaXmtGXcDJEmSDgSGLkmSpAYM\nXZIkSQ0YuiRJkhowdEmSJDVg6JIkSWrA0KVlk+QFSf5xhPVdmeTJ/f3fTvKmEdb9G0nOGlV9kkYv\nydeTHL2f9W9M8utL3MePJrlmiXW8N8nPL6WOgbqekmT7wOMvJvmxUdTd1/fpJE8dVX3av3idrpUp\nydeBmV/efYBvA3f2y06uqrcv8/7/Bngu8K1+0dXAPwCnV9XXF1jXW4EdVfVbC9jmt4H1VfXCheyr\n3/Y44K+q6nsXuq2kvSW5GjgcuAMIXR/011X18nG2a7GS/Cjw1qo6ch/r9wDfoHue3wYuBf6yqt6x\niH3tAR5eVV9YwDZfBF5UVR9exP7eDFxTVb+50G01Go50rVBVdUhV3a+q7gfsBJ49sOw7AleSe4y6\nCcDvVtWhwIOAFwE/Anwkyb1GuaNlaPvMPwZJS1d0/c/9Bvqg5oErSav/ZwU8ru97jwHeApyZ5DWL\nrGuflqHv05gZulaH9Le7F3SH385P8rYktwHPT/LWJL85UOa4/lPTzOP1Sf4+yZeTfD7JS4fZeVX9\nR1V9HPgJYB3wC319L0ryz/39J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3Zf0k
"text/plain": [
"<matplotlib.figure.Figure at 0x2a462940978>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"simulate_casino([1/3, 1/12, 1/12, 1/12, 1/12, 1/3],\n",
" [1/3, 1/12, 1/12, 1/12, 1/12, 1/3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attempt 3\n",
"\n",
"The casino will now use one die biased towards 1 and 6, and one die towards 3 and 4."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Simulated Annealing time: 0.09s\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl0AAAJZCAYAAACTE4A9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3X2cZGV55//PFyb4iIgaZ+IgoCiP0RB2M5KYaCdsFHDD\nsEk0oLs+4O7yiyD8ousC5oFJskkkroouIS4rEjAoQU0i2aAiq73RGHlQESMDjFGGYYAxgE/oT8LD\n9fvjnMGaonq6urv6VHX35/169WuqzrnPfa5T03X1Vfe5z6lUFZIkSVpcu4w7AEmSpJXAokuSJKkD\nFl2SJEkdsOiSJEnqgEWXJElSByy6JEmSOmDRpUWT5JVJ/naE/d2Y5Pnt499P8t4R9v1bSc4dVX+S\nRi/Jd5Psu5P1f5rkNxe4jxcm2bLAPi5P8h8W0kdPXz+bZGPP868n+YVR9N32949JXjCq/rRz8T5d\nS1OS7wLb//MeB9wHPNguO7GqPrDI+/9z4KXAD9pFtwB/A5xVVd+dY1/vAzZV1e/NYZvfB9ZW1Qlz\n2Ve77RHAe6rqGXPdVtKOktwCPBV4AAhNDvqzqjplnHHNV5IXAu+rqr1nWP8Q8D2a47wPuA74X1V1\n6Tz29RDwrKr62hy2+Trw2qr65Dz2dwGwpap+Z67bajQc6Vqiqmr3qnpCVT0B2Ay8pGfZIwquJLuO\nOgTgD6pqD+BHgd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CGSRxWllaYqhrl/Z1mswX4y3a/1yR5MMmTmTnH\nbWXH+yHNmPvMX9LKNNccNsxI1zXAs5Ls09435TiaLwyeycMBVNU2YEt++GXBRwA3zLRhVU3Mz5ln\nnjn2GIxnacdkPDv/6UD
"text/plain": [
"<matplotlib.figure.Figure at 0x2a4625ea898>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"simulate_casino([1/3, 1/12, 1/12, 1/12, 1/12, 1/3],\n",
" [1/12, 1/12, 1/3, 1/3, 1/12, 1/12])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attempt 4\n",
"\n",
"We'll now finally go to a fair casino to make sure that we can still recognize a positive result."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Simulated Annealing time: 0.02s\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl0AAAJZCAYAAACTE4A9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XucZHV95//XGyZ4BUSNM3G4eeOaKGGTkVzthEQBNwyb\nRAO6iwE3yy+K8NOsAcyFMdkkoKuiIZhlRQJGJHhJxA0qstqJxiigEogMMkYZYIAmgKLoT+Ty+f1x\nTmNNUT1d3V19qrvn9Xw86jFV53zP93yqpurTn/qe7zmVqkKSJEmLa4dxByBJkrQ9sOiSJEnqgEWX\nJElSByy6JEmSOmDRJUmS1AGLLkmSpA5YdGnRJDk2yd+PsL8bkvxMe/+Pk7x7hH3/fpJzRtWfpNFL\n8u0ke29j/TuT/N4C9/GCJLcssI/LkvyXhfTR09fPJtnY8/jrSX5xFH23/f1rkp8fVX/atnidruUp\nybeB6f+8JwD3Aw+1y06oqvct8v7/GngJ8L120U3AR4Azq+rbc+zrPcCmqvqjOWzzx8Daqjp+Lvtq\ntz0UeFdVPWOu20raWpKbgKcBDwKhyUF/VVUnjTOu+Ury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5Lh/7mm3pV32IFuf\nsdp/2RCNWM3jt1Sl5WRRJ9IneRLNqNheNNf6eGKSly3mPiVpBr9NM19rT5oC7N1jjkfSdmaYka4t\nNBfOnLY7M18zpd8vAV+rqnsA2rMlfprmzIutJHFYWdrOVNUor+80m1dU1cntfj+Q5F3t8i1sfd2j\n6Rw30/JHMX9J26e55rBhRrquAp6dZK/2uilH0/xg8Ex6A7gZOCTJY9uJqYcCG2fasKqWzO30008f\newzGs7xjMp5t3zrQ/0P
"text/plain": [
"<matplotlib.figure.Figure at 0x2a462619cf8>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"simulate_casino(np.repeat(1/6, 6), np.repeat(1/6, 6))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attempt 5\n",
"\n",
"We've so far been working with a large amount of data - 10,000 data points. Can we now scale things back to only 250 throws? We'll start with two dice biased high."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration: 1000; Current score (higher is better): -551.6995384525453\n",
"Iteration: 2000; Current score (higher is better): -547.7803673440676\n",
"Iteration: 3000; Current score (higher is better): -547.9805613193807\n",
"Iteration: 4000; Current score (higher is better): -546.7574874775273\n",
"Iteration: 5000; Current score (higher is better): -549.5798007672656\n",
"Iteration: 6000; Current score (higher is better): -545.0354060154496\n",
"Iteration: 7000; Current score (higher is better): -550.1134504086606\n",
"Iteration: 8000; Current score (higher is better): -549.9306537114975\n",
"Iteration: 9000; Current score (higher is better): -550.7075182119111\n",
"Iteration: 10000; Current score (higher is better): -549.400679551826\n",
"Simulated Annealing time: 1.94s\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl0AAAJZCAYAAACTE4A9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xu4ZFV95//3BzoyXgAxxj7aCO0lYiRR9BcJiYkeQ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1CSbaN64e9FMTDx5wf5DaP7oPg14fVWd0rNvf5o/qj9Ok92/oqouHNoz\nkKQhaa9+vZRmAckXJNkC/AZN8g1N+/Z3I6vglIuLd2rKrVusQNsInUZzefwNwMVJzq2qK3uK3UIz\nVNVv/s/bgfOr6kXtUM2D+pSRpHFwPHAFzZWwO53S+0VSkpZrkOHFw2l+omV7e0XFOcAu63tU1c1V\ndSnNz0Dcq51Y/XNVdWZb7u52nSRJGitJDqQZYn/3wl0jqM5a5XCVptogSdcGdr0sd+Gl83vyGODm\nNKsEfy7J6UkeuNRKSlIH3kZzQcbCP/zHpfkFhXe7EOTqqqpXOLSoabbaE+nX0czz+tOqehrNmh4n\nrfI5JWlJ2itc56vqMnbt2Xon8NiqOozmSjOHGSUt26JzumiuMuv9tfUD2f2aKQtdRzMpcudl/B8G\nTuxXMIndytIaU1XjMnT3DJplQo4CHgjsm+TsqnppT5k/Bz7S72DbL2ltWmobNkhP18XA45Mc3K6b\ncjTNDwbvzr0VqKp54Nrc92PBRwJf3t2BVTU2ty1btoy8DtZnsutkffZ8GydV9fqqOqiqHkvTxn2i\nql66YHHJXwa+tIcYU3Ebt/fJWn8ePpfxvS3Hoj1dVXVPkuNo1gTZuWTEtiTHNrvr9CTradbz2BfY\nkeR44El133oo70vzS+V
"text/plain": [
"<matplotlib.figure.Figure at 0x2a4625bb1d0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"simulate_casino([1/9, 1/9, 1/9, 2/9, 2/9, 2/9],\n",
" [1/9, 1/9, 1/9, 2/9, 2/9, 2/9],\n",
" n=250)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The results are surprisingly good. While the actual optimization process took much longer to finish than in the other examples, we still have a very good guess. As a caveat though: the recovered distribution tends to overfit the data. That is, if the data doesn't fit the underlying distribution well, the model will also fail.\n",
"\n",
"# Conclusion\n",
"\n",
"Given the results above, we can see that we have indeed come up with a very good algorithm to determine the distribution of two dice given their results. As a benefit, we have even seen that results come back very quickly; it's not uncommon for the optimization to converge within a tenth of a second.\n",
"\n",
"Additionally, we have seen that the algorithm can intuit the distribution even when there is not much data. While the final example shows that we can 'overfit' on the dataset, we can still get valuable information from a relatively small dataset.\n",
"\n",
"We can declare at long last: **the mathematicians have again triumphed over the casino**.\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Justification of Simulated Annealing\n",
"\n",
"## Why Simulated Annealing?\n",
"\n",
"So why even use an algorithm with a fancy title like Simulated Annealing? First of all, because the title is sexy. Second of all, because this is a reasonably complicated problem to try and solve. We have a parameter space where each value $p_{ij} \\in (0, 1); i, j \\in \\{1, \\ldots, 6\\}$, for a total of 12 different variables we are trying to optimize over. Additionally, given a 12-dimensional function we are trying to optimize, simulated annealing makes sure that we don't fall into a local minimum.\n",
"\n",
"## Why not something else?\n",
"\n",
"This is a fair question. There are two classes of algorithms that can also be used to solve this problem: [Non-linear optimization](https://en.wikipedia.org/wiki/Nonlinear_programming) methods, and the [EM algorithm](https://en.wikipedia.org/wiki/Expectation%E2%80%93maximization_algorithm).\n",
"\n",
"1. I chose not to use non-linear optimization simply because I'm a bit concerned that it will trap me in a local maximum. Instead of running multiple different optimizations from different starting points, I can just use simulated annealing to take that into account. In addition, throughout the course of testing the simulated annealing code converged *incredibly* quickly - far more quickly than any non-linear solver would be able to accomplish. \n",
"\n",
"2. The EM Algorithm was originally what I intended to write this blog post with. Indeed, the post was inspired by the [crooked casino](http://web.stanford.edu/class/stats366/hmmR2.html) example which uses the EM algorithm to solve it. However, after modeling the likelihood function I realized that the algebra would very quickly get out of hand. Trying to compute all the polynomial terms would not be fun, which would be needed to actually optimize for each parameter. So while the EM algorithm would likely be much faster in raw speed terms, the amount of time needed to program and verify it meant that I was far better off using a different method for optimization."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}