<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>The blog of Luis Martin Gil</title>
 <link href="http://www.luismartingil.com/atom.xml" rel="self"/>
 <link href="http://www.luismartingil.com"/>
 <updated>2015-04-17T10:43:39+00:00</updated>
 <id>http://www.luismartingil.com</id>
 <author>
   <name>Luis Martin Gil</name>
   <email>martingil.luis@gmail.com</email>
 </author>

 
 <entry>
   <title>Mining 911 Emergency calls using Apache Hadoop</title>
   <link href="http://www.luismartingil.com/blog/2014/01/05/b2bua-911-mining.html"/>
   <updated>2014-01-05T00:00:00+00:00</updated>
   <id>http://www.luismartingil.com/blog/2014/01/05/b2bua-911-mining</id>
   <content type="html">&lt;p&gt;This study started in order to practice the concepts learnt at &lt;a href=&quot;http://www.bigdatatechcon.com/&quot;&gt;BigData Techcon&lt;/a&gt; Boston, 2013. It uses &lt;a href=&quot;http://hadoop.apache.org/&quot;&gt;Apache Hadoop&lt;/a&gt; and &lt;a href=&quot;http://www.python.org/&quot;&gt;Python&lt;/a&gt; as tools to get the job done.&lt;/p&gt;

&lt;p&gt;Self-moved by my internal curiosity, &lt;strong&gt;The goal is to understand the flow of the 911 calls for a given time-period&lt;/strong&gt;. The 911 industry doesn’t have a huge level of call traffic but it demands a strictly well-engineered network design. Knowing how our servers are being used is a must. This study will help to understand the VoIP traffic of 911 Emergency calls.&lt;/p&gt;

&lt;p&gt;This is &lt;a href=&quot;www.luismartingil.com&quot;&gt;@luismartingil&lt;/a&gt; solution. More info &lt;a href=&quot;https://github.com/luismartingil/post_b2bua-911-mining&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;where-am-i-getting-the-dataset-from&quot;&gt;Where am I getting the dataset from?&lt;/h4&gt;
&lt;p&gt;Dataset is taken from different random-selected &lt;a href=&quot;http://en.wikipedia.org/wiki/Back-to-back_user_agent&quot;&gt;SIP b2buas&lt;/a&gt; from our IP network. These servers route calls based on different SIP headers and databases using the Session Initiation Protocol. Obviously all the SIP messages they send/receive are properly logged with the internal state of the servers. Today I’ll take some log files from these servers and have fun with them.&lt;/p&gt;

&lt;p&gt;Log files have lines with the following structure: {&lt;code&gt;INVITE sip:number@domain:5060 SIP/2.0&lt;/code&gt;, &lt;code&gt;SIP/2.0 100 Trying&lt;/code&gt;, &lt;code&gt;SIP/2.0 180 Ringing&lt;/code&gt;, &lt;code&gt;SIP/2.0 183 Session Progress&lt;/code&gt;, &lt;code&gt;SIP/2.0 200 OK&lt;/code&gt;, &lt;code&gt;BYE sip:domain SIP/2.0&lt;/code&gt;}.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;what-stats-am-i-looking-for-1-2-3&quot;&gt;What stats am I looking for? {1, 2, 3}&lt;/h4&gt;

&lt;p&gt;1) &lt;strong&gt;Overall days&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;2) &lt;strong&gt;Calls for the given days of the week&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;3) &lt;strong&gt;Calls for the given hour of the day&lt;/strong&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;defining-the-problem-in-hadoop&quot;&gt;Defining the problem in Hadoop&lt;/h3&gt;
&lt;p&gt;We have to properly model our solution in the Hadoop world, this is required for Hadoop to understand what we are trying to do. I consider this step as the hardest one. If we can’t model our solution into a Hadoop’s valid input we won’t be able to use it and to get the benefits of the parallelism and distribution in the cluster.&lt;/p&gt;

