{"id":860,"date":"2012-11-21T18:28:57","date_gmt":"2012-11-21T22:28:57","guid":{"rendered":"http:\/\/www.hargaden.com\/enda\/?p=427"},"modified":"2012-11-21T18:28:57","modified_gmt":"2012-11-21T22:28:57","slug":"polya-urn-script","status":"publish","type":"post","link":"https:\/\/hargaden.com\/enda\/polya-urn-script\/","title":{"rendered":"Polya urn script"},"content":{"rendered":"<p>I had to present on path dependence recently. Paul David&#8217;s famous QWERTY paper (American Economic Review P&amp;P, 1985) was covered, as was Paul Pierson&#8217;s <i>Increasing Returns, Path Dependence, and the Study of Politics<\/i> (American Political Science Review, 2000).<\/p>\n<p>Both of these papers reference the\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Polya_urn_model\" target=\"_blank\" rel=\"noopener\">Polya urn model<\/a>. This is a ball-and-urn process where, instead of simple replacement, there is replacement with addition of a ball of the same colour. This gives one colour a dominant position in the long run.<\/p>\n<p>I coded a script to run Polya urn simulations. It&#8217;s in JavaScript, and easily adaptable. I tested it in Chrome.<\/p>\n<p>[sourcecode language=&#8221;javascript&#8221;]<br \/>\n&lt;html&gt;&lt;body&gt;&lt;script language=&quot;JavaScript&quot;&gt;<br \/>\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/p>\n<p>\/\/ Number of iterations<br \/>\nvar runs = 400;<\/p>\n<p>\/\/ Number of blue balls initially in urn<br \/>\nvar num_blue = 1;<\/p>\n<p>\/\/ Number of red balls initially in urn<br \/>\nvar num_red = 1;<\/p>\n<p>\/\/ Number of additional red\/blue balls added after red\/blue &quot;wins&quot;<br \/>\nvar extra_extra = 1;<\/p>\n<p>\/\/ Show which colour was picked etc<br \/>\nvar details = true;<\/p>\n<p>\/\/ Summarize the mean at the end<br \/>\nvar mean_details = true;<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/p>\n<p>var initial_dist = num_red \/ (num_red + num_blue);<br \/>\nred_width = initial_dist*400;<\/p>\n<p>\/\/ Graph initial distribution<br \/>\ndocument.write(&quot;Initial Distribution&lt;br \/&gt;&lt;div style=&#8217;background-color:blue;height:10;width:400;&#8217;&gt;&quot;);<br \/>\ndocument.write(&quot;&lt;div style=&#8217;background-color:red;height:10;width:&quot; + red_width +&quot;;&#8217;&gt;&lt;\/div&gt;&lt;\/div&gt;&quot;);<\/p>\n<p>\/\/ Random number<br \/>\nvar r_n = 0;<\/p>\n<p>var red_share = new Array();<br \/>\nred_share[0] = num_red \/ (num_red + num_blue);<\/p>\n<p>for(i=1;i&lt;runs;i++)<br \/>\n{<\/p>\n<p>\/\/ Random number on unit interval<br \/>\nr_n = Math.random();<\/p>\n<p>\tif (r_n &gt; red_share[i-1])<br \/>\n\t{<br \/>\n\tnum_blue = num_blue + extra_extra;<br \/>\n\t\t\tif(details){document.write(&quot;Blue.&quot;);}<br \/>\n\t}<\/p>\n<p>\tif (r_n &lt;= red_share[i-1])<br \/>\n\t{<br \/>\n\tnum_red = num_red + 1;<br \/>\n\t\t\tif(details){document.write(&quot;Red.&quot;);}<br \/>\n\t}<\/p>\n<p>red_share[i] = num_red \/ (num_red + num_blue);<br \/>\n\t\t\tif(details){document.write(&quot; Red Share = &quot; + num_red + &quot;\/(&quot; + num_red + &quot; + &quot; + num_blue + &quot;) = &quot; +<\/p>\n<p>red_share[i]);}<\/p>\n<p>document.write(&quot;&lt;div style=&#8217;background-color:blue;height:10;width:400;&#8217;&gt;&quot;);<br \/>\ndocument.write(&quot;&lt;div style=&#8217;background-color:red;height:10;width:&quot; + red_share[i]*400 + &quot;;&gt;&lt;\/div&gt;&quot;);<br \/>\ndocument.write(&quot;&lt;\/div&gt;&lt;div style=&#8217;background-color:white;height:5;width:400;&#8217;&gt;&lt;\/div&gt;&quot;);<br \/>\n}<\/p>\n<p>var total = 0;<br \/>\nvar mean = 0;<br \/>\nfor(j=0;j&lt;runs;j++)<br \/>\n{total = total+red_share[j]}<br \/>\nmean = total\/runs;<br \/>\nif(mean_details){document.write(&quot;Mean of red share: &quot; + mean);}<br \/>\n&lt;\/script&gt;&lt;\/body&gt;&lt;\/html&gt;<br \/>\n[\/sourcecode]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had to present on path dependence recently. Paul David&#8217;s famous QWERTY paper (American Economic Review P&amp;P, 1985) was covered, as was Paul Pierson&#8217;s Increasing Returns, Path Dependence, and the Study of Politics (American Political&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-860","post","type-post","status-publish","format-standard","hentry","category-economics"],"_links":{"self":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts\/860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/comments?post=860"}],"version-history":[{"count":0,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/posts\/860\/revisions"}],"wp:attachment":[{"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/media?parent=860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/categories?post=860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hargaden.com\/enda\/wp-json\/wp\/v2\/tags?post=860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}