<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Weiqi Gao&#039;s Observations - groovy tag</title>
  <link>http://www.weiqigao.com/blog/tags/groovy/</link>
  <description>Sharing My Experience...</description>
  <language>en</language>
  <copyright>Weiqi Gao</copyright>
  <lastBuildDate>Fri, 11 May 2012 12:48:36 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  <image>
    <url>http://pebble.sourceforge.net/common/images/powered-by-pebble.gif</url>
    <title>Weiqi Gao&#039;s Observations</title>
    <link>http://www.weiqigao.com/blog/</link>
  </image>
  
  
  <item>
    <title>Jeff Brown&#039;s Probability Quiz: What Are The Chances ...</title>
    <link>http://www.weiqigao.com/blog/2009/08/03/jeff_browns_probability_quiz_what_are_the_chances.html</link>
    
      
        <description>
          &lt;p&gt;... Jeff is of course talking about &lt;a href= &#034;http://javajeff.blogspot.com/2009/07/grails-springsource-and-clojure.html&#034; &gt;Grails, SpringSource, and Clojure&lt;/a&gt;.  Near the end of the post, Jeff challenged his readers with a probability brain teaser:&lt;/p&gt;

&lt;blockquote&gt;
&lt;a href= &#034;http://javajeff.blogspot.com/2009/07/grails-springsource-and-clojure.html&#034; &gt;Jeff Brown&lt;/a&gt;: I am honestly not sure how many technologists work for SpringSource but for the sake of having a number to work with, lets say there are 50.  Lets also say that Clojure is interesting enough that 15% of JVM developers are interested in learning more about it.  I can&#039;t back that number up with any research, lets just go with it.  If 15% of JVM developers are interested in the language and you take a random group of 50 JVM developers (the 50 in question are certainly not 50 random developers, these are the edge cutters which probably makes them more likely to be interested in keeping an eye on what is new, but work with the idea that they are random)... Do the math.  No, really... Do the math.  Do the math to figure out the likelihood that 3 of the 50 would be interested in Clojure.  I challenge you to do the math in Clojure and post your solution in a comment here.  Do it in Scala,  Do it in Groovy.  Pick a JVM language and do the math.&lt;br&gt;
&lt;/blockquote&gt;

&lt;p&gt;My answer got a little bit too long for a comment on Jeff&#039;s blog.  So I&#039;m putting it here.&lt;/p&gt;

&lt;p&gt;First of all, Jeff, its probability, not mathematics.  They branched off a long time ago, just like computer science branched off more recently.  It&#039;s nevertheless an interesting problem to ponder about in a Saturday morning.  It also helps that I&#039;m married to a statistician.  Here&#039;s how the conversation went:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Me&lt;/b&gt;: Jeff Brown blogged about an interesting probability problem.  (&lt;em&gt;Reading the problem&lt;/em&gt;)&lt;br/&gt;
&lt;b&gt;Her&lt;/b&gt;: The problem is not complete.  He didn&#039;t mention how many JVM developers there are.  The answer probably depends on it.&lt;br/&gt;
&lt;b&gt;Me&lt;/b&gt;: For the sake of argument, let&#039;s say there are 100 of them.&lt;br/&gt;
&lt;b&gt;Her&lt;/b&gt;: OK.  Then the problem is equivalent to the following:&lt;/p&gt;

&lt;p style=&#034;margin-left:3em&#034;&gt;&lt;b&gt;Problem&lt;/b&gt;: There is a jar of 100 balls. Among them 85 balls are black, and 15 balls are read.  A batch of 50 balls are selected at random from the jar.  A group of 3 balls are selected at random from the batch.  What is the probability that all 3 balls in the selected group are red?&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Me&lt;/b&gt;: That sounds like a probability problem the students could tackle.&lt;br/&gt;
&lt;b&gt;Her&lt;/b&gt;: You analyze the scenario into more concrete cases: What if the batch of 50 contains no red balls? 1 red ball? 2? 3?, ... Use the &lt;a href= &#034;http://en.wikipedia.org/wiki/Law_of_total_probability&#034; &gt;Law of Total Probability&lt;/a&gt;.  Here&#039;s the book!&lt;p&gt;