&lt;p&gt;Two different methods need to be defined: &lt;strong&gt;map&lt;/strong&gt; and &lt;strong&gt;reduce&lt;/strong&gt;. I love drawings, let’s visualize this (high resolution &lt;a href=&quot;https://raw.github.com/luismartingil/post_b2bua-911-mining/master/img/map_reduce_single.png&quot;&gt;here&lt;/a&gt;):
&lt;img src=&quot;https://raw.github.com/luismartingil/post_b2bua-911-mining/master/img/map_reduce_single_small.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Main idea of my solution is to let the &lt;strong&gt;map&lt;/strong&gt; ‘bucketize’ (make buckets) the input and let the &lt;strong&gt;reduce&lt;/strong&gt; count them all.&lt;/p&gt;

&lt;h4 id=&quot;a-mapping&quot;&gt;a. Mapping&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;map&lt;/strong&gt; gets the log files from our SIP servers as input and generates keys-value pairs. The concept is very similar to creating different hash functions. Our keys are defined differently based on the nature of {1, 2, 3}.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;a1) &lt;strong&gt;Overall days&lt;/strong&gt;, we want each day to count as a key. Mapper generates : &lt;em&gt;01/03/2013 1&lt;/em&gt;, &lt;em&gt;02/06/2013 1&lt;/em&gt;, &lt;em&gt;21/01/2013 1&lt;/em&gt;, &lt;em&gt;01/09/2013 1&lt;/em&gt;, &lt;em&gt;01/11/2013 1&lt;/em&gt;, …&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;a2) &lt;strong&gt;Calls for the given days of the week&lt;/strong&gt;, each day of the week will be a key : &lt;em&gt;06sat 1&lt;/em&gt;, &lt;em&gt;02tue 1&lt;/em&gt;, &lt;em&gt;01mon 1&lt;/em&gt;, &lt;em&gt;00sun 1&lt;/em&gt;, &lt;em&gt;00sun 1&lt;/em&gt;, &lt;em&gt;06sat 1&lt;/em&gt;, …&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;a3) &lt;strong&gt;Calls for the given hour of the day&lt;/strong&gt;, mapper will generate key-value pairs based on the time of the day : &lt;em&gt;03am 1&lt;/em&gt;, &lt;em&gt;02pm 1&lt;/em&gt;, &lt;em&gt;02am 1&lt;/em&gt;, &lt;em&gt;06am 1&lt;/em&gt;, &lt;em&gt;04am 1&lt;/em&gt;, &lt;em&gt;02pm 1&lt;/em&gt;, &lt;em&gt;02pm 1&lt;/em&gt;, &lt;em&gt;11pm 1&lt;/em&gt;, …&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h6 id=&quot;key-assignation-process-based-on-buckets&quot;&gt;Key assignation process based on buckets&lt;/h6&gt;
&lt;p&gt;&lt;img src=&quot;https://raw.github.com/luismartingil/post_b2bua-911-mining/master/img/map_reduce_buckets_small.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;b-reducing&quot;&gt;b. Reducing&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;reduce&lt;/strong&gt; properly joins map’s output and provides the final result. In our scenario it just merges the key-value pairs and sums the values associated to a key. In this case it’s the same for {1, 2, 3}.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;b1) &lt;strong&gt;Overall days&lt;/strong&gt;, reducer generates : &lt;em&gt;01/03/2013 D1&lt;/em&gt;, &lt;em&gt;02/06/2013 D2&lt;/em&gt;, &lt;em&gt;21/01/2013 D3&lt;/em&gt;, &lt;em&gt;01/09/2013 D4&lt;/em&gt;, &lt;em&gt;01/11/2013 DN&lt;/em&gt;, …&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;b2) &lt;strong&gt;Calls for the given days of the week&lt;/strong&gt;, reducer generates : &lt;em&gt;06sat W1&lt;/em&gt;, &lt;em&gt;02tue W2&lt;/em&gt;, &lt;em&gt;01mon W3&lt;/em&gt;, &lt;em&gt;00sun W4&lt;/em&gt;, &lt;em&gt;00sun W5&lt;/em&gt;, &lt;em&gt;06sat WN&lt;/em&gt;, …&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;b3) &lt;strong&gt;Calls for the given hour of the day&lt;/strong&gt;, reducer generates : &lt;em&gt;03am H1&lt;/em&gt;, &lt;em&gt;02pm H2&lt;/em&gt;, &lt;em&gt;02am H3&lt;/em&gt;, &lt;em&gt;06am H4&lt;/em&gt;, &lt;em&gt;04am H5&lt;/em&gt;, &lt;em&gt;02pm H6&lt;/em&gt;, &lt;em&gt;02pm H7&lt;/em&gt;, &lt;em&gt;11pm H8&lt;/em&gt;, …&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;are-there-more-solutions-to-this-problem&quot;&gt;Are there more solutions to this problem?&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Yes&lt;/strong&gt;. I consider myself a &lt;strong&gt;learner&lt;/strong&gt; in the Hadoop world, there might be better Hadoop implementations to this easy-problem but this is the approach I came up with. It doesn’t mean that is the only one by any means. You can have more information about the MapReduce model &lt;a href=&quot;http://en.wikipedia.org/wiki/MapReduce&quot;&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;does-hadoop-cluster-look-always-the-same&quot;&gt;Does Hadoop cluster look always the same?&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;No&lt;/strong&gt;. In a real cluster map-reduce takes place in a multi-level hierarchy. This picture gives an idea of what it could look like in a more realistic scenario (high resolution &lt;a href=&quot;https://raw.github.com/luismartingil/post_b2bua-911-mining/master/img/map_reduce_cluster.png&quot;&gt;here&lt;/a&gt;).
&lt;img src=&quot;https://raw.github.com/luismartingil/post_b2bua-911-mining/master/img/map_reduce_cluster_small.png&quot; alt=&quot;&quot; /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;where-are-your-mapperreducers&quot;&gt;Where are your mapper/reducers?&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/luismartingil/post_b2bua-911-mining&quot;&gt;&lt;strong&gt;Here&lt;/strong&gt;&lt;/a&gt; you can find all the source code. Mapper and reducer are both implemented in Python. Mapper {1, 2, 3} is selected using an env variable, which makes things easy when using the &lt;a href=&quot;https://github.com/luismartingil/post_b2bua-911-mining/blob/master/run.sh&quot;&gt;&lt;strong&gt;BASH script&lt;/strong&gt;&lt;/a&gt; to populate all the jobs with one command. &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;doing-the-job-getting-the-results&quot;&gt;Doing the job. Getting the results&lt;/h3&gt;

