<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Random Hacks: Refactoring probability distributions, part 2: Random sampling</title>
    <link>http://www.randomhacks.net/articles/2007/02/21/randomly-sampled-distributions</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"Refactoring probability distributions, part 2: Random sampling" by Eric</title>
      <description>&lt;p&gt;I suspect that newtype-deriving will work quite nicely. Thanks!&lt;/p&gt;


	&lt;p&gt;It might also be desirable to use &lt;a href="http://haskell.org/haskellwiki/New_monads/MonadRandom"&gt;the Rand monad from the Haskell wiki&lt;/a&gt;, or perhaps the splittable version (which might make it feasible to run sampling computations in parallel?).&lt;/p&gt;</description>
      <pubDate>Tue, 27 Feb 2007 10:44:23 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8dcd7edb-9a98-4a33-85a2-f6a91c95d381</guid>
      <link>http://www.randomhacks.net/articles/2007/02/21/randomly-sampled-distributions#comment-319</link>
    </item>
    <item>
      <title>"Refactoring probability distributions, part 2: Random sampling" by David House</title>
      <description>&lt;p&gt;Couldn&amp;#8217;t you just use newtype-deriving to make Rand a monad, like you did for making Prob an instance of Num in the first part? Also, &lt;code&gt;sequence (replicate f xs)&lt;/code&gt; is just &lt;code&gt;replicateM f xs&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Great articles, by the way!&lt;/p&gt;</description>
      <pubDate>Tue, 27 Feb 2007 10:05:28 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:632436a6-3b4c-47c8-8a33-f7f0eea3107f</guid>
      <link>http://www.randomhacks.net/articles/2007/02/21/randomly-sampled-distributions#comment-318</link>
    </item>
    <item>
      <title>Refactoring probability distributions, part 2: Random sampling</title>
      <description>&lt;p&gt;In &lt;a href="http://www.randomhacks.net/articles/2007/02/21/refactoring-probability-distributions"&gt;Part 1&lt;/a&gt;, we cloned &lt;a href="http://web.engr.oregonstate.edu/~erwig/pfp/"&gt;PFP&lt;/a&gt;, a library for computing with probability distributions. PFP represents a distribution as a list of possible values, each with an associated probability.&lt;/p&gt;

&lt;p&gt;But in the real world, things aren&amp;#8217;t always so easy.  What if we wanted to pick  a random number between 0 and 1? Our previous implementation would break, because there&amp;#8217;s an infinite number of values between 0 and 1&amp;#8212;they don&amp;#8217;t exactly fit in a list.&lt;/p&gt;

&lt;p&gt;As it turns out, Sungwoo Park and colleagues found &lt;a href="http://citeseer.ist.psu.edu/752237.html"&gt;an elegant solution&lt;/a&gt; to this problem. They represented probability distributions as sampling functions, resulting in something called the &amp;lambda;&lt;sub&gt;&amp;#x25EF;&lt;/sub&gt; calculus. (I have no idea how to pronounce this!)&lt;/p&gt;

&lt;p&gt;With a little bit of hacking, we can use their sampling functions as a drop-in replacement for PFP.&lt;/p&gt;

&lt;h3&gt;A common interface&lt;/h3&gt;

&lt;p&gt;Since we will soon have two ways to represent probability distributions, we need to define a common interface.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='hs-keyword'&gt;type&lt;/span&gt; &lt;span class='hs-conid'&gt;Weight&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&lt;/span&gt; &lt;span class='hs-conid'&gt;Float&lt;/span&gt;

&lt;span class='hs-keyword'&gt;class&lt;/span&gt; &lt;span class='hs-layout'&gt;(&lt;/span&gt;&lt;span class='hs-conid'&gt;Functor&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt;&lt;span class='hs-layout'&gt;,&lt;/span&gt; &lt;span class='hs-conid'&gt;Monad&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt;&lt;span class='hs-layout'&gt;)&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='hs-conid'&gt;Dist&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt; &lt;span class='hs-keyword'&gt;where&lt;/span&gt;
  &lt;span class='hs-varid'&gt;weighted&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;::&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;[&lt;/span&gt;&lt;span class='hs-layout'&gt;(&lt;/span&gt;&lt;span class='hs-varid'&gt;a&lt;/span&gt;&lt;span class='hs-layout'&gt;,&lt;/span&gt; &lt;span class='hs-conid'&gt;Weight&lt;/span&gt;&lt;span class='hs-layout'&gt;)&lt;/span&gt;&lt;span class='hs-keyglyph'&gt;]&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt; &lt;span class='hs-varid'&gt;a&lt;/span&gt;