&lt;p&gt;(&lt;em&gt;Saturday morning turned into Sunday evening, and we finally have a chance to work the problem out.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;We&#039;ll solve the probability problem by hand and reduce it to a computation problem that is as simple as possible, and then use the computing machinery.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Solution&lt;/b&gt;: We use the following letters to denote the various quantities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;i&gt;n&lt;/i&gt;&amp;mdash;the total number of balls&lt;/li&gt;
&lt;li&gt;&lt;i&gt;r&lt;/i&gt;&amp;mdash;the number of red balls&lt;/li&gt;
&lt;li&gt;&lt;i&gt;m&lt;/i&gt;&amp;mdash;the number of balls in the batch&lt;/li&gt;
&lt;li&gt;&lt;i&gt;k&lt;/i&gt;&amp;mdash;the number of balls in the group&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The event of selecting a batch of &lt;i&gt;m&lt;/i&gt; balls and then selecting exactly &lt;i&gt;k&lt;/i&gt; red balls from the &lt;i&gt;m&lt;/i&gt; can be decomposed to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;selecting a batch of &lt;i&gt;m&lt;/i&gt; balls &lt;span style=&#034;color:red&#034;&gt;containing exactly &lt;i&gt;k&lt;/i&gt; red balls&lt;/span&gt; and then selecting exactly &lt;i&gt;k&lt;/i&gt; red balls from the &lt;i&gt;m&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;selecting a batch of &lt;i&gt;m&lt;/i&gt; balls &lt;span style=&#034;color:red&#034;&gt;containing exactly &lt;i&gt;k + 1&lt;/i&gt; red balls&lt;/span&gt; and then selecting exactly &lt;i&gt;k&lt;/i&gt; red balls from the &lt;i&gt;m&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;li&gt;selecting a batch of &lt;i&gt;m&lt;/i&gt; balls &lt;span style=&#034;color:red&#034;&gt;containing exactly &lt;i&gt;r&lt;/i&gt; red balls&lt;/span&gt; and then selecting exactly &lt;i&gt;k&lt;/i&gt; red balls from the &lt;i&gt;m&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The law of total probability dictates that the probability of out target event is the sum of the products of the probabilities of each of the successive events in the above list:&lt;/p&gt;

&lt;center&gt;
&lt;i&gt;P = &lt;big&gt;&amp;Sigma;&lt;/big&gt;&lt;sub&gt;i = k&lt;/sub&gt;&lt;sup&gt;r&lt;/sup&gt; P(n, r; m, i) P(m, i; k, k)&lt;/i&gt;
&lt;/center&gt;

&lt;p&gt;where &lt;i&gt;P(n, r; m, i)&lt;/i&gt; is the probability of selecting &lt;i&gt;m&lt;/i&gt; balls from a jar of &lt;i&gt;n&lt;/i&gt; balls containing &lt;i&gt;r&lt;/i&gt; red balls and having exactly &lt;i&gt;i&lt;/i&gt; red balls in the selected batch.  The probability of this event is given by the formula&lt;/p&gt;

&lt;center&gt;
&lt;i&gt;P(n, r; m, i) = C(r, i) C(n - r, m - i) / C(n, m)&lt;/i&gt;
&lt;/center&gt;

&lt;p&gt;where &lt;i&gt;C(r, i)&lt;/i&gt; is the binomial coefficient.&lt;/p&gt;

&lt;p&gt;With the problem thus &lt;i&gt;solved&lt;/i&gt;, I went ahead to &#034;&lt;i&gt;Pick a JVM language and do the math.&lt;/i&gt;&#034;  Guess which language I picked? Java.  Yes, Java.  Not Clojure.  Not Scala.  Not Groovy, but the old dependable Java.  &#034;Why?&#034; you ask.  Because doing it in any other language would have taken me longer to get the results out.&lt;/p&gt;

&lt;p&gt;Here are the classes I wrote:&lt;/p&gt;

&lt;style type=&#034;text/css&#034;&gt;
.ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }
.s0 { color: rgb(0,0,128); font-weight: bold; }
.s1 { }
.s2 { color: rgb(0,128,0); font-weight: bold; }
.s3 { color: rgb(0,0,255); }
&lt;/style&gt;