&lt;p&gt;I’m using Cloudera quickstart Virtual Machine &lt;code&gt;4.4.0-1&lt;/code&gt; with Hadoop &lt;code&gt;2.0.0-cdh4.4.0&lt;/code&gt; for the cluster simulation. You can have a quick look to the commands used &lt;a href=&quot;https://github.com/luismartingil/post_b2bua-911-mining/blob/master/README.md&quot;&gt;here&lt;/a&gt;. Basically I copied the files to be mined into the HDFS file system and executed the &lt;code&gt;run.sh&lt;/code&gt;. Results were copied back from the HDFS to the VM by the script. Here is an output of one of the jobs. As you can see the reducing process starts before the mapper is done.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;14/02/01 23:49:17 INFO streaming.StreamJob:  map 0%  reduce 0%
14/02/01 23:49:32 INFO streaming.StreamJob:  map 3%  reduce 0%
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;...&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
14/02/01 23:53:06 INFO streaming.StreamJob:  map 93%  reduce 15%
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;...&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
14/02/01 23:53:24 INFO streaming.StreamJob:  map 100%  reduce 50%
14/02/01 23:53:34 INFO streaming.StreamJob:  map 100%  reduce 100%&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;1) &lt;strong&gt;Overall days&lt;/strong&gt;&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;Last months each b2bua instance has been getting an aprox average of 6500 calls per day. So some groups of counties of Indiana have been getting around &lt;strong&gt;4 calls per minute&lt;/strong&gt;. This number could sound ridiculous from the carriers’ perspective, in the 911 world is a valid number though.&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;There is a relevant peak in the plot. On &lt;strong&gt;November 17th 2013&lt;/strong&gt; each instance got around 12600 calls, which almost &lt;strong&gt;doubled&lt;/strong&gt; the normal traffic of the servers. What did happened that day? We had one of the worst weather conditions of the last years. Tornados were all over the midwest of the USA, hitting hard some counties in Indiana. &lt;a href=&quot;http://en.wikipedia.org/wiki/November_17,_2013_tornado_outbreak&quot;&gt;&lt;strong&gt;Tornado outbreak&lt;/strong&gt;&lt;/a&gt;. I’m glad we did help the people. Very interesting.&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.github.com/luismartingil/post_b2bua-911-mining/master/img/eday.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;2) &lt;strong&gt;Calls for the given days of the week&lt;/strong&gt; (From September 1st 2013 - Jan 1st 2014)&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Social interactions are pretty defined in the next plot. &lt;strong&gt;Weekends are pretty much the hottest points in the 911 call-generation&lt;/strong&gt; for a given week. That’s probably what all of us guessed, but it’s always nice to backup the thoughts with the real-data.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.github.com/luismartingil/post_b2bua-911-mining/master/img/dayow.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;3) &lt;strong&gt;Calls for the given hour of the day&lt;/strong&gt;  (From September 1st 2013 - Jan 1st 2014)&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Next plot also makes a lot of sense. Being &lt;strong&gt;5pm&lt;/strong&gt; the rush hour and people usually sleeping during night (funny), this sinusoidal wave perfectly defines the normal schedule for the people interactions (and 911 call-generation).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.github.com/luismartingil/post_b2bua-911-mining/master/img/hourod.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h6 id=&quot;graphs-are-made-using-gnuplothttpsgithubcomluismartingilpostb2bua-911-miningblobmasterresultmakegraphsgnuplot-and-tweaked-with-gimphttpwwwgimporg-open-source-powered-&quot;&gt;( Graphs are made using &lt;a href=&quot;https://github.com/luismartingil/post_b2bua-911-mining/blob/master/result/make_graphs.gnuplot&quot;&gt;Gnuplot&lt;/a&gt; and tweaked with &lt;a href=&quot;http://www.gimp.org/&quot;&gt;Gimp&lt;/a&gt;. Open source powered! )&lt;/h6&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;em&gt;People’s habits&lt;/em&gt; are described in these plots, also events like the &lt;em&gt;tornado outbreak&lt;/em&gt; are pointed in our data. Results are &lt;em&gt;reasonable and pretty logical&lt;/em&gt;. &lt;strong&gt;Using technology to approach these studies is pretty neat&lt;/strong&gt;. Learnt several things in the mining side as well. &lt;strong&gt;Very interesting project overall&lt;/strong&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;I would like to &lt;strong&gt;thanks INdigital, my actual employer&lt;/strong&gt;. I’m proud to be part of a team of devs that are making the 911 Emergency technology better. We are doing a job that has a great impact in the society making it better for the people to help each other.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;did-i-really-need-to-use-hadoop-for-this&quot;&gt;Did I really need to use Hadoop for this?&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;For files of this size of this configuration, obviously no. I can easily do this using a terminal command piping the map and reduce myself. We start to get the benefit of Hadoop using a real cluster and mining X files Y times bigger (X, Y will be something interesting to research). This is an enjoyable approach to do the job as the same time we play with the basic technologies and concepts of big data. Obviously this could be a good starting point as a research for a valid &lt;strong&gt;scalable solution&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Getting a fair result with an unfair coin</title>
   <link href="http://www.luismartingil.com/blog/puzzle/2013/11/20/puzzle-unfair-coin.html"/>
   <updated>2013-11-20T00:00:00+00:00</updated>
   <id>http://www.luismartingil.com/blog/puzzle/2013/11/20/puzzle-unfair-coin</id>
   <content type="html">&lt;blockquote&gt;
  &lt;p&gt;How can you get a fair coin toss if someone hands you a coin that is weighted to come up heads more often than tails?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;solution&quot;&gt;Solution&lt;/h3&gt;

&lt;p&gt;This is &lt;a href=&quot;www.luismartingil.com&quot;&gt;@luismartingil&lt;/a&gt; solution. Please check all the &lt;a href=&quot;http://www.python.org/&quot;&gt;Python&lt;/a&gt; source code &lt;a href=&quot;https://github.com/luismartingil/post_fair-result-unfair-coin&quot;&gt;here&lt;/a&gt;. This solution uses &lt;code&gt;matplotlib&lt;/code&gt; for the plots.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;We can have two different types of results when throwing the coin: {heads, tails}&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;We are going to have two players playing the game: {&lt;code&gt;player-heads&lt;/code&gt;, &lt;code&gt;player-tails&lt;/code&gt;}&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;player-heads&lt;/code&gt; is the player that chooses heads. &lt;code&gt;player-tails&lt;/code&gt; the one choosing tails.&lt;/li&gt;
  &lt;li&gt;A fair game means that both players have the same probability of winning (50%).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;We first need to know how unfair the game is in order to make it fair&lt;/strong&gt;. We have to know how many times the coin comes up heads vs the times it comes up tails. We do this throwing the coin N times, where N is a huge number. After doing this we have a percentage of the times the coin comes up heads, lets call it &lt;code&gt;p1&lt;/code&gt;. We can define the percentage of times the coin comes up tails as &lt;code&gt;p2&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Based on the problem:
