<?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/"
	>

<channel>
	<title>Higher-Order &#187; model view controller</title>
	<atom:link href="http://blog.higher-order.net/category/model-view-controller/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.higher-order.net</link>
	<description>topics: functional programming, concurrency, web-development, REST, dynamic languages</description>
	<lastBuildDate>Mon, 19 Jul 2010 06:45:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Monty hall and Bayesian probability theory</title>
		<link>http://blog.higher-order.net/2009/06/23/monty-hall-and-bayesian-probability-theory/</link>
		<comments>http://blog.higher-order.net/2009/06/23/monty-hall-and-bayesian-probability-theory/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 06:26:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[model view controller]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/?p=316</guid>
		<description><![CDATA[Jeff Atwood discusses the &#8220;Monty hall&#8221; problem. I made a comment about why I believe people&#8217;s intuition is often wrong when presented with the problem: I believe it is due to the way probability theory is taught in schools and universities. The notion of probability simply as an extension of classical logic as presented by [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codinghorror.com/blog/archives/001278.html">Jeff Atwood discusses the &#8220;Monty hall&#8221; problem</a>. I made a comment about why I believe people&#8217;s intuition is often wrong when presented with the problem: I believe it is due to the way probability theory is taught in schools and universities. The notion of probability simply as an extension of classical logic as presented by Edwin Jaynes matches intuition much more closely. To solve the Monty Hall problem we need three simple principles (which apply to a <strong>wide</strong> range of problems):</p>
<ul>
<li> Probability is about information. Forget notions of &#8220;random&#8221; experiments, e.g., the outcome of throwing a die is about the laws of physics &#8211; there is no magical randomness built into the die causing it to come up with each side equally often &#8220;in the long run&#8221; (what ever that means). If we know the laws of physics and have enough information about the experiment we might assign other probabilities to the outcomes. This means that <em>if new information is obtained the probability changes</em>!</li>
</ul>
<ul>
<li>In the name of science, we shall not make any unwarranted conclusions, and we promise to use all the information at hand, not deliberately discarding information (for what ever reason).</li>
</ul>
<ul>
<li>We use the laws of probability theory.</li>
</ul>
<p>The first point is where the problem lies. In fact the probabilities are just a formalization of the <em>information</em> we have. This is why we always condition the probability on some information e.g., P(A | I) is the probability of proposition A given the information I.</p>
<p>Let&#8217;s solve Monty Hall using the principles presented in Jaynes&#8217; book. Notice that these techniques are completely general and can solve many different problems of this nature; the key in Monty Hall is not to make any unjustified conclusions, but simply <em>consider all the information at hand!</em></p>
<p>Lets name the doors A, B and C, and let us assume that we initially selected door A (the argument applies for any other initial choice as well). Let us write I for the information we have initially (before opening any doors) &#8211; this includes the rules of the game. More precisely I is the conjunction of statements:</p>
<p>* We have chosen door A.</p>
<p>* The host knows where the car is, and may only open a door that does not contain the car.</p>
<p>* The host may not open the door we selected (A) (even if it does not contain the car).</p>
<p>We formalize the information I in probability equations below. Let us consider three mutually exclusive and exhaustive propositions</p>
<p>A = the car is behind door A;</p>
<p>B = the car is behind door B;</p>
<p>C = the car is behind door C</p>
<p>By the principle of indifference, given only the initial information, I, we have</p>
<p>P(A | I) = P(B | I) = P(C | I) = 1/3</p>
<p>Notice also that so far intuition is completely clear.</p>
<p>Let us now assume that the host opens door C. Let H be the statement:</p>
<p>H = the host opened door C</p>
<p>(the argument is similar for any other choice). The important point here is that we are given additional information beyond our prior information, I. Now let us first derive the correct solution, we then analyze where peoples intuition usually goes wrong.</p>
<p>By the rules of probability theory (specifically Bayes theorem) we have</p>
<p>P(A | HI) = P(A|I) P(H | AI)/P(H|I)</p>
<p>The quantity P(A|HI) is what we are interested in: the probability of the car being behind door A given <em>all the information available to us</em>.</p>
<p>We know P(A|I) is 1/3 so we can focus on P(H|AI) and P(H|I) &#8211; notice these are both probabilities about the host&#8217;s actions &#8211; probabilities that are very relevant to the problem (by the equation above), but also probabilities that most people would not consider to analyse (because probability is taught the way it is)! The former is the probability of the host opening door C given the car is behind door A; the latter is the a-priori probability of the host opening door C given only the information I. The former, P(H|AI), is the most simple: given we know that the car is behind door A what is the probability of the host opening door C. Since we have no information about which door the host opens when the car is behind the door we selected, the principle of indifference applies and we have:</p>
<p>P(H|AI) = P(not H|AI) = 1/2</p>
<p>Now consider P(H|I): the a-priori probability of the host opening door C. Since propositions A, B and C are mutually exclusive and exhaustive, the rules of probability theory imply</p>
<p>P(H | I) = P(H | AI)P(A|I) + P(H | BI)P(B | I) + P(H | CI)P(C | I)</p>
<p>We know that P(H | CI) = 0 because by information I, the host may not open door C if the car is behind door C.  We already figured out P(H | AI) and P(A|I) so the first term is 1/2*1/3 = 1/6. Also we know P(B | I) = 1/3. Now we can focus on P(H | BI): the probability of host opening door C when the car is behind door B and we have chosen door A. But here the rules of the game are clear: the host <strong>must</strong> open door C in this case since he may not open A and may not reveal the car. Hence P(H | BI) = 1. We get:</p>
<p>P(H | I) = 1/6 + 1*1/3 = 3/6 = 1/2.</p>
<p>We now have all the quantities needed to calculate P(A | HI):</p>
<p>P(A | HI) = 1/3 * [(1/2)/(1/2)] = 1/3. Hence P(B | HI) = 2/3 and we should switch doors!</p>
<p>The technique is completely mechanical. I have not used any clever arguments or mathematical ingenuity: only the rules of the game, the principle of indifference and the laws of probability theory. All calculations could be performed by a machine with these inputs.</p>
<p>So where does our intuition go wrong? Two places, I believe. First because of schooling our intuition tells us that somehow we can only reason about &#8220;random&#8221; events. Instead we should focus on <em>information</em> e.g., &#8216;the car is placed &#8220;at random&#8221; behind a door&#8217; versus &#8220;we are given the information that there is a car behind one door and sheep behind the two others.&#8221; Once we free ourselves to consider probabilities as expressing the information we have available, it is natural that the probabilities change when we obtain more information. This is crucial. The second, I believe, is a consequence of the first: most people seem to identify the following two statements in the Monty Hall problem:</p>
<p>* the host opens door C</p>
<p>* the car is not behind door C</p>
<p>But these two pieces of information are not equivalent when we know the rules of the game (i.e. given our prior information). Considering them equal would violate our commitment to science, to be objective and to consider <em>all</em> the information at hand <img src='http://blog.higher-order.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>To illustrate let us analyse a variant of Monty Hall where &#8220;H&#8221; means the latter instead.</p>
<p>Given this alternate piece of information, our first step is still to apply Bayes theorem so:</p>
<p>P(A | HI) = P(A|I) P(H | AI)/P(H|I)</p>
<p>We still have P(A|I) = 1/3. P(H|I) is the probability that the car is not behind door C given our initial information. Since H = not C, and since A, B, C are mutually exclusive and exhaustive, the rules of probability theory imply</p>
<p>P(H | I) = P(not C|I) = P(A or B|I) = P(A|I) + P(B|I) = 2/3</p>
<p>Now P(H | AI) is calculated by classical logic: since A implies not C we have P(H | AI) = 1. Hence:</p>
<p>P(A | HI) = 1/3 * 1/[2/3] = 1/2</p>
<p>Which is the false result that most people&#8217;s intuition prefer. This is the correct conclusion, but for a different game than Monty Hall, namely the game without a host where you simply get an additional information about where the car is not.</p>
<p>Notice that we easily analysed both variants of the game using simple probability theory, and in its interpretation as an extension of classical logic, the intuition follows along nicely. The key is to use <em>all the information at hand</em>, not discarding any information that could be of relevance to the problem (i.e. we got the information that host selected C, not that door C had a sheep behind it).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2009/06/23/monty-hall-and-bayesian-probability-theory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE doesn&#8217;t understand HTML or HTTP</title>
		<link>http://blog.higher-order.net/2008/12/20/ie-doesnt-understand-html-or-http/</link>
		<comments>http://blog.higher-order.net/2008/12/20/ie-doesnt-understand-html-or-http/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 12:46:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[model view controller]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/?p=213</guid>
		<description><![CDATA[The so-called &#8220;web&#8221;-browser Internet Explorer version 7 (and probably all versions below) doesn&#8217;t get HTML or HTTP. In a recent project for a client we&#8217;ve been building an advanced, 100% JavaScript client application that had to run in IE7 (as a minimal requirement). The application is rather complex and has to live as a component [...]]]></description>
			<content:encoded><![CDATA[<p>The so-called &#8220;web&#8221;-browser Internet Explorer version 7 (and probably all versions below) doesn&#8217;t get HTML or HTTP. In a recent project for a client we&#8217;ve been building an advanced, 100% JavaScript client application that had to run in IE7 (as a minimal requirement). The application is rather complex and has to live as a component in <em>every (darn) webpage/webapp</em> on the client&#8217;s intranet, including all legacy apps; essentially, the applications include this component by inserting a script tag in their pages. The component&#8217;s most basic job is to help the user navigate to the existing multitude of intranet applications, supplying these applications with appropriate input (e.g., navigate to the customer mangagement app with a customer reference as input).</p>
<p>Now these legacy applications are of different ages and have been developed in various technologies, with varying quality and standards-awareness. Now these requirements brings a bunch of problems, and I won&#8217;t start complaining too much here. Instead I&#8217;ll just focus on one issue: character encodings! <em>One</em> of the aspects of this scenarios is that the applications are using different character-encodings, e.g., ISO-8859-1 and UTF-8. This gives the following requirements.</p>
<ul>
<li>Cross page (cross encoding) navigation. For example, our client app often has to navigate from a UTF-8 encoded page to an ISO-8859-1 encoded page, <strong>transferring properly encoded parameters</strong> from the first page to the second.</li>
<li>Localization. The client app is available in various languages; together with the requirement that it must exist in UTF-8 and ISO-8859-1 encoded pages, this means that the actual JavaScript being sent on the wire must be available in different languages and encodings, based on the browsers settings.</li>
</ul>
<p>You&#8217;d expect these things to be pretty basic stuff for an HTTP based app; something that would be easy in a version 7 browser.</p>
<h2>Cross page (cross encoding) navigation</h2>
<p>For various reasons irrelevant here, our client app navigates to an application by dynamically creating a form with method &#8220;GET&#8221;, and plugging in the URL and the parameters for the receiving application, i.e.,</p>
<pre><tt>
&lt;form method="GET" action="URL"&gt;
&lt;input type="hidden" name="pname" value="pvalue" &gt;
…
&lt;/form&gt;
</tt></pre>
<p>There is an implicit attribute called &#8216;accept-charset&#8217; which <a href="http://www.w3.org/TR/html401/interact/forms.html">according to HTML 4.01 satisfies</a>:</p>
<blockquote><p>&#8220;This attribute specifies the list of character encodings for input data that is accepted by the server processing this form. The value is a space- and/or comma-delimited list of charset values. The client must interpret this list as an exclusive-or list, i.e., the server is able to accept any single character encoding per entity received. The default value for this attribute is the reserved string &#8220;UNKNOWN&#8221;. User agents may interpret this value as the character encoding that was used to transmit the document containing this FORM element.&#8221;</p></blockquote>
<p>The problem with IE7 is that it <em>ignores the accept-charset property entirely on forms</em>. In fact, it instead does the default behaviour for &#8220;UNKNOWN&#8221;, i.e.,  &#8220;the character encoding that was used to transmit the document containing this form element.&#8221; Actually, to be precise it doesn&#8217;t even do that either: It turns out the IE7 encodes the form according to the current value of <tt>document.charset</tt>. Fortunately it is possible to set <tt>document.charset</tt> using JavaScript, this means that it is possible to control the encoding of form input data by setting <tt>document.charset</tt> just before the form submits. And this works. Problem solved. Kind of …</p>
<p>Unfortunately, this hack turns out to trigger a somewhat strange and nasty bug: After form submission, IE navigates to the desired page and input data is encoded according to <tt>document.charset</tt>, which is just what we want. However, if you click the &#8216;back&#8217;-button on the next page, IE navigates back to the original page, <em>but the <tt>document.charset</tt> property somehow persists</em> which means that the page is not interpreted with the correct encoding. OK, so we should clean up after ourselves: One would think that restoring the <tt>document.charset</tt> to it&#8217;s original value after calling <tt>form.submit()</tt> would work: it doesn&#8217;t.</p>
<p>After some experimentation I found that the following works. To submit a form with input data encoded in a particular encoding:</p>
<ol>
<li>on document load, store the original page encoding</li>
<li>on form submit change the value of <tt>document.charset</tt> to the desired encoding, e.g., &#8220;ISO-8859-1&#8243;</li>
<li>on &#8216;beforeunload&#8217; restore <tt>document.charset</tt> to it&#8217;s original value.</li>
</ol>
<h2>Script character encodings</h2>
<p>Our JavaScript client app sometimes lives in a UTF-8 encoded page, sometimes in an ISO-8859-1 encoded page. The client app is loaded by including a &lt;script&gt; tag that points to a servlet that delivers the JavaScript code (customized for the requesting user and his preferred locale). By default when IE loads script, interprets the bytes it receives as characters <em>in the encoding of the current page</em>. However, it doesn&#8217;t tell the server which encoding that is. More specifically, our servlet receives a request for the JavaScript client with these HTTP headers:</p>
<pre><tt>
Accept : */*
Referer : http://localhost:10045/wps/portal/sn
Accept-Language : da
UA-CPU : x86
Accept-Encoding : gzip, deflate
User-Agent : Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; MS-RTC LM 8; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Connection : Keep-Alive
Cache-Control : no-cache
</tt></pre>
<p>Furthermore, even if we explicitly tell IE which encoding we have chosen, it will still ignore that and use the page encoding. More specifically, even if we return this HTTP:</p>
<pre><tt>
Content-Type: application/javascript; charset=UTF-8
</tt></pre>
<p>IE ignores the Content-Type charset. In other words: if we choose as a default (when no Accept-Charset header is present) to deliver the script in UTF-8 encoded, it breaks for ISO-8859-1 pages, and vice versa. So what to do? IE apparently doesn&#8217;t speak HTTP.</p>
<p>It turns out that one should use a script tag with a charset property to tell IE how to interpret the script. Ironically this means that on UTF-8 encoded pages one would need to say &lt;script charset=&#8221;ISO-8859-1&#8243;&gt;. In other words, it is not the HTTP headers describing the resource that describes its encoding. This must be know in advance by all documents that link to the resource by embedding a charset property. Yet another reason that these are tough times to be RESTful…</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/12/20/ie-doesnt-understand-html-or-http/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HTML-free Web-applications with ExtJS [Designing client/server web-apps, Part I, Section III]</title>
		<link>http://blog.higher-order.net/2008/04/19/html-free-web-applications-with-extjs-designing-clientserver-web-apps-part-i-section-iii/</link>
		<comments>http://blog.higher-order.net/2008/04/19/html-free-web-applications-with-extjs-designing-clientserver-web-apps-part-i-section-iii/#comments</comments>
		<pubDate>Sat, 19 Apr 2008 19:41:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ext]]></category>
		<category><![CDATA[Extjs]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[model view controller]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/2008/04/19/html-free-web-applications-with-extjs-designing-clientserver-web-apps-part-i-section-iii/</guid>
		<description><![CDATA[HTML-free Web-applications with ExtJS]]></description>
			<content:encoded><![CDATA[<p><a href="http://higher-order.blogspot.com/2008/03/html-free-web-applications-with-extjs.html">HTML-free Web-applications with ExtJS</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/04/19/html-free-web-applications-with-extjs-designing-clientserver-web-apps-part-i-section-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Client/Server Web-apps &#8212; the model (Part I, Section II)</title>
		<link>http://blog.higher-order.net/2008/03/08/clientserver-web-apps-the-model-part-i-section-ii/</link>
		<comments>http://blog.higher-order.net/2008/03/08/clientserver-web-apps-the-model-part-i-section-ii/#comments</comments>
		<pubDate>Sat, 08 Mar 2008 19:00:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ext]]></category>
		<category><![CDATA[Extjs]]></category>
		<category><![CDATA[HTML free web-applications]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[model view controller]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/2008/03/08/clientserver-web-apps-the-model-part-i-section-ii/</guid>
		<description><![CDATA[Client/Server Web-apps &#8212; the model]]></description>
			<content:encoded><![CDATA[<p><a href="http://higher-order.blogspot.com/2008/03/clientserver-web-apps-model-part-i.html">Client/Server Web-apps &#8212; the model</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/03/08/clientserver-web-apps-the-model-part-i-section-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