&lt;TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH=&#034;100%&#034; BGCOLOR=&#034;#C0C0C0&#034; &gt;
&lt;TR&gt;&lt;TD&gt;&lt;CENTER&gt;
&lt;FONT FACE=&#034;Arial, Helvetica&#034; COLOR=&#034;#000000&#034;&gt;
Foo.java&lt;/FONT&gt;
&lt;/center&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;
&lt;pre&gt;
&lt;a name=&#034;l1&#034;&gt;&lt;span class=&#034;ln&#034;&gt;1    &lt;/span&gt;&lt;/a&gt;&lt;span class=&#034;s0&#034;&gt;public class &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;Foo { 
&lt;a name=&#034;l2&#034;&gt;&lt;span class=&#034;ln&#034;&gt;2    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;private int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;n; 
&lt;a name=&#034;l3&#034;&gt;&lt;span class=&#034;ln&#034;&gt;3    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;private int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;r; 
&lt;a name=&#034;l4&#034;&gt;&lt;span class=&#034;ln&#034;&gt;4    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;private int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;m; 
&lt;a name=&#034;l5&#034;&gt;&lt;span class=&#034;ln&#034;&gt;5    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;private int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;k; 
&lt;a name=&#034;l6&#034;&gt;&lt;span class=&#034;ln&#034;&gt;6    &lt;/span&gt;&lt;/a&gt; 
&lt;a name=&#034;l7&#034;&gt;&lt;span class=&#034;ln&#034;&gt;7    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;Foo(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;n, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;r, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;m, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;k) { 
&lt;a name=&#034;l8&#034;&gt;&lt;span class=&#034;ln&#034;&gt;8    &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;this&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;.n = n; 
&lt;a name=&#034;l9&#034;&gt;&lt;span class=&#034;ln&#034;&gt;9    &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;this&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;.r = r; 
&lt;a name=&#034;l10&#034;&gt;&lt;span class=&#034;ln&#034;&gt;10   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;this&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;.m = m; 
&lt;a name=&#034;l11&#034;&gt;&lt;span class=&#034;ln&#034;&gt;11   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;this&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;.k = k; 
&lt;a name=&#034;l12&#034;&gt;&lt;span class=&#034;ln&#034;&gt;12   &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l13&#034;&gt;&lt;span class=&#034;ln&#034;&gt;13   &lt;/span&gt;&lt;/a&gt; 
&lt;a name=&#034;l14&#034;&gt;&lt;span class=&#034;ln&#034;&gt;14   &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public static double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;C(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;n, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;k) { 
&lt;a name=&#034;l15&#034;&gt;&lt;span class=&#034;ln&#034;&gt;15   &lt;/span&gt;&lt;/a&gt;    System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;C(&amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ n + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;, &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ k + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;) called.&amp;quot;&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;); 
&lt;a name=&#034;l16&#034;&gt;&lt;span class=&#034;ln&#034;&gt;16   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;prod = &lt;/span&gt;&lt;span class=&#034;s3&#034;&gt;1.0&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;; 
&lt;a name=&#034;l17&#034;&gt;&lt;span class=&#034;ln&#034;&gt;17   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;for &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;i = &lt;/span&gt;&lt;span class=&#034;s3&#034;&gt;1&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;; i &amp;lt;= k; i++) { 
&lt;a name=&#034;l18&#034;&gt;&lt;span class=&#034;ln&#034;&gt;18   &lt;/span&gt;&lt;/a&gt;      prod *= ((&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;) (n - i + &lt;/span&gt;&lt;span class=&#034;s3&#034;&gt;1&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;)) / ((&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;) i); 
&lt;a name=&#034;l19&#034;&gt;&lt;span class=&#034;ln&#034;&gt;19   &lt;/span&gt;&lt;/a&gt;      System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;\t&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;Iteration &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ i + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;: prod = &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ prod); 
&lt;a name=&#034;l20&#034;&gt;&lt;span class=&#034;ln&#034;&gt;20   &lt;/span&gt;&lt;/a&gt;    } 
&lt;a name=&#034;l21&#034;&gt;&lt;span class=&#034;ln&#034;&gt;21   &lt;/span&gt;&lt;/a&gt;    System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;\t&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;Returning &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ prod); 
&lt;a name=&#034;l22&#034;&gt;&lt;span class=&#034;ln&#034;&gt;22   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;return &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;prod; 
&lt;a name=&#034;l23&#034;&gt;&lt;span class=&#034;ln&#034;&gt;23   &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l24&#034;&gt;&lt;span class=&#034;ln&#034;&gt;24   &lt;/span&gt;&lt;/a&gt; 
&lt;a name=&#034;l25&#034;&gt;&lt;span class=&#034;ln&#034;&gt;25   &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public static double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;P(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;n, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;r, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;m, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;i) { 
&lt;a name=&#034;l26&#034;&gt;&lt;span class=&#034;ln&#034;&gt;26   &lt;/span&gt;&lt;/a&gt;    System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;P(&amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ n + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;, &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ r + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;, &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ m + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;, &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ i + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;) called.&amp;quot;&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;); 
&lt;a name=&#034;l27&#034;&gt;&lt;span class=&#034;ln&#034;&gt;27   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;retVal = C(r, i) * C(n - r, m - i) / C(n, m); 
&lt;a name=&#034;l28&#034;&gt;&lt;span class=&#034;ln&#034;&gt;28   &lt;/span&gt;&lt;/a&gt;    System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;\t&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;Returning &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ retVal); 
&lt;a name=&#034;l29&#034;&gt;&lt;span class=&#034;ln&#034;&gt;29   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;return &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;retVal; 
&lt;a name=&#034;l30&#034;&gt;&lt;span class=&#034;ln&#034;&gt;30   &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l31&#034;&gt;&lt;span class=&#034;ln&#034;&gt;31   &lt;/span&gt;&lt;/a&gt; 
&lt;a name=&#034;l32&#034;&gt;&lt;span class=&#034;ln&#034;&gt;32   &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;P() { 
&lt;a name=&#034;l33&#034;&gt;&lt;span class=&#034;ln&#034;&gt;33   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;sum = &lt;/span&gt;&lt;span class=&#034;s3&#034;&gt;0.0&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;; 
&lt;a name=&#034;l34&#034;&gt;&lt;span class=&#034;ln&#034;&gt;34   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;for &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;i = k; i &amp;lt;= r; i++) { 
&lt;a name=&#034;l35&#034;&gt;&lt;span class=&#034;ln&#034;&gt;35   &lt;/span&gt;&lt;/a&gt;      sum += P(n, r, m, i) * P(m, i, k, k); 
&lt;a name=&#034;l36&#034;&gt;&lt;span class=&#034;ln&#034;&gt;36   &lt;/span&gt;&lt;/a&gt;    } 
&lt;a name=&#034;l37&#034;&gt;&lt;span class=&#034;ln&#034;&gt;37   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;return &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;sum; 
&lt;a name=&#034;l38&#034;&gt;&lt;span class=&#034;ln&#034;&gt;38   &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l39&#034;&gt;&lt;span class=&#034;ln&#034;&gt;39   &lt;/span&gt;&lt;/a&gt;} 
&lt;a name=&#034;l40&#034;&gt;&lt;span class=&#034;ln&#034;&gt;40   &lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH=&#034;100%&#034; BGCOLOR=&#034;#C0C0C0&#034; &gt;
&lt;TR&gt;&lt;TD&gt;&lt;CENTER&gt;
&lt;FONT FACE=&#034;Arial, Helvetica&#034; COLOR=&#034;#000000&#034;&gt;
Main.java&lt;/FONT&gt;
&lt;/center&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;
&lt;pre&gt;
&lt;a name=&#034;l1&#034;&gt;&lt;span class=&#034;ln&#034;&gt;1    &lt;/span&gt;&lt;/a&gt;&lt;span class=&#034;s0&#034;&gt;public class &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;Main { 
&lt;a name=&#034;l2&#034;&gt;&lt;span class=&#034;ln&#034;&gt;2    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public static void &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;main(String[] args) { 
&lt;a name=&#034;l3&#034;&gt;&lt;span class=&#034;ln&#034;&gt;3    &lt;/span&gt;&lt;/a&gt;    Foo foo = &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;new &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;Foo(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;100&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;15&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;50&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;3&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;); 
&lt;a name=&#034;l4&#034;&gt;&lt;span class=&#034;ln&#034;&gt;4    &lt;/span&gt;&lt;/a&gt;    System.out.println(foo.P()); 
&lt;a name=&#034;l5&#034;&gt;&lt;span class=&#034;ln&#034;&gt;5    &lt;/span&gt;&lt;/a&gt;    System.out.println(Foo.P(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;100&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;15&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;3&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;3&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;)); 
&lt;a name=&#034;l6&#034;&gt;&lt;span class=&#034;ln&#034;&gt;6    &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l7&#034;&gt;&lt;span class=&#034;ln&#034;&gt;7    &lt;/span&gt;&lt;/a&gt;} 
&lt;a name=&#034;l8&#034;&gt;&lt;span class=&#034;ln&#034;&gt;8    &lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;According to my calculation, the probability of the event that Jeff described is 0.0028138528138528123 or 0.28% assuming there are 100 JVM programmers.&lt;/p&gt;