- &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt; are in this range &lt;code&gt;[0, 100]&lt;/code&gt;
- &lt;code&gt;p1&lt;/code&gt; will be bigger than &lt;code&gt;p2&lt;/code&gt;. &lt;code&gt;p1 &amp;gt; p2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can assume that &lt;code&gt;p2 = 100 - p1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In order the game to be fair, &lt;code&gt;player-heads&lt;/code&gt; has to win more than the &lt;code&gt;p1&lt;/code&gt; times of the times played in order to win the game. The bigger N is, the fairer the game is.&lt;/strong&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;proving-the-solution&quot;&gt;Proving the solution&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Always back what you are saying!&lt;/em&gt;. A neat way to back my solution is to write some code and perform a simulation. That’s what I did.&lt;/p&gt;

&lt;h4 id=&quot;game-simulation&quot;&gt;Game simulation&lt;/h4&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Game&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;coin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;simulate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HEADS&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TAILS&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;throw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;whoWon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;whoWon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt;             &lt;span class=&quot;c&quot;&gt;# PLAY_TAILS wins in case of even&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt;             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PLAY_HEADS&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getCondHeads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PLAY_TAILS&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 id=&quot;unfairgame&quot;&gt;UnfairGame&lt;/h4&gt;

&lt;p&gt;Original game. It doesn’t know if the coin is weighted or not.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;lineno&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UnfairGame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Game&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;2&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getCondHeads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;3&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HEADS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TAILS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 id=&quot;fairgame&quot;&gt;FairGame&lt;/h4&gt;

&lt;p&gt;A handicap is applied to the &lt;code&gt;player-tails&lt;/code&gt;. Takes into account thw weight of the coin (&lt;code&gt;coin.getPercent()&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;lineno&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FairGame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Game&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;2&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getCondHeads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;3&lt;/span&gt;         &lt;span class=&quot;c&quot;&gt;# PLAY_HEADS needs to win more to win the game. Handicap ;-)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;4&lt;/span&gt;         &lt;span class=&quot;c&quot;&gt;# @luismartingil solution to the problem.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;5&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HEADS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TAILS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;6&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simulation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HEADS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getPercent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 id=&quot;coins&quot;&gt;Coins&lt;/h4&gt;

&lt;p&gt;Both &lt;code&gt;FairGame&lt;/code&gt; and &lt;code&gt;UnfairGame&lt;/code&gt; will be played with different coins.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coin 1)&lt;/strong&gt; 50% - 50%&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coin 2)&lt;/strong&gt; 60% - 40%&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coin 3)&lt;/strong&gt; 80% - 20%&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;some-plots-playing-the-games&quot;&gt;Some plots. Playing the games&lt;/h3&gt;

