{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The Puzzle of the Misanthropic Neighbors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider [this puzzle](http://fivethirtyeight.com/features/can-you-solve-the-puzzle-of-your-misanthropic-neighbors/) from [The Riddler](http://fivethirtyeight.com/tag/the-riddler/):\n", " \n", "> *The misanthropes are coming. Suppose there is a row of some number, N, of houses in a new, initially empty development. Misanthropes are moving into the development one at a time and selecting a house at random from those that have nobody in them and nobody living next door. They keep on coming until no acceptable houses remain. At most, one out of two houses will be occupied; at least one out of three houses will be. But what’s the expected fraction of occupied houses as the development gets larger, that is, as N goes to infinity?*\n", "\n", "# Consider *N*=4 Houses\n", "\n", "![N houses](http://www.publicdomainpictures.net/pictures/180000/nahled/street-doodle.jpg)\n", "\n", "To make sure we understand the problem, let's try a simple example, with *N*=4 houses. We will represent the originally empty row of four houses by four dots:\n", "\n", " ....\n", " \n", "Now the first person chooses one of the four houses (which are all acceptable). We'll indicate an occupied house by a 1, so the four equiprobable choices are:\n", "\n", " 1...\n", " .1..\n", " ..1.\n", " ...1\n", " \n", "When a house is occupied, any adjacent houses become unacceptable. We'll indicate that with a 0:\n", "\n", " 10..\n", " 010.\n", " .010\n", " ..01\n", " \n", "In all four cases, a second occupant has a place to move in, but then there is no place for a third occupant. \n", "The occupancy is 2 in all cases, and thus the *expected occpancy* is 2. The occupancy fraction, or *density*, is 2/*N* = 2/4 = 1/2.\n", " \n", "\n", "# Consider *N*=7 Houses\n", "\n", "With *N*=7 houses, there are 7 equiprobable choices for the first occupant:\n", "\n", " 10.....\n", " 010....\n", " .010...\n", " ..010..\n", " ...010.\n", " ....010\n", " .....01\n", " \n", "Now we'll add something new: the lengths of the **runs** of consecutive acceptable houses to the left and right of the chosen house:\n", "\n", " 10..... runs = (0, 5)\n", " 010.... runs = (0, 4)\n", " .010... runs = (1, 3)\n", " ..010.. runs = (2, 2)\n", " ...010. runs = (3, 1)\n", " ....010 runs = (4, 0)\n", " .....01 runs = (5, 0)\n", " \n", "# Defining occ(n)\n", "\n", "This gives me a key hint as to how to analyze the problem. I'll define occ(n) to be the expected number of occupied houses in a row of n houses. So:\n", "\n", " 10..... runs = (0, 5); occupancy = occ(0) + 1 + occ(5)\n", " 010.... runs = (0, 4); occupancy = occ(0) + 1 + occ(4)\n", " .010... runs = (1, 3); occupancy = occ(1) + 1 + occ(3)\n", " ..010.. runs = (2, 2); occupancy = occ(2) + 1 + occ(2)\n", " ...010. runs = (3, 1); occupancy = occ(3) + 1 + occ(1)\n", " ....010 runs = (4, 0); occupancy = occ(4) + 1 + occ(0)\n", " .....01 runs = (5, 0); occupancy = occ(5) + 1 + occ(0) \n", " \n", "So we can say that occ(n) is:\n", "\n", "- 0 when n is 0 (because no houses means no occupants),\n", "- 1 when n is 1 (because one isolated acceptable house has one occupant),\n", "- else the mean, over the n choices for the first occupied house,\n", "of the sum of that house plus the occupancy of the runs to the left and right.\n", "\n", "We can implement that:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from statistics import mean\n", "\n", "def occ(n):\n", " \"The expected occupancy for a row of n houses (under misanthrope rules).\"\n", " return (0 if n == 0 else\n", " 1 if n == 1 else\n", " mean(occ(L) + 1 + occ(R)\n", " for (L, R) in runs(n)))\n", "\n", "def runs(n):\n", " \"\"\"A list [(L, R), ...] where the i-th tuple contains the lengths of the runs\n", " of acceptable houses to the left and right of house i.\"\"\"\n", " return [(max(0, i - 1), max(0, n - i - 2))\n", " for i in range(n)]\n", "\n", "def density(n): return occ(n) / n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check that occ(4) is 2, as we computed it should be:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "occ(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And that runs(7) is what we described above:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(0, 5), (0, 4), (1, 3), (2, 2), (3, 1), (4, 0), (5, 0)]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "runs(7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check on n = 7:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.3238095238095235" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "occ(7)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.47482993197278905" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "density(7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That seems reasonable, but I don't know for sure that it is correct. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dynamic Programming Version of occ\n", "\n", "The computation of occ(n) makes multiple calls to occ(n-1), occ(n-2), etc. To avoid re-computing the same calls over and over, we will modify occ to save previous results using [dynamic programming](https://en.wikipedia.org/wiki/Dynamic_programming). I could implement that in one line with the decorator [@functools.lru_cache](https://docs.python.org/3/library/functools.html#functools.lru_cache), but instead I will explicitly manage a list, cache, such that cache[n] holds occ(n):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def occ(n, cache=[0, 1]):\n", " \"The expected occupancy for a row of n houses (under misanthrope rules).\"\n", " # Store occ(i) in cache[i] for all as-yet-uncomputed values of i up to n:\n", " for i in range(len(cache), n+1):\n", " cache.append(mean(cache[L] + 1 + cache[R]\n", " for (L, R) in runs(i)))\n", " return cache[n]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make sure this new version gets the same results as the old version:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "occ(4) == 2" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.47482993197278905" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "density(7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make sure the caching makes computation faster the second time:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 6.23 s, sys: 115 ms, total: 6.34 s\n", "Wall time: 6.97 s\n" ] }, { "data": { "text/plain": [ "864.9617138385324" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time occ(2000)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 5 µs, sys: 0 ns, total: 5 µs\n", "Wall time: 9.06 µs\n" ] }, { "data": { "text/plain": [ "864.9617138385324" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time occ(2000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting density(n)\n", "\n", "To get a feel for the limit of density(n), start by drawing a plot over some small values of n:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "def plot_density(ns):\n", " \"Plot density(n) for each n in the list of numbers ns.\"\n", " plt.xlabel('n houses'); plt.ylabel('density(n)')\n", " plt.plot(ns, [density(n) for n in ns], 's-')\n", " return density(ns[-1])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.4353323288377046" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYkAAAEPCAYAAAC3NDh4AAAABHNC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wa5HMiXdDpdph1A4eEmZk15ZCwnpFP7bI6nzX1cUl3SvqVJpufLem7kn5YrCok\nfTa/+dOjkt6Xrztb0m2Fbf5a0u/kz6/Of9ZKSZ/J171O0jck/SB/vLWwn0fymV0fknRosoNhVlLS\ne1ybdaETgfdHxMWS/hF4L/C1YbZ7fUScJWk22Xw3t0h6L3BKRJws6ShghaTl+fb7TV2QTxlyfkTM\nypcPz1/6AvC5iPiepDcCdwFzgE8Cl0TE/fnU3zvG7VObjZFDwnrNUxHxWP78IeBNTbb7FkBErMkD\nAbKZaL+er39BUh14M/BSk31sBbZL+iJwO/DP+fp3ArMLdwQ7LA+F7wKfl3QjcEtE9NRsx9ad3N1k\nveblwvPdNP9Dqbhds/tXDK7fRTa53qApAPnU1XPJpnl/N3Bn4X1viYjT88cxEbEtIq4hm3jtYOC7\nhVlezTrGIWG9ZsT7+bZ4z/8D3i9pkqQjgbeRzcb7NFll0J/fBOccgLw6eHVE3Al8Ajgl388ysnt1\nk293av7v8RHxRER8hmyK/F66l7t1KXc3Wa8pM+3xsHc1i4hvSjoTeBTYA3wqIl4AkHQT8DjwFPBw\n/r7DgVslTcmX/zD/9zLgOkmPklUg9wGXAP9N0tvJKpwngDtG//HMxpenCjczs6bc3WRmZk05JMzM\nrCmHhJmZNeWQMDOzphwSZmbWlEPCzMyackiYmVlTDgkzM2vq/wNokhP4XD3+tQAAAABJRU5ErkJg\ngg==\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot_density(range(1, 100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is something funny going on with the first few values of n. Let's separately look at the first few:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.46203174603174607" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEPCAYAAABGP2P1AAAABHNCSVQICAgIfAhkiAAAAAlwS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ZHOV5TLJGhmReBgMDOzCg4G6xmSdpW0OJ259CFJP5O05TDD\nD5P0G0m/K996kPTV9GIs90s6MV12mKT/Khvz/ySdmj6+MP2s+yR9JV22vaSr0gsr/VbSB8rWM3hh\nnHvS6TrMRl3hp8Qwa7M9gJMi4kxJPwT+nOrz2rw9Ig6VNINkrpmrJf058J6I2FfS24CFkm5Lx282\nhUA6RcLxEbFX+nxy+tK3gK9HxO2SdgZuAvYGPksyncId6VQUa9v2VZs1wMFgveaxiHgwfXwPsNsw\n464FiIiBNAQgmS32B+nyVZL6gfcBrwyzjpeA1yR9F7gB+Em6/EhgRjoJHCRTR08EfgN8Q9L3gasj\n4okmvj6zlnlXkvWadWWPNzD8P0fl44ab2Xdw+RskVxUbNAEgnRZ6JskU2h8Bflb2vvenl83cP52B\ndk1EzCO5RORWwG/KZlo1G1UOBus1zUzfPvieXwEnpVfKeyvwIeAukhkrZ0jaIr2QzBGwcWbSbdNZ\ndz8DvCddz80k18EmHbdfer97RDwcEV8BFgK5Xkfcepd3JVmvqWc64aFjAiAirpF0MHA/8Cbw+YhY\nBSDpRyTXdHgMWJS+bzJwnaQJ6fO/S+/PBS6WdD/JlsYvgU8C/0fS4SRbMg8DP238yzNrnafdNjOz\nCt6VZGZmFRwMZmZWwcFgZmYVHAxmZlbBwWBmZhUcDGZmVsHBYGZmFRwMZmZW4f8DwIc9pERldEwA\nAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot_density(range(1, 11))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And at a wider range:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.43240754751464183" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAEPCAYAAAAEfBBiAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/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mZWSmcgMzMrBROQGZmVgon\nIDMzK4UTkJmZlcIJyMzMSvH/AUDe4DLY0Sj0AAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot_density(range(100, 4000, 50))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The density is going down, and the curve is almost but not quite flat. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "# lim n → ∞ density(n)\n", "\n", "[Thes puzzle](http://fivethirtyeight.com/features/can-you-solve-the-puzzle-of-your-misanthropic-neighbors/)\n", "is to figure out the limit of density(n) as n goes to infinity. The plot above makes it look like 0.432+something, but we can't answer the question just by plotting; we'll need to switch modes from *computational* thinking to *mathematical* thinking.\n", "\n", "At this point I started playing around with density(n), looking at various values, differences of values, ratios of values, and ratios of differences of values, hoping to achieve some mathematical insight. Mostly I got dead ends.\n", "But eventually I hit on something promising. I looked at the difference between density values (using the function diff), and particularly the difference as you double n:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.0014849853757253895" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def diff(n, m): return density(n) - density(m)\n", "\n", "diff(100, 200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And compared that to the difference when you double n again:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.0007424926878626947" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(200, 400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hmm—I noticed that the first difference is just about twice as much as the second. Let's check:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(100, 200) / diff(200, 400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wow—not only is it *close* to twice as much, it is *exactly* twice as much (to the precision of floating point numbers). Let's try other starting values for n:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 500; diff(n, 2*n) / diff(2*n, 4*n)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.9999999999992524" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 1000; diff(n, 2*n) / diff(2*n, 4*n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OK, I'm convinced this is real! \n", "\n", "Now, what mathematical function behaves like this? I figured out that *f*(*n*) = (1 / *n*) does. The ratio of the differences would be:\n", "\n", "$$\\frac{f(n) - f(2n)}{f(2n) - f(4n)} = \\frac{1/n - 1/(2n)}{1/(2n) - 1 / (4n)}\\;\\;$$\n", "\n", "Multiplying top and bottom by *n* you get:\n", "\n", "\n", "$$\\frac{1 - 1/2}{1/2 - 1 /4} = \\frac{1/2}{1/4} = 2\\;\\;$$\n", "\n", "If the function (1 / *n*) fits the pattern, then so does any *affine transformation* of (1 / *n*), because we are taking the ratio of differences. So that means a density function of the form\n", "\n", " density(n) = A + B / n \n", " \n", "would fit the patterm. I can try a curve_fit to estimate the parameters *A* and *B*:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 7.64810902e-34, -2.42654280e-31],\n", " [ -2.42654280e-31, 4.67778766e-28]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from scipy.optimize import curve_fit\n", "\n", "Ns = list(range(100, 10001, 100))\n", "\n", "def f(n, A, B): return A + B / n\n", "\n", "((A, B), covariance) = curve_fit(f=f, xdata=Ns, ydata=[density(n) for n in Ns])\n", "\n", "covariance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The curve_fit function returns a sequence of parameter values, and a covariance matrix. The fact that all the numbers in the covariance matrix are really small indicates that the parameters are a really good fit. Here are the parameters, A and B:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0.43233235838169343, 0.29699707514520357)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A, B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can plug them into a function that estimates the density:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def estimated_density(n): return A + B / n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we can test how close this function is to the true density function:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.8857805861880479e-16" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(abs(density(n) - estimated_density(n))\n", " for n in range(200, 4000))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That says that, for all values of n from 200 to 4,000, density(n) and estimated_density(n) agree at least through the first 15 decimal places!\n", "\n", "We now have a plausible answer to the puzzle:\n", "\n", "> lim n→∞ density(n) ≅ A = 0.43233235838169343" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Why?\n", "\n", "Theis answer is empirically strong (15 decimal places of accuracy) but theoretically weak: we don't have a **proof**, and we don't have an explanation for **why** the density function has this form. We need some more mathematical thinking. \n", "\n", "I didn't have any ideas, so I looked to see if anyone else had written something about the number 0.43233235838169343. I tried several searches and found two interesting formulas:\n", "\n", "- Search: [[0.4323323]](https://www.google.com/search?q=0.4323323) Formula: sinh(1) / exp(1) [(Page)](http://arxiv.org/pdf/1310.4360.pdf)\n", "- Search: [[0.432332358]](https://www.google.com/search?q=0.432332358) Formula: 0.5(1-e^(-2)) [(Page)](http://www.actuarialoutpost.com/actuarial_discussion_forum/archive/index.php/t-52095.html)\n", "\n", "I can verify that the two formulas are equivalent, and that they are indeed equal to A to at least 15 decimal places:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0.43233235838169365, 0.43233235838169365, 0.43233235838169343)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import sinh, exp, e\n", "\n", "S = sinh(1) / exp(1)\n", "E = 0.5 * (1 - e ** (-2))\n", "\n", "assert S == E\n", "\n", "S, E, A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So I now have a suspicion that\n", "\n", "> lim n → ∞ density(n) = (1 - e-2) / 2\n", "\n", "but I still have no proof, nor any intuition as to why this is so." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# John Lamping to the Rescue\n", "\n", "I reported my results to Anne Paulson and John Lamping, who had originally related the problem to me, and the next day John wrote back with the following:\n", "\n", "
\n", "I got a derivation of the formula!\n", "\n", "

Suppose that each house has a different \"attractiveness\", and that when it is a misanthrope's turn to pick a house, they consider the houses in order, from most attractive to least, and pick the first house not next to any other house. If the attractiveness are chosen independently, this process gives the same probabilities as each misanthrope picking from the available houses randomly.\n", "\n", "

To be more concrete, let the attractivenesses range from 0 to 1 with uniform probability, with lower numbers being being considered earlier, and hence more attractive. (This makes the math come out easier.)\n", "\n", "

Given the attractiveness, a, of a house, we can compute the chance that it will end up getting picked. It will get picked if and only if neither house on either side gets considered earlier and gets picked. Lets consider one side, and assume the houses, and their attractivenesses, are labeled a, b, c, d, e, f, g, ... with the house we are considering being a. There are an infinite number of cases where b won't get considered before a and picked. Here are the first few:\n", "\n", "

a < b (a is considered before b)\n", "

a > b > c < d (Someone considered c first among these 4 and picked it. A later person considered b before a, but rejected it because c had already been chosen.)\n", "

a > b > c > d > e < f (Someone considered e first among these 6, and picked it. A later person considered d, but rejected it because e was chosen, then considered c, which they picked. Still later, someone considered b, which they rejected because c had been chosen.)\n", "\n", "

We can write down the probabilities of these cases as a function of the attractiveness of a:\n", "\n", "

a < b: The chance that b is greater than a: 1 - a.\n", "

a > b > c < d: Let y = a - c, so y is between 0 and a. The probability is the integral over the possible y of the chance that b is between a and c, times the chance that d is greater than c, or integral from 0 to a of y * (1 - a + y) dy.\n", "

a > b > c > d > e < f: Let y = a - e. The probability is the integral of (the chance that b, c, and d are all between a and e, and ordered right, which is y^3 / 3!, times the chance that f is greater than e, which is (1 - a + y))\n", "\n", "

If you work out the definite integrals, you get\n", "\n", "

a < b: 1 - a\n", "

a > b > c < d: a^2 / 2 - a^3 / 3!\n", "

a > b > c > d > e < f: a^4 / 4! - a^5 / 5!\n", "\n", "

Add them all up, and you have 1 - a + a^2 / 2 - a^3 / 3! + a ^4 / 4! ... the Taylor expansion for e^-a. \n", "\n", "

Now, there will be a house at a if both adjacent houses are not picked earlier, so the chance is the square of the chance for one side: e^(-2a). Integrate that from 0 to 1, and you get 1/2 (1 - e^-2).\n", "

\n", "\n", "You can compare John Lamping's solution to the solutions by [Jim Ferry](http://fivethirtyeight.com/features/the-perplexing-puzzle-of-the-proud-partygoers/) and [Andrew Mascioli](http://andrewmascioli.com/index.php/2016/04/24/the-misanthropic-neighborhood/). \n", "\n", "# Styles of Thinking\n", "\n", "It is clear that different write-ups of this problem display different styles of thinking. I'll attempt to name and describe them:\n", "\n", "- **Mathematical Publication Style**: This style uses sophisticated mathematics (e.g. generating functions, differentiation, asymptotic analysis, and manipulation of summations). It defines new abstractions without\n", "necessarily trying to motivate them first, it is terse and formal, and it gets us to the conclusion in a way that is\n", "clearly correct, but does not describe all the steps of how the author came up with the ideas. (Ferry and Mascioli)\n", "- **Mathematical Exploration Style**: Like the Mathematical Publication Style, but with more explanatory prose. (Lamping)\n", "- **Computational Thinking**: A more concrete style; tends to use programs with specific values of n rather than \n", "creating a proof for all values of n; produces tables or plots to help guide intuition; verifies results\n", "with tests or alternative implementations. (Norvig)\n", "\n", "In this specific puzzle, my steps were:\n", "- Analyze the problem and implement code to solve it for small values of n.\n", "- Plot results and examine them for insight.\n", "- Play with the results and get a guess at a partial solution density(n) ≅ A + B / n.\n", "- Solve numerically for A and B.\n", "- Do a search with the numeric value of A to find a formula with that value.\n", "- Given that formula, let Lamping figure out how it corresponds to the problem.\n", "\n", "This is mostly computational thinking, with a little mathematical thrown in. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Validation by Anticipating Objections\n", "\n", "Is our implementation of occ(n) correct? I think it is, but I can anticipate some objections and answer them:\n", "\n", "- In occ(n), is it ok to start from all empty houses, rather than considering layouts of partially-occupied houses? Yes, I think it is ok, because the problem states that initially all houses are empty, and each choice of a house breaks the street up into runs of acceptable houses, flanked by unacceptable houses. If we get the computation right for a run of n acceptable houses, then we can get the whole answer right. A key point is that the chosen first house breaks the row of houses into 2 runs of *acceptable* houses, not 2 runs of *unoccupied* houses. If it were unoccupied houses, then we would have to also keep track of whether there were occupied houses to the right and/or left of the runs. By considering runs of acceptable houses, eveything is clean and simple.\n", "\n", "- In occ(7), if the first house chosen is 2, that breaks the street up into runs of 1 and 3 acceptable houses. There is only one way to occupy the 1 house, but there are several ways to occupy the 3 houses. Shouldn't the average give more weight to the 3 houses, since there are more possibilities there? No, I don't think so. We are caclulating occupancy, and there is a specific number (5/3) which is the expected occupancy of 3 houses; it doesn't matter if there is one combination or a million combinations that contribute to that expected value, all that matters is what the expected value is.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Validation by Simulation\n", "\n", "\n", "A simulation can add credence to our implementation of density(n), for two reasons:\n", "- The code for the simulation can have a more direct corresponance to the problem statement.\n", "- When two very different implementations get the same result, that is evidence supporting both of them.\n", "\n", "\n", "The simulation will start with an empty set of occupied houses. Following Lamping's suggestion, we sample n houses (to get a random ordering) and go through them, occupying just the ones that have no neighbor\" " ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import random\n", "\n", "def simulate(n):\n", " \"Simulate moving in to houses, and return a sorted tuple of occupied houses.\"\n", " occupied = set()\n", " for house in random.sample(range(n), n):\n", " if (house - 1) not in occupied and (house + 1) not in occupied:\n", " occupied.add(house)\n", " return sorted(occupied)\n", "\n", "def simulated_density(n, repeat=10000):\n", " \"Estimate density by simulation, repeated repeat times.\"\n", " return mean(len(simulate(n)) / n \n", " for _ in range(repeat))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see if the simulation returns results that match the actual density function and the estimated_density function:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " n simul density estimated\n", " 25 0.444 0.444 0.444\n", " 50 0.438 0.438 0.438\n", "100 0.435 0.435 0.435\n", "200 0.434 0.434 0.434\n", "400 0.433 0.433 0.433\n" ] } ], "source": [ "print(' n simul density estimated')\n", "for n in (25, 50, 100, 200, 400):\n", " print('{:3} {:.3} {:.3} {:.3}'\n", " .format(n, simulated_density(n), density(n), estimated_density(n)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We got perfect agreement (at least to 3 decimal places), suggesting that either our three implementations are all correct, or we've made mistakes in all three. \n", "\n", "The simulate function can also give us insights when we look at the results it produces:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simulate(7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's repeat that multiple times, and store the results in a Counter, which tracks how many times it has seen each result:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Counter({(0, 2, 4, 6): 3303,\n", " (0, 2, 5): 1255,\n", " (0, 3, 5): 959,\n", " (0, 3, 6): 783,\n", " (1, 3, 5): 1426,\n", " (1, 3, 6): 974,\n", " (1, 4, 6): 1300})" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from collections import Counter\n", "\n", "Counter(tuple(simulate(7)) for _ in range(10000))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That says that about 1/3 of the time, things work out so that the 4 even-numbered houses are occupied. But if anybody ever chooses an odd-numbered house, then we are destined to have 3 houses occupied (in one of 6 different ways, of which (1, 3 5) is the most common, probably because it is the only one that has three chances of getting started with an odd-numbered house)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Verification by Test\n", "\n", "Another way to gain more confidence in the code is to run a test suite:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'ok'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def test():\n", " assert occ(0) == 0\n", " assert occ(1) == occ(2) == 1\n", " assert occ(3) == 5/3\n", " assert density(3) == occ(3) / 3\n", " assert density(100) == occ(100) / 100\n", " assert runs(3) == [(0, 1), (0, 0), (1, 0)]\n", " assert runs(7) == [(0, 5), (0, 4), (1, 3), (2, 2), (3, 1), (4, 0), (5, 0)]\n", " for n in (3, 7, 10, 20, 100, 101, 200, 201):\n", " for repeat in range(500):\n", " assert_valid(simulate(n), n) \n", " return 'ok'\n", "\n", "def assert_valid(occupied, n):\n", " \"\"\"Assert that, in this collection of occupied houses, no house is adjacent to an\n", " occupied house, and every unoccupied position is adjacent to an occupied house.\"\"\"\n", " occupied = set(occupied) # coerce to set\n", " for house in range(n):\n", " if house in occupied:\n", " assert (house - 1) not in occupied and (house + 1) not in occupied\n", " else:\n", " assert (house - 1) in occupied or (house + 1) in occupied\n", "\n", "test()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conclusion\n", "\n", "I'm happy with my result:\n", "\n", "> lim n → ∞ density(n) = (1 - e-2) / 2 ≅ 0.43233235838169365 \n", "\n", "I got the fun of working on the puzzle, the satisfaction of seeing John Lamping work out a proof, and the awe of seeing the mathematically sophisticated solutions of Jim Ferry and Andrew Mascioli, solutions that I know I could never come up with, but that I could get near to by coming at the problem with a different style of thinking." ] } ], "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 }