<?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>Coranac</title>
	<atom:link href="http://www.coranac.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coranac.com</link>
	<description>my own little world</description>
	<lastBuildDate>Sat, 19 Nov 2011 16:43:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Tiny little tonc fix</title>
		<link>http://www.coranac.com/2011/11/tiny-little-tonc-fix/</link>
		<comments>http://www.coranac.com/2011/11/tiny-little-tonc-fix/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 18:37:18 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[tonc]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=227</guid>
		<description><![CDATA[I just noticed that the gfxmake files in tonc&#8217;s advanced examples used an absolute link to grit.exe, which of course won&#8217;t work unless you have my file system. They no use local links now. Should compile right out of the box again now. &#160; New tonc-code.zip]]></description>
			<content:encoded><![CDATA[<p>
I just noticed that the <code>gfxmake</code> files in tonc&#8217;s advanced examples used an absolute link to <tt>grit.exe</tt>, which of course won&#8217;t work unless you have my file system. They no use local links now. Should compile right out of the box again now.
</p>
<p><div>&nbsp;</div></p>
<p><a href="/files/tonc-code.zip">New tonc-code.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2011/11/tiny-little-tonc-fix/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Today&#8217;s steam deal : SpaceChem</title>
		<link>http://www.coranac.com/2011/07/steam-deal-spacechem/</link>
		<comments>http://www.coranac.com/2011/07/steam-deal-spacechem/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 18:23:59 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=222</guid>
		<description><![CDATA[This is just a small head&#8217;s up (I intend to do a larger discussion later) about one of the most awesome and geeky indie-games available at the moment: SpaceChem by Zachatronics Industries. &#160; The basic premise behind SpaceChem is chemical engineering: in each assignment, you get some input molecules and you have to turn them [...]]]></description>
			<content:encoded><![CDATA[<p>
This is just a small head&#8217;s up (I intend to do a larger discussion later) about one of the most awesome and geeky indie-games available at the moment: <a href="http://store.steampowered.com/app/92800/">SpaceChem</a> by <a href="http://www.zachtronicsindustries.com/">Zachatronics Industries</a>.
</p>
<p><div>&nbsp;</div></p>
<p>
The basic premise behind SpaceChem is chemical engineering: in each assignment, you get some input molecules and you have to turn them into the requested output molecules via bonding and unbonding their atoms, and the occasional nuclear fusion. A simple (and decidedly sub-optimal) example:
</p>
<p><div>&nbsp;</div><object width="500" height="306"><param name="movie" value="http://www.youtube.com/v/Gk8JwvtVs38?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Gk8JwvtVs38?version=3" type="application/x-shockwave-flash" width="500" height="306" allowscriptaccess="always" allowfullscreen="true"></embed></object><div>&nbsp;</div></p>
<p>
The chemistry part is just a front, though. What the game <i>really</i> is, is a programming simulator. You have a tiny board (10&times;8) board with input and output zones and two &#8216;waldos&#8217; that trace a path on which you place and execute instructions: basically a tiny dual-threaded CPU with a select instruction set. I know it sounds simple, and that&#8217;s exactly right: it <i>sounds</i> simple. However, working out a workable design hard, VERY hard. Especially when you try to optimize a design for least time or components.
</p>
<p>
There are in-game leaderboards, showing you how much a design that you have toiled away at for hours actually sucks sweaty donkey balls, and a more detailed scoreboard at <a href="http://spacechem.net/">spacechem.net</a>. You can also upload your solutions to youtube. Here&#8217;s one of mine for &ldquo;Gas Works Park&rdquo;, where you turn some random carbon strings (C<sub>3</sub> / C<sub>4</sub>) and water (H<sub>2</sub>O) and turn them into methane (CH<sub>4</sub>) and carbondioxide (CO<sub>2</sub>). Solution details can be found in the video description.
</p>
<p><div>&nbsp;</div><object width="500" height="306"><param name="movie" value="http://www.youtube.com/v/dTVM_1sJq5o?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/dTVM_1sJq5o?version=3" type="application/x-shockwave-flash" width="500" height="306" allowscriptaccess="always" allowfullscreen="true"></embed></object><div>&nbsp;</div></p>
<ul>
<li>
Publisher site (with other engineering games): <a href="http://www.zachtronicsindustries.com/">Zachatronics Industries</a>
  </li>
<li>
Game site: <a href="http://www.spacechemthegame.com/">http://www.spacechemthegame.com/</a>
  </li>
<li>
Review: <a href="http://www.rockpapershotgun.com/2011/01/10/wot-i-think-spacechem">http://www.rockpapershotgun.com/2011/01/10/wot-i-think-spacechem</a>
  </li>
</ul>
<p><div>&nbsp;</div></p>
<p>
If you like programming and games (which probably describes readers here pretty well), you need to give this a try.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2011/07/steam-deal-spacechem/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Netherlands &#8211; FUCK YEAH!</title>
		<link>http://www.coranac.com/2010/07/the-netherlands-fuck-yeah/</link>
		<comments>http://www.coranac.com/2010/07/the-netherlands-fuck-yeah/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 21:30:30 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[tainment]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=214</guid>
		<description><![CDATA[<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
Normally, I don&#8217;t really do this kind of stuff, but &#8230; Finals!!! woooOOOOOOOOOOOOOOOOOOOooo!!! &#160; Holy crap, that was a tight game. First a beautiful goal by van Bronckhorst. Then the equalizer by Forl&#225;n just before the break. Then a hideous goal by not-Sneijder, but he gets the point anyway, quickly followed by Robben&#8217;s header. And [...]]]></description>
			<content:encoded><![CDATA[<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<p>
Normally, I don&#8217;t really do this kind of stuff, but &hellip;
</p>
<p><center></p>
<p><span style="font-size:200%;"><br />
<b>Finals!!!</b><br />
<b>woooOOO<span style="font-size:150%;">OOO<span style="font-size:150%;">OOOOOOO</span>OOO</span>OOOooo!!!</b><br />
</span>
</p>
<p></center><div>&nbsp;</div></p>
<p><center><br />
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" 
    alt="" width="" /></a><br />
  
</div>
<br />
</center></p>
<p>
Holy crap, that was a tight game. First a beautiful goal by van Bronckhorst. Then the equalizer by Forl&aacute;n just before the break. Then a hideous goal by not-Sneijder, but he gets the point anyway, quickly followed by Robben&#8217;s header. And then <i>in overtime</i> they still almost manage to blow it when Pereira makes it 2&ndash;3 and then drama in the rest of the overtime! OMFGWTFBBQ!!111!!ELEVENTYONE!!!
</p>
<p><div>&nbsp;</div></p>
<p>
Netherlands &ndash; Germany finals incoming!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/07/the-netherlands-fuck-yeah/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Portal is free :D</title>
		<link>http://www.coranac.com/2010/05/portal-is-free/</link>
		<comments>http://www.coranac.com/2010/05/portal-is-free/#comments</comments>
		<pubDate>Thu, 13 May 2010 10:24:27 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[tainment]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=206</guid>
		<description><![CDATA[It seems that Valve is &#8216;selling&#8217; the absolutely awesome Portal for zero dollars until May 24th. If you don&#8217;t have it yet, this would be a good time to get it. &#160; Linky: http://store.steampowered.com/freeportal/]]></description>
			<content:encoded><![CDATA[<p>
It seems that Valve is &#8216;selling&#8217; the absolutely awesome Portal for zero dollars until May 24th. If you don&#8217;t have it yet, this would be a good time to get it.
</p>
<p><div>&nbsp;</div></p>
<p>Linky:
</p>
<p>
<a href="http://store.steampowered.com/freeportal/">http://store.steampowered.com/freeportal/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/05/portal-is-free/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Probabilities in simple random samples</title>
		<link>http://www.coranac.com/2010/05/probabilities-in-simple-random-samples/</link>
		<comments>http://www.coranac.com/2010/05/probabilities-in-simple-random-samples/#comments</comments>
		<pubDate>Wed, 05 May 2010 17:41:51 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=200</guid>
		<description><![CDATA[<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
1 Introduction 2 Probability and its rules 3 Combinatorics : counting evolved 4 Examples 5 Summary 1 Introduction When it comes to math, one of the things that people are phenomenally bad at &#8211; aside from the obvious everything &#8211; is probabilities. Specifically, calculating probabilities. People are bad enough at estimating chances, but properly calculating [...]]]></description>
			<content:encoded><![CDATA[<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<br />
<b>Warning</b>:  getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/coranac/public_html/wordpress/wp-content/plugins/crnfilters.php</b> on line <b>466</b><br />
<p></p>
<p><ul>
  <li> <a href="#sec-intro">1
Introduction
</a> </li>
  <li> <a href="#sec-prob">2
Probability and its rules
</a> </li>
  <li> <a href="#sec-cmb">3
Combinatorics : counting evolved
</a> </li>
  <li> <a href="#sec-eg">4
Examples
</a> </li>
  <li> <a href="#sec-summary">5
Summary
</a> </li>
</ul>
</p>
<p><h2 id="sec-intro">1
Introduction
</h2>
</p>
<p>
When it comes to math, one of the things that people are phenomenally<br />
bad at &ndash; aside from the obvious everything &ndash; is<br />
probabilities. Specifically, <i>calculating</i> probabilities.<br />
People are bad enough at<br />
estimating chances, but properly calculating then has its own<br />
hurdles.
</p>
<p>
In this particular case, I&#8217;d like to take a look at the probability of<br />
a 
<a href="http://en.wikipedia.org/wiki/Simple%20random%20sample">Simple random sample</a>: getting a particular distribution<br />
of results from a number of samples, like getting 3 sixes in a<br />
certain number of rolls. Specifically, I&#8217;ll look at the case<br />
<b>with replacement</b>. In this case, the sample pool remains the same<br />
after each individual sample. The case <b>without replacement</b><br />
(like picking colored marbles from a vase without putting them back)<br />
is a bit trickier so I won&#8217;t cover that now.
</p>
<p><span id="more-200"></span></p>
<h4 id="sssec-binom">The binomial distribution</h4>
<p>
One of the equations that is relevant here is the<br />

<a href="http://en.wikipedia.org/wiki/binomial%20distribution">binomial distribution</a><br />
(see Eq&nbsp;1), the scourge of math students<br />
everywhere. What I&#8217;d like to do is explain why it looks the way it does,<br />
what the terms mean and how you can generalize it to cover more than<br />
two outcomes (that&#8217;s what the &ldquo;bi-&rdquo; stands for: two).<br />
Basically, I&#8217;d like to show you how to &ldquo;read&rdquo; the equation<br />
and to interpret the terms in it, which will make it easier to deal<br />
with the subject.
</p>
<p><table class="eqtbl" id="eq-binom-std">
<tr>
<td class="eqnrcell">(1)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28W%3Dk%29%20%3D%20%7Bn%5Cchoose%20k%7D%20p%5Ek%20%281-p%29%5E%7Bn-k%7D'<br />
	title="P(W=k) = {n\choose k} p^k (1-p)^{n-k}"<br />
	alt="P(W=k) = {n\choose k} p^k (1-p)^{n-k}" /><br />
</td>
</tr>
</table></p>
<p>
First, a few words about the Eq&nbsp;1 itself. Say you have<br />
a random event that can have two outcomes: coin-flip, true/false question,<br />
but also a die-roll if you just look at an X vs non-X result. Or, in<br />
the parlance of our times, whether you have a win (<b>W</b>) or a<br />
fail (<b>F</b>). What Eq&nbsp;1 gives you is the probability<br />
of getting exactly <i>k</i> wins in <i>n</i> trials.
</p>
<p><div>&nbsp;</div></p>
<p>
That&#8217;s all well and good, of course, but unless you actually understand<br />
how the equation is built up and what all the terms in it mean, it&#8217;ll<br />
just be yet another magic formula that someone dreamed up to torture you<br />
with. In practical terms, it also means that it&#8217;s harder to apply it<br />
correctly. So let&#8217;s look at what it means, starting with what each<br />
individual symbol means.
</p>
<div class=cblock>
<table id="tbl-binom" class="cblock"<br />
	border=1 cellpadding=2 cellspacing=0><br />
<caption>
  <b>Table&nbsp;1</b>: Explanation of symbols in<br />
  Eq&nbsp;1.<br />
</caption>
<tr>
<th>Symbol</th>
<th>Meaning</th>
</tr>
<tr>
<th>W</th>
<td>Win index, or number of wins.</td>
</tr>
<tr>
<th>F</th>
<td>Fail index, or number of fails.</td>
</tr>
<tr>
<th>n</th>
<td>Number of trials.</td>
</tr>
<tr>
<th>k</th>
<td>Selection value (usually linked to the number of<br />
	wins).</td>
</tr>
<tr>
<th>p</th>
<td>Single-trial probability (usually linked to<br />
	wins).</td>
</tr>
<tr>
<th>P(<i>cond</i>)</th>
<td>Total probability, with the condition.</td>
</tr>
</table>
</div>
<p><!--<br />
<div class=note id="id-binom-std">
<div  class=nhcare>Equation imprecise</div>
</p>
<p>
I should probably mention that while Eq&nbsp;1 is<br />
correct, it is imprecise.
</p>
<p></div>
<br />
&#8211;></p>
<p>
Here&#8217;s an example of its use. Say you have 7 dice (7d6) and you want to<br />
know the probability of throwing 3 sixes. This means you have<br />
<i>n</i>&nbsp;=&nbsp;7, <i>k</i>&nbsp;=&nbsp;3 and <i>p</i>&nbsp;=&nbsp;1/6, and the condition is<br />
<i>W</i>&nbsp;=&nbsp;<i>k</i>&nbsp;=&nbsp;3.
</p>
<h4>Divide and conquer</h4>
<p>
The next step is to split the equation into the two parts that are<br />
of true relevance, namely:
</p>
<p><div class="cptfr" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-binom-div"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;1. </b>
</div>
</p>
<ol>
<li>
    The base chance of getting a specific result that satisfies your<br />
	conditions.
  </li>
<li>
    The number of results that satisfies the conditions (each with the<br />
	same base probability).
  </li>
</ol>
<p>
I&#8217;ve always found this division to make the most sense. Note that I&#8217;ve<br />
split it into two parts, not three. The terms involving <i>p</i> and<br />
(1&minus;<i>p</i>) belong in the same group, as I&#8217;ll explain later. The<br />
nice thing about this division is that the two parts neatly correspond<br />
to two concepts of mathematics: probability and combinatorics. If you<br />
understand those two, equations like Eq&nbsp;1 should<br />
start to make sense.
</p>
<div class="lblock">
</div>
<p><h2 id="sec-prob">2
Probability and its rules
</h2>
</p>
<p>
When I say you need to understand probability theory to understand<br />
the binomial distribution, what I really mean is that you understand<br />
the <i>rules</i> or probability, and how to combine single chance<br />
occurrences into larger conditions. See also<br />

<a href="http://en.wikipedia.org/wiki/Probability%23Mathematical_treatment">wiki:probability</a>.
</p>
<p><h3 id="ssec-prob-def">2.1
Probability definition
</h3>
</p>
<p>
The probability of a selected case <i>x</i> is the number of times it<br />
occurs relative to the total pool of cases. This is the fraction given in<br />
Eq&nbsp;2. All other rules are the result of this one.
</p>
<p><table class="eqtbl" id="eq-prob-def">
<tr>
<td class="eqnrcell">(2)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%29%20%3D%20%7B%7B%23%20%5Ctext%7BSelected%20case%7D%7D%20%5Cover%20%7B%20%23%20%5Ctext%7BTotal%20cases%7D%7D%7D'<br />
	title="P(X) = {{# \text{Selected case}} \over { # \text{Total cases}}}"<br />
	alt="P(X) = {{# \text{Selected case}} \over { # \text{Total cases}}}" /><br />
</td>
</tr>
</table></p>
<p>
For example, we roll a d6 and we&#8217;re interested in getting a six.<br />
Since there are 6 sides and only 1 six, the probability is<br />
<i>P</i>(<i>1&nbsp;six</i>)&nbsp;=&nbsp;1/6.
</p>
<p><h3 id="ssec-prob-range">2.2
Probability range
</h3>
</p>
<p>
Since the total number of items is <i>at least</i> as large as the<br />
selection, each probability itself is at most 1. A probability can&#8217;t be<br />
lower than zero either, because counts are natural numbers.
</p>
<p>
This also holds for the sum probability of multiple items. Also, the<br />
probability to get any result at all is 1, because obviously something<br />
needs to happen. If you sum up probabilities and you get an answer<br />
greater than 1, some of the cases you&#8217;ve counted actually overlap. If<br />
you add up all probabilities and you get less than 1, you&#8217;ve missed<br />
a few cases.
</p>
<p><table class="eqtbl" id="eq-prob-range">
<tr>
<td class="eqnrcell">(3)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?0%20%5Cle%20P%28X%29%20%5Cle%201'<br />
	title="0 \le P(X) \le 1"<br />
	alt="0 \le P(X) \le 1" /><br />
</td>
</tr>
</table></p>
<p><h3 id="ssec-prob-not">2.3
Complementary rule : NOT
</h3>
</p>
<p>
If the probability of some event is <i>P</i>(<i>X</i>) and the sum<br />
probability of all events, then the<br />
probability of it <b>not</b> happening is 1&minus;<i>P</i>(<i>X</i>). The<br />
symbol for &ldquo;not&rdquo; is &not;.
</p>
<p><table class="eqtbl" id="eq-prob-not">
<tr>
<td class="eqnrcell">(4)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28%5Ctext%7Bnot%7D%5C%2CX%29%20%3D%20P%28%5Cneg%20X%29%20%3D%201%20-%20P%28X%29'<br />
	title="P(\text{not}\,X) = P(\neg X) = 1 - P(X)"<br />
	alt="P(\text{not}\,X) = P(\neg X) = 1 - P(X)" /><br />
</td>
</tr>
</table></p>
<p>
For example, if the probability of a six on a d6 is 1/6, the<br />
probability of <i>not</i> getting a six is <i>P</i>(~<i>six</i>)&nbsp;=&nbsp;5/6.
</p>
<p><h3 id="ssec-prob-xor">2.4
Disjoint conditions : XOR
</h3>
</p>
<p>
If you have multiple conditions that are <b>mutually exclusive</b><br />
(disjoint), the probabilities add. This corresponds to an &ldquo;or&rdquo;<br />
case.
</p>
<p>
Please note the &ldquo;mutually&rdquo; here. It must be <i>either</i><br />
<i>X</i> <i>or</i> <i>Y</i>. For the programmers: the English word<br />
&ldquo;or&rdquo; actually mean an exclusive OR ( ^ in C), not an<br />
inclusive OR ( | ). This difference is the main reason why calculated<br />
probabilities can end up greater than 1. There are several symbols<br />
that can be used for a disjoint condition, all of which are problematic.<br />
I&#8217;ll use the boolean XOR symbol,  &oplus;, because even though it may<br />
be a little obscure for most people, at least it&#8217;s non-ambiguous.
</p>
<p><table class="eqtbl" id="eq-prob-or">
<tr>
<td class="eqnrcell">(5)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%5Ctext%7B%20or%20%7DY%29%20%3D%20P%28X%20%5Coplus%20Y%29%20%3D%20P%28X%29%20%2B%20P%28Y%29'<br />
	title="P(X\text{ or }Y) = P(X \oplus Y) = P(X) + P(Y)"<br />
	alt="P(X\text{ or }Y) = P(X \oplus Y) = P(X) + P(Y)" /><br />
</td>
</tr>
</table></p>
<p>
For example, the chance of getting a 6 <b>or</b> a 5 on a single d6.<br />
is <i>P</i>(<i>six</i> or <i>5</i>)&nbsp;=&nbsp;1/6&nbsp;+&nbsp;1/6&nbsp;=&nbsp;1/3. You can also<br />
approach it from the definition of Eq&nbsp;2, where<br />
you still have a total of 6, but now you have 2 valid cases, leading<br />
directly to 2/6.
</p>
<p><div class=note id="nt-xor">
<div  class=nhcare>Language issues and silly symbols</div>
</p>
<p>
The term &#8220;or&#8221; is one of those areas where human languages, mathematics<br />
and logic are at odds. As said, the word &ldquo;or&rdquo; actually<br />
means an exclusive or (one of the two, but not both), even though in math<br />
and logic it&#8217;s used for the union (the full combination; and/or).
</p>
<p>
It gets even more vexing when one looks at the symbols used. The C<br />
equivalent for OR (the inclusive OR) is <code>|</code> or<br />
<code>||</code>. However, the <code>|</code> symbol is also used for<br />
a 
<a href="http://en.wikipedia.org/wiki/conditional%20probability">conditional probability</a>. So that&#8217;s out. I can&#8217;t use the<br />
C XOR symbol, <code>^</code>either, as that actually means AND in logic.<br />
There&#8217;s also the union symbol, <code>&cup;</code>, but I think that<br />
stands for <i>in</i>clusive OR, not exclusive. So in the end it comes<br />
down to the O-plus, &oplus;. While relatively unknown, at least it<br />
only has a single meaning.
</p>
<p>
If anyone has a better idea, I&#8217;d love to hear it.
</p>
<p></div>
</p>
<p><h3 id="ssec-prob-and">2.5
Joint conditions : AND
</h3>
</p>
<p>
When you have two independent conditions that both need to be true,<br />
the probabilities multiply. Think of it as a sub-selection, where you<br />
have to get a fraction of a fraction. This corresponds to an<br />
&ldquo;and&rdquo; case. The logical symbol for it is a cap ( &cap; ),<br />
but since it looks too much like a cup, I&#8217;ll use the actual AND-symbol<br />
( &amp; ).
</p>
<p><table class="eqtbl" id="eq-prob-and">
<tr>
<td class="eqnrcell">(6)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%5Ctext%7B%20and%20%7DY%29%20%3D%20P%28X%20%5C%26%20Y%29%20%3D%20P%28X%29%20%5Ccdot%20P%28Y%29'<br />
	title="P(X\text{ and }Y) = P(X \&amp; Y) = P(X) \cdot P(Y)"<br />
	alt="P(X\text{ and }Y) = P(X \&amp; Y) = P(X) \cdot P(Y)" /><br />
</td>
</tr>
</table></p>
<p>
An example of this rule is the chance to throw two sixes with two dice.<br />
First, you need to throw a six with one for a 1/6 chance, and in the<br />
event that that happens, there&#8217;s another 1/6 chance for the other,<br />
combining into<br />
<i>P</i>(<i>six</i> and <i>six</i>)&nbsp;=&nbsp;1/6&nbsp;&middot;&nbsp;1/6&nbsp;=&nbsp;1/36. You<br />
can also get this result from the definition. With 2 dice you have 36<br />
possibilities in total, and only one of them will contain (6,6). Note<br />
that for a 5 and 6 the rules are different, since both (5,6) and (6,5)<br />
are possible here.
</p>
<p><h3 id="ssec-prob-use">2.6
Usage in general and binomials in particular
</h3>
</p>
<p>
When considering probabilities, you&#8217;ll often have multiple<br />
trials/samples and a diffent type of answer for each trial. For example,<br />
say you have three different options, <i>a</i>, <i>b</i> and <i>c</i>.<br />
Each option has a different probability,<br />
<i>p</i><sub>a</sub>, <i>p</i><sub>b</sub>, and <i>p</i><sub>c</sub>.<br />
Now I&#8217;m looking for the probability of getting 2 <i>a</i>, 3 <i>b</i>s<br />
and 4 <i>c</i>s. In other words, the number of results for each<br />
possibility is <i>k</i><sub>a</sub>&nbsp;=&nbsp;2,<br />
<i>k</i><sub>b</sub>&nbsp;=&nbsp;3 and <i>k</i><sub>c</sub>&nbsp;=&nbsp;4, for a total<br />
number of trials<br />
<i>n</i>&nbsp;=&nbsp;&Sigma;<sub>i</sub>&nbsp;<i>k</i><sub>i</sub>&nbsp;=&nbsp;10. Now look<br />
at one possible result that satisfies the condition, say,<br />
&#8220;aabbbcccc&#8221;. This is an AND case, so probabilities multiply. Each<br />
trial result gets the probability belonging to that answer, which<br />
in this case leads to
</p>
<p><table class="eqtbl">
<tr>
<td class="eqnrcell"></td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28%5Ctext%7Bexactly%20%7D%20aabbbcccc%29%20%3D%20p_a%5E2%5C%2Cp_b%5E3%5C%2Cp_c%5E4.'<br />
	title="P(\text{exactly } aabbbcccc) = p_a^2\,p_b^3\,p_c^4."<br />
	alt="P(\text{exactly } aabbbcccc) = p_a^2\,p_b^3\,p_c^4." /><br />
</td>
</tr>
</table></p>
<p>
The generalized case for this has <i>m</i> classes, labelled<br />
<i>X</i><sub>j</sub>, and we&#8217;re the number of outcomes we&#8217;re looking<br />
for for each class is <i>n</i><i>j</i>. The probability for a<br />
<i>single</i> result that satisfies this condition is<br />
Eq&nbsp;7. It may look a little hairy, but all it<br />
really says is that you should multiply the probability of each of the<br />
result in the condition.
</p>
<p><table class="eqtbl" id="eq-prob-single">
<tr>
<td class="eqnrcell">(7)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28k_1%20X_1%5C%2C%5C%26%5C%2Ck_2%20X_2%5C%2C%5C%26%20...%20%5C%26%5C%2Ck_m%20X_m%29%20%3D%20p_1%5E%7Bk_1%7D%20%5C%2C%5Ctimes%5C%2C%20p_2%5E%7Bk_2%7D%20%5C%2C%5Ctimes%5C%2C%20...%20%5C%2C%5Ctimes%5C%2C%20p_m%5E%7Bk_m%7D%20%3D%20%5Cprod_%7Bj%3D1%7D%5Em%20p_j%5E%7Bk_j%7D'<br />
	title="P(k_1 X_1\,\&amp;\,k_2 X_2\,\&amp; ... \&amp;\,k_m X_m) = p_1^{k_1} \,\times\, p_2^{k_2} \,\times\, ... \,\times\, p_m^{k_m} = \prod_{j=1}^m p_j^{k_j}"<br />
	alt="P(k_1 X_1\,\&amp;\,k_2 X_2\,\&amp; ... \&amp;\,k_m X_m) = p_1^{k_1} \,\times\, p_2^{k_2} \,\times\, ... \,\times\, p_m^{k_m} = \prod_{j=1}^m p_j^{k_j}" /><br />
</td>
</tr>
</table></p>
<p>
Now, look back at the binomial probability. In that case, you have two<br />
options, <b>W</b>in and <b>F</b>ail, with probabilities<br />
<i>p</i><sub>w</sub> and <i>p</i><i>f</i>, respectively. Let&#8217;s look at<br />
the case for <i>k</i> wins. That is<br />
<i>W</i>&nbsp;=&nbsp;<i>k</i><sub>w</sub>&nbsp;=&nbsp;<i>k</i>. Now, because there are only<br />
two options (that&#8217;s the &ldquo;bi-&rdquo; in binomial), the<br />
probabilities of the fail-case are now also set. The probability for<br />
failing is <i>p</i><sub>f</sub>&nbsp;=&nbsp;<i>p</i><sub>&not;w</sub>&nbsp;=&nbsp;1&minus;<i>p</i><sub>w</sub>.<br />
As for the number of failed cases, remember that the binomial looks<br />
for <i>exactly</i> <i>k</i> wins, meaning the rest of the cases fail,<br />
leading to <i>k</i><sub>f</sub>&nbsp;=&nbsp;<i>n</i>&minus;<i>k</i>. This little<br />
fact is never mentioned explicitly, but it shouldn&#8217;t be overlooked.
</p>
<p>
In the end, what you<br />
get is Eq&nbsp;8. The only difference with the<br />
actual binomial equation is that the subscript has been removed in the<br />
latter. Never underestimate the mathematician&#8217;s laziness when it comes<br />
to writing stuff down.
</p>
<p><table class="eqtbl" id="eq-prob-binom-single">
<tr>
<td class="eqnrcell">(8)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28W%3Dk_w%5C%2C%5C%26%5C%2C%20F%3Dk_f%29%20%3D%20p_w%5E%7Bk_w%7D%5C%2Cp_f%5E%7Bk_f%7D%20%3D%20p_w%5Ek%5C%2C%281-p_w%29%5E%7Bn-k%7D'<br />
	title="P(W=k_w\,\&amp;\, F=k_f) = p_w^{k_w}\,p_f^{k_f} = p_w^k\,(1-p_w)^{n-k}"<br />
	alt="P(W=k_w\,\&amp;\, F=k_f) = p_w^{k_w}\,p_f^{k_f} = p_w^k\,(1-p_w)^{n-k}" /><br />
</td>
</tr>
</table></p>
<p>
There&#8217;s still one thing missing though: both<br />
Eq&nbsp;7 and Eq&nbsp;8<br />
only cover the possibility of one <i>specific</i> valid result. In<br />
most cases, there will be others as well. In the <i>a</i>,<i>b</i>,<i>c</i><br />
case, for example, I only looked at &#8220;aabbbcccc&#8221;, but &#8220;bbbaacccc&#8221;,<br />
&#8220;abcabcbcc&#8221;, or &#8220;cccbbaaa&#8221; would have worked as well, as would any<br />
other combination. You need take all of these into account, but for<br />
that you need a way to quickly find all variations first. And that&#8217;s<br />
the topic of the next section: combinatorics.
</p>
<p><h2 id="sec-cmb">3
Combinatorics : counting evolved
</h2>
</p>
<p>
Very briefly, 
<a href="http://en.wikipedia.org/wiki/combinatorics">combinatorics</a> is the study of counting<br />
discrete variations. In particular, how you can <b>quickly</b> count<br />
them all in an orderly and correct fashion.<br />
In this case, what I&#8217;ll need is a way to quickly count the different<br />
possible ways of distributing items over a number of bins. What also<br />
matters is whether the items are <b>unique</b> (that is to say, whether<br />
you can tell them apart from other items) or <b>non-unique</b>.
</p>
<p>
I&#8217;ll start with unique items and then show how to incorporate<br />
non-unique ones. This will involve factorials (<i>n</i>!). Along the<br />
way, we&#8217;ll pick up permutations (<i>n</i>P<i>r</i>) and<br />
combinations (<i>n</i>C<i>r</i>), which are merely special cases of the<br />
general formula.
</p>
<p><h3 id="ssec-cmb-unique">3.1
Arrangements with unique items
</h3>
</p>
<p>
The specific task here is to find out in how many ways you can<br />
distribute <i>n</i> unique objects over <i>n</i> different slots. This<br />
is a case of combinatorics <dfn>without replacement</dfn>, because once<br />
you&#8217;ve placed an object in a particular slot, you can&#8217;t use it anywhere<br />
else.
</p>
<p>
I&#8217;ll start by looking at an example with four objects,<br />
<i>a</i>, <i>b</i>, <i>c</i> and <i>d</i>. Naturally, there will also<br />
be four slots to put them in. To count the possibilities, we&#8217;ll make<br />
a tree (see Fig&nbsp;2). There are two ways to fill the<br />
tree: by a slot or by item. When working without replacement, it&#8217;s more<br />
convenient to fill by item. Right now it won&#8217;t matter that much,<br />
because when you start dealing with groups of non-unique items, it has<br />
a nice way of grouping all of those on the same depth.
</p>
<div class="cblock">
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-tree-abcd"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;2. </b>
</div>

</div>
<p>
As you can see in Fig&nbsp;2, we start with four open<br />
slots and four items. First we place <i>a</i>, which has a choice of<br />
all four slots. For <i>b</i>, only three slots are left open; <i>c</i><br />
has only 2 and <i>d</i> takes the last remaining slot. The point here<br />
is that each next item has one fewer slot. The last column has all the<br />
different arrangements, and as you can see there are 24 in total.<br />
The really important point, though, is to note that all the branches<br />
for a given level has the same number of sub-branches. This means that<br />
you can simply multiply the number of slots left, leading to<br />
4&middot;3&middot;2&middot;1. In other words, it&#8217;s simply the<br />
factorial: 4!.
</p>
<p>
Generalizing this, given <i>n</i> unique items, the number of variations<br />
is <i>n</i>!.
</p>
<p><h3 id="ssec-cmb-non-unique">3.2
Arrangements with non-unique items
</h3>
</p>
<p>
Now lets look at what happens when there are also identical-looking<br />
items. For example, lets take one <i>a</i>, one <i>b</i> and<br />
<b>three</b> <i>c</i>s, for a total of five items<br />
(see Fig&nbsp;3). Technically, for<br />
five items you&#8217;d get 5! variations, however because the <i>c</i>s are<br />
identical some solutions are actually the same: we&#8217;ve overcounted.<br />
To resolve this, we need to know how many variations are actually the<br />
same. This is simply the number of variations within the<br />
<i>c</i>-group. With 3 <i>c</i>s, that means 3!. So to get the final<br />
answer, we divide by 3!.
</p>
<div class="cblock">
<div class="cpt" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-tree-abccc"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;3. </b>
</div>
</p>
<p>
This is becoming similar to the case in section 2.6.<br />
Again, take the case that you have <i>m</i> groups,<br />
<i>k</i><sub>j</sub> identical items per group, and<br />
<i>n</i>&nbsp;=&nbsp;&Sigma;<sub>j</sub>&nbsp;<i>k</i><sub>j</sub> items in total.<br />
If all items were unique, you&#8217;d get <i>n</i>! variations, but in each<br />
group you&#8217;re counting <i>k</i><sub>j</sub>! times too many. The proper<br />
number is <i>n</i>! divided by all the overcounts. This term is called<br />
the <dfn>
<a href="http://en.wikipedia.org/wiki/Multinomial_theorem">multinomial coefficient</a></dfn>,<br />
and defined as:
</p>
<p><table class="eqtbl" id="eq-cmb-mnom">
<tr>
<td class="eqnrcell">(9)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?A%28k_1%20X_1%2C%20...%2C%20k_m%20X_m%29%20%3D%20%7B%20n%20%5Cchoose%20k_1%2C%5C%2C...%2C%5C%2Ck_m%20%7D%20%3D%20%7B%20%7B%20%5C%28k_1%20%2B%20...%20%2B%20k_m%5C%29%21%20%7D%20%5Cover%20%7B%20k_1%21%20%5C%2C%5Ccdot%5C%2C%20...%20%5C%2C%5Ccdot%5C%2C%20k_m%21%20%7D%20%7D%20%3D%20%7B%20%7Bn%21%7D%20%5Cover%20%7B%20%5Cprod_%7Bj%3D1%7D%5Em%5C%2C%20k_j%21%7D%20%7D'<br />
	title="A(k_1 X_1, ..., k_m X_m) = { n \choose k_1,\,...,\,k_m } = { { \(k_1 + ... + k_m\)! } \over { k_1! \,\cdot\, ... \,\cdot\, k_m! } } = { {n!} \over { \prod_{j=1}^m\, k_j!} }"<br />
	alt="A(k_1 X_1, ..., k_m X_m) = { n \choose k_1,\,...,\,k_m } = { { \(k_1 + ... + k_m\)! } \over { k_1! \,\cdot\, ... \,\cdot\, k_m! } } = { {n!} \over { \prod_{j=1}^m\, k_j!} }" /><br />
</td>
</tr>
</table></p>
<p>
As an extra example of this, consider again the case with 2 <i>a</i>s,<br />
3 <i>b</i>s and 4 <i>c</i>s. That is, <i>k</i><sub>a</sub>&nbsp;=&nbsp;2,<br />
<i>k</i><sub>b</sub>&nbsp;=&nbsp;3 and <i>k</i><sub>c</sub>&nbsp;=&nbsp;4, and<br />
<i>n</i>&nbsp;=&nbsp;9. The number of arrangements is<br />
n! / (<i>k</i><sub>a</sub>! <i>k</i><sub>b</sub>!<br />
<i>k</i><sub>c</sub>!)&nbsp;=&nbsp; 9!/(2! 3! 4!) = 1260.
</p>
<p><div>&nbsp;</div></p>
<p>
There&#8217;s also a neat little way of describing this graphically, via a<br />
table (see Table&nbsp;2). First, you take the most regular<br />
answer, where everything&#8217;s divided into groups, like<br />
<i>aabbbcccc</i>. Write the indices above each term, ultimately<br />
resulting in <i>n</i>!. Now break up the string into groups index<br />
each group, giving you  all the <i>k</i><sub>j</sub>! terms. The<br />
result is dividing the full indexed part with the group-parts.
</p>
<div class="lblock">
<table id="tbl-mnom"<br />
  border=0 cellpadding=2 cellspacing=0><br />
<caption>
  <b>Table&nbsp;2</b>. Multinomial expressed in tabular form.<br />
</caption>
<tr>
<td>
<table border=0 cellpadding=2 cellspacing=0>
<tr>
<td class="bdrL">9</td>
<td>8</td>
<td>7</td>
<td>6</td>
<td>5</td>
<td>4</td>
<td>3</td>
<td>2</td>
<td class="bdrR">1</td>
</tr>
<tr class="bdrT bdrB">
<th class="bdrL">a</th>
<th class="bdrR">a</th>
<th>b</th>
<th>b</th>
<th class="bdrR">b</th>
<th>c</th>
<th>c</th>
<th>c</th>
<th class="bdrR">c</th>
</tr>
<tr>
<td class="bdrL">2</td>
<td class="bdrR">1</td>
<td>3</td>
<td>2</td>
<td class="bdrR">1</td>
<td>4</td>
<td>3</td>
<td>2</td>
<td class="bdrR">1</td>
</tr>
</table>
</td>
<td>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5C%3B%5C%3B%5CRightarrow%5C%3B%5C%3B%7B%209%21%20%5Cover%202%21%203%21%204%21%7D'<br />
	title="\;\;\Rightarrow\;\;{ 9! \over 2! 3! 4!}"<br />
	alt="\;\;\Rightarrow\;\;{ 9! \over 2! 3! 4!}" /></p>
</td>
</tr>
</table>
</div>
<p><h3 id="ssec-cmb-pc">3.3
Permutations and combinations
</h3>
</p>
<p>
Permutations and combinations are merely special cases of<br />
Eq&nbsp;9. This is not how it&#8217;s generally taught, but<br />
it&#8217;s still true. In  <dfn>permutation</dfn>, you have <i>r</i> <b>unique</b><br />
items and <i>n</i> slots. In other words, you have <i>n</i>&minus;<i>r</i><br />
identical, empty slots. You only have an overcount for the empty<br />
slots, so you end up with
</p>
<p><table class="eqtbl" id="eq-cmb-perm">
<tr>
<td class="eqnrcell">(10)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5EnP_r%20%3D%20%7B%20%7B%20n%21%7D%20%5Cover%20%7B%20%5Cunderbrace%7B1%21...1%21%7D_%7Br%5C%2C%5Ctext%7Btimes%7D%7D%20%28n-r%29%21%20%7D%20%7D%20%3D%20%7B%20%7B%20n%21%7D%20%5Cover%20%7B%28n-r%29%21%20%7D%20%7D.'<br />
	title="^nP_r = { { n!} \over { \underbrace{1!...1!}_{r\,\text{times}} (n-r)! } } = { { n!} \over {(n-r)! } }."<br />
	alt="^nP_r = { { n!} \over { \underbrace{1!...1!}_{r\,\text{times}} (n-r)! } } = { { n!} \over {(n-r)! } }." /><br />
</td>
</tr>
</table></p>
<p>
This also has another name: the <dfn>
<a href="http://en.wikipedia.org/wiki/falling%20factorial">falling factorial</a></dfn>,<br />
written as <i>n</i><sub>(r)</sub>. It&#8217;s called that because it&#8217;s<br />
basically the top <i>r</i> terms of a factorial. For example,<br />
<sup>7</sup>P<sub>3</sub> = 7<sub>(3)</sub> = 7&middot;6&middot;5.<br />
It&#8217;s written out in the equation below. Notice how the lower terms<br />
cancel out.
</p>
<p><table class="eqtbl">
<tr>
<td class="eqnrcell"></td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5E7P_3%20%3D%20%5Cfrac%7B7%21%7D%7B%287-3%29%21%7D%20%3D%20%5Cfrac%7B7%21%7D%7B4%21%7D%20%3D%20%5Cfrac%7B7%20%5Ccdot%206%20%5Ccdot%205%20%5Ccdot%7D%7B%5C%2C%7D%20%5Cfrac%7B4%20%5Ccdot%203%20%5Ccdot%202%20%5Ccdot%201%7D%7B4%20%5Ccdot%203%20%5Ccdot%202%20%5Ccdot%201%7D%3D%207%20%5Ccdot%206%20%5Ccdot%205'<br />
	title="^7P_3 = \frac{7!}{(7-3)!} = \frac{7!}{4!} = \frac{7 \cdot 6 \cdot 5 \cdot}{\,} \frac{4 \cdot 3 \cdot 2 \cdot 1}{4 \cdot 3 \cdot 2 \cdot 1}= 7 \cdot 6 \cdot 5"<br />
	alt="^7P_3 = \frac{7!}{(7-3)!} = \frac{7!}{4!} = \frac{7 \cdot 6 \cdot 5 \cdot}{\,} \frac{4 \cdot 3 \cdot 2 \cdot 1}{4 \cdot 3 \cdot 2 \cdot 1}= 7 \cdot 6 \cdot 5" /><br />
</td>
</tr>
</table></p>
<p>
For <b>combinations</b>, you have <i>r</i> <b>non-unique</b> items<br />
over <i>n</i> slots. In this case you have two groups with overcounts,<br />
one of size <i>r</i> and one of size <i>n</i>&minus;r. This becomes
</p>
<p><table class="eqtbl" id="eq-cmb-combi">
<tr>
<td class="eqnrcell">(11)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5EnC_r%20%3D%20%7B%20n%20%5Cchoose%20r%20%7D%20%3D%20%7B%20n%21%20%5Cover%20r%21%20%28n-r%29%21%20%7D%20%3D%20%7B%5EnP_r%20%5Cover%20r%21%7D.'<br />
	title="^nC_r = { n \choose r } = { n! \over r! (n-r)! } = {^nP_r \over r!}."<br />
	alt="^nC_r = { n \choose r } = { n! \over r! (n-r)! } = {^nP_r \over r!}." /><br />
</td>
</tr>
</table></p>
<p>
The combination has three interesting properties.
</p>
<ol>
<li>
    It can be interpreted as a variation from the permutation,<br />
	namely by switching from unique to non-unique items. That&#8217;s<br />
	where the division by <i>r</i>! comes from.
  </li>
<li>
    Two, it&#8217;s symmetric between <i>r</i> and <i>n</i>&minus;<i>r</i>.<br />
	That is, <sup>n</sup>C<sub>r</sub> and <sup>n</sup>C<sub>n&minus;r</sub><br />
	are identical.
  </li>
<li>
    Three, it uses two groups of identical items, which is exactly<br />
	what you have in the case of the binomial distribution: one set<br />
	of wins and one set of fails. This is why it shows up as a<br />
	multiplier in Eq&nbsp;1, and why the combination<br />
	is also called the <dfn>binomial coefficient</dfn>.
  </li>
</ol>
<p><div>&nbsp;</div></p>
<p>
Instead of looking at unique versus non-unique, you can also<br />
interpret permutation and combination by looking at whether the order<br />
of the positions matter. If it does (that is, &#8220;123&#8243; is not the same as<br />
&#8220;132&#8243;), then it&#8217;s a permutation. If the order doesn&#8217;t matter, you have<br />
a combination.
</p>
<p><div>&nbsp;</div></p>
<p>
In the above , I&#8217;ve considered the binomial coefficient as a special<br />
case of the multinomial, but you can also do the reverse: string<br />
multiple binomials together to form a multinomial. In<br />
this case, you first distribute <i>k</i><sub>1</sub> elements over the<br />
whole <i>n</i> slots. This can be done in <sup>n</sup>C<sub>k1</sub><br />
ways. At this point, you&#8217;ve got <i>n</i>&minus;<i>k</i><sub>1</sub> slots<br />
remaining, over which you distribute <i>k</i><sub>2</sub>. This<br />
leaves <i>n</i>&minus;<i>k</i><sub>1</sub>&minus;<i>k</i><sub>2</sub> for group<br />
3, and so on until you&#8217;ve got no slots and items left. This is illustrated<br />
by Eq&nbsp;12. Note how one part of the denominator of<br />
one term cancels out against the numerator of the next.
</p>
<p><table class="eqtbl" id="eq-cmb-mnom2">
<tr>
<td class="eqnrcell">(12)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20%7B%20n%20%5Cchoose%20k_1%20%7D%20%7B%20n-k_1%20%5Cchoose%20k_2%20%7D%20%7B%20n-k_1-k_2%20%5Cchoose%20k_3%20%7D%20%5Cdots%20%7B%20k_m%20%5Cchoose%20k_m%20%7D%20%26%3D%26%20%7B%20n%21%20%5Cover%20k_1%21%20%28n-k_1%29%21%20%7D%5C%2C%20%7B%20%28n-k_1%29%21%20%5Cover%20k_2%21%20%28n-k_1-k_2%29%21%20%7D%20%5C%2C%5Ccdots%5C%2C%20%7B%20k_m%21%20%5Cover%20k_m%21%200%21%20%7D%20%5C%5C%20%26%3D%26%20%7B%20n%21%20%5Cover%20k_1%21%5C%2Ck_2%21%5C%2C%5Ccdots%5C%2Ck_m%21%20%7D%20%5C%5C%20%26%3D%26%20%7B%20n%20%5Cchoose%20k_1%2C%5C%2C%20k2%2C%20%5C%2C...%5C%2C%20%2C%5C%2C%20k_m%20%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} { n \choose k_1 } { n-k_1 \choose k_2 } { n-k_1-k_2 \choose k_3 } \dots { k_m \choose k_m } &amp;=&amp; { n! \over k_1! (n-k_1)! }\, { (n-k_1)! \over k_2! (n-k_1-k_2)! } \,\cdots\, { k_m! \over k_m! 0! } \\ &amp;=&amp; { n! \over k_1!\,k_2!\,\cdots\,k_m! } \\ &amp;=&amp; { n \choose k_1,\, k2, \,...\, ,\, k_m } \end{eqnarray}"<br />
	alt="\begin{eqnarray} { n \choose k_1 } { n-k_1 \choose k_2 } { n-k_1-k_2 \choose k_3 } \dots { k_m \choose k_m } &amp;=&amp; { n! \over k_1! (n-k_1)! }\, { (n-k_1)! \over k_2! (n-k_1-k_2)! } \,\cdots\, { k_m! \over k_m! 0! } \\ &amp;=&amp; { n! \over k_1!\,k_2!\,\cdots\,k_m! } \\ &amp;=&amp; { n \choose k_1,\, k2, \,...\, ,\, k_m } \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
The binomial and multinomial coefficients are usually written as a<br />
2-by-1 matrix between parentheses (see Eq&nbsp;13).<br />
The construct is pronounced &ldquo;<i>n</i> choose <i>k</i>&rdquo;.<br />
No, I don&#8217;t know why either.
</p>
<p><table class="eqtbl" id="eq-cmb-choose">
<tr>
<td class="eqnrcell">(13)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20%5Ctext%7BBinomial%3A%20%7D%26%20%7B%20n%20%5Cchoose%20k%7D%3D%20%7B%20n%21%20%5Cover%20k%21%5C%2C%28n-k%29%21%20%7D%20%5C%5C%20%5Ctext%7BMultinomial%3A%20%7D%26%20%7B%20n%20%5Cchoose%20k_1%2C%20k_2%2C%20%5C%2C...%5C%2C%2C%20k_m%20%7D%3D%20%7B%20n%21%20%5Cover%20k_1%21%5C%2Ck_2%21%5C%2C%5Ccdots%5C%2Ck_m%21%20%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} \text{Binomial: }&amp; { n \choose k}= { n! \over k!\,(n-k)! } \\ \text{Multinomial: }&amp; { n \choose k_1, k_2, \,...\,, k_m }= { n! \over k_1!\,k_2!\,\cdots\,k_m! } \end{eqnarray}"<br />
	alt="\begin{eqnarray} \text{Binomial: }&amp; { n \choose k}= { n! \over k!\,(n-k)! } \\ \text{Multinomial: }&amp; { n \choose k_1, k_2, \,...\,, k_m }= { n! \over k_1!\,k_2!\,\cdots\,k_m! } \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p><h3>
Back to the binomial
</h3>
</p>
<p>
Now that both parts of the division has been covered, lets go back to<br />
the binomial distribution and see how they fit together. For<br />
convenience, here&#8217;s what we were talking about again.
</p>
<p><table class="eqtbl" id="eq-binom-ex">
<tr>
<td class="eqnrcell">(14)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20P%28W%3Dk%20%5Ctext%7B%20in%20%7D%20n%29%20%26%3D%26%20%7Bn%5Cchoose%20k%7D%20p%5Ek%20%281-p%29%5E%7Bn-k%7D%20%5C%5C%20%5Ctext%7Bor%7D%20%5C%5C%20P%28W%3Dk%2C%20F%3Dn-k%29%20%26%3D%26%20%7Bn%5Cchoose%20k%7D%20p_w%5Ek%20%5C%2C%20p_f%5E%7Bn-k%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} P(W=k \text{ in } n) &amp;=&amp; {n\choose k} p^k (1-p)^{n-k} \\ \text{or} \\ P(W=k, F=n-k) &amp;=&amp; {n\choose k} p_w^k \, p_f^{n-k} \end{eqnarray}"<br />
	alt="\begin{eqnarray} P(W=k \text{ in } n) &amp;=&amp; {n\choose k} p^k (1-p)^{n-k} \\ \text{or} \\ P(W=k, F=n-k) &amp;=&amp; {n\choose k} p_w^k \, p_f^{n-k} \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<ol>
<li>
    The base chance of getting a specific result that satisfies your<br />
	conditions.
  </li>
<li>
    The number of results that satisfies the conditions (each with the<br />
	same base probability).
  </li>
</ol>
<p>
The thing about these two parts is that they actually represent two of<br />
the rules of probability, namely those describing how probabilities<br />
combine. Part 1, the base chance is a joint condition; an AND case.<br />
The second part covers all the mutually exclusive ways that the joint<br />
condition can be satisfied.
</p>
<p>
For example with 2 <i>a</i>s and 1 <i>b</i>,<br />
the ways the cases combine are <i>aab</i>, <i>aba</i> or <i>baa</i>. If<br />
you were to write it out completely, you&#8217;d get
</p>
<p><table class="eqtbl">
<tr>
<td class="eqnrcell"></td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28A%3D2%2CB%3D1%29%20%3D%20%5Cbegin%7Beqnarray%7D%20%28%5C%2CP%28A%29%20%5Ctext%7B%20and%20%7D%20P%28A%29%20%5Ctext%7B%20and%20%7D%20P%28B%29%5C%2C%29%20%26%5Ctext%7B%20or%20%7D%20%5C%5C%20%28%5C%2CP%28A%29%20%5Ctext%7B%20and%20%7D%20P%28B%29%20%5Ctext%7B%20and%20%7D%20P%28A%29%5C%2C%29%20%26%5Ctext%7B%20or%20%7D%20%5C%5C%20%28%5C%2CP%28B%29%20%5Ctext%7B%20and%20%7D%20P%28A%29%20%5Ctext%7B%20and%20%7D%20P%28A%29%5C%2C%29%20%26%20%5Cend%7Beqnarray%7D'<br />
	title="P(A=2,B=1) = \begin{eqnarray} (\,P(A) \text{ and } P(A) \text{ and } P(B)\,) &amp;\text{ or } \\ (\,P(A) \text{ and } P(B) \text{ and } P(A)\,) &amp;\text{ or } \\ (\,P(B) \text{ and } P(A) \text{ and } P(A)\,) &amp; \end{eqnarray}"<br />
	alt="P(A=2,B=1) = \begin{eqnarray} (\,P(A) \text{ and } P(A) \text{ and } P(B)\,) &amp;\text{ or } \\ (\,P(A) \text{ and } P(B) \text{ and } P(A)\,) &amp;\text{ or } \\ (\,P(B) \text{ and } P(A) \text{ and } P(A)\,) &amp; \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p>
Obviously, you&#8217;re not going to write this out all the time,<br />
especially not when the numbers get large, but this is really what<br />
you&#8217;re looking for. The use of the base-chance and combinations are<br />
ways of shortcircuiting this. As said, the base-chance is the<br />
probability of the string of ANDs, which will be the same for case.<br />
Because of this, the addition of all the (X)ORs simply becomes a<br />
multiplication by the number of (X)ORs that you have, which is<br />
given by the binomial, or multinomial if you have more than one option.
</p>
<p><div>&nbsp;</div></p>
<p>
Another interesting point is that the binomial and multinomial<br />
coefficients themselves can be read as probabilities as well. It&#8217;s just<br />
that because we&#8217;re certain <i>k</i><sub>j</sub> items will exist if<br />
we&#8217;re just arranging items, the probability for each group is 1, so<br />
the whole probability part is unnecessary.
</p>
<p><h2 id="sec-eg">4
Examples
</h2>
</p>
<p>
Because I know all of this can be a little mesmerizing at first, I<br />
thought I&#8217;d give a few examples as well.
</p>
<p><h3 id="ssec-eg-binom">4.1
Beastly dice
</h3>
</p>
<p>
First, simple example of the binomial distribution. Say we have<br />
five dice (5d6) and we&#8217;re looking at the different ways of getting<br />
exactly three sixes. Note that this also means the remaining cases<br />
should be a non-six!
</p>
<p>
First, the probability of a six is <i>p</i><sub>6</sub>&nbsp;=&nbsp;1/6, and<br />
of a non-six <i>p</i><sub>&not;6</sub>&nbsp;=&nbsp;5/6. As for the group-counts,<br />
we have 2 groups with <i>k</i>&nbsp;=&nbsp;<i>k</i><sub>6</sub>&nbsp;=&nbsp;3 and<br />
<i>k</i><sub>&not;6</sub>&nbsp;=&nbsp;2, and <i>n</i>&nbsp;=&nbsp;5. Putting this together<br />
results in Eq&nbsp;15. Note the base chance fo getting<br />
any triple-six result, and the number of ways you can vary this<br />
over five dice.
</p>
<p><table class="eqtbl" id="eq-eg-666">
<tr>
<td class="eqnrcell">(15)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%283%20%5Ctimes%206%20%5Ctext%7B%20in%20%7D%205%29%20%3D%20P%283%20%5Ctimes%206%20%5C%2C%5C%26%5C%2C%202%20%5Ctimes%20%5Cnot6%29%20%3D%20%7B%205%20%5Cchoose%203%7D%20p_6%5E3%20p_%7B%5Cnot6%7D%5E2%20%3D%2010%5Ccdot%5C%28%5Cfrac16%5C%29%5E3%5C%28%5Cfrac56%5C%29%5E2%20%3D%203.21%25'<br />
	title="P(3 \times 6 \text{ in } 5) = P(3 \times 6 \,\&amp;\, 2 \times \not6) = { 5 \choose 3} p_6^3 p_{\not6}^2 = 10\cdot\(\frac16\)^3\(\frac56\)^2 = 3.21%"<br />
	alt="P(3 \times 6 \text{ in } 5) = P(3 \times 6 \,\&amp;\, 2 \times \not6) = { 5 \choose 3} p_6^3 p_{\not6}^2 = 10\cdot\(\frac16\)^3\(\frac56\)^2 = 3.21%" /><br />
</td>
</tr>
</table></p>
<p>
There&#8217;s also another way of getting to this result, namely by direct<br />
use of the definition of probability, Eq&nbsp;2. To do<br />
this, we need know the total number of possibilities, and how many<br />
will result in 3 sixes. With five dice, the total is<br />
<i>T</i>&nbsp;=&nbsp;6<sup>5</sup>. There are <sup>5</sup>C<sub>3</sub>&nbsp;=&nbsp;10<br />
ways of getting exactly 3 sixes in five dice and 5 non-six options<br />
for the remaining two dice, leading to a total of valid cases of<br />
10&middot;5<sup>2</sup>&nbsp;=&nbsp;250.
</p>
<p><table class="eqtbl">
<tr>
<td class="eqnrcell"></td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%283%20%5Ctimes%206%20%5Ctext%7B%20in%20%7D%205%29%20%3D%20%7B%20%23%283%5Ctimes%206%20%5Ctext%7B%20in%20%7D%205%29%20%5Cover%20%23%5Ctext%7Btotal%7D%20%7D%20%3D%20%7B%2010%20%5Ccdot%205%5E2%20%5Cover%205%5E6%20%7D%20%3D%20%5Cfrac%7B250%7D%7B7776%7D%20%3D%203.21%25'<br />
	title="P(3 \times 6 \text{ in } 5) = { #(3\times 6 \text{ in } 5) \over #\text{total} } = { 10 \cdot 5^2 \over 5^6 } = \frac{250}{7776} = 3.21%"<br />
	alt="P(3 \times 6 \text{ in } 5) = { #(3\times 6 \text{ in } 5) \over #\text{total} } = { 10 \cdot 5^2 \over 5^6 } = \frac{250}{7776} = 3.21%" /><br />
</td>
</tr>
</table></p>
<p><h3 id="ssec-eg-full">4.2
Yahtzee : full house
</h3>
</p>
<p>
More dice, this time looking at the yahtzee game. A full house is three<br />
of a kind plus a pair, like 66655, 11122 or 22333, and any variation<br />
thereof. In this particular case I want to look at getting a full house<br />
in one go, because to take all three turns into account is way too<br />
hairy.
</p>
<p>
The case is actually similar to the previous one in that you have<br />
3 of one group and 2 of another, but the probabilities are a little<br />
different. For a specific full house, say 11122 the base chance is<br />
1/6<sup>5</sup>. However, we&#8217;re not looking for a specific one, but any<br />
full house. This means that there will be 6 options for the first group,<br />
and 5 for the second. The final probability is
</p>
<p><table class="eqtbl" id="eq-eg-full">
<tr>
<td class="eqnrcell">(16)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28%5Ctext%7Bany%20full%20house%7D%29%20%3D%206%20%5Ccdot%205%20%5Ctimes%20P%28%5Ctext%7Bfull%20house%7D%29%20%3D%2030%5Ccdot%7B%205%20%5Cchoose%203%20%7D%20%5Ctimes%20%5C%28%5Cfrac16%5C%29%5E5%20%3D%2030%5Ccdot10%20%5Ctimes%20%5Cfrac1%7B7776%7D%20%3D%203.86%25'<br />
	title="P(\text{any full house}) = 6 \cdot 5 \times P(\text{full house}) = 30\cdot{ 5 \choose 3 } \times \(\frac16\)^5 = 30\cdot10 \times \frac1{7776} = 3.86%"<br />
	alt="P(\text{any full house}) = 6 \cdot 5 \times P(\text{full house}) = 30\cdot{ 5 \choose 3 } \times \(\frac16\)^5 = 30\cdot10 \times \frac1{7776} = 3.86%" /><br />
</td>
</tr>
</table></p>
<p>
An alternative approach is to consider the group-determining dice to be<br />
&ldquo;free&rdquo;, giving the probability 6/6 and 5/6 for the first<br />
and second group, respectively. After that, the remaining dice have to<br />
follow suit to give (1/6)<sup>3</sup>.
</p>
<p><h3 id="ssec-eg-wheel">4.3
Multinomial : spinning wheels
</h3>
</p>
<p><div class="cptfr" style="width:px;">
  <a href="" target="_blank">  <img src="" id="img-wheel"
    alt="" width="" /></a><br />
  <b>Fig&nbsp;4. </b>
</div>
</p>
<p>
And now for an application of the multinomial. Consider the<br />
wheel from Fig&nbsp;4. It has 2 <i>a</i>s, 3 <i>b</i>s<br />
and 5 <i>c</i>s: <i>n</i><sub>a</sub>&nbsp;=&nbsp;2,<br />
<i>n</i><sub>b</sub>&nbsp;=&nbsp;3, <i>n</i><sub>c</sub>&nbsp;=&nbsp;5.<br />
The probabilities for each letter follow from the probability definition,<br />
resulting in<br />
<i>p</i><sub>a</sub>&nbsp;=&nbsp;2/10&nbsp;=&nbsp;20%,<br />
<i>p</i><sub>b</sub>&nbsp;=&nbsp;3/10&nbsp;=&nbsp;30% and<br />
<i>p</i><sub>c</sub>&nbsp;=&nbsp;5/10&nbsp;=&nbsp;50%.
</p>
<p>
The question is: what&#8217;s the chance of getting 3 <i>a</i>s, 2 <i>c</i>s<br />
in 9 spins?
</p>
<p><div>&nbsp;</div></p>
<p>
Well, first you have to note that 3+2 is less than 9, so there&#8217;s a<br />
hidden condition: the remaining 4 tries should be a <i>b</i>. This<br />
transforms the question into 3 <i>a</i>s, 4 <i>b</i>s and 2 <i>c</i>s,<br />
or<br />
<i>k</i><sub>a</sub>&nbsp;=&nbsp;3,<br />
<i>k</i><sub>b</sub>&nbsp;=&nbsp;4,<br />
<i>k</i><sub>c</sub>&nbsp;=&nbsp;2, Also note that the number of results have<br />
nothing to do with how many appear on the wheel.
</p>
<p><table class="eqtbl" id="eq-eg-wheel">
<tr>
<td class="eqnrcell">(17)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20P%28A%3D3%2CC%3D2%20%5Ctext%7B%20in%20%7D%209%29%20%26%3D%26%20P%28A%3D3%2CB%3D4%2CC%3D2%29%20%5C%5C%20%5Cvspace%7B10%7D%20%5C%5C%20%26%3D%26%20%7B%20%5C%28k_a%20%2B%20k_b%20%2B%20k_c%5C%29%21%20%5Cover%20k_a%21%5C%2Ck_b%21%5C%2Ck_c%21%20%7D%20p_a%5E%7Bk_a%7D%5C%2Cp_b%5E%7Bk_b%7D%5C%2Cp_c%5E%7Bk_c%7D%5C%2C%20%5C%5C%20%5Cvspace%7B10%7D%20%5C%5C%20%26%3D%26%20%7B%20%283%2B4%2B2%29%21%20%5Cover%203%21%204%21%202%21%20%7D%20%5C%28%5Cfrac2%7B10%7D%5C%29%5E3%20%5C%2C%20%5C%28%5Cfrac3%7B10%7D%5C%29%5E4%20%5C%2C%20%5C%28%5Cfrac5%7B10%7D%5C%29%5E2%5C%2C%20%5C%5C%20%5Cvspace%7B10%7D%20%5C%5C%20%26%3D%26%201260%20%5Ccdot%20%5Cfrac%7B8%20%5Ccdot%2081%20%5Ccdot%2025%20%7D%7B10%5E9%7D%20%3D%202.04%20%25%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} P(A=3,C=2 \text{ in } 9) &amp;=&amp; P(A=3,B=4,C=2) \\ \vspace{10} \\ &amp;=&amp; { \(k_a + k_b + k_c\)! \over k_a!\,k_b!\,k_c! } p_a^{k_a}\,p_b^{k_b}\,p_c^{k_c}\, \\ \vspace{10} \\ &amp;=&amp; { (3+4+2)! \over 3! 4! 2! } \(\frac2{10}\)^3 \, \(\frac3{10}\)^4 \, \(\frac5{10}\)^2\, \\ \vspace{10} \\ &amp;=&amp; 1260 \cdot \frac{8 \cdot 81 \cdot 25 }{10^9} = 2.04 % \end{eqnarray}"<br />
	alt="\begin{eqnarray} P(A=3,C=2 \text{ in } 9) &amp;=&amp; P(A=3,B=4,C=2) \\ \vspace{10} \\ &amp;=&amp; { \(k_a + k_b + k_c\)! \over k_a!\,k_b!\,k_c! } p_a^{k_a}\,p_b^{k_b}\,p_c^{k_c}\, \\ \vspace{10} \\ &amp;=&amp; { (3+4+2)! \over 3! 4! 2! } \(\frac2{10}\)^3 \, \(\frac3{10}\)^4 \, \(\frac5{10}\)^2\, \\ \vspace{10} \\ &amp;=&amp; 1260 \cdot \frac{8 \cdot 81 \cdot 25 }{10^9} = 2.04 % \end{eqnarray}" /><br />
</td>
</tr>
</table></p>
<p><h2 id="sec-summary">5
Summary
</h2>
</p>
<p>
As this post covers quite a bit, I think a summary might be in order.
</p>
<ul>
<li>
    Rules of probability:</p>
<ol>
<li>
	    <b>Definition</b>: the probability of event <i>X</i> is the<br />
		number of occurances over the total number of events.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%29%20%3D%20%7B%7B%23%20%5Ctext%7BXs%7D%7D%20%5Cover%20%7B%20%5Ctext%7BAll%20events%7D%7D%7D'<br />
	title="P(X) = {{# \text{Xs}} \over { \text{All events}}}"<br />
	alt="P(X) = {{# \text{Xs}} \over { \text{All events}}}" /><br />
<br/><br />
		Do not forget this rule, as its the basis for all the others.
	  </li>
<li>
	    <b>Complementary rule (NOT)</b>. The probability of not getting<br />
		<i>X</i> is the rest of the cases.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28%5Cneg%20X%29%20%3D%201%20-%20P%28X%29'<br />
	title="P(\neg X) = 1 - P(X)"<br />
	alt="P(\neg X) = 1 - P(X)" /></p>
</li>
<li>
	    <b>Disjoint/Parallel condition ((X)OR)</b> :<br />
		intersection of cases; probabilities add.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%20%5Coplus%20Y%29%20%3D%20P%28X%29%20%2B%20P%28Y%29'<br />
	title="P(X \oplus Y) = P(X) + P(Y)"<br />
	alt="P(X \oplus Y) = P(X) + P(Y)" /></p>
</li>
<li>
	    <b>Joint/Serial condition (AND)</b> :<br />
		union of cases; probabilities multiply.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?P%28X%20%5C%26%20Y%29%20%3D%20P%28X%29%20%5Ccdot%20P%28Y%29'<br />
	title="P(X \&amp; Y) = P(X) \cdot P(Y)"<br />
	alt="P(X \&amp; Y) = P(X) \cdot P(Y)" /></p>
</li>
</ol>
<p>	The NOT, XOR and AND terms have commonalities with those used in<br />
	boolean logic, and electrical engineering.
  </li>
<li>
    Compound-condition probabilities usually involve both AND and<br />
    OR conditions, making up the two parts of a<br />
	binomial/multinomial probability.</p>
<ol>
<li>The AND conditions results in the probability of<br />
	    <i>one</i> solution, which multiplies all the probabilities of<br />
	    each sub-sample.
	  </li>
<li>
	    The OR condition counts how many solutions there are,<br />
		given by the bi-/multinomial coefficient.
	  </li>
</ol>
</li>
</ul>
<p><div>&nbsp;</div></p>
<ul>
<li>
    <b>Factorial ( <i>n</i>! )</b>.<br />
	The factorial represents the number of ways you can arrange<br />
	<i>n</i> unique/distinct items.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?n%21%20%3D%201%5Ccdot2%5Ccdot%20%5C%2C...%5C%2C%20%5Ccdot%28n-1%29%5Ccdot%20n'<br />
	title="n! = 1\cdot2\cdot \,...\, \cdot(n-1)\cdot n"<br />
	alt="n! = 1\cdot2\cdot \,...\, \cdot(n-1)\cdot n" /></p>
</li>
<li>
    <b>Permutation ( <sup>n</sup>P<sub>r</sub> )</b>.<br />
	Permutation refers to how many ways you can distribute <i>r</i><br />
	unique/distinct items over <i>n</i> slots, or when the order<br />
	of the elements matters.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5EnP_r%20%3D%20n_%7B%28r%29%7D%20%3D%20%7B%20%7B%20n%21%7D%20%5Cover%20%7B%28n-r%29%21%20%7D%20%7D'<br />
	title="^nP_r = n_{(r)} = { { n!} \over {(n-r)! } }"<br />
	alt="^nP_r = n_{(r)} = { { n!} \over {(n-r)! } }" /></p>
</li>
<li>
    <b>Combination ( <sup>n</sup>C<sub>r</sub> )</b>.<br />
	Combination refers to how many ways you can distribute <i>r</i><br />
	non-unique/indistinct items over <i>n</i> slots, or when the order<br />
	of the elements does not matter.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5EnC_r%20%3D%20%7B%5EnP_r%7D%2Fr%21%20%3D%20%7B%20n%21%20%5Cover%20r%21%28n-r%29%21%20%7D'<br />
	title="^nC_r = {^nP_r}/r! = { n! \over r!(n-r)! }"<br />
	alt="^nC_r = {^nP_r}/r! = { n! \over r!(n-r)! }" /></p>
</li>
<li>
    <b>Binomial / Multinomial coefficient (<br />
	<i>n</i> choose <i>k</i>(<sub>1</sub>,<br />
	<i>k</i><sub>2</sub>, &hellip; <i>k</i><sub>m</sub>) )</b>,<br />
	The binomial is the number of ways you can arrange 2 groups with<br />
	<i>k</i> and <i>n</i>&minus;<i>k</i> non-unique elements over <i>n</i><br />
	bins. This is basically the same thing as the combination.<br />
	The multinomial is the extension to <i>m</i> groups, with<br />
	<i>k</i><sub>j</sub> elements in each group. In both cases<br />
	you take the base <i>n</i>! for the unique variations, and divide<br />
	by <i>k</i><sub>j</sub>! for the overcounts for all the groups.<br/></p>
<p><img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20%5Ctext%7BBinomial%3A%20%7D%26%20%7B%20n%20%5Cchoose%20k%7D%3D%20%7B%20n%21%20%5Cover%20k%21%5C%2C%28n-k%29%21%20%7D%20%5C%5C%20%5Ctext%7BMultinomial%3A%20%7D%26%20%7B%20n%20%5Cchoose%20k_1%2C%20k_2%2C%20%5C%2C...%2C%5C%2C%20k_m%20%7D%3D%20%7B%20n%21%20%5Cover%20k_1%21%5C%2Ck_2%21%5C%2C%5Ccdots%5C%2Ck_m%21%20%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} \text{Binomial: }&amp; { n \choose k}= { n! \over k!\,(n-k)! } \\ \text{Multinomial: }&amp; { n \choose k_1, k_2, \,...,\, k_m }= { n! \over k_1!\,k_2!\,\cdots\,k_m! } \end{eqnarray}"<br />
	alt="\begin{eqnarray} \text{Binomial: }&amp; { n \choose k}= { n! \over k!\,(n-k)! } \\ \text{Multinomial: }&amp; { n \choose k_1, k_2, \,...,\, k_m }= { n! \over k_1!\,k_2!\,\cdots\,k_m! } \end{eqnarray}" /></p>
</li>
</ul>
<p>
And lastly,
</p>
<ul>
<li><b>Bi-/multinomial probability</b>.<br />
  When you have <i>m</i> groups with probability <i>p</i><sub>j</sub><br />
  for each group and you want to know the chance of drawing exactly<br />
  <i>k</i><i>j</i> items in<br />
  <i>n</i>&nbsp;=&nbsp;&Sigma;<sub>j</sub>&nbsp;<i>k</i><i>j</i> samples, the<br />
  probability is:<br/><br />
  <table class="eqtbl" id="eq-prob-mnom">
<tr>
<td class="eqnrcell">(18)</td>
  <td class="eqcell"><br />
<img src='http://www.coranac.com/cgi-bin/mimetex.cgi?%5Cbegin%7Beqnarray%7D%20P%28X_1%3Dk_1%5C%2C...%5C%2CX_m%3Dk_m%29%20%26%3D%26%20%7B%20n%20%5Cchoose%20k_1%2C%5C%2C...%2C%5C%2Ck_m%20%7D%20%26%5Ccdot%26%20%5Cprod_%7Bj%3D1%7D%5Em%20p_j%5Em%20%5C%5C%20%26%3D%26%20%7B%20n%21%20%5Cover%20k_1%21%5Ccdot%5C%2C...%5C%2C%20%5Ccdot%20k_m%21%20%7D%20%26%5Ccdot%26%20p_1%5E%7Bk_1%7D%5Ccdot%5C%2C...%5C%2C%5Ccdot%20p_m%5E%7Bk_m%7D%20%5Cend%7Beqnarray%7D'<br />
	title="\begin{eqnarray} P(X_1=k_1\,...\,X_m=k_m) &amp;=&amp; { n \choose k_1,\,...,\,k_m } &amp;\cdot&amp; \prod_{j=1}^m p_j^m \\ &amp;=&amp; { n! \over k_1!\cdot\,...\, \cdot k_m! } &amp;\cdot&amp; p_1^{k_1}\cdot\,...\,\cdot p_m^{k_m} \end{eqnarray}"<br />
	alt="\begin{eqnarray} P(X_1=k_1\,...\,X_m=k_m) &amp;=&amp; { n \choose k_1,\,...,\,k_m } &amp;\cdot&amp; \prod_{j=1}^m p_j^m \\ &amp;=&amp; { n! \over k_1!\cdot\,...\, \cdot k_m! } &amp;\cdot&amp; p_1^{k_1}\cdot\,...\,\cdot p_m^{k_m} \end{eqnarray}" /><br />
</td>
</tr>
</table><br/><br />
  And remember that this holds for just two groups (the binomial case)<br />
  as well, except that often they&#8217;ll just pretend that the second<br />
  group isn&#8217;t really there. Which is unfortunate, as<br />
  that&#8217;s where the (1&minus;p) and (<i>n</i>&minus;<i>k</i>) terms come from.
  </li>
</ul>
<p><!-- EOF --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/05/probabilities-in-simple-random-samples/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>setds 1.01 and an annoying yet oddly fitting bug</title>
		<link>http://www.coranac.com/2010/04/setds-1-01/</link>
		<comments>http://www.coranac.com/2010/04/setds-1-01/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 19:09:21 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[nds]]></category>
		<category><![CDATA[setds]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=189</guid>
		<description><![CDATA[It seems that a bug in the hiscore entry code that I thought I&#8217;d fixed over a month ago got resurrected in setds v1.0. So I fixed it &#8211; refixed it &#8211; today and we&#8217;re now at setds v1.01. &#160; Sigh. Gaddammit &#62;_&#60;]]></description>
			<content:encoded><![CDATA[<p>
It seems that a bug in the hiscore entry code that I <i>thought</i> I&#8217;d fixed over a month ago got resurrected in setds v1.0. So I fixed it &ndash; <i>re</i>fixed it &ndash; today and we&#8217;re now at <a href="[$HOME/projects/setds">setds v1.01</a>.
</p>
<p><div>&nbsp;</div></p>
<p>
<small>Sigh. Gaddammit <kbd>&gt;_&lt;</kbd></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/04/setds-1-01/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>setds 1.0</title>
		<link>http://www.coranac.com/2010/04/setds-1-0/</link>
		<comments>http://www.coranac.com/2010/04/setds-1-0/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 08:57:34 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[nds]]></category>
		<category><![CDATA[setds]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=185</guid>
		<description><![CDATA[I&#8217;ve finally taken the time to add proper saving that works on hardware (at least on my R4, which is the only card I have right now). I&#8217;ve also fixed a very annoying game-hanging bug and did some tweaking here and there, like correcting the decay constant so that I can finally get the scores [...]]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;ve finally taken the time to add proper saving that works on hardware<br />
(at least on my R4, which is the only card I have right now). I&#8217;ve also fixed a<br />
very annoying game-hanging bug and did some tweaking here and there, like<br />
correcting the decay constant so that I can finally get the scores that I was used to on tatset (2300+, wooo!).
</p>
<p>
This is pretty much where I wanted to go with this game, so it&#8217;s now version 1.0. I may add some other things later, but for now I guess it&#8217;s done.
</p>
<h4>Changes</h4>
<ul>
<li>new: FAT saving enabled <kbd>:D</kbd>. It&#8217;s intended for<br />
    <a href="http://www.devkitpro.org/hbmenu">argv-capable</a><br />
    cards, though. You can still save without one of those, but you&#8217;ll have to<br />
    put in a little work.
  </li>
<li>
    new: Reset/Back buttons for high-score screen.
  </li>
<li>
    fix: Bug in shuffle code. Fixes the occasional hang at start of a new game.
  </li>
<li>
    tweak: I&#8217;ve altered the decay constant slightly; it should be closer to<br />
    the original tatset now. The upshot is that scores will be a little higher.
  </li>
<li>
    tweak: Slightly bigger buttons.
  </li>
</ul>
<h4>Links</h4>
<ul>
<li><a href="http://www.coranac.com/projects/setds">setds project page</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/04/setds-1-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DMA vs ARM9, round 2 : invalidate considered harmful</title>
		<link>http://www.coranac.com/2010/03/dma-vs-arm9-round-2/</link>
		<comments>http://www.coranac.com/2010/03/dma-vs-arm9-round-2/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 17:29:08 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[nds]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=175</guid>
		<description><![CDATA[It would seem these two aren&#8217;t finished with each other yet. &#160; A while ago, I wrote an article about NDS caching , how it can interfere with DMA transfers and what you can do about them. A little later I got a pingback from ant512, who had tried the &#8220;safe&#8221; DMA routines I made [...]]]></description>
			<content:encoded><![CDATA[<p>
It would seem these two aren&#8217;t finished with each other yet.
</p>
<p><div>&nbsp;</div></p>
<p>
A while ago, I wrote<br />
<a href="http://www.coranac.com/dma-vs-arm9-fight/">an article about NDS caching</a><br />
, how it can interfere with DMA transfers and what you can do about them.<br />
A little later I got a<br />
<a href="http://ant.simianzombie.com/?p=1114">pingback from ant512</a>,<br />
who had tried the &ldquo;safe&rdquo; DMA routines I made and found they<br />
weren&#8217;t nearly as safe as I&#8217;d hoped. I&#8217;m still not sure what the actual<br />
problem was, but this incident did make me think about one possible<br />
reason, namely the one that will be discussed in this post: problematic<br />
cache invalidation.
</p>
<p><h3 id="sec-tests">1
Test base
</h3>
</p>
<p>
But first things first. Let&#8217;s start with some simple test code, see below.<br />
We have a simple struct definition, two arrays using this struct, and<br />
some default data for both arrays that we&#8217;ll use later.
</p>
<div class="cpp">
<div class="cpp proglist" style=" "><span class="co1">// A random struct, 32-bits in size.</span><br />
<span class="kw1">struct</span> Foo<br />
{<br />
&nbsp; &nbsp; u8&nbsp; type;<br />
&nbsp; &nbsp; u8&nbsp; id;<br />
&nbsp; &nbsp; u16 data;<br />
} ALIGN(<span class="nu0">4</span>);</p>
<p><span class="co1">// Define some globals. We only use 4 of each.</span><br />
Foo g_src[<span class="nu0">16</span>] ALIGN(<span class="nu0">32</span>);<br />
Foo g_dst[<span class="nu0">16</span>] ALIGN(<span class="nu0">32</span>);</p>
<p><span class="kw1">const</span> Foo c_fooIn[<span class="nu0">2</span>][<span class="nu0">4</span>]= <br />
{<br />
&nbsp; &nbsp; { &nbsp; <span class="co1">// Initial source data.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; { <span class="nu0">0&#215;55</span>, <span class="nu0">0</span>, <span class="nu0">0&#215;5111</span> }, <br />
&nbsp; &nbsp; &nbsp; &nbsp; { <span class="nu0">0&#215;55</span>, <span class="nu0">1</span>, <span class="nu0">0&#215;5111</span> },<br />
&nbsp; &nbsp; &nbsp; &nbsp; { <span class="nu0">0&#215;55</span>, <span class="nu0">2</span>, <span class="nu0">0&#215;5111</span> }, <br />
&nbsp; &nbsp; &nbsp; &nbsp; { <span class="nu0">0&#215;55</span>, <span class="nu0">3</span>, <span class="nu0">0&#215;5111</span> } <br />
&nbsp; &nbsp; }, <br />
&nbsp; &nbsp; { &nbsp; <span class="co1">// Initial destination data.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; { <span class="nu0">0xDD</span>, <span class="nu0">0</span>, <span class="nu0">0xD111</span> },<br />
&nbsp; &nbsp; &nbsp; &nbsp; { <span class="nu0">0xDD</span>, <span class="nu0">1</span>, <span class="nu0">0xD111</span> },<br />
&nbsp; &nbsp; &nbsp; &nbsp; { <span class="nu0">0xDD</span>, <span class="nu0">2</span>, <span class="nu0">0xD111</span> },<br />
&nbsp; &nbsp; &nbsp; &nbsp; { <span class="nu0">0xDD</span>, <span class="nu0">3</span>, <span class="nu0">0xD111</span> } <br />
&nbsp; &nbsp; }, <br />
};</div>
</div>
<p>
And now we&#8217;re going to do some simple things with these arrays that<br />
we always do: some reads, some writes, and a struct copy. And for the<br />
copying, I&#8217;m going to use DMA, because DMA-transfers are fast,<br />
amirite<span class="fnote"><a href="#ft-nr1" title="No I&#8217;m not. For NDS WRAM-WRAM copies, DMA is actually
slow as hell and outperformed by every other method. But hopefully more
on that later. For now, though, I need the DMA for testing
purposes.">(1)</a></span>? The specific actions I will do are the following:
</p>
<h5>Initialization</h5>
<ul>
<li>Zero out <code>g_src</code> and <code>g_dst</code>.</li>
<li>Initialize the arrays with some data, in this case from<br />
    <code>c_fooIn</code>.</li>
<li>Cache-Flush both arrays to ensure they&#8217;re uncached.</li>
</ul>
<h5>Testing</h5>
<ul>
<li>Modify element in <code>g_src</code>, namely <code>g_src[0]</code>.</li>
<li>Modify an element in <code>g_dst</code>, namely <code>g_dst[3]</code>.</li>
<li>DMA-copy <code>g_src[0]</code> to <code>g_dst[3]</code>.</li>
</ul>
<p>
In other words, this:
</p>
<div class="cpp">
<div class="cpp proglist" style=" "><span class="kw1">void</span> test_init()<br />
{<br />
&nbsp; &nbsp; <span class="co1">// Zero out everything</span><br />
&nbsp; &nbsp; <span class="kw3">memset</span>(g_src, <span class="nu0">0</span>, <span class="kw3">sizeof</span>(g_src));<br />
&nbsp; &nbsp; <span class="kw3">memset</span>(g_dst, <span class="nu0">0</span>, <span class="kw3">sizeof</span>(g_dst));</p>
<p>&nbsp; &nbsp; <span class="co1">// Fill 4 of each.</span><br />
&nbsp; &nbsp; <span class="kw1">for</span>(<span class="kw1">int</span> i=<span class="nu0">0</span>; i&lt;<span class="nu0">4</span>; i++)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; g_src[i]= c_fooIn[<span class="nu0">0</span>][i];<br />
&nbsp; &nbsp; &nbsp; &nbsp; g_dst[i]= c_fooIn[<span class="nu0">1</span>][i];<br />
&nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; <span class="co1">// Flush data to be sure.</span><br />
&nbsp; &nbsp; DC_FlushRange(g_src, <span class="kw3">sizeof</span>(g_src));<br />
&nbsp; &nbsp; DC_FlushRange(g_dst, <span class="kw3">sizeof</span>(g_dst));<br />
}</p>
<p><span class="kw1">void</span> test_dmaCopy()<br />
{<br />
&nbsp; &nbsp; test_init();</p>
<p>&nbsp; &nbsp; <span class="co1">// Change g_src[0] and g_dst[3]</span><br />
&nbsp; &nbsp; g_src[<span class="nu0">0</span>].id += <span class="nu0">0&#215;10</span>;<br />
&nbsp; &nbsp; g_src[<span class="nu0">0</span>].data= <span class="nu0">0&#215;5222</span>;</p>
<p>&nbsp; &nbsp; g_dst[<span class="nu0">3</span>].id += <span class="nu0">0&#215;10</span>;<br />
&nbsp; &nbsp; g_dst[<span class="nu0">3</span>].data= <span class="nu0">0xD333</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// DMA src[0] into dst[0];</span><br />
&nbsp; &nbsp; dmaCopy(&amp;g_src[<span class="nu0">0</span>], &amp;g_dst[<span class="nu0">0</span>], <span class="kw3">sizeof</span>(Foo));<br />
}</div>
</div>
<p>
Note that there is nothing spectacularly interesting going on here.<br />
There&#8217;s just your average struct definition, run of the mill array<br />
definitions, and boring old accesses without even any pointer magic<br />
that might hint at something tricky going on. Yes, alignment is forced,<br />
but that just makes the test more reliable. Also, the fact that I&#8217;m<br />
incrementing <code>Foo.id</code> rather than just reading from it is<br />
only because ARM9 cache is read-allocate, and I need to have these<br />
things end up in cache. The main point is that the actions in<br />
<code>test_dmaCopy()</code> are very ordinary.
</p>
<p><h3 id="sec-results">2
Results
</h3>
</p>
<p>
It should be obvious what the outcome of the test should be. However,<br />
when you run the test (on hardware! not emulator), the result seems to<br />
be something different.
</p>
<div class="none">
<div class="none proglist" style=" "><span class="co1">// Just dmaCopy.</span></p>
<p>&nbsp; &nbsp; <span class="co1">// Result &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Expected:</span><br />
&nbsp; &nbsp; <span class="co1">// Source (hex)</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, <span class="nu0">10</span>, <span class="nu0">5222</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 10, 5222</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 01, <span class="nu0">5111</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// 55, 01, 5111</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 02, <span class="nu0">5111</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 02, 5111</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 03, <span class="nu0">5111</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 03, 5111</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// Destination (hex)</span><br />
&nbsp; &nbsp; DD, 00, D111&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// 55, 10, 5222 (bad!)</span><br />
&nbsp; &nbsp; DD, 01, D111 &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// DD, 01, D111</span><br />
&nbsp; &nbsp; DD, 02, D111 &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// DD, 02, D111</span><br />
&nbsp; &nbsp; DD, <span class="nu0">13</span>, D333 &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// DD, 13, D333</span></div>
</div>
<p>
Notice that the changed values of <code>g_src[0]</code> never<br />
end up in <code>g_dst[0]</code>. Not only that, not even the<br />
<i>original</i> values <code>g_src[0]</code> have been copied.<br />
It&#8217;s as if the transfer never happened at all.
</p>
<p>
The reason for this was covered in detail in the original article.<br />
Basically, it&#8217;s because cache is invisible to DMA. Once a part of<br />
memory is cached, the CPU only looks to the contents of the cache and<br />
not the actual addresses, meaning that DMA not only reads out-of-date<br />
(stale) source data, but also puts it where the CPU won&#8217;t look.<br />
Two actions allow you to remedy this. The first is the<br />
<dfn>cache flush</dfn>, which write the cache-lines back to RAM and<br />
frees the cache-line. Then there&#8217;s <dfn>cache invalidate</dfn>, which<br />
just frees the cache-line. Note that in both cases, the cache is<br />
dissociated from memory.
</p>
<p>
With this information, it should be obvious what to do. When DMA-ing<br />
from RAM, you need to flush  the cache before the transfer to update<br />
the source&#8217;s memory. When DMA-ing to RAM, you need to invalidate<br />
after the transfer because now it&#8217;s actually the cache&#8217;s data that&#8217;s<br />
stale.<br />
When you think about it a little this makes perfect sense, and it&#8217;s easy<br />
enough to implement:
</p>
<div class="cpp">
<div class="cpp proglist" style=" "><span class="co1">// New DMA-code:</span><br />
&nbsp; &nbsp; DC_FlushRange(&amp;g_src[<span class="nu0">0</span>], <span class="kw3">sizeof</span>(Foo));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Flush source.</span><br />
&nbsp; &nbsp; dmaCopy(&amp;g_src[<span class="nu0">0</span>], &amp;g_dst[<span class="nu0">0</span>], <span class="kw3">sizeof</span>(Foo)); &nbsp; &nbsp; <span class="co1">// Transfer.</span><br />
&nbsp; &nbsp; DC_InvalidateRange(&amp;g_dst[<span class="nu0">0</span>], <span class="kw3">sizeof</span>(Foo)); &nbsp; &nbsp; <span class="co1">// Invalidate destination.</span></div>
</div>
<p>
Unfortunately, this doesn&#8217;t work right either. And if you think about<br />
it a lot instead of merely a little, you&#8217;ll see why. Maybe showing the<br />
results will make you see what I mean. The transfer seems to work now,<br />
but the earlier changes to <code>g_dst[3]</code> have been erased. How<br />
come?
</p>
<div class="none">
<div class="none proglist" style=" ">&nbsp; &nbsp; <span class="co1">// Result:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Expected:</span><br />
&nbsp; &nbsp; <span class="co1">// Source (hex)</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, <span class="nu0">10</span>, <span class="nu0">5222</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 10, 5222</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 01, <span class="nu0">5111</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// 55, 01, 5111</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 02, <span class="nu0">5111</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 02, 5111</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 03, <span class="nu0">5111</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 03, 5111</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// Destination (hex)</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, <span class="nu0">10</span>, D222&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// 55, 10, 5222</span><br />
&nbsp; &nbsp; DD, 01, D111 &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// DD, 01, D111</span><br />
&nbsp; &nbsp; DD, 02, D111 &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// DD, 02, D111</span><br />
&nbsp; &nbsp; DD, <span class="nu0">13</span>, D111 &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// DD, 13, D333 (wut?)</span></div>
</div>
<p>
The problem is that a cache-invalidate invalidates entire<br />
<i>cache-lines</i>, not just the range you supply. If the start or<br />
end of the data you want invalidate does not align to a cache-line,<br />
the adjacent data contained in that line is also thrown away. I hope<br />
you can see that this is bad.
</p>
<p>
This is exactly what&#8217;s happening here. The ARM9&#8242;s cache-lines are 32<br />
bytes in size. Because of the alignment I gave the arrays, elements<br />
0 through 3 lie on the same cache-line. The changes to<br />
<code>g_dst[3]</code> occur in cache (but only because I read from it<br />
through <code>+=</code>). The invalidate of <code>g_dst[0]</code><br />
<i>also</i> invalidates <code>g_dst[3]</code>, which throws out the<br />
perfectly legit data and you&#8217;re left in a bummed state. And again,<br />
I&#8217;ve done nothing spectacularly interesting here; all I did was<br />
modify something and then invalidated data that just happened to be<br />
adjacent to it. But that was enough. Very, <i>very</i> bad.
</p>
<p>
Just to be sure, this is <i>not</i> due to a bad implementation of<br />
<code>DC_InvalidateRange()</code>. The function does exactly what it&#8217;s<br />
supposed to do. The problem is inherent in the hardware. If your<br />
data does not align correctly to cache-lines, an invalidate will apply<br />
to the adjacent data as well. If you do not want that to happen, do<br />
<i>not</i> invalidate.
</p>
<p><h3 id="sec-solution">3
Solutions
</h3>
</p>
<p>
So what to do? Well, there is one thing, but I&#8217;m not sure how foolproof<br />
this is, but instead of invalidating the destination afterwards, you<br />
can also flush it before the transfer. This frees up the cache-lines<br />
without loss of data, and then it should be safe to DMA-copy to it.
</p>
<div class="cpp">
<div class="cpp proglist" style=" ">&nbsp; &nbsp; DC_FlushRange(&amp;g_src[<span class="nu0">0</span>], <span class="kw3">sizeof</span>(Foo));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Flush source.</span><br />
&nbsp; &nbsp; DC_FlushRange(&amp;g_dst[<span class="nu0">0</span>], <span class="kw3">sizeof</span>(Foo));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Flush destination.</span><br />
&nbsp; &nbsp; dmaCopy(&amp;g_src[<span class="nu0">0</span>], &amp;g_dst[<span class="nu0">0</span>], <span class="kw3">sizeof</span>(Foo)); &nbsp; &nbsp; <span class="co1">// Transfer.</span></div>
</div>
<div class="none">
<div class="none proglist" style=" ">&nbsp; &nbsp; <span class="co1">// Result:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Expected:</span><br />
&nbsp; &nbsp; <span class="co1">// Source (hex)</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, <span class="nu0">10</span>, <span class="nu0">5222</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 10, 5222</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 01, <span class="nu0">5111</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// 55, 01, 5111</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 02, <span class="nu0">5111</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 02, 5111</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, 03, <span class="nu0">5111</span> &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// 55, 03, 5111</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// Destination (hex)</span><br />
&nbsp; &nbsp; <span class="nu0">55</span>, <span class="nu0">10</span>, D222&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// 55, 10, 5222</span><br />
&nbsp; &nbsp; DD, 01, D111 &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// DD, 01, D111</span><br />
&nbsp; &nbsp; DD, 02, D111 &nbsp; &nbsp; &nbsp;&nbsp; <span class="co1">// DD, 02, D111</span><br />
&nbsp; &nbsp; DD, <span class="nu0">13</span>, D333 &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// DD, 13, D333</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// Yay \o/</span></div>
</div>
<p>
Alternatively, you can also disable the underlying reason behind the<br />
problem with invalidation: the write-buffer. The ARM9 cache allows<br />
two modes for writing: <dfn>write-through</dfn>, which also updates<br />
the memory related to the cache-line; and <dfn>write-back</dfn>, which<br />
doesn&#8217;t. Obviously, the write-back is faster, so that&#8217;s how libnds<br />
sets things up. I know that putting the cache in write-through mode<br />
fixes this problem, because in libnds 1.4.0 the write-buffer had been<br />
accidentally disabled and my test cases didn&#8217;t fail. This is probably<br />
not the route you want to take, though.
</p>
<p><h3 id="sec-conc">4
Conclusions
</h3>
</p>
<p>
So what have we learned?
</p>
<ul>
<li>
    Cache &#8211; DMA interactions suck and can cause really subtle bugs.<br />
	Ones that will only show up on hardware too.
  </li>
<li>
    Cache-flushes and invalidates cover the cache-lines of the requested<br />
	ranges, which exceed the range you actually wanted.
  </li>
<li>
    To safely DMA from cachable memory, flush the source range first.
  </li>
<li>
    Contrary to what I wrote earlier, to DMA to cachable memory,<br />
	do <i>not</i> cache-invalidate &ndash; at least not when<br />
	the range is not properly aligned to cache-lines. Instead, flush<br />
	the destination range before the transfer (at which time<br />
	invalidation should be unnecessary). That said, invalidate should<br />
	still be safe if the write-buffer is disabled.
  </li>
</ul>
<p><a href="http://www.coranac.com/files/nds/invalidate.zip">Link to test code.</a></p>
<p><div>&nbsp;</div><br />
<!-- EOF --></p>
<hr /><div class="footnotes">
<h5>Notes:</h5>
<ol>
<li id="ft-nr1"> 
  No I&#8217;m not. For NDS WRAM-WRAM copies, DMA is actually<br />
slow as hell and outperformed by every other method. But hopefully more<br />
on that later. For now, though, I need the DMA for testing<br />
purposes.
</li>
</ol>
</div
<hr />
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/03/dma-vs-arm9-round-2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Grit 0.8.6 : synchronization update</title>
		<link>http://www.coranac.com/2010/03/grit-0-8-6-synchronization-update/</link>
		<comments>http://www.coranac.com/2010/03/grit-0-8-6-synchronization-update/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 12:56:43 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=169</guid>
		<description><![CDATA[This is just an update to synchronize what I have with devkitPro&#8216;s distribution of grit. This includes updates to the makefile, and turning back the way the size-constant was defined back to a #define. Apparently, consts aren&#8217;t constanty enough for C compilers for use in array declarations. Shame, I would have liked to get rid [...]]]></description>
			<content:encoded><![CDATA[<p>
This is just an update to synchronize what I have with <a href="http://www.devkitpro.org">devkitPro</a>&#8216;s distribution of grit. This includes updates to the makefile, and turning back the way the size-constant was defined back to a <code>#define</code>. Apparently, <code>const</code>s aren&#8217;t constanty enough for C compilers for use in array declarations. Shame, I would have liked to get rid of macros as much as possible  <kbd>:(</kbd>.
</p>
<p></p>
<p>
In any case, the two versions should be identical again <small>(with one small exception, namely that my version emits a <code>.size</code> directive for assembly, but that&#8217;s a minor something that should not affect anyone.)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/03/grit-0-8-6-synchronization-update/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>grit 0.8.4 (out with the old bugs, in with the new)</title>
		<link>http://www.coranac.com/2010/02/grit-0-8-4/</link>
		<comments>http://www.coranac.com/2010/02/grit-0-8-4/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 20:04:14 +0000</pubDate>
		<dc:creator>cearn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.coranac.com/?p=154</guid>
		<description><![CDATA[Okay, so it&#8217;s been a while, but there&#8217;s finally a new version for grit. &#160; First of all, the vector::insert should finally be fixed. And there was much rejoicing. I&#8217;ve also added an option for forcing the map palette-index (-mp &#60;num&#62;, which should help with NDS backgrounds that use ext-palettes. &#160; Also &#8211; and this [...]]]></description>
			<content:encoded><![CDATA[<p>
Okay, so it&#8217;s been a while, but there&#8217;s finally a new version for grit.
</p>
<p><div>&nbsp;</div></p>
<p>
First of all, the <code>vector::insert</code> should finally be fixed. And there was much rejoicing. I&#8217;ve also added an option for forcing the map palette-index (<tt>-mp &lt;num&gt;</tt>, which should help with NDS backgrounds that use ext-palettes.
</p>
<p><div>&nbsp;</div></p>
<p>
Also &ndash; and this one is pretty big &ndash; I&#8217;ve <b>completely replaced</b> the tile-mapping routines for something more general. The new method should be able to handle variable-sized tiles (<tt>-tw &lt;n&gt;</tt> and <tt>-th &lt;n&gt;</tt>) and is mostly independent of bitdepth. Specifically, bitdepths over 8 bpp can be handled as well, at least in principle. It also means that the external tileset can be a metatile-set as well now, which is good if you&#8217;re using metatiles.
</p>
<p>
With this new method also comes a way to create a custom bitformat for maps (<tt>-mB</tt> flag). I&#8217;m not entirely sure how this can be used yet, but using more than 10 bits for the tile-index, or a 1bpp collision map should be possible now.
</p>
<p>
Since this is a fairly major change, I kinda expect there&#8217;s still some bugs in the system. I have tested it for a number of options, but you know how it is with multi-platform stuff. In particular, if any of you big-endian-system users have trouble now, this will probably be the cause.
</p>
<p>
And now I will leave you with a &hellip;
</p>
<ul>
<li><a href=/projects/#grit>link to grit</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.coranac.com/2010/02/grit-0-8-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!--
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
</head>
<body>
<p>
My database has called in sick. Please imagine some 
annoying elevator tune till he gets back.
</p>
<p>
<small>[[Doo-di-doo tooo. Dum-di-dum-di-doo-dooo.]]</small>
</p>
</body>
</html>

-->