&lt;span class='hs-definition'&gt;uniform&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;::&lt;/span&gt; &lt;span class='hs-conid'&gt;Dist&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;[&lt;/span&gt;&lt;span class='hs-varid'&gt;a&lt;/span&gt;&lt;span class='hs-keyglyph'&gt;]&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt; &lt;span class='hs-varid'&gt;a&lt;/span&gt;
&lt;span class='hs-definition'&gt;uniform&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&lt;/span&gt; &lt;span class='hs-varid'&gt;weighted&lt;/span&gt; &lt;span class='hs-varop'&gt;.&lt;/span&gt; &lt;span class='hs-varid'&gt;map&lt;/span&gt; &lt;span class='hs-layout'&gt;(&lt;/span&gt;&lt;span class='hs-keyglyph'&gt;\&lt;/span&gt;&lt;span class='hs-varid'&gt;x&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='hs-layout'&gt;(&lt;/span&gt;&lt;span class='hs-varid'&gt;x&lt;/span&gt;&lt;span class='hs-layout'&gt;,&lt;/span&gt; &lt;span class='hs-num'&gt;1&lt;/span&gt;&lt;span class='hs-layout'&gt;)&lt;/span&gt;&lt;span class='hs-layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The function &lt;code&gt;uniform&lt;/code&gt; will create an equally-weighted distribution from a list of values. Using this API, we can represent a two-child family as follows:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='hs-keyword'&gt;data&lt;/span&gt; &lt;span class='hs-conid'&gt;Child&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&lt;/span&gt; &lt;span class='hs-conid'&gt;Girl&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;|&lt;/span&gt; &lt;span class='hs-conid'&gt;Boy&lt;/span&gt;
  &lt;span class='hs-keyword'&gt;deriving&lt;/span&gt; &lt;span class='hs-layout'&gt;(&lt;/span&gt;&lt;span class='hs-conid'&gt;Show&lt;/span&gt;&lt;span class='hs-layout'&gt;,&lt;/span&gt; &lt;span class='hs-conid'&gt;Eq&lt;/span&gt;&lt;span class='hs-layout'&gt;,&lt;/span&gt; &lt;span class='hs-conid'&gt;Ord&lt;/span&gt;&lt;span class='hs-layout'&gt;)&lt;/span&gt;

&lt;span class='hs-definition'&gt;child&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;::&lt;/span&gt; &lt;span class='hs-conid'&gt;Dist&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt; &lt;span class='hs-conid'&gt;Child&lt;/span&gt;
&lt;span class='hs-definition'&gt;child&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&lt;/span&gt; &lt;span class='hs-varid'&gt;uniform&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;[&lt;/span&gt;&lt;span class='hs-conid'&gt;Girl&lt;/span&gt;&lt;span class='hs-layout'&gt;,&lt;/span&gt; &lt;span class='hs-conid'&gt;Boy&lt;/span&gt;&lt;span class='hs-keyglyph'&gt;]&lt;/span&gt;

&lt;span class='hs-definition'&gt;family&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;::&lt;/span&gt; &lt;span class='hs-conid'&gt;Dist&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='hs-varid'&gt;d&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;[&lt;/span&gt;&lt;span class='hs-conid'&gt;Child&lt;/span&gt;&lt;span class='hs-keyglyph'&gt;]&lt;/span&gt;
&lt;span class='hs-definition'&gt;family&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&lt;/span&gt; &lt;span class='hs-keyword'&gt;do&lt;/span&gt;
  &lt;span class='hs-varid'&gt;child1&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='hs-varid'&gt;child&lt;/span&gt;
  &lt;span class='hs-varid'&gt;child2&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='hs-varid'&gt;child&lt;/span&gt;
  &lt;span class='hs-varid'&gt;return&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;[&lt;/span&gt;&lt;span class='hs-varid'&gt;child1&lt;/span&gt;&lt;span class='hs-layout'&gt;,&lt;/span&gt; &lt;span class='hs-varid'&gt;child2&lt;/span&gt;&lt;span class='hs-keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, we need to implement this API two different ways: Once with lists, and a second time with sampling functions.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.randomhacks.net/articles/2007/02/21/randomly-sampled-distributions"&gt;Read More&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 21 Feb 2007 23:53:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:48ce73c7-a197-4909-af77-6da8d71db1a8</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2007/02/21/randomly-sampled-distributions</link>
      <category>Haskell</category>
      <category>Math</category>
      <category>Monads</category>
      <category>Probability</category>
      <category>ProbabilityMonads</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/302</trackback:ping>
    </item>
  </channel>
</rss>