&lt;p&gt;There are two points that&#039;s worth mentioning:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The condition of there being 50 SpringSource programmers can be actually shown to be superfluous.  And the calculation can therefore be simplified to merely that of &lt;i&gt;P(100, 15; 3, 3)&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;It can be shown that &lt;i&gt;P(n, r, k, k)&lt;/i&gt; tends to &lt;i&gt;(r/n)^k&lt;/i&gt; as &lt;i&gt;n&lt;/i&gt; and &lt;i&gt;r&lt;/i&gt; grows to infinity while the ratio &lt;i&gt;r/n&lt;/i&gt; is kept constant.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(&lt;em&gt;Now it&#039;s Monday morning.  After staring at the 0.28% probability for a while, I realized something...&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;What I calculated is not what Jeff wanted.  He clearly said he wanted &#034;the likelihood that 3 of the 50 would be interested in Clojure&#034;, or, in our language, &#034;the probability of selecting 50 balls that contains exactly 3 red balls.&#034;  That&#039;s &lt;i&gt;P(100, 15; 50, 3)&lt;/i&gt;, which is 0.009392308830476828 or 0.94%.&lt;/p&gt;  A simple experiment suggests that as &lt;i&gt;n&lt;/i&gt; and &lt;i&gt;r&lt;/i&gt; grows to infinity while the ratio &lt;i&gt;r/n&lt;/i&gt; is kept constant, the probability tends to 3.19%.&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;It&#039;s Monday evening now.  And I again have doubts about if I solved the right problem.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;Did Jeff mean &#034;the likelihood that at least 3 of the 50 would be interested in Clojure&#034;?  After all, from the description of the story I can&#039;t infer that the other 47 are not interested in Clojure.&lt;/p&gt;

