speice.io/2016/05/the-unfair-casino/index.html

180 lines
236 KiB
HTML
Raw Normal View History

<!doctype html><html lang=en dir=ltr class="blog-wrapper blog-post-page plugin-blog plugin-id-default" data-has-hydrated=false><meta charset=UTF-8><meta name=generator content="Docusaurus v3.6.0"><title data-rh=true>The unfair casino | The Old Speice Guy</title><meta data-rh=true name=viewport content="width=device-width,initial-scale=1.0"><meta data-rh=true name=twitter:card content=summary_large_image><meta data-rh=true property=og:url content=https://speice.io/2016/05/the-unfair-casino><meta data-rh=true property=og:locale content=en><meta data-rh=true name=docusaurus_locale content=en><meta data-rh=true name=docusaurus_tag content=default><meta data-rh=true name=docsearch:language content=en><meta data-rh=true name=docsearch:docusaurus_tag content=default><meta data-rh=true property=og:title content="The unfair casino | The Old Speice Guy"><meta data-rh=true name=description content="Trying to figure out how exactly two dice are loaded in a cheating casino."><meta data-rh=true property=og:description content="Trying to figure out how exactly two dice are loaded in a cheating casino."><meta data-rh=true property=og:type content=article><meta data-rh=true property=article:published_time content=2016-05-15T12:00:00.000Z><link data-rh=true rel=icon href=/img/favicon.ico><link data-rh=true rel=canonical href=https://speice.io/2016/05/the-unfair-casino><link data-rh=true rel=alternate href=https://speice.io/2016/05/the-unfair-casino hreflang=en><link data-rh=true rel=alternate href=https://speice.io/2016/05/the-unfair-casino hreflang=x-default><script data-rh=true type=application/ld+json>{"@context":"https://schema.org","@id":"https://speice.io/2016/05/the-unfair-casino","@type":"BlogPosting","author":{"@type":"Person","name":"Bradlee Speice"},"dateModified":"2024-11-04T00:16:00.000Z","datePublished":"2016-05-15T12:00:00.000Z","description":"Trying to figure out how exactly two dice are loaded in a cheating casino.","headline":"The unfair casino","isPartOf":{"@id":"https://speice.io/","@type":"Blog","name":"Blog"},"keywords":[],"mainEntityOfPage":"https://speice.io/2016/05/the-unfair-casino","name":"The unfair casino","url":"https://speice.io/2016/05/the-unfair-casino"}</script><link rel=alternate type=application/rss+xml href=/rss.xml title="The Old Speice Guy RSS Feed"><link rel=alternate type=application/atom+xml href=/atom.xml title="The Old Speice Guy Atom Feed"><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/katex@0.13.24/dist/katex.min.css integrity=sha384-odtC+0UGzzFL/6PNoE8rX/SPcQDXBJ+uRepguP4QkPCm2LBxH3FA3y+fKSiJ+AmM crossorigin><link rel=stylesheet href=/assets/css/styles.ae6ff4a3.css><script src=/assets/js/runtime~main.751b419d.js defer></script><script src=/assets/js/main.62ce6156.js defer></script><body class=navigation-with-keyboard><script>!function(){var t,e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return window.localStorage.getItem("theme")}catch(t){}}();t=null!==e?e:"light",document.documentElement.setAttribute("data-theme",t)}(),function(){try{for(var[t,e]of new URLSearchParams(window.location.search).entries())if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id=__docusaurus><div role=region aria-label="Skip to main content"><a class=skipToContent_fXgn href=#__docusaurus_skipToContent_fallback>Skip to main content</a></div><nav aria-label=Main class="navbar navbar--fixed-top"><div class=navbar__inner><div class=navbar__items><button aria-label="Toggle navigation bar" aria-expanded=false class="navbar__toggle clean-btn" type=button><svg width=30 height=30 viewBox="0 0 30 30" aria-hidden=true><path stroke=currentColor stroke-linecap=round stroke-miterlimit=10 stroke-width=2 d="M4 7h22M4 15h22M4 23h22"/></svg></button><a class=navbar__brand href=/><div class=navbar__logo><img src=/img/logo.svg alt="Sierpinski Gasket" class="themedComponent_mlkZ themedComponent--light_NVdE"><img src=/img/logo-dark.svg alt="Sierpinski Ga
<p>In the ongoing eternal cycle of mathematicians asking generally useless questions about probability, I dreamt up another one. The scenario is as follows:</p>
<p><strong>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?</strong></p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=proving-we-can-detect-cheating>Proving we can detect cheating<a href=#proving-we-can-detect-cheating class=hash-link aria-label="Direct link to Proving we can detect cheating" title="Direct link to Proving we can detect cheating"></a></h2>
<p>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.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> numpy </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">as</span><span class="token plain"> np</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> pandas </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">as</span><span class="token plain"> pd</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> matplotlib</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">pyplot </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">as</span><span class="token plain"> plt</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token operator" style="color:hsl(221, 87%, 60%)">%</span><span class="token plain">matplotlib inline</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">fair_1 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">random</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">randint</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">7</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">10000</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">fair_2 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">random</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">randint</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">7</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">10000</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">pd</span><span class="token punctuation" style="color:hsl(119, 3
<p><img decoding=async loading=lazy alt=png src=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEKCAYAAAA8QgPpAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAHVRJREFUeJzt3X20XXV95/H3hyBPEhB1yK2JkCANBgtq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FQ/A7i+C3lHTCL7IqmWx8DA7LrffExSHR3CknQ10ABeIulBYKntZS2bmGeLyzpJK4B1wDZgcUtX4lzgq8B+wI22V3Uy74jJ5ynq6fnA0NC4fpRGH+n4EFa3ZQgrOqnOIay6CkiGz/pDlSGsrk6iR0yEgYHZDA1t3P2GEdFR6YHElFNfLwDq6wmkBxKdNVkn0SMiogelgERERCUpIBERUUkKSEREVJICEhERlaSAREREJSkgERFRSQpIRERUkgISERGVpIBEREQlKSAREVFJCkhERFSSAhIREZWkgERERCUdLSCSLpc0JGltS9unJd0v6V5JX5d0UMtzSyStL58/saV9vqS1kn4i6bOdzDkiItrT6R7IMuCkUW03A6+y/RpgPbAEQNLRwGnAPOAU4NLyHugAXwDOtj0XmCtp9D4jIqLLOlpAbN8OPD6qbbXt7eXqncCscnkhcI3tYdsbKIrLAkkDwHTbd5XbXQEs6mTeERGxe3XPgZwF3FguzwQ2tTy3pWybCWxuad9ctkVERI1quye6pE8A22x/baL3PTg4uGO50WjQaDQmOkRExJTWbDZpNpt7tI+O3xNd0uHADbaPbWk7E/gw8A7bT5VtFwC2f
<p>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 <a href=https://en.wikipedia.org/wiki/Triangular_distribution target=_blank rel="noopener noreferrer">Triangle Distribution</a>, which is itself a special case of the <a href=https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution target=_blank rel="noopener noreferrer">Irwin-Hall Distribution</a>.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=the-edge-cases>The Edge Cases<a href=#the-edge-cases class=hash-link aria-label="Direct link to The Edge Cases" title="Direct link to The Edge Cases"></a></h2>
<p>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.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=edge-case-1>Edge Case 1<a href=#edge-case-1 class=hash-link aria-label="Direct link to Edge Case 1" title="Direct link to Edge Case 1"></a></h3>
<p>What happens when one die is biased low, and one die is biased high? That is, where:</p>
<span class=katex-display><span class=katex><span class=katex-mathml><math display=block><semantics><mtable rowspacing=0.25em columnalign=right columnspacing=""><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mtable rowspacing=0.16em columnalign="center center" columnspacing=1em><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><msub><mi>D</mi><mn>1</mn></msub><mo>=</mo><mrow><mo fence=true>{</mo><mtable rowspacing=0.16em columnalign="left right" columnspacing=1em><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>1</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>3</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>2</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>3</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>3</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>4</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>5</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>6</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr></mtable></mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><msub><mi>D</mi><mn>2</mn></msub><mo>=</mo><mrow><mo fence=true>{</mo><mtable rowspacing=0.16em columnalign="left right" columnspacing=1em><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>1</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>2</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>3</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>4</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>5</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>3</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>6</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>3</mn></mrow></ms
\begin{array}{cc}
D_1 = \left\{
\begin{array}{lr}
1 & w.p. 1/3\\
2 & w.p. 1/3\\
3 & w.p. 1/12\\
4 & w.p. 1/12\\
5 & w.p. 1/12\\
6 & w.p. 1/12
\end{array}
\right. &
D_2 = \left\{
\begin{array}{lr}
1 & w.p. 1/12\\
2 & w.p. 1/12\\
3 & w.p. 1/12\\
4 & w.p. 1/12\\
5 & w.p. 1/3\\
6 & w.p. 1/3
\end{array}
\right. \\
\mathbb{E}[D_1] = 2.5 & \mathbb{E}[D_2] = 4.5
\end{array}\\
\mathbb{E}[D_1 + D_2] = 7 = \mathbb{E}[D_{fair} + D_{fair}]
\end{align*}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:10.2em;vertical-align:-4.85em></span><span class=mord><span class=mtable><span class=col-align-r><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:5.35em><span style=top:-7.35em><span class=pstrut style=height:6.45em></span><span class=mord><span class=mord><span class=mtable><span class=arraycolsep style=width:0.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:4.45em><span style=top:-6.45em><span class=pstrut style=height:5.85em></span><span class=mord><span class=mord><span class="mord mathnormal" style=margin-right:0.02778em>D</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3011em><span style=top:-2.55em;margin-left:-0.0278em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">1</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:0.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:0.2778em></span><span class=minner><span class=mopen><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.85em><span style=top:-1.366em><span class=pstrut style=height:3.816em></span><span class="delimsizinginner delim-size4"><span></span></span></span><span style=top:-1.358em><span class=pstrut style=height:3.816em></span><span style=height:1.816em;width:0.8889em><svg width=0.8889em height=1.816em style=width:0.8889em viewBox="0 0 888.89 1816" preserveAspectRatio=xMinYMin><path d="M384 0 H504 V1816 H384z M384 0 H504 V1816 H384z"/></svg></span></span><span style=top:-3.816em><span class=pstrut style=height:3.816em></span><span class="delimsizinginner delim-size4"><span></span></span></span><span style=top:-4.958em><span class=pstrut style=height:3.816em></span><span style=height:1.816em;width:0.8889em><svg width=0.8889em height=1.816em style=width:0.8889em viewBox="0 0 888.89 1816" preserveAspectRatio=xMinYMin><path d="M384 0 H504 V1816 H384z M384 0 H504 V1816 H384z"/></svg></span></span><span style=top:-6.766em><span class=pstrut style=height:3.816em></span><span class="delimsizinginner delim-size4"><span></span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:3.35em><span></span></span></span></span></span></span><span class=mord><span class=mtable><span class=arraycolsep style=width:0.5em></span><span class=col-align-l><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.85em><span style=top:-6.01em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>1</span></span></span><span style=top:-4.81em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>2</span></span></span><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>3</span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>4</span></span></span><span style=top:-1.21em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>5</span></span></span><span style=top:-0.01em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>6</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:3.35em><span></span></span></span></span></span><span class=arraycolsep style=width:0.5em></span><span class=arraycolsep style=width:0.5em></span><span class=col-align-r><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.85em><span style=top:-6.01em><span class=pstrut style=height:3em></span><span cl
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token keyword" style="color:hsl(301, 63%, 40%)">def</span><span class="token plain"> </span><span class="token function" style="color:hsl(221, 87%, 60%)">unfair_die</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">p_vals</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> n</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> x </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">random</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">multinomial</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> p_vals</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> n</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">return</span><span class="token plain"> x</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">nonzero</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token plain"> </span><span class="token operator" style="color:hsl(221, 87%, 60%)">+</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">d1 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation
<p><img decoding=async loading=lazy alt=png src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEMCAYAAADqG+D0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAG2RJREFUeJzt3X20XXV95/H3ByJP8lB8gKsJTz6EBlrEIPGBihetgG150I4RdRURdLkUBWtnRsJ0hminU+laiFgLqz5hYMAIKAothZAFV8ZpNRRJQZKBVA0QMBcfEEVYmJDP/LF/lxxubpJzd7LPvvecz2uts7LP7+y9f9+zc8/5nt/D3lu2iYiImKwd2g4gIiKmpySQiIioJQkkIiJqSQKJiIhakkAiIqKWJJCIiKglCSQiImpJAomIiFqSQGK7k/RjSW/cwus/kHR0D+O5VNInt/D6FuOdqvrpOE8m1un6/9WPZrQdQAwe27/XdgyDYDod5+kUa2yUBBLPIul1wF8BTwHXALsAvw/cZfuSNmPrJznO0Q/ShRXPYvtfgPXAZ2x/2fbFtj8IvEPS8CR2NU/SPZJ+LulLknYae6GzC0LSxyX9h6RflW6Mkzt3Ul5fU15fKemYUv4iSddIekTSDyV9pGObV0q6Q9JjkhZTfTl3RdIcSbdKelTS3ZJOKOWnSbquY71Vkr7W8fwBSYd1W88AH+dXSvr3cnwXj8U7vltK0lxJ3y/7vqqs+8nN7Oerne87eicJJJ5F0nOAVwPfGffSd4GTN91is94FvBl4KXAw8JebWe8/gKNs7wl8AvjfkvYtscwGzgSOKK8fB6yWJOB64E7gRcCbgLMlvbnEfy2wCHgecDXwp90ELGkGcB1wI/BC4CzgCkkvB74N/EFZ70XAc4DXlucvAZ5r+67uDs1AH+e3A8cCBwGHAaeNX6Hs+xvAl8u+vwq8dQv7ecVE+4nmJYHEeK8G7rH9xLjyFwFPT2I/f2f7Ydu/BP6a6otuE7a/bnu0L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YfB1YCM8vLEwV6ErDY9nrbq4FVwLySiPawfXtZ7zLg5CZjj4iILevZGIikA4HDge+Vog9LWi7pi5L2KmUzgQc7NnuolM0E1nSUr2FjIoqIiBb0JIGU7qtrgLNLS+Ri4CW2DwfWAhf0Io6IiNh+ZjRdgaQZVMnjctvfArD9045VvgBcX5YfAvbreG1WKdtc+YQWLlz4zPLw8DDDw8O144+I6EcjIyOMjIxs0z4aHUQHkHQZ8DPbH+soG7K9tiz/OXCk7XdJOgS4Ang1VRfVzWwcRP8ucBZwO/BPwGdt3zhBfRlEj8ZkED36VZ1B9EZbIJKOAt4N3C3pTqpP3rnAuyQdDmwAVgMfALC9QtJVwApgHfChjmxwJvAVYBfghomSR0RE9E7jLZBeSwskmpQWSPSrKTeNNyIi+lcSSERE1JIEEhERtSSBRERELUkgERFRSxJIRETUkgQSERG1JIFEREQtSSAREVFLEkhERNSSBBIREbUkgURERC1JIBERUUsSSERE1JIEEhERtSSBRERELUkgERFRSxJIRETUkgQSERG1JIFEREQtSSAREVFLEkhERNSSBBIREbUkgURERC1JIBERUUsSSERE1JIEEhERtSSBRERELUkgERFRS6MJRNIsSbdIukfS3ZLOKuV7S1oi6V5JN0naq2ObBZJWSVop6diO8rmS7pJ0n6TPNBl3RERsXdMtkPXAx2wfCrwWOFPS7wLnAEttHwzcAiwAkHQIMB+YA7wFuFiSyr4uAc6wPRuYLem4hmOPiIgtaDSB2F5re3lZfhxYCcwCTgIWldUWASeX5ROBxbbX214NrALmSRoC9rB9e1nvso5tIiKiBT0bA5F0IHA48F1gX9ujUCUZYJ+y2kzgwY7NHiplM4E1HeVrSllERLRk
<p>We can see that while the 7 value remains the most likely (as expected), the distribution is not so nicely shaped any more.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=edge-case-2>Edge Case 2<a href=#edge-case-2 class=hash-link aria-label="Direct link to Edge Case 2" title="Direct link to Edge Case 2"></a></h3>
<p>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:</p>
<span class=katex-display><span class=katex><span class=katex-mathml><math display=block><semantics><mtable rowspacing=0.25em columnalign=right columnspacing=""><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mtable rowspacing=0.16em columnalign="center center" columnspacing=1em><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><msub><mi>D</mi><mn>1</mn></msub><mo>=</mo><mrow><mo fence=true>{</mo><mtable rowspacing=0.16em columnalign="left right" columnspacing=1em><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>1</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>3</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>2</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>3</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>4</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>5</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>6</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>3</mn></mrow></mstyle></mtd></mtr></mtable></mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><msub><mi>D</mi><mn>2</mn></msub><mo>=</mo><mrow><mo fence=true>{</mo><mtable rowspacing=0.16em columnalign="left right" columnspacing=1em><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>1</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>3</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>2</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>3</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>4</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>5</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>12</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mn>6</mn></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>w</mi><mi mathvariant=normal>.</mi><mi>p</mi><mi mathvariant=normal>.</mi><mn>1</mn><mi mathvariant=normal>/</mi><mn>3</mn></mrow></ms
\begin{array}{cc}
D_1 = \left\{
\begin{array}{lr}
1 & w.p. 1/3\\
2 & w.p. 1/12\\
3 & w.p. 1/12\\
4 & w.p. 1/12\\
5 & w.p. 1/12\\
6 & w.p. 1/3
\end{array}
\right. &
D_2 = \left\{
\begin{array}{lr}
1 & w.p. 1/3\\
2 & w.p. 1/12\\
3 & w.p. 1/12\\
4 & w.p. 1/12\\
5 & w.p. 1/12\\
6 & w.p. 1/3
\end{array}
\right. \\
\mathbb{E}[D_1] = 3.5 & \mathbb{E}[D_2] = 3.5
\end{array}\\
\mathbb{E}[D_1 + D_2] = 7 = \mathbb{E}[D_{fair} + D_{fair}]
\end{align*}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:10.2em;vertical-align:-4.85em></span><span class=mord><span class=mtable><span class=col-align-r><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:5.35em><span style=top:-7.35em><span class=pstrut style=height:6.45em></span><span class=mord><span class=mord><span class=mtable><span class=arraycolsep style=width:0.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:4.45em><span style=top:-6.45em><span class=pstrut style=height:5.85em></span><span class=mord><span class=mord><span class="mord mathnormal" style=margin-right:0.02778em>D</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3011em><span style=top:-2.55em;margin-left:-0.0278em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">1</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:0.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:0.2778em></span><span class=minner><span class=mopen><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.85em><span style=top:-1.366em><span class=pstrut style=height:3.816em></span><span class="delimsizinginner delim-size4"><span></span></span></span><span style=top:-1.358em><span class=pstrut style=height:3.816em></span><span style=height:1.816em;width:0.8889em><svg width=0.8889em height=1.816em style=width:0.8889em viewBox="0 0 888.89 1816" preserveAspectRatio=xMinYMin><path d="M384 0 H504 V1816 H384z M384 0 H504 V1816 H384z"/></svg></span></span><span style=top:-3.816em><span class=pstrut style=height:3.816em></span><span class="delimsizinginner delim-size4"><span></span></span></span><span style=top:-4.958em><span class=pstrut style=height:3.816em></span><span style=height:1.816em;width:0.8889em><svg width=0.8889em height=1.816em style=width:0.8889em viewBox="0 0 888.89 1816" preserveAspectRatio=xMinYMin><path d="M384 0 H504 V1816 H384z M384 0 H504 V1816 H384z"/></svg></span></span><span style=top:-6.766em><span class=pstrut style=height:3.816em></span><span class="delimsizinginner delim-size4"><span></span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:3.35em><span></span></span></span></span></span></span><span class=mord><span class=mtable><span class=arraycolsep style=width:0.5em></span><span class=col-align-l><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.85em><span style=top:-6.01em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>1</span></span></span><span style=top:-4.81em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>2</span></span></span><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>3</span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>4</span></span></span><span style=top:-1.21em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>5</span></span></span><span style=top:-0.01em><span class=pstrut style=height:3em></span><span class=mord><span class=mord>6</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:3.35em><span></span></span></span></span></span><span class=arraycolsep style=width:0.5em></span><span class=arraycolsep style=width:0.5em></span><span class=col-align-r><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.85em><span style=top:-6.01em><span class=pstrut style=height:3em></span><span cl
<p>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:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">d1 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">d2 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> d1</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">x1 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> unfair_die</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">10000</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">x2 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> unfair_die</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d2</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">10000</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="c
<p><img decoding=async loading=lazy alt=png src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEMCAYAAADqG+D0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAGuhJREFUeJzt3X2UXXV97/H3ByIC8lAskmCigNUg4ANGDSrLdkB50CqgvSLiKuLTcikCVXsvhNpL7JPStRTQXlxVEQMXmgv4BJZCYOHUZXslEUGQ5EJaDZBABhVEKQgJ+dw/9m/gOJmHM3uyz54583mtdVb2+Z398N1nJvM5v9/eZ2/ZJiIiYrK2a7uAiIiYmRIgERFRSwIkIiJqSYBEREQtCZCIiKglARIREbUkQCIiopYESMw4ki6S9Fc92M7PJB0+zus/kfSHTdfRsb2e7Hev9et+zQYJkIiabL/E9vfarmPYRIHXxfKnSFol6beSvrota5sKSSdIWi3pEUlrJR3adk1RmdN2ATH9SXod8NfA48CVwI7AS4HbbH+xzdrGMhNrngY2UL1nRwE7tVwLAJKOAD4NHG97laS9264pnpYeSEzI9r8Dm4HzbH/V9gW2Pwy8U9JAN+uQdIak/5D06zL0c9yI138m6ROSfizpIUn/JGmH8torJN0s6WFJy6nCoPGai8WS7pD0S0kXDtfUUfPhXe7fGZLWl9fXSDqstO8t6UpJD0j6T0mndizT9X5Luhh4PnB12cafS3qxpO+W9/N2SW+d4D37lu2rgAcnelPG29/xfpaT3S9gKfBXtleVGu+3ff9E9UVvJEBiQpKeARwCfH/ESz8Ajtt6iVH9B3Co7d2ATwH/W9LcEfO8AzgS2A94OXBy2fY3gWXAs4ErgD/pUc0AJwJHAH8A7A98coz5xtw/SQuBU4BXltePAtZJEnA1cAuwN/AG4HRJR0x2v22fBNwDvKVs47yy7muB5wCnAZdKetEk9n08E/08t/pZwlM/l672S9J2wKuAvcrQ1T2SviDpmdtoH2KKEiDRjUOAO2w/OqJ9b+DJblZg++u2h8r0FcBa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NBFNEz8+bti6SeP+bN27ftXY/YSk8CpAxfXQmcXnoiFwAvsH0wsBH4bC/qiJiqoaG7Aff8UW03YnqZ0/QGJM2hCo9LbH8bwPbPO2b5MnB1md4APK/jtQWlbaz2US1duvSp6YGBAQYGBmrXHxHRjwYHBxkcHJzSOho9iA4g6WLgF7Y/3tE2z/bGMv0x4NW2T5R0IHApcAjVENX1PH0Q/QfAacAq4J+Bz9u+dpTt5SB6NEYSVa+g51smv9fRpDoH0RvtgUg6FHg3cLukW6j+550FnCjpYGALsA74EIDt1ZIuB1YDm4CPdKTBKcDXgB2Ba0YLj4iI6J3GeyC9lh5INCk9kOhX0+403oiI6F8JkIiIqCUBEhERtSRAIiKilgRIRETUkgCJiIhaEiAREVFLAiQiImpJgERERC0JkIiIqCUBEhERtSRAIiKilgRIRETUkgCJiIhaEiAREVFLAiQiImpJgERERC0JkIiIqCUBEhERtSRAIiKilgRIRETUkgCJiIhaEiAREVFLAiQiImpJgERERC0JkIiIqCUBEhERtSRAIiKilgRIRETU0miASFog6UZJd0i6XdJppX0PSSsk3SnpOkm7dyyzRNJaSWskHdnRvkjSbZLuknRek3VHRMTEmu6BbAY+bvsg4LXAKZJeDJwJ3GB7f+BGYAmApAOB44EDgDcBF0hSWdcXgffbXggslHRUw7VHRMQ4Gg0Q2xtt31qmHwHWAAuAY4FlZbZlwHFl+hhgue3NttcBa4HFkuYBu9peVea7uGOZiIhoQc+OgUjaFzgY+AEw1/YQVCED7FVmmw/c27HYhtI2H1jf0b6+tEVEREvm9GIjknYB
<p>In a very un-subtle way, we have of course made the values 2 and 12 far more likely.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=detection-conclusion>Detection Conclusion<a href=#detection-conclusion class=hash-link aria-label="Direct link to Detection Conclusion" title="Direct link to Detection Conclusion"></a></h2>
<p>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.</p>
<p>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.</p>
<p>We can thus finally state: <strong>just by looking at the distribution of results from this game, we can immediately conclude whether there is cheating.</strong></p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=simulated-annealing>Simulated Annealing<a href=#simulated-annealing class=hash-link aria-label="Direct link to Simulated Annealing" title="Direct link to Simulated Annealing"></a></h2>
<p>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 <a href=#Justification-of-Simulated-Annealing>justification</a>. If you don't care about how I set up the model and just want to see the code, check out <a href=#The-actual-code>the actual code</a>.</p>
<p><a href=https://en.wikipedia.org/wiki/Simulated_annealing target=_blank rel="noopener noreferrer">Simulated Annealing</a> is a variation of the <a href=https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm target=_blank rel="noopener noreferrer">Metropolis-Hastings Algorithm</a>, 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 <strong>likelihood</strong> function.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=deriving-the-likelihood-function>Deriving the Likelihood function<a href=#deriving-the-likelihood-function class=hash-link aria-label="Direct link to Deriving the Likelihood function" title="Direct link to Deriving the Likelihood function"></a></h3>
<p>To derive our likelihood function, we want to know: <strong>what is the probability of seeing a specific result given those hidden parameters?</strong> 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.</p>
<p>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 <a href=https://en.wikipedia.org/wiki/Categorical_distribution target=_blank rel="noopener noreferrer">Categorical distribution</a>. That is:</p>
<span class=katex-display><span class=katex><span class=katex-mathml><math display=block><semantics><mtable rowspacing=0.25em columnalign=right columnspacing=""><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mrow><mi mathvariant=script>L</mi><mo stretchy=false>(</mo><mi mathvariant=script>x</mi><mo stretchy=false>)</mo></mrow><mo>=</mo><mrow><mo fence=true>{</mo><mtable rowspacing=0.16em columnalign="left right" columnspacing=1em><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><msub><mi>p</mi><mn>2</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>x</mi><mo>=</mo><mn>2</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><msub><mi>p</mi><mn>3</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>x</mi><mo>=</mo><mn>3</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><mo lspace=0em rspace=0em></mo></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow/></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><msub><mi>p</mi><mn>11</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>x</mi><mo>=</mo><mn>11</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=0 displaystyle=false><msub><mi>p</mi><mn>12</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=false><mrow><mi>x</mi><mo>=</mo><mn>12</mn></mrow></mstyle></mtd></mtr></mtable></mrow></mrow></mstyle></mtd></mtr></mtable><annotation encoding=application/x-tex>\begin{align*}
\mathcal{L(x)} = \left\{
\begin{array}{lr}
p_2 & x = 2\\
p_3 & x = 3\\
\ldots & \\
p_{11} & x = 11\\
p_{12} & x = 12
\end{array}
\right.
\end{align*}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:6.3em;vertical-align:-2.9em></span><span class=mord><span class=mtable><span class=col-align-r><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.4em><span style=top:-5.4em><span class=pstrut style=height:5.25em></span><span class=mord><span class=mord><span class="mord mathcal">L</span><span class=mopen>(</span><span class="mord mathnormal">x</span><span class=mclose>)</span></span><span class=mspace style=margin-right:0.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:0.2778em></span><span class=minner><span class=mopen><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.25em><span style=top:-1.366em><span class=pstrut style=height:3.216em></span><span class="delimsizinginner delim-size4"><span></span></span></span><span style=top:-1.358em><span class=pstrut style=height:3.216em></span><span style=height:1.216em;width:0.8889em><svg width=0.8889em height=1.216em style=width:0.8889em viewBox="0 0 888.89 1216" preserveAspectRatio=xMinYMin><path d="M384 0 H504 V1216 H384z M384 0 H504 V1216 H384z"/></svg></span></span><span style=top:-3.216em><span class=pstrut style=height:3.216em></span><span class="delimsizinginner delim-size4"><span></span></span></span><span style=top:-4.358em><span class=pstrut style=height:3.216em></span><span style=height:1.216em;width:0.8889em><svg width=0.8889em height=1.216em style=width:0.8889em viewBox="0 0 888.89 1216" preserveAspectRatio=xMinYMin><path d="M384 0 H504 V1216 H384z M384 0 H504 V1216 H384z"/></svg></span></span><span style=top:-5.566em><span class=pstrut style=height:3.216em></span><span class="delimsizinginner delim-size4"><span></span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:2.75em><span></span></span></span></span></span></span><span class=mord><span class=mtable><span class=arraycolsep style=width:0.5em></span><span class=col-align-l><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:3.25em><span style=top:-5.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">p</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3011em><span style=top:-2.55em;margin-left:0em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.15em><span></span></span></span></span></span></span></span></span><span style=top:-4.21em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">p</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3011em><span style=top:-2.55em;margin-left:0em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">3</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.15em><span></span></span></span></span></span></span></span></span><span style=top:-3.01em><span class=pstrut style=height:3em></span><span class=mord><span class=minner></span></span></span><span style=top:-1.81em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">p</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3011em><span style=top:-2.55em;margin-left:0em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=v
<p>Where each <span class=katex><span class=katex-mathml><math><semantics><mrow><msub><mi>p</mi><mi>i</mi></msub></mrow><annotation encoding=application/x-tex>p_i</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:0.625em;vertical-align:-0.1944em></span><span class=mord><span class="mord mathnormal">p</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3117em><span style=top:-2.55em;margin-left:0em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.15em><span></span></span></span></span></span></span></span></span></span> is the probability of seeing that specific result. However, we need to calculate what each probability is! I'll save you the details, but <a href=http://math.stackexchange.com/a/1646360/320784 target=_blank rel="noopener noreferrer">this author</a> explains how to do it.</p>
<p>Now, we would like to know the likelihood of our entire data-set. This is trivial:</p>
<span class=katex-display><span class=katex><span class=katex-mathml><math display=block><semantics><mtable rowspacing=0.25em columnalign="right left" columnspacing=0em><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mi mathvariant=script>L</mi><mo stretchy=false>(</mo><mi mathvariant=bold>X</mi><mo stretchy=false>)</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mrow/><mo>=</mo><munderover><mo></mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><mi>L</mi><mo stretchy=false>(</mo><mi>x</mi><mo stretchy=false>)</mo></mrow></mstyle></mtd></mtr></mtable><annotation encoding=application/x-tex>\begin{align*}
\mathcal{L(\mathbf{X})} &= \prod_{i=1}^n L(x)
\end{align*}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:3.2291em;vertical-align:-1.3645em></span><span class=mord><span class=mtable><span class=col-align-r><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.8645em><span style=top:-3.8645em><span class=pstrut style=height:3.6514em></span><span class=mord><span class=mord><span class="mord mathcal">L</span><span class=mopen>(</span><span class="mord mathbf">X</span><span class=mclose>)</span></span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:1.3645em><span></span></span></span></span></span><span class=col-align-l><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.8645em><span style=top:-3.8645em><span class=pstrut style=height:3.6514em></span><span class=mord><span class=mord></span><span class=mspace style=margin-right:0.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:0.2778em></span><span class="mop op-limits"><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.6514em><span style=top:-1.8723em;margin-left:0em><span class=pstrut style=height:3.05em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span><span class="mrel mtight">=</span><span class="mord mtight">1</span></span></span></span><span style=top:-3.05em><span class=pstrut style=height:3.05em></span><span><span class="mop op-symbol large-op"></span></span></span><span style=top:-4.3em;margin-left:0em><span class=pstrut style=height:3.05em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:1.2777em><span></span></span></span></span></span><span class=mspace style=margin-right:0.1667em></span><span class="mord mathnormal">L</span><span class=mopen>(</span><span class="mord mathnormal">x</span><span class=mclose>)</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:1.3645em><span></span></span></span></span></span></span></span></span></span></span></span>
<p>However, it's typically much easier to work with the <span class=katex><span class=katex-mathml><math><semantics><mrow><mi>log</mi><mo></mo><mo stretchy=false>(</mo><mi mathvariant=script>L</mi><mo stretchy=false>)</mo></mrow><annotation encoding=application/x-tex>\log(\mathcal{L})</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:1em;vertical-align:-0.25em></span><span class=mop>lo<span style=margin-right:0.01389em>g</span></span><span class=mopen>(</span><span class="mord mathcal">L</span><span class=mclose>)</span></span></span></span> function instead. This is critically important from a computational perspective: when you multiply so many small numbers together (i.e. the product of <span class=katex><span class=katex-mathml><math><semantics><mrow><mi>L</mi><mo stretchy=false>(</mo><mi>x</mi><mo stretchy=false>)</mo></mrow><annotation encoding=application/x-tex>L(x)</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:1em;vertical-align:-0.25em></span><span class="mord mathnormal">L</span><span class=mopen>(</span><span class="mord mathnormal">x</span><span class=mclose>)</span></span></span></span> 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.</p>
<span class=katex-display><span class=katex><span class=katex-mathml><math display=block><semantics><mtable rowspacing=0.25em columnalign="right left" columnspacing=0em><mtr><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mi>log</mi><mo></mo><mo stretchy=false>(</mo><mi mathvariant=script>L</mi><mo stretchy=false>)</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=0 displaystyle=true><mrow><mrow/><mo>=</mo><munderover><mo></mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><mi>log</mi><mo></mo><mo stretchy=false>(</mo><mi>L</mi><mo stretchy=false>)</mo></mrow></mstyle></mtd></mtr></mtable><annotation encoding=application/x-tex>\begin{align*}
\log(\mathcal{L}) &= \sum_{i=1}^n \log(L)
\end{align*}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:3.2291em;vertical-align:-1.3645em></span><span class=mord><span class=mtable><span class=col-align-r><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.8645em><span style=top:-3.8645em><span class=pstrut style=height:3.6514em></span><span class=mord><span class=mop>lo<span style=margin-right:0.01389em>g</span></span><span class=mopen>(</span><span class="mord mathcal">L</span><span class=mclose>)</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:1.3645em><span></span></span></span></span></span><span class=col-align-l><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.8645em><span style=top:-3.8645em><span class=pstrut style=height:3.6514em></span><span class=mord><span class=mord></span><span class=mspace style=margin-right:0.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:0.2778em></span><span class="mop op-limits"><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.6514em><span style=top:-1.8723em;margin-left:0em><span class=pstrut style=height:3.05em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">i</span><span class="mrel mtight">=</span><span class="mord mtight">1</span></span></span></span><span style=top:-3.05em><span class=pstrut style=height:3.05em></span><span><span class="mop op-symbol large-op"></span></span></span><span style=top:-4.3em;margin-left:0em><span class=pstrut style=height:3.05em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:1.2777em><span></span></span></span></span></span><span class=mspace style=margin-right:0.1667em></span><span class=mop>lo<span style=margin-right:0.01389em>g</span></span><span class=mopen>(</span><span class="mord mathnormal">L</span><span class=mclose>)</span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:1.3645em><span></span></span></span></span></span></span></span></span></span></span></span>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=the-process-of-simulated-annealing>The process of Simulated Annealing<a href=#the-process-of-simulated-annealing class=hash-link aria-label="Direct link to The process of Simulated Annealing" title="Direct link to The process of Simulated Annealing"></a></h3>
<p>The means by which we optimize our likelihood function is the simulated annealing algorithm. The way it works is as follows:</p>
<ol>
<li>
<p>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.</p>
</li>
<li>
<p>For each iteration of the algorithm:</p>
<ol>
<li>Generate a new "proposed" set of parameters based on the current parameters -
i.e. slightly modify the current parameters to get a new set of parameters.</li>
<li>Calculate the value of <span class=katex><span class=katex-mathml><math><semantics><mrow><mi>log</mi><mo></mo><mo stretchy=false>(</mo><mi mathvariant=script>L</mi><mo stretchy=false>)</mo></mrow><annotation encoding=application/x-tex>\log(\mathcal{L})</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:1em;vertical-align:-0.25em></span><span class=mop>lo<span style=margin-right:0.01389em>g</span></span><span class=mopen>(</span><span class="mord mathcal">L</span><span class=mclose>)</span></span></span></span> for each set of parameters. If the function value for the
proposed parameter set is higher than for the current, automatically switch to the new parameter set
and continue the next iteration.</li>
<li>Given the new parameter set performs worse, determine a probability of switching to the new parameter set anyways: <span class=katex><span class=katex-mathml><math><semantics><mrow><mi mathvariant=script>P</mi><mo stretchy=false>(</mo><msub><mi>p</mi><mrow><mi>c</mi><mi>u</mi><mi>r</mi><mi>r</mi><mi>e</mi><mi>n</mi><mi>t</mi></mrow></msub><mo separator=true>,</mo><msub><mi>p</mi><mrow><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>o</mi><mi>s</mi><mi>e</mi><mi>d</mi></mrow></msub><mo stretchy=false>)</mo></mrow><annotation encoding=application/x-tex>\mathcal{P}(p_{current}, p_{proposed})</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:1.0361em;vertical-align:-0.2861em></span><span class="mord mathcal" style=margin-right:0.08222em>P</span><span class=mopen>(</span><span class=mord><span class="mord mathnormal">p</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.2806em><span style=top:-2.55em;margin-left:0em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">c</span><span class="mord mathnormal mtight">u</span><span class="mord mathnormal mtight">rre</span><span class="mord mathnormal mtight">n</span><span class="mord mathnormal mtight">t</span></span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.15em><span></span></span></span></span></span></span><span class=mpunct>,</span><span class=mspace style=margin-right:0.1667em></span><span class=mord><span class="mord mathnormal">p</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3361em><span style=top:-2.55em;margin-left:0em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">p</span><span class="mord mathnormal mtight">ro</span><span class="mord mathnormal mtight">p</span><span class="mord mathnormal mtight">ose</span><span class="mord mathnormal mtight">d</span></span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.2861em><span></span></span></span></span></span></span><span class=mclose>)</span></span></span></span></li>
<li>Switch to the new parameter set with probability <span class=katex><span class=katex-mathml><math><semantics><mrow><mi mathvariant=script>P</mi></mrow><annotation encoding=application/x-tex>\mathcal{P}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:0.6833em></span><span class="mord mathcal" style=margin-right:0.08222em>P</span></span></span></span>. If you fail to switch, begin the next iteration.</li>
</ol>
</li>
<li>
<p>The algorithm is complete after we fail to make a transition <span class=katex><span class=katex-mathml><math><semantics><mrow><mi>n</mi></mrow><annotation encoding=application/x-tex>n</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:0.4306em></span><span class="mord mathnormal">n</span></span></span></span> times in a row.</p>
</li>
</ol>
<p>If everything goes according to plan, we will have a value that is close to the true distribution of each die.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=the-actual-code>The actual code<a href=#the-actual-code class=hash-link aria-label="Direct link to The actual code" title="Direct link to The actual code"></a></h2>
<p>We start by defining the score function. This will tell us how well the proposed die densities actually explain the results.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> numpy </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">as</span><span class="token plain"> np</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token keyword" style="color:hsl(301, 63%, 40%)">from</span><span class="token plain"> numpy </span><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> polynomial</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain" style=display:inline-block></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token keyword" style="color:hsl(301, 63%, 40%)">def</span><span class="token plain"> </span><span class="token function" style="color:hsl(221, 87%, 60%)">density_coef</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d1_density</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> d2_density</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token comment" style="color:hsl(230, 4%, 64%)"># Calculating the probabilities of each outcome was taken</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token comment" style="color:hsl(230, 4%, 64%)"># from this author: http://math.stackexchange.com/a/1710392/320784</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> d1_p </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> polynomial</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">Polynomial</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d1_density</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> d2_p </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> polynomial</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">Polynomial</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d2_density</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> coefs </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d1_p </span><span class="token operator" style="color:hsl(221, 87%, 60%)">*</span><span class="token plain"> d2_p</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">coef</span><br></span><span class=token-line styl
<p>Afterward, we need to write something to permute the proposal densities. We make random modifications, and eventually the best one survives.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token keyword" style="color:hsl(301, 63%, 40%)">def</span><span class="token plain"> </span><span class="token function" style="color:hsl(221, 87%, 60%)">permute</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d1_density</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> d2_density</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token comment" style="color:hsl(230, 4%, 64%)"># To ensure we have legitimate densities, we will randomly</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token comment" style="color:hsl(230, 4%, 64%)"># increase one die face probability by `change`,</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token comment" style="color:hsl(230, 4%, 64%)"># and decrease one by `change`.</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token comment" style="color:hsl(230, 4%, 64%)"># This means there are something less than (1/`change`)^12 possibilities</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token comment" style="color:hsl(230, 4%, 64%)"># we are trying to search over.</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> change </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">.01</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> d1_index1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> d1_index2 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">random</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">randint</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token number" style="color:hsl(35, 99%, 36%)">0</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">2</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> d2_index1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> d2_index2 </span><span class="token opera
<p>Now we've got the main algorithm code to do. This is what brings all the pieces together.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token keyword" style="color:hsl(301, 63%, 40%)">def</span><span class="token plain"> </span><span class="token function" style="color:hsl(221, 87%, 60%)">optimize</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">data</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> conv_count</span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token number" style="color:hsl(35, 99%, 36%)">10</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> max_iter</span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token number" style="color:hsl(35, 99%, 36%)">1e4</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> switch_failures </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">0</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> iter_count </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">0</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token comment" style="color:hsl(230, 4%, 64%)"># Start with guessing fair dice</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> cur_d1 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">repeat</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> cur_d2 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">repeat</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119
<p>And now we have finished the hard work!</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=catching-the-casino>Catching the Casino<a href=#catching-the-casino class=hash-link aria-label="Direct link to Catching the Casino" title="Direct link to Catching the Casino"></a></h2>
<p>Let's go through a couple of scenarios and see if we can catch the casino cheating with some loaded dice. <strong>In every scenario we start with an assumption of fair dice</strong>, and then try our hand to figure out what the <em>actual</em> distribution was.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=attempt-1>Attempt 1<a href=#attempt-1 class=hash-link aria-label="Direct link to Attempt 1" title="Direct link to Attempt 1"></a></h3>
<p>The casino is using two dice that are both biased low. How well can we recover the distribution?</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token keyword" style="color:hsl(301, 63%, 40%)">import</span><span class="token plain"> time</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"></span><span class="token keyword" style="color:hsl(301, 63%, 40%)">def</span><span class="token plain"> </span><span class="token function" style="color:hsl(221, 87%, 60%)">simulate_casino</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d1_dist</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> d2_dist</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> n</span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token number" style="color:hsl(35, 99%, 36%)">10000</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">:</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> d1_vals </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> unfair_die</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d1_dist</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> n</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> d2_vals </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> unfair_die</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d2_dist</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> n</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> start </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> time</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">perf_counter</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> max_d1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> max_d2</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> final_d1</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> final_d2 </span><span class="token operator" style="color:hsl(221, 87%, 60%)">=</span><span class="token plain"> optimize</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">d1_vals </span><span class="token operator" style="color:hsl(221, 87%, 60%)">+</span><span class="token plain"> d2_vals</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token plain"></span><b
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 1000; Current score (higher is better): -22147.004400281654</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Simulated Annealing time: 0.30s</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
<p><img decoding=async loading=lazy alt=png src=/assets/images/_notebook_14_1-22c2867e2cee02c45b87635ab4d3b76a.png width=605 height=601 class=img_ev3q></p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=attempt-2>Attempt 2<a href=#attempt-2 class=hash-link aria-label="Direct link to Attempt 2" title="Direct link to Attempt 2"></a></h3>
<p>The casino now uses dice that are both biased towards 1 and 6.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">simulate_casino</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Simulated Annealing time: 0.08s</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
<p><img decoding=async loading=lazy alt=png src=/assets/images/_notebook_16_1-eaa9ebc93d4557216c77a63df7df5192.png width=605 height=601 class=img_ev3q></p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=attempt-3>Attempt 3<a href=#attempt-3 class=hash-link aria-label="Direct link to Attempt 3" title="Direct link to Attempt 3"></a></h3>
<p>The casino will now use one die biased towards 1 and 6, and one die towards 3 and 4.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">simulate_casino</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">12</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">3</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="c
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Simulated Annealing time: 0.09s</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
<p><img decoding=async loading=lazy alt=png src=/assets/images/_notebook_18_1-f61745c7c099b114becca8acb3175541.png width=605 height=601 class=img_ev3q></p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=attempt-4>Attempt 4<a href=#attempt-4 class=hash-link aria-label="Direct link to Attempt 4" title="Direct link to Attempt 4"></a></h3>
<p>We'll now finally go to a fair casino to make sure that we can still recognize a positive result.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">simulate_casino</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token plain">np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">repeat</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> np</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">.</span><span class="token plain">repeat</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">6</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">)</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Simulated Annealing time: 0.02s</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
<p><img decoding=async loading=lazy alt=png src=/assets/images/_notebook_20_1-ea40789d2365d20a0725aca866680217.png width=605 height=601 class=img_ev3q></p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=attempt-5>Attempt 5<a href=#attempt-5 class=hash-link aria-label="Direct link to Attempt 5" title="Direct link to Attempt 5"></a></h3>
<p>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.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain">simulate_casino</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">(</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">2</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">2</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">2</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">]</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"></span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> </span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">[</span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">1</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">2</span><span class="token operator" style="color:hsl(221, 87%, 60%)">/</span><span class="token number" style="color:hsl(35, 99%, 36%)">9</span><span class="token punctuation" style="color:hsl(119, 34%, 47%)">,</span><span class="token plain"> </span><span class="token number" style="color:hsl(35, 99%, 36%)">2</span><span class="token operator" style="color:h
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-background-color:hsl(230, 1%, 98%);--prism-color:hsl(230, 8%, 24%)"><div class=codeBlockContent_biex><pre tabindex=0 class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="background-color:hsl(230, 1%, 98%);color:hsl(230, 8%, 24%)"><code class=codeBlockLines_e6Vv><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 1000; Current score (higher is better): -551.6995384525453</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 2000; Current score (higher is better): -547.7803673440676</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 3000; Current score (higher is better): -547.9805613193807</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 4000; Current score (higher is better): -546.7574874775273</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 5000; Current score (higher is better): -549.5798007672656</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 6000; Current score (higher is better): -545.0354060154496</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 7000; Current score (higher is better): -550.1134504086606</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 8000; Current score (higher is better): -549.9306537114975</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 9000; Current score (higher is better): -550.7075182119111</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Iteration: 10000; Current score (higher is better): -549.400679551826</span><br></span><span class=token-line style="color:hsl(230, 8%, 24%)"><span class="token plain"> Simulated Annealing time: 1.94s</span><br></span></code></pre><div class=buttonGroup__atx><button type=button aria-label="Copy code to clipboard" title=Copy class=clean-btn><span class=copyButtonIcons_eSgA aria-hidden=true><svg viewBox="0 0 24 24" class=copyButtonIcon_y97N><path fill=currentColor d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"/></svg><svg viewBox="0 0 24 24" class=copyButtonSuccessIcon_LjdS><path fill=currentColor d=M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z /></svg></span></button></div></div></div>
<p><img decoding=async loading=lazy alt=png src=/assets/images/_notebook_22_1-210909ddb46467b78aa3f65c8ae519fd.png width=605 height=601 class=img_ev3q></p>
<p>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.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=conclusion>Conclusion<a href=#conclusion class=hash-link aria-label="Direct link to Conclusion" title="Direct link to Conclusion"></a></h2>
<p>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.</p>
<p>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.</p>
<p>We can declare at long last: <strong>the mathematicians have again triumphed over the casino</strong>.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id=justification-of-simulated-annealing>Justification of Simulated Annealing<a href=#justification-of-simulated-annealing class=hash-link aria-label="Direct link to Justification of Simulated Annealing" title="Direct link to Justification of Simulated Annealing"></a></h2>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=why-simulated-annealing>Why Simulated Annealing?<a href=#why-simulated-annealing class=hash-link aria-label="Direct link to Why Simulated Annealing?" title="Direct link to Why Simulated Annealing?"></a></h3>
<p>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 <span class=katex><span class=katex-mathml><math><semantics><mrow><msub><mi>p</mi><mrow><mi>i</mi><mi>j</mi></mrow></msub><mo></mo><mo stretchy=false>(</mo><mn>0</mn><mo separator=true>,</mo><mn>1</mn><mo stretchy=false>)</mo><mo separator=true>;</mo><mi>i</mi><mo separator=true>,</mo><mi>j</mi><mo></mo><mo stretchy=false>{</mo><mn>1</mn><mo separator=true>,</mo><mo></mo><mo separator=true>,</mo><mn>6</mn><mo stretchy=false>}</mo></mrow><annotation encoding=application/x-tex>p_{ij} \in (0, 1); i, j \in \{1, \ldots, 6\}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:0.8252em;vertical-align:-0.2861em></span><span class=mord><span class="mord mathnormal">p</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:0.3117em><span style=top:-2.55em;margin-left:0em;margin-right:0.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style=margin-right:0.05724em>ij</span></span></span></span></span><span class=vlist-s></span></span><span class=vlist-r><span class=vlist style=height:0.2861em><span></span></span></span></span></span></span><span class=mspace style=margin-right:0.2778em></span><span class=mrel></span><span class=mspace style=margin-right:0.2778em></span></span><span class=base><span class=strut style=height:1em;vertical-align:-0.25em></span><span class=mopen>(</span><span class=mord>0</span><span class=mpunct>,</span><span class=mspace style=margin-right:0.1667em></span><span class=mord>1</span><span class=mclose>)</span><span class=mpunct>;</span><span class=mspace style=margin-right:0.1667em></span><span class="mord mathnormal">i</span><span class=mpunct>,</span><span class=mspace style=margin-right:0.1667em></span><span class="mord mathnormal" style=margin-right:0.05724em>j</span><span class=mspace style=margin-right:0.2778em></span><span class=mrel></span><span class=mspace style=margin-right:0.2778em></span></span><span class=base><span class=strut style=height:1em;vertical-align:-0.25em></span><span class=mopen>{</span><span class=mord>1</span><span class=mpunct>,</span><span class=mspace style=margin-right:0.1667em></span><span class=minner></span><span class=mspace style=margin-right:0.1667em></span><span class=mpunct>,</span><span class=mspace style=margin-right:0.1667em></span><span class=mord>6</span><span class=mclose>}</span></span></span></span>, 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.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id=why-not-something-else>Why not something else?<a href=#why-not-something-else class=hash-link aria-label="Direct link to Why not something else?" title="Direct link to Why not something else?"></a></h3>
<p>This is a fair question. There are two classes of algorithms that can also be used to solve this problem: <a href=https://en.wikipedia.org/wiki/Nonlinear_programming target=_blank rel="noopener noreferrer">Non-linear optimization</a> methods, and the <a href=https://en.wikipedia.org/wiki/Expectation%E2%80%93maximization_algorithm target=_blank rel="noopener noreferrer">EM algorithm</a>.</p>
<ol>
<li>
<p>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 <em>incredibly</em> quickly - far more quickly than any non-linear solver would be able to accomplish.</p>
</li>
<li>
<p>The EM Algorithm was originally what I intended to write this blog post with. Indeed, the post was inspired by the <a href=http://web.stanford.edu/class/stats366/hmmR2.html target=_blank rel="noopener noreferrer">crooked casino</a> 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.</p>
</li>
</ol></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Blog post page navigation"><a class="pagination-nav__link pagination-nav__link--prev" href=/2016/04/tick-tock><div class=pagination-nav__sublabel>Older post</div><div class=pagination-nav__label>Tick tock...</div></a><a class="pagination-nav__link pagination-nav__link--next" href=/2016/06/event-studies-and-earnings-releases><div class=pagination-nav__sublabel>Newer post</div><div class=pagination-nav__label>Event studies and earnings releases</div></a></nav></main><div class="col col--2"><div class="tableOfContents_bqdL thin-scrollbar"><ul class="table-of-contents table-of-contents__left-border"><li><a href=#proving-we-can-detect-cheating class="table-of-contents__link toc-highlight">Proving we can detect cheating</a><li><a href=#the-edge-cases class="table-of-contents__link toc-highlight">The Edge Cases</a><ul><li><a href=#edge-case-1 class="table-of-contents__link toc-highlight">Edge Case 1</a><li><a href=#edge-case-2 class="table-of-contents__link toc-highlight">Edge Case 2</a></ul><li><a href=#detection-conclusion class="table-of-contents__link toc-highlight">Detection Conclusion</a><li><a href=#simulated-annealing class="table-of-contents__link toc-highlight">Simulated Annealing</a><ul><li><a href=#deriving-the-likelihood-function class="table-of-contents__link toc-highlight">Deriving the Likelihood function</a><li><a href=#the-process-of-simulated-annealing class="table-of-contents__link toc-highlight">The process of Simulated Annealing</a></ul><li><a href=#the-actual-code class="table-of-contents__link toc-highlight">The actual code</a><li><a href=#catching-the-casino class="table-of-contents__link toc-highlight">Catching the Casino</a><ul><li><a href=#attempt-1 class="table-of-contents__link toc-highlight">Attempt 1</a><li><a href=#attempt-2 class="table-of-contents__link toc-highlight">Attempt 2</a><li><a href=#attempt-3 class="table-of-contents__link toc-highlight">Attempt 3</a><li><a href=#attempt-4 class="table-of-contents__link toc-highlight">Attempt 4</a><li><a href=#attempt-5 class="table-of-contents__link toc-highlight">Attempt 5</a></ul><li><a href=#conclusion class="table-of-contents__link toc-highlight">Conclusion</a><li><a href=#justification-of-simulated-annealing class="table-of-contents__link toc-highlight">Justification of Simulated Annealing</a><ul><li><a href=#why-simulated-annealing class="table-of-contents__link toc-highlight">Why Simulated Annealing?</a><li><a href=#why-not-something-else class="table-of-contents__link toc-highlight">Why not something else?</a></ul></ul></div></div></div></div></div><footer class=footer><div class="container container-fluid"><div class="footer__bottom text--center"><div class=footer__copyright>Copyright © 2024 Bradlee Speice</div></div></div></footer></div>