<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>My Weblog</title>
	<atom:link href="http://rkje.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rkje.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Fri, 02 Jan 2009 08:25:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='rkje.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>My Weblog</title>
		<link>http://rkje.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://rkje.wordpress.com/osd.xml" title="My Weblog" />
	<atom:link rel='hub' href='http://rkje.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Boy or Girl?</title>
		<link>http://rkje.wordpress.com/2009/01/01/boy-or-girl/</link>
		<comments>http://rkje.wordpress.com/2009/01/01/boy-or-girl/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 11:03:19 +0000</pubDate>
		<dc:creator>rkj</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[math probability]]></category>

		<guid isPermaLink="false">http://rkje.wordpress.com/?p=7</guid>
		<description><![CDATA[UPDATE: OK, I was wrong, do not read the post . And remember you should not do the math if you are after a party . Recently Jeff Atwood posted The Problem of the Unfinished Game. It is a simple question: Let&#8217;s say, hypothetically speaking, you met someone who told you they had two children, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rkje.wordpress.com&amp;blog=4549847&amp;post=7&amp;subd=rkje&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE</strong>: OK, I was wrong, do not read the post <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . And remember you should not do the math if you are after a party <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>Recently Jeff Atwood posted <a href="http://www.codinghorror.com/blog/archives/001203.html">The Problem of the Unfinished Game</a>.</p>
<p>It is a simple question:</p>
<p><em>Let&#8217;s say, hypothetically speaking, you met someone who told you they had two children, and one of them is a girl. What are the odds that person has a boy and a girl? </em></p>
<p>Using common sense you can tell that the answer is 50%. Every child is completely independent event, and assuming that probability of having a boy or a girl is equal to 50% you have to guess only the second child. It can be boy or a girl. Each have 50%. You want boy, so the answer is 50%.</p>
<p>In the comments there are many opinions. One of them is summed up here: <a href="http://mathforum.org/dr.math/faq/faq.boy.girl.html">Boy or Girl?</a>.</p>
<p>It can be proven wrong by reduction to absurd. The answer states that if person says the gender of one of her two children then we know with higher probability that she has a mixed gender children. It does not matter if she says it is a boy, or it is a girl. If you know one gender then the probability is 2/3. It is plain stupid.</p>
<p>If she didn’t tell me I would still know that her child is either boy or a girl and this changes nothing to me. The gender of the second child is still not known. And it still can be boy or a girl equally probably.</p>
<p>I think that for this particular resoning the error is in the sample space:<br />
{BB, BG, GB, GG} &#8211; it is not it. We are not interested in order of births. We should have sets: {{B, B}, {B, G}, {G, G}}, where {B, B} and {G, G} are 25% and {B, G} is 50% probable. But, if I know the have a girl then sample space is: {{B, G}, {G, G}}. And the probabilities are equal not 2/3 and 1/3 because only the one child is unknown (so it is the same as the space would be: {B, G}).</p>
<p>The reasoning with the tree is also wrong. All probabilities have to change because of our knowledge. We can’t just divide probability equaly between the leaves… We know that she have girl. And we can back-propagate that knowledge to the probability tree. And if I am correct we can say that the her first child was a girl with 0.75 probability. But do the math yourself <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Bet</h3>
<p>And if I am wrong, here is the bet:<br />
I toss a coin two times. I tell you one of the outcomes. You always bet the that the other outcome is different. If I win you pay me 60 cents, If you win I pay you 40 cents. We repeat it 999 times. Because different outcomes are more probable &#8211; 2 of 3 times &#8211; you should win 999 * 2/3 = 666 times and I 333 times. So you get 666 * 0.4 &#8211; 333 * 0.6 = 66.6 dollar. Are you in? I&#8217;d be glad, because I am sure I would one hundred backs easily.</p>
<p>And here is an interesting movie in the topic: <a href="http://www.ted.com/index.php/talks/peter_donnelly_shows_how_stats_fool_juries.html">How juries are fooled by statistics</a> <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rkje.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rkje.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rkje.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rkje.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rkje.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rkje.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rkje.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rkje.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rkje.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rkje.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rkje.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rkje.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rkje.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rkje.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rkje.wordpress.com&amp;blog=4549847&amp;post=7&amp;subd=rkje&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rkje.wordpress.com/2009/01/01/boy-or-girl/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab46e9ad76540da32912dedaabc2def8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rkj</media:title>
		</media:content>
	</item>
		<item>
		<title>Use Ruby&#8217;s lambda in for loop with care</title>
		<link>http://rkje.wordpress.com/2007/12/29/use-rubys-lambda-in-for-loop-with-care/</link>
		<comments>http://rkje.wordpress.com/2007/12/29/use-rubys-lambda-in-for-loop-with-care/#comments</comments>
		<pubDate>Sat, 29 Dec 2007 19:27:00 +0000</pubDate>
		<dc:creator>rkj</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://rkje.wordpress.com/2007/12/29/use-rubys-lambda-in-for-loop-with-care/</guid>
		<description><![CDATA[Recently I was reading about planned closures in Java. One of the easy looking examples: public class Test { &#160;private static final int N = 10; &#160;public static void main(String[] args) { &#160;&#160;List&#60;{ =&#62; int}&#62; closures = new ArrayList&#60;{ =&#62; int}&#62;(); &#160;&#160;for (int i = 0; i &#60; N; i++) &#160;&#160;&#160;closures.add( { =&#62; i } [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rkje.wordpress.com&amp;blog=4549847&amp;post=4&amp;subd=rkje&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I was reading about <a href="http://www.javac.info/bloch-closures-controversy.ppt">planned closures in Java</a>. One of the easy looking examples:</p>
<div class="code">
public class Test {<br />
&nbsp;private static final int N = 10;<br />
&nbsp;public static void main(String[] args) {<br />
&nbsp;&nbsp;List&lt;{ =&gt; int}&gt; closures = new ArrayList&lt;{ =&gt; int}&gt;();</p>
<p>&nbsp;&nbsp;for (int i = 0; i &lt; N; i++)<br />
&nbsp;&nbsp;&nbsp;closures.add( { =&gt; i } );</p>
<p>&nbsp;&nbsp;int total = 0;<br />
&nbsp;&nbsp;for ({ =&gt; int} closure : closures)<br />
&nbsp;&nbsp;&nbsp;total += closure.invoke();<br />
&nbsp;&nbsp;System.out.println(total);<br />
&nbsp;}<br />
}
</div>
<p>gave quite unexpected results &#8212; 100 instead of 45. I thought, well it is a Java problem, Ruby sure got it right. But is it? Here is the code:</p>
<div class="code">
procs = []<br />
for i in 1&#8230;10<br />
&nbsp;procs &lt;&lt; Proc.new {i}<br />
end<br />
puts procs.inject(0) { |sum, f| sum + f.call() }
</div>
<p>And guess what &#8211; result is 81. Now a bit confused I started adding some more similar code:</p>
<div class="code">
procs = []<br />
(1&#8230;10).each do |i|<br />
&nbsp;procs &lt;&lt; Proc.new { i }<br />
end<br />
puts procs.inject(0) { |sum, f| sum + f.call() }
</div>
<p>But gosh &#8211; still 81. I started googling and wondering what is wrong. After some time I started over with:</p>
<div class="code">
(1&#8230;10).map { |i| Proc.new {i} }.inject(0) { |sum, f| sum + f.call() }
</div>
<p>And yes, it works &#8211; the result is 45. Nice, but still why previous examples failed? I added:</p>
<div class="code">
procs = []<br />
(1&#8230;10).each { |i| procs &lt;&lt; Proc.new {i} }<br />
puts procs.inject(0) { |sum, f| sum + f.call() }
</div>
<p>And it still working. Hmm&#8230; Looks almost the same as one of the previous &#x27;wrong&#x27; examples.</p>
<p>
Cause of this weird behaviour is that nevertheless you can read<br />
<a href="http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#for">here</a> that:</p>
<blockquote>
<p>Syntax:</p>
<div class="code">
for lhs&#8230; in expr [do]<br />
&nbsp;expr..<br />
end
</div>
<p>
Executes body for each element in the result of<br />
expression. <code>for</code> is the syntax sugar for:
</p>
<div class="code">
(expr).each `{&#x27; `|&#x27; lhs..`|&#x27; expr.. `}&#x27;
</div>
</blockquote>
<p>or <a href="http://www.rubyist.net/~slagell/ruby/control.html">here</a>:</p>
<blockquote>
<p>
But we&#x27;re getting ahead of ourselves.  <code>for</code> is really<br />
another way of writing <code>each</code>, which, it so happens, is our first<br />
example of an iterator.  The following two forms are<br />
equivalent:
</p>
<div class="code">
#&nbsp;  If you&#8217;re used to C or Java, you might prefer this.<br />
for element in collection<br />
&nbsp; &#8230;<br />
end</p>
<p>#&nbsp; A Smalltalk programmer might prefer this.<br />
collection.each {|element|<br />
&nbsp; &#8230;<br />
}
</div>
</blockquote>
<p>it is not all the truth. They differ in small but important factor &#8211; scope.<br />
<a href="http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html">Pragmatic Programmer&#x27;s Guide</a>&nbsp;gives the right answer:</p>
<blockquote><p>
The only difference between the for loop and the each form is the scope of local variables that are defined in the body.
</p></blockquote>
<p>So this one:</p>
<pre><span class="ident">array</span> <span class="punct">=</span> <span class="number">1</span><span class="punct">...</span><span class="number">10</span>
<span class="ident">puts</span> <span class="ident">array</span><span class="punct">.</span><span class="ident">map</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">i</span><span class="punct">|</span> <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span><span class="ident">i</span><span class="punct">}</span> <span class="punct">}.</span><span class="ident">inject</span><span class="punct">(</span><span class="number">0</span><span class="punct">)</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">sum</span><span class="punct">,</span> <span class="ident">f</span><span class="punct">|</span> <span class="ident">sum</span> <span class="punct">+</span> <span class="ident">f</span><span class="punct">.</span><span class="ident">call</span><span class="punct">()</span> <span class="punct">}</span>
<span class="ident">i</span> <span class="punct">=</span> <span class="constant">nil</span> <span class="comment">#or the for loop which also defines local variable</span>
<span class="ident">puts</span> <span class="ident">array</span><span class="punct">.</span><span class="ident">map</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">i</span><span class="punct">|</span> <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span><span class="ident">i</span><span class="punct">}</span> <span class="punct">}.</span><span class="ident">inject</span><span class="punct">(</span><span class="number">0</span><span class="punct">)</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">sum</span><span class="punct">,</span> <span class="ident">f</span><span class="punct">|</span> <span class="ident">sum</span> <span class="punct">+</span> <span class="ident">f</span><span class="punct">.</span><span class="ident">call</span><span class="punct">()</span> <span class="punct">}</span>
</pre>
<p>produces 45 and 81.<br />
<br />
I think that is not an easy to find such an error in your code.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rkje.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rkje.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rkje.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rkje.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rkje.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rkje.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rkje.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rkje.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rkje.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rkje.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rkje.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rkje.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rkje.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rkje.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rkje.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rkje.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rkje.wordpress.com&amp;blog=4549847&amp;post=4&amp;subd=rkje&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rkje.wordpress.com/2007/12/29/use-rubys-lambda-in-for-loop-with-care/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab46e9ad76540da32912dedaabc2def8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rkj</media:title>
		</media:content>
	</item>
	</channel>
</rss>