&lt;p&gt;The probability of that would be&lt;/p&gt;

&lt;center&gt;
&lt;i&gt;&lt;big&gt;&amp;Sigma&lt;/big&gt;&lt;sub&gt;i = 3&lt;/sub&gt;&lt;sup&gt;15&lt;/sup&gt; P(100, 15; 50, i)&lt;/i&gt;
&lt;/center&gt;

&lt;p&gt;which turns out to be 99.8%.&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;I think I&#039;ll turn my work in now.  Or I&#039;ll start doubt my solution again tomorrow morning.&lt;/em&gt;)&lt;/p&gt;


        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2009/08/03/jeff_browns_probability_quiz_what_are_the_chances.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2009/08/03/jeff_browns_probability_quiz_what_are_the_chances.html</guid>
    <pubDate>Tue, 04 Aug 2009 02:40:08 GMT</pubDate>
  </item>
  
  <item>
    <title>Your jirb, groovysh and clj Commands Doesn&#039;t Work In Cygwin</title>
    <link>http://www.weiqigao.com/blog/2008/04/08/your_jirb_groovysh_and_clj_commands_doesnt_work_in_cygwin.html</link>
    
      
        <description>
          &lt;p&gt;This is another one of those &lt;em&gt;making things work in Cygwin&lt;/em&gt; posts.  You can safely skip this post if you are not married to &lt;a href= &#034;http://cygwin.com/&#034; &gt;Cygwin&lt;/a&gt;, or have not followed my &lt;a href= &#034;http://www.weiqigao.com/blog/2006/08/14/ten_steps_to_higher_cygwin_productivity.html&#034; &gt;Ten Steps To Higher Cygwin Productivity&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The issue this time is with a little library called &lt;a href= &#034;http://jline.sourceforge.net/&#034; &gt;JLine&lt;/a&gt;, which is a BSD licensed library that brings GNU readline style command line editing to Java.  I first learned about it through the &lt;a href= &#034;http://clojure.org/reference/getting_started.html&#034; &gt;Clo&lt;i&gt;j&lt;/i&gt;ure&lt;/a&gt; programming language (a LISP with immutable variables and software transactional memory (STM)).&lt;/p&gt;