&lt;h4 id=&quot;plots-1-player-heads-wins&quot;&gt;Plots-1, &lt;em&gt;Player heads wins&lt;/em&gt;&lt;/h4&gt;

&lt;p&gt;These plots contains information about the wins of the &lt;code&gt;player-head&lt;/code&gt; in a percentage based. Whether we have a fair or unfair {game, coin} the goal for a fair game is to see the points right in the 50% horizontal line, which would be the fairest game possible.&lt;/p&gt;

&lt;p&gt;Some comments,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Coin 1)&lt;/strong&gt;. It doen’t matter which game we play using a fair coin (50% - 50%). It is always fair.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Coin 2-3)&lt;/strong&gt;. &lt;code&gt;UnfairGame&lt;/code&gt; is always a looser game for &lt;code&gt;player-tails&lt;/code&gt; using unweighted coin. This means that &lt;code&gt;player-tails&lt;/code&gt; is likely to win in the beginning, but if he plays a bunch of times he will loose.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Coin 2-3)&lt;/strong&gt;. The more unweight is the coin, the faster it will converge.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Coin 2-3)&lt;/strong&gt;. Our &lt;code&gt;FairGame&lt;/code&gt; solution &lt;em&gt;works&lt;/em&gt;, it converges to 50% in all the cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.github.com/luismartingil/post_fair-result-unfair-coin/master/player-heads-wins/player-heads-wins_80.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;plots-2-fairness-ratio--how-fair-is-this-solution&quot;&gt;Plots-2, &lt;em&gt;Fairness ratio : How fair is this solution?&lt;/em&gt;&lt;/h4&gt;

&lt;p&gt;I was curious about the fact of grading a fair solution. I came up with a function which returns how fair the solution is based on how close it is from the 50% percent. You can see the code and some plots below.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;processFairness&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt;     &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot; Return a value in [0, 1] defining how fair is the percent.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    0, worst.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    1, best.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    Applies the fairness function based on:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    if x == 50 , y = 1&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    if 0 &amp;lt;= x &amp;lt; 50, y = x/50.0&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    if 50 &amp;lt; x &amp;lt;= 100, y=-x/50.0 + 2&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |      (50,1)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |         _&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |        /|\&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |       / | \&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |      /  |  \&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |     /   |   \&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |    /    |    \&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |   /     |     \&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |  /      |      \&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    | /       |       \&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    |/        |        \&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    ---------------------------------------------------------------&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    (0,0)   (50,0)   (100,0)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26&lt;/span&gt; &lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;50.0&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;50.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;percentOutOfRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;Error calculating fairness. percent:&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Some comments,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Coin 1)&lt;/strong&gt;. Coin (50% - 50%) has the best GPA and always converges to grade A.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Coin 2-3)&lt;/strong&gt;. &lt;code&gt;UnfairGame&lt;/code&gt; always converges to rate 0 when using an unfair coin.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Coin 2-3)&lt;/strong&gt;. Our &lt;code&gt;FairGame&lt;/code&gt; solution &lt;em&gt;rates very good&lt;/em&gt; for a med-high N. Nothing that we didn’t know.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.github.com/luismartingil/post_fair-result-unfair-coin/master/fairness/fairness_80.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Bet your money in &lt;code&gt;heads&lt;/code&gt; when the coin is weighted to come up heads more often than tails. Save your money in all other cases. Better than a casino.&lt;/p&gt;

&lt;hr /&gt;
</content>
 </entry>
 
 
</feed>