&lt;p&gt;And of course, it doesn&#039;t work in my Cygwin xterm window.  A little Googling landed me at &lt;a href= &#034;http://sourceforge.net/tracker/index.php?func=detail&amp;aid=1822900&amp;group_id=64033&amp;atid=506056&#034; &gt;JLine issue-1822900&lt;/a&gt;.  A little looking around in the JLine source and some experiments later, I had a partial workaround, which I added as a comment to the issue.&lt;/p&gt;

&lt;p&gt;While doing the Google search, I also noticed that both Groovy and JRuby use JLine to some extend (in jirb and groovysh).  Sure enough, the same issue showed up in the Groovy JIRA as &lt;a href= &#034;http://jira.codehaus.org/browse/GROOVY-2584&#034; &gt; 	 GROOVY-2584&lt;/a&gt;, to which I added the workaround.&lt;/p&gt;

&lt;p&gt;A fresh download of the just released JRuby 1.1 showed the same symptom.  The JLine workaround can be applied directly to the &lt;tt&gt;jruby&lt;/tt&gt; script, which, thankfully, does contain infrastructure for Cygwin support.&lt;/p&gt;

&lt;p&gt;There are other issues with JLine on a Cygwin xterm.  But the workaround at least makes the repls workable.&lt;/p&gt;

&lt;p&gt;The part that makes me uncomfortable is that I don&#039;t see how this workaround can be incorporated into a patch for JLine.  In that regard, I concede Cygwin is a somewhat hacky platform.  (Jonathan and Adam, if you are still reading...)&lt;/p&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2008/04/08/your_jirb_groovysh_and_clj_commands_doesnt_work_in_cygwin.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2008/04/08/your_jirb_groovysh_and_clj_commands_doesnt_work_in_cygwin.html</guid>
    <pubDate>Tue, 08 Apr 2008 13:28:08 GMT</pubDate>
  </item>
  
  <item>
    <title>Java News Brief (JNB): Introduction to Grails</title>
    <link>http://www.weiqigao.com/blog/2007/03/06/java_news_brief_jnb_introduction_to_grails.html</link>
    
      
        <description>
          &lt;p&gt;The &lt;a href= &#034;http://www.ociweb.com/jnb/jnbMar2007.html&#034; &gt;March issue&lt;/a&gt; of the &lt;a href= &#034;http://www.ociweb.com/jnb/&#034; &gt;Java News Brief (JNB)&lt;/a&gt; is out.  &lt;a href= &#034;http://javajeff.blogspot.com/&#034; &gt;Jeff Brown&lt;/a&gt; gives an introduction to &lt;a href= &#034;http://grails.org/&#034; &gt;Grails&lt;/a&gt;&amp;mdash;the &#034;framework for agile web development using Groovy and Java.&#034;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href= &#034;http://www.ociweb.com/jnb/jnbMar2007.html&#034; &gt;Jeff Brown&lt;/a&gt;: Grails is a remarkably flexible framework while maintaining an ease of use that is unparalleled by other web application frameworks targeted for the JVM. Use the exercises in this article as a jump start with Grails and have fun exploring the possibilities.&lt;/p&gt;
&lt;/blockquote&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2007/03/06/java_news_brief_jnb_introduction_to_grails.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2007/03/06/java_news_brief_jnb_introduction_to_grails.html</guid>
    <pubDate>Tue, 06 Mar 2007 17:49:08 GMT</pubDate>
  </item>
  
  </channel>
</rss>

