<?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>John Myles White &#187; Psychology</title>
	<atom:link href="http://www.johnmyleswhite.com/notebook/category/psychology/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnmyleswhite.com</link>
	<description>&#34;He who refuses to do arithmetic is doomed to talk nonsense.&#34;</description>
	<lastBuildDate>Wed, 26 Oct 2011 11:36:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Psychology of Music and the &#8216;tuneR&#8217; Package</title>
		<link>http://www.johnmyleswhite.com/notebook/2011/10/25/the-psychology-of-music-and-the-tuner-package/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2011/10/25/the-psychology-of-music-and-the-tuner-package/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 01:28:41 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Music]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Psychology]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=4311</guid>
		<description><![CDATA[Introduction This semester I&#8217;m TA&#8217;ing a course on the Psychology of Music taught by Phil Johnson-Laird. It&#8217;s been a great course to teach because (i) so much of the material is new to me and (ii) because the study of the psychology of music brings together so many of the intellectual tools I enjoy, including [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>This semester I&#8217;m TA&#8217;ing a course on the <a href="http://psych.princeton.edu/psychology/research/johnson_laird/music.php">Psychology of Music</a> taught by <a href="http://psych.princeton.edu/psychology/research/johnson_laird/">Phil Johnson-Laird</a>. It&#8217;s been a great course to teach because (i) so much of the material is new to me and (ii) because the study of the psychology of music brings together so many of the intellectual tools I enjoy, including music theory, psychophysics and Fourier analysis.</p>
<p>One topic this semester that was completely new to me was the theory of tuning: I had known about the invention of the <a href="http://en.wikipedia.org/wiki/Well_temperament">well-tempered system of tuning</a>, but had never heard of <a href="http://en.wikipedia.org/wiki/Pythagorean_tuning">Pythagorean tuning</a> or <a href="http://en.wikipedia.org/wiki/Just_intonation">just tuning</a> &#8212; and certainly was not aware that the well-tempered system <a href="http://en.wikipedia.org/wiki/The_Well-Tempered_Clavier">Bach celebrated</a> was not identical to our current equal-tempered system of tuning.</p>
<p>As a way of consolidating some of the knowledge I&#8217;ve gained, I decided I&#8217;d write a blog entry after several months of neglecting this blog. (For that neglect, I&#8217;ll blame a combination of grant writing, book writing, ongoing research projects and personal life developments.) In what follows, I&#8217;ll give a brief overview of the theory of tuning at a theoretical level that should be accessible to anyone who&#8217;s familiar with the names of intervals and feels comfortable thinking quantitatively.</p>
<p>After surveying the field, I&#8217;ll turn to a discussion of some code I&#8217;ve written in R that implements these ideas using the &#8216;tuneR&#8217; package, which is one of my favorite hidden gems from CRAN. Along the way, I&#8217;ll introduce some of the simplest tools from the &#8216;tuneR&#8217; package that can be used for generating computer music.</p>
<h3>Tuning Systems: Pythagorean, Just and 12-Tet</h3>
<p>It&#8217;s worth noting right at the start that tuning is a misleading name for the topic we&#8217;ll be discussing: we&#8217;re not talking about how one tunes a fixed instrument so that it sounds in tune, but rather we&#8217;re interested in how one defines the very notes that the instrument should be able to produce when it&#8217;s perfectly in tune.</p>
<p>To make that clear, let&#8217;s assume that we&#8217;ve accepted as a given that a frequency of 440 Hz will be called A. Our problem then becomes one of deciding which of the infinitely many frequencies we could produce  actually deserves the label of A#, B, C, C#, and so on.</p>
<h4>Pythagorean Tuning</h4>
<p>The simplest solution to this problem I know of is the <a href="http://en.wikipedia.org/wiki/Pythagorean_tuning">Pythagorean tuning system</a>. It&#8217;s based on constructing all of the possible notes using a series of perfect fifths. If you remember the <a href="http://en.wikipedia.org/wiki/Circle_of_fifths">Circle of Fifths</a>, you&#8217;ll remember that you can reach every chromatic note by ascending fifths: if you start at A, you&#8217;ll proceed through E, B, F# and so on.</p>
<p>The Pythagorean system implements the Circle of Fifths directly using repeated multiplication of a base frequency. To do this, you first declare that a perfect fifth is at a frequency 3/2 above your base frequency. For example, this definition implies that the perfect fifth above the A at 440 Hz has to be at a frequency of 3/2 * 440 = 660 Hz. Once you do this, you&#8217;ve defined the frequency we&#8217;ll call E.</p>
<p>And following on with this logic, you produce a B at 990 Hz. Of course, this B occurs an octave above the base A at 440 Hz, so you transpose it down an octave to produce the B you&#8217;ll actually use. To do this, you need to assume that an octave is at a frequency 2 times the base frequency. Since we&#8217;ve accepted that 990 Hz is a B, we divide 990 by 2 and conclude that 495 Hz should be B.</p>
<p>With these three notes defined, we have the following table of frequency/note pairs:</p>
<table>
<tr>
<th>Note</th>
<th>Frequency</th>
<th>Ratio with 440 Hz</th>
</tr>
<tr>
<td>A</td>
<td>440 Hz</td>
<td>1</td>
</tr>
<tr>
<td>E</td>
<td>660 Hz</td>
<td>3/2</td>
</tr>
<tr>
<td>B</td>
<td>495 Hz</td>
<td>9/8</td>
</tr>
</table>
<p>If we continue on with this logic and calculate many more multiplications by 3/2 and divisions by 2, we will eventually produce a complete table for all of the notes in the chromatic scale that looks like the following:</p>
<table>
<tr>
<th>Note</th>
<th>Frequency</th>
<th>Ratio</th>
</tr>
<tr>
<td>A</td>
<td>440</td>
<td>1</td>
</tr>
<tr>
<td>A#</td>
<td>463.5391</td>
<td>256/243</td>
</tr>
<tr>
<td>B</td>
<td>495</td>
<td>9/8</td>
</tr>
<tr>
<td>C</td>
<td>521.4815</td>
<td>32/27</td>
</tr>
<tr>
<td>C#</td>
<td>556.875</td>
<td>81/64</td>
</tr>
<tr>
<td>D</td>
<td>586.6667</td>
<td>4/3</td>
</tr>
<tr>
<td>D#</td>
<td>626.4844</td>
<td>729/512</td>
</tr>
<tr>
<td>E</td>
<td>660</td>
<td>3/2</td>
</tr>
<tr>
<td>F</td>
<td>695.3086</td>
<td>128/81</td>
</tr>
<tr>
<td>F#</td>
<td>742.5</td>
<td>27/16</td>
</tr>
<tr>
<td>G</td>
<td>782.2222</td>
<td>16/9</td>
</tr>
<tr>
<td>G#</td>
<td>835.3125</td>
<td>243/128</td>
</tr>
<tr>
<td>A</td>
<td>880</td>
<td>2</td>
</tr>
</table>
<p>One thing about this table might strike you as odd if you&#8217;re mathematically savvy: the octave, which we&#8217;ve defined by fiat as a ratio of 2:1, could never have been produced by successive multiplication by 3/2, since no power of 3 will be evenly divisible by a power of 2. This is the one flub in the Pythagorean system: you can&#8217;t really produce the entire chromatic scale using only multiples of 3/2. Here we&#8217;ve solved that problem by replacing the note we would have called A with a true octave generated using multiplication by 2. Because the exact octave produced by Pythagorean tuning is slightly out of tune with our preferred definition of an octave, you may hear people refer to this discrepancy as the <a href="http://en.wikipedia.org/wiki/Pythagorean_comma">the Pythagorean comma</a>.</p>
<h4>Just Tuning</h4>
<p>Given that we had to cheat a bit to create a proper octave using the Pythagorean tuning system based on multiples of 3/2, it makes sense to ask why we shouldn&#8217;t just allow ourselves to use other multipliers than 3/2. Looking at the Pythagoren tuning table, we see some pretty ugly fractions like 729/512. What if we forced these fractions to be simpler by employing ratios like 4/3 and 5/4 to build up the whole system?</p>
<p>The result of allowing ourselves several fractions beyond just those derived from 3/2 is called the <a href="http://en.wikipedia.org/wiki/Just_intonation">just tuning system</a>. Here we assume that perfect fifths occur at a frequency ratio of 3/2 and that perfect fourths occur at a frequency ratio of 4/3. Continuing on with this process, we eventually end up with the following tuning table:</p>
<table>
<tr>
<th>Note</th>
<th>Frequency</th>
<th>Ratio</th>
</tr>
<tr>
<td>A</td>
<td>440</td>
<td>1</td>
</tr>
<tr>
<td>A#</td>
<td>469.3333</td>
<td>16/15</td>
</tr>
<tr>
<td>B</td>
<td>495</td>
<td>9/8</td>
</tr>
<tr>
<td>C</td>
<td>528</td>
<td>6/5</td>
</tr>
<tr>
<td>C#</td>
<td>550</td>
<td>5/4</td>
</tr>
<tr>
<td>D</td>
<td>586.6667</td>
<td>4/3</td>
</tr>
<tr>
<td>D#</td>
<td>625.7778</td>
<td>64/45</td>
</tr>
<tr>
<td>E</td>
<td>660</td>
<td>3/2</td>
</tr>
<tr>
<td>F</td>
<td>704</td>
<td>8/5</td>
</tr>
<tr>
<td>F#</td>
<td>733.3333</td>
<td>5/3</td>
</tr>
<tr>
<td>G</td>
<td>782.2222</td>
<td>16/9</td>
</tr>
<tr>
<td>G#</td>
<td>825</td>
<td>15/8</td>
</tr>
<tr>
<td>A</td>
<td>880</td>
<td>2</td>
</tr>
</table>
<p>This is the tuning that early Classical music was written in. Looking at the table you con immediately appreciate the theoretical assertion that the relative dissonance of an interval is determined by the simplicity of the ratio of frequencies between the two notes: perfect fifths are 3/2 and major thirds are 5/4, while minor seconds are 16/15 and major sevenths are 15/8. This is one of the things I most enjoy about the theory of harmony: there&#8217;s a match between the aesthetics of fractions and the aesthetics of sounds that, for me, helps to justify my sense that certain fractions are more beautiful than others.</p>
<h4>12 Tet / Equal-Temperament</h4>
<p>Now, if you know the history of Bach&#8217;s Well-Tempered Clavier, you know that there is a problem with the just tuning system: it sounds great in the key you used as the base (here A), but it sounds a bit out of tune in other keys. The modern <a href="http://en.wikipedia.org/wiki/Equal_temperament">12-tet system</a> is the most recent approach to solving this problem: you assume the gap between two semitones (e.g. A to A# or A# to B) is always the exact same multiple. Since you&#8217;ll repeat this multiplication 12 times before reaching an octave, you can conclude that two notes that are a semitone apart must be separated by the 12th root of 2. Building a tuning system using that ratio alone gives us our modern system of tuning, which is shown in the table above using the decimal expansion of the ratios instead of their representation as powers of the 12th root of 2:</p>
<table>
<tr>
<th>Note</th>
<th>Frequency</th>
<th>Ratio</th>
</tr>
<tr>
<td>A</td>
<td>440</td>
<td>1.000000</td>
</tr>
<tr>
<td>A#</td>
<td>466.1638</td>
<td>1.059463</td>
</tr>
<tr>
<td>B</td>
<td>493.8833</td>
<td>1.122462</td>
</tr>
<tr>
<td>C</td>
<td>523.2511</td>
<td>1.189207</td>
</tr>
<tr>
<td>C#</td>
<td>554.3653</td>
<td>1.259921</td>
</tr>
<tr>
<td>D</td>
<td>587.3295</td>
<td>1.334840</td>
</tr>
<tr>
<td>D#</td>
<td>622.2540</td>
<td>1.414214</td>
</tr>
<tr>
<td>E</td>
<td>659.2551</td>
<td>1.498307</td>
</tr>
<tr>
<td>F</td>
<td>698.4565</td>
<td>1.587401</td>
</tr>
<tr>
<td>F#</td>
<td>739.9888</td>
<td>1.681793</td>
</tr>
<tr>
<td>G</td>
<td>783.9909</td>
<td>1.781797</td>
</tr>
<tr>
<td>G#</td>
<td>830.6094</td>
<td>1.887749</td>
</tr>
<tr>
<td>A</td>
<td>880</td>
<td>2.000000</td>
</tr>
</table>
<h3>Listening to the Results</h3>
<p>We&#8217;ve just described three ways to define the notes used in Western music. But how different do they sound? To answer that, I decided to produce a series of simple sine wave audio samples that were tuned using each of the three tuning systems. To produce those audio samples, I used the &#8216;tuneR&#8217; package, which I&#8217;ll describe now. Before you read on, you should install it from CRAN using the standard <code>install.packages('tuneR')</code> invocation.</p>
<h3>A tuneR Tutorial</h3>
<p>The <a href="http://cran.r-project.org/web/packages/tuneR/index.html">tuneR</a> package is an extremely convenient tool for generating audio files from R based on a numeric description of the audio stream. For the purposes of this discussion of tuning systems, we simply need to produce basic sine waves. Thankfully, that&#8217;s very easy to do with tuneR. Here&#8217;s an example:</p>

<div class="wp_codebox"><table><tr id="p43115"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p4311code5"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'tuneR'</span><span style="color: #009900;">&#41;</span>
&nbsp;
sound <span style="color: #339933;">&lt;-</span> sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">440</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span>
&nbsp;
writeWave<span style="color: #009900;">&#40;</span>sound<span style="color: #339933;">,</span> <span style="color: #ff0000;">'440.wav'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Here we&#8217;ve loaded the tuneR package, created a 1s snippet of sine wave audio at 16 bits resolution using the <code>sine</code> function, and then written out the audio to a WAV file using <code>writeWave</code>. If you look at your current directory and listen to this file, you&#8217;ll hear a sine wave at 440 Hz.</p>
<p>If you want to explore the use of <code>sine</code>, you can easily play with the duration of the sound by changing the <code>duration</code> parameter. If you want to, you can also change the sample rate and the bit rate, but I don&#8217;t see any reason to do that while exploring ideas about tuning.</p>
<p>More important is knowing that you can superimpose two sine waves using the <code>`+`</code> operator and that you can concatenate them using the <code>bind</code> function. To show off producing octaves, for example, you might use the following code to hear an A at 440 Hz, then an A an octave above it, and finally the harmony they produce together:</p>

<div class="wp_codebox"><table><tr id="p43116"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p4311code6"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'tuneR'</span><span style="color: #009900;">&#41;</span>
&nbsp;
sound <span style="color: #339933;">&lt;-</span> bind<span style="color: #009900;">&#40;</span>sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">440</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
              sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">880</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
              sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">440</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">880</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
writeWave<span style="color: #009900;">&#40;</span>sound<span style="color: #339933;">,</span> <span style="color: #ff0000;">'octaves.wav'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Unfortunately, this sample code produces an error because of the naive addition we&#8217;ve implemented using the <code>`+`</code> operator. Adding two sine waves directly together overfills the bit rate we&#8217;re using. To safely perform addition of two sine waves, we need to normalize the results of our summation using the <code>normalize</code> function. This gives us just one more line of code:</p>

<div class="wp_codebox"><table><tr id="p43117"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p4311code7"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'tuneR'</span><span style="color: #009900;">&#41;</span>
&nbsp;
sound <span style="color: #339933;">&lt;-</span> bind<span style="color: #009900;">&#40;</span>sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">440</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
              sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">880</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
              sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">440</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> sine<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">880</span><span style="color: #339933;">,</span> bit <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
sound <span style="color: #339933;">&lt;-</span> normalize<span style="color: #009900;">&#40;</span>sound<span style="color: #339933;">,</span> unit <span style="color: #339933;">=</span> <span style="color: #ff0000;">'16'</span><span style="color: #009900;">&#41;</span>
&nbsp;
writeWave<span style="color: #009900;">&#40;</span>sound<span style="color: #339933;">,</span> <span style="color: #ff0000;">'octaves.wav'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>For reasons that are not clear to me, you have to specify the bit rate to <code>normalize</code> using the <code>unit</code> parameter rather than the <code>bit</code> parameter.</p>
<h3>Demoing Tuning Systems</h3>
<p>Our little octave demo is cute, but we really want to know what more interesting harmonies like major thirds and minor seconds sound like in the various tuning systems we described. To do that, I first wrote a function called <code>interval</code> that spits out the multiplier you need to use to produce a given interval for any of the three tuning systems. That function is in a <a href="https://github.com/johnmyleswhite/computer_music">GitHub repository</a> I&#8217;ve set up with code for making these demos. If you download that repository, you could load my <code>interval</code> function using a simple call to <code>source</code> like the one seen below. And using this <code>interval</code> function, we can generate demos of various intervals as follows:</p>

<div class="wp_codebox"><table><tr id="p43118"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p4311code8"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'tuneR'</span><span style="color: #009900;">&#41;</span>
source<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'interval.R'</span><span style="color: #009900;">&#41;</span>
&nbsp;
base <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">440</span>
&nbsp;
sound <span style="color: #339933;">&lt;-</span> sine<span style="color: #009900;">&#40;</span>base<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> sine<span style="color: #009900;">&#40;</span>interval<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'minor-second'</span><span style="color: #339933;">,</span>
                                    tuning <span style="color: #339933;">=</span> <span style="color: #ff0000;">'pythagorean'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> base<span style="color: #009900;">&#41;</span>
&nbsp;
sound <span style="color: #339933;">&lt;-</span> normalize<span style="color: #009900;">&#40;</span>sound<span style="color: #339933;">,</span> unit <span style="color: #339933;">=</span> <span style="color: #ff0000;">'16'</span><span style="color: #009900;">&#41;</span>
&nbsp;
writeWave<span style="color: #009900;">&#40;</span>sound<span style="color: #339933;">,</span> <span style="color: #ff0000;">'minor_second_pythagorean.wav'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>On GitHub there&#8217;s a file called <code>test_intervals.R</code> that will go through and generate all of the intervals in all three tuning systems. If you run that file, you&#8217;ll generate a lot of audio files you can listen to as demos of the three tuning systems we&#8217;ve described. For me, these tuning systems all produce intervals that sound surprisingly similar, though at high volumes I find it moderately easy to hear slight differences between the tuning systems. That said, I very much doubt I would pick up on them in a normal musical context.</p>
<p>That&#8217;s the end of my little introduction to tuning systems and the use of the tuneR package to explore them. If you&#8217;re interested in thinking computationally about music, I highly recommend playing around with tuneR until you feel like you can produce interesting results. I&#8217;m already working on trying to build up some interesting timbres to work with.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2011/10/25/the-psychology-of-music-and-the-tuner-package/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Twitter Math Puzzle and Solution</title>
		<link>http://www.johnmyleswhite.com/notebook/2011/07/07/twitter-math-puzzle-and-solution/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2011/07/07/twitter-math-puzzle-and-solution/#comments</comments>
		<pubDate>Thu, 07 Jul 2011 13:48:07 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Psychology]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=4302</guid>
		<description><![CDATA[Yesterday I posted a very simple math puzzle to Twitter that I found in Jonathan Baron&#8217;s book, Thinking and Deciding. The puzzle is the following: Show that every number of the form ABC,ABC is divisible by 13. The puzzle comes up in Baron&#8217;s book as an example of an &#8220;insight problem&#8221; in which one goes [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I posted a very simple math puzzle to Twitter that I found in Jonathan Baron&#8217;s book, <a href="http://amzn.to/npM5Uk">Thinking and Deciding</a>. The puzzle is the following:</p>
<blockquote><p>
Show that every number of the form ABC,ABC is divisible by 13.
</p></blockquote>
<p>The puzzle comes up in Baron&#8217;s book as an example of an &#8220;insight problem&#8221; in which one goes from not knowing the answer at all to knowing the complete answering in a sudden moment of insight.</p>
<p>Several people replied to my tweet with solutions: I especially like <a href="https://twitter.com/#!/willtownes/status/88735472028876800">Will Townes&#8217;s</a> solution. In particular, if you&#8217;re familiar with <a href="http://en.wikipedia.org/wiki/Modular_arithmetic">modular arithmetic</a>, I like the logic of Will&#8217;s answer because it gives a simple generalization. First, represent ABC,ABC as ABC * 1000 + ABC * 1 rather than as ABC * 1001. Then notice that</p>
<ol>
<li>1 = 1 mod 13</li>
<li>1000 = -1 mod 13</li>
</ol>
<p>Thus ABC,ABC = ABC * -1 + ABC * 1 = 0 mod 13. This logic can be easily extended to show that (ABC,ABC,)*ABC,ABC = 0 mod 13 no matter how many times you repeat the ABC,ABC pattern.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2011/07/07/twitter-math-puzzle-and-solution/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Speeding Up MLE Code in R</title>
		<link>http://www.johnmyleswhite.com/notebook/2011/06/18/speeding-up-mle-code-in-r/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2011/06/18/speeding-up-mle-code-in-r/#comments</comments>
		<pubDate>Sun, 19 Jun 2011 00:02:29 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Economics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Psychology]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=4264</guid>
		<description><![CDATA[Recently, I&#8217;ve been fitting some models from the behavioral economics literature to choice data. Most of these models amount to non-linear variants of logistic regression in which I want to infer the parameters of a utility function. Because several of these models aren&#8217;t widely used, I&#8217;ve had to write my own maximum likelihood code to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been fitting some models from the behavioral economics literature to choice data. Most of these models amount to non-linear variants of logistic regression in which I want to infer the parameters of a utility function. Because several of these models aren&#8217;t widely used, I&#8217;ve had to write my own maximum likelihood code to estimate the parameters of these models.</p>
<p>In the process, I&#8217;ve started to learn something about how to write code that runs quickly in R. In this post, I&#8217;ll try to share some of that knowledge by describing three ways of performing maximum likelihood estimation in R whose runtimes differ by two orders of magnitude. The differences seem to depend upon two factors: (1) how I access the entries of a data frame and (2) whether I use loops or vectorized operations to perform basic arithmetic.</p>
<p>To simplify things, I&#8217;ll present a model that should be familiar to people with a background in economics: the exponentially discounted utility model. To implement it in R, we define the discounted value of <code>x</code> dollars at time <code>t</code> as:</p>

<div class="wp_codebox"><table><tr id="p426416"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p4264code16"><pre class="c" style="font-family:monospace;">discounted.<span style="color: #202020;">value</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> t<span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">*</span> delta <span style="color: #339933;">^</span> t<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In addition to the discounted utility model, we assume that choices originate from a stochastic choice model with logistic noise. To invert this noise during inference, we&#8217;ll use the inverse logit transform:</p>

<div class="wp_codebox"><table><tr id="p426417"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p4264code17"><pre class="c" style="font-family:monospace;">invlogit <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">+</span> exp<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>z<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To test my inference routine, I need to generate &#8220;stochastic&#8221; data of the sort you would expect to see from an exponentially discounting agent that&#8217;s indifferent between having $1 at time t = 0 and $3 at time t = 1. I&#8217;ll refer to the first good as (X1, T1) and the second good as (X2, T2). If the agent chooses (X2, T2), I&#8217;ll write that as <code>C == 1</code>; if they choose (X1, T1), I&#8217;ll write that as <code>C == 0</code>. With those conventions, the sample data is generated as:</p>

<div class="wp_codebox"><table><tr id="p426418"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p4264code18"><pre class="c" style="font-family:monospace;">n <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">100</span>
&nbsp;
choices <span style="color: #339933;">&lt;-</span> data.<span style="color: #202020;">frame</span><span style="color: #009900;">&#40;</span>X1 <span style="color: #339933;">=</span> rep<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> each <span style="color: #339933;">=</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                      T1 <span style="color: #339933;">=</span> rep<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> each <span style="color: #339933;">=</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                      X2 <span style="color: #339933;">=</span> rep<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> each <span style="color: #339933;">=</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                      T2 <span style="color: #339933;">=</span> rep<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> each <span style="color: #339933;">=</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                      C <span style="color: #339933;">=</span> rep<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> by <span style="color: #339933;">=</span> n <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>To fit the exponential model to this data set, we&#8217;ll use the <code>optim</code> function to minimize the negative log likelihood of the data by setting two parameters: <code>a</code>, the variance of the noise in the utility function; and <code>delta</code>, the discount factor in the discounted utility model. The three implementations of this model that I&#8217;ll show only differ in the definition of the log likelihood function, so the final call to <code>optim</code> to perform maximum likelihood estimation is constant across all examples:</p>

<div class="wp_codebox"><table><tr id="p426419"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p4264code19"><pre class="c" style="font-family:monospace;">logit.<span style="color: #202020;">estimator</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>choices<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span> 
  wrapper <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">-</span>log.<span style="color: #202020;">likelihood</span><span style="color: #009900;">&#40;</span>choices<span style="color: #339933;">,</span> x<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
  optimization.<span style="color: #202020;">results</span> <span style="color: #339933;">&lt;-</span> optim<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> wrapper<span style="color: #339933;">,</span> method <span style="color: #339933;">=</span> <span style="color: #ff0000;">'L-BFGS-B'</span><span style="color: #339933;">,</span> lower <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> upper <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span>Inf<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>optimization.<span style="color: #202020;">results</span>$par<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here, I had to specify bounds for the parameters, <code>a</code> and <code>delta</code>, because it&#8217;s assumed that <code>a</code> must be positive and that <code>delta</code> must lie in the interval [0, 1]. To deal with these bounds, one has to use the L-BFGS-B method in <code>optim</code>.</p>
<p>The first implementation I&#8217;ll show is the one I find most natural to write, even though it turns out to be the least efficient by far:</p>

<div class="wp_codebox"><table><tr id="p426420"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p4264code20"><pre class="c" style="font-family:monospace;">log.<span style="color: #202020;">likelihood</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>choices<span style="color: #339933;">,</span> a<span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  ll <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">0</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i in <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>nrow<span style="color: #009900;">&#40;</span>choices<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    u2 <span style="color: #339933;">&lt;-</span> discounted.<span style="color: #202020;">value</span><span style="color: #009900;">&#40;</span>choices<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">,</span> <span style="color: #ff0000;">'X2'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> choices<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">,</span> <span style="color: #ff0000;">'T2'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
    u1 <span style="color: #339933;">&lt;-</span> discounted.<span style="color: #202020;">value</span><span style="color: #009900;">&#40;</span>choices<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">,</span> <span style="color: #ff0000;">'X1'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> choices<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">,</span> <span style="color: #ff0000;">'T1'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
&nbsp;
    p <span style="color: #339933;">&lt;-</span> invlogit<span style="color: #009900;">&#40;</span>a <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>u2 <span style="color: #339933;">-</span> u1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>choices<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">,</span> <span style="color: #ff0000;">'C'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      ll <span style="color: #339933;">&lt;-</span> ll <span style="color: #339933;">+</span> log<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      ll <span style="color: #339933;">&lt;-</span> ll <span style="color: #339933;">+</span> log<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">-</span> p<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>ll<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the second implementation, I define a row level likelihood function, so that the summing and logarithmic transform are vectorized.</p>

<div class="wp_codebox"><table><tr id="p426421"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p4264code21"><pre class="c" style="font-family:monospace;">rowwise.<span style="color: #202020;">likelihood</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>row<span style="color: #339933;">,</span> a<span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  u2 <span style="color: #339933;">&lt;-</span> discounted.<span style="color: #202020;">value</span><span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'X2'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> row<span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'T2'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
  u1 <span style="color: #339933;">&lt;-</span> discounted.<span style="color: #202020;">value</span><span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'X1'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> row<span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'T1'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
  p <span style="color: #339933;">&lt;-</span> invlogit<span style="color: #009900;">&#40;</span>a <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>u2 <span style="color: #339933;">-</span> u1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>ifelse<span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'C'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> p<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">-</span> p<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
log.<span style="color: #202020;">likelihood</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>choices<span style="color: #339933;">,</span> a<span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  likelihoods <span style="color: #339933;">&lt;-</span> apply<span style="color: #009900;">&#40;</span>choices<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>rowwise.<span style="color: #202020;">likelihood</span><span style="color: #009900;">&#40;</span>row<span style="color: #339933;">,</span> a<span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#40;</span>log<span style="color: #009900;">&#40;</span>likelihoods<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the third implementation, I define a fully vectorized log likelihood function that avoids any explicit iteration and therefore removes most of the data frame indexing operations:</p>

<div class="wp_codebox"><table><tr id="p426422"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p4264code22"><pre class="c" style="font-family:monospace;">log.<span style="color: #202020;">likelihood</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>choices<span style="color: #339933;">,</span> a<span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  u2 <span style="color: #339933;">&lt;-</span> discounted.<span style="color: #202020;">value</span><span style="color: #009900;">&#40;</span>choices$X2<span style="color: #339933;">,</span> choices$T2<span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
  u1 <span style="color: #339933;">&lt;-</span> discounted.<span style="color: #202020;">value</span><span style="color: #009900;">&#40;</span>choices$X1<span style="color: #339933;">,</span> choices$T1<span style="color: #339933;">,</span> delta<span style="color: #009900;">&#41;</span>
  p <span style="color: #339933;">&lt;-</span> invlogit<span style="color: #009900;">&#40;</span>a <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>u2 <span style="color: #339933;">-</span> u1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  likelihoods <span style="color: #339933;">&lt;-</span> ifelse<span style="color: #009900;">&#40;</span>choices$C <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> p<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">-</span> p<span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#40;</span>log<span style="color: #009900;">&#40;</span>likelihoods<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The code I used to call all of these implementations and compare them is up on <a href="https://github.com/johnmyleswhite/fastR">GitHub</a> for those interested. The results, which strike me as remarkable, are below:</p>
<ol>
<li>On my laptop, implementation 1 takes ~1.0 second to run.</li>
<li>On my laptop, implementation 2 takes ~0.25 seconds to run.</li>
<li>On my laptop, implementation 3 takes ~0.01 seconds to run.</li>
</ol>
<p>In short, the third implementation is 100x faster than the first implementation with only minor changes to the code I originally wrote. Hopefully this example will help inspire others who have R code they&#8217;d like to speed up, but aren&#8217;t sure where to start.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2011/06/18/speeding-up-mle-code-in-r/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Inconsistencies in Bayesian Models of Decision-Making</title>
		<link>http://www.johnmyleswhite.com/notebook/2011/01/20/inconsistencies-in-bayesian-models-of-decision-making/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2011/01/20/inconsistencies-in-bayesian-models-of-decision-making/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 03:48:04 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Economics]]></category>
		<category><![CDATA[Psychology]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=4199</guid>
		<description><![CDATA[But modeling devices that make sense for an unbiased decisionmaker may not make sense for a biased one. For example, why would individuals have priors and posteriors if they are destined to apply Bayes&#8217; law incorrectly?1 A question I often ask myself. Wolfgang Pesendorfer : Behavioral Economics Comes of Age: A Review Essay on Advances [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>
But modeling devices that make sense for an unbiased decisionmaker may not make sense for a biased one. For example, why would individuals have priors and posteriors if they are destined to apply Bayes&#8217; law incorrectly?<sup><a href="http://www.johnmyleswhite.com/notebook/2011/01/20/inconsistencies-in-bayesian-models-of-decision-making/#footnote_0_4199" id="identifier_0_4199" class="footnote-link footnote-identifier-link" title="Wolfgang Pesendorfer : Behavioral Economics Comes of Age: A Review Essay on Advances in Behavioral Economics">1</a></sup>
</p></blockquote>
<p>A question I often ask myself.</p>
<ol class="footnotes"><li id="footnote_0_4199" class="footnote"><a href="http://www.princeton.edu/~pesendor/">Wolfgang Pesendorfer</a> : <a href="http://www.jstor.org/stable/30032350">Behavioral Economics Comes of Age: A Review Essay on Advances in Behavioral Economics</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2011/01/20/inconsistencies-in-bayesian-models-of-decision-making/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Academic Jargon: Field-Specific Insults</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/12/12/academic-jargon-field-specific-insults/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/12/12/academic-jargon-field-specific-insults/#comments</comments>
		<pubDate>Sun, 12 Dec 2010 23:22:49 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Academia]]></category>
		<category><![CDATA[Economics]]></category>
		<category><![CDATA[Psychology]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=4191</guid>
		<description><![CDATA[Every academic field seems to develop a set of generic insults based on their intellectual toolkit. Here are two examples I hear often: Probabilists and Statisticians: &#8220;I think that&#8217;s an interesting case, but it&#8217;s in a set with measure zero.&#8221; Economists: &#8220;X group&#8217;s behavior is clearly rent-seeking.&#8221; Do any readers have good examples from other [...]]]></description>
			<content:encoded><![CDATA[<p>Every academic field seems to develop a set of generic insults based on their intellectual toolkit. Here are two examples I hear often:</p>
<ol>
<li><b>Probabilists and Statisticians</b>: &#8220;I think that&#8217;s an interesting case, but it&#8217;s in a set with measure zero.&#8221;</li>
<li><b>Economists</b>: &#8220;X group&#8217;s behavior is clearly rent-seeking.&#8221;</li>
</ol>
<p>Do any readers have good examples from other fields?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/12/12/academic-jargon-field-specific-insults/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Response Times, The Exponential Distribution and Poisson Processes</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/09/18/response-times-the-exponential-distribution-and-poisson-processes/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/09/18/response-times-the-exponential-distribution-and-poisson-processes/#comments</comments>
		<pubDate>Sat, 18 Sep 2010 19:40:05 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Psychology]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=4063</guid>
		<description><![CDATA[I&#8217;m currently reading Luce&#8217;s &#8220;Response Times&#8221;. If you don&#8217;t know anything about response times, they are very easily defined: a response time is the length of time it takes a person to respond to a simple request, measured from the moment when the request is made to the moment when the person&#8217;s response is recorded. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently reading <a href="http://www.amazon.com/Response-Times-Elementary-Organization-Psychology/dp/0195070011/ref=sr_1_1?s=books&#038;ie=UTF8&#038;qid=1284837406&#038;sr=1-1">Luce&#8217;s &#8220;Response Times&#8221;.</a> If you don&#8217;t know anything about response times, they are very easily defined: a response time is the length of time it takes a person to respond to a simple request, measured from the moment when the request is made to the moment when the person&#8217;s response is recorded. In principle, you can measure response times when asking people to indicate that they&#8217;ve heard a tone as easily as you can measure them when you&#8217;ve asked people to solve a problem in calculus. In practice, response times are most easily analyzed when the task a person is performing is so short that the person completing it can usually be assumed not to be performing other tasks simultaneously.</p>
<p>To convince non-psychologists that response times are a quantity worth measuring, I&#8217;ll give an example from my own work that I suspect is widely generalizable. I consistently find that the quality of data I can extract from Mechanical Turk HIT&#8217;s grows considerably if I simply weight each person&#8217;s responses by a function of their time to complete the HIT. For one recent task, the histogram of task completion times looked like the following:</p>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2010/09/rts.png" alt="rts.png" border="0" width="480" height="480" /></div>
<p>As you can obviously see, there are two noticeable clusters of participants for this HIT: a substantial chunk who spent shockingly little time on the task and a large majority of people who spent a longer and more variable period of time performing the task. In this case, completely excluding those who completed this specific task within ten seconds improved the results of my later analyses considerably at virtually no cost to myself. Other approaches, like using k-means clustering, mixture modeling or simple quantile cut-offs, can be equally useful. <b>In general, response times are a useful proxy for the amount of effort a person puts into a task.</b></p>
<p>Hopefully that example convinces you that reaction times are worth studying carefully. If you&#8217;re interested, I strongly recommend Luce&#8217;s book as an introduction to the use of response times to infer information about the mental state of specific human beings and about the global architecture of the human mind in general. It&#8217;s quite long, so many people would probably gain the most from only reading the first few chapters.</p>
<p>In the rest of this post, I&#8217;m going to discuss a detail about the measurement of response times that I found particularly beautiful and insightful. Specifically, I was struck by a point that Luce raises in the second chapter of his book: the proper measurement of response times requires the use of an experimental design that employs the exponential distribution to produce a Poisson process of stimuli. That&#8217;s probably opaque to most readers, so let me unpack it:</p>
<ol>
<li><b>Our Objective</b>: We want to estimate the speed with which a normal person can respond to a stimulus.</li>
<li><b>Our High-Level Method</b>: We present subjects with a train of simple sensory stimuli, like 10 dB tones, and ask them to respond as quickly as possible to each by pressing a button. We record the time from the onset of the tone to the moment when they press the button.</li>
<li><b>Our Challenge</b>: The length of time before a stimulus is presented must be randomly distributed, so that subjects cannot adopt a fixed strategy for timing their responses. More importantly, we must insure that the mere act of waiting for a stimulus is not itself informative: the subject should not be able to notice that the arrival of a stimulus is becoming progressively more or less likely as the time they&#8217;ve been waiting grows. This means that a naive sampling distribution for waiting times before presenting a stimulus, like the uniform distribution, is simply not acceptable.</li>
<li><b>Our Solution</b>: We calculate a measure of the informativeness of each moment of waiting: the measure we use is called the <a href="http://en.wikipedia.org/wiki/Hazard_rate">hazard rate</a> and can be calculated for any probability distribution. Using this metric of informativeness, we simply need to find a probability distribution that has a constant hazard rate. The distribution we arrive at is <a href="http://en.wikipedia.org/wiki/Exponential_distribution">the exponential distribution</a>. A train of stimuli separated by exponentially distributed waiting periods is called a <a href="http://en.wikipedia.org/wiki/Poisson_process">Poisson process</a>. It allows us to treat all of the instants within a single trial&#8217;s waiting period as comparable.</li>
</ol>
<p>For me, this was a revelatory example that finally made the exponential distribution&#8217;s relevance clear. Understanding this point made me understood intuitively, rather than simply algebraically, why Luce says this in the first chapter:</p>
<blockquote><p>
[We] see that the answer is [...] the exponential distribution. This may come as a bit of a surprise since, after all, the largest value of the exponential value is at 0 &#8212; instantaneous repetition &#8212; which must tend to produce clustering if the process is repeated in time. This is, indeed, the case, as can either be seen, as in Figure 1.3, or heard if such a process is made audible in time &#8212; it seems to sputter. To neither the eye nor the ear does it seem uniform in time, but that is an illusion, confusing what one sees or hears with the underlying rate of these events. The tendency for the event to occur is constant. Such a process is called <i>Poisson</i> &#8212; more of it in Chapters 4 and 9. It is the basic concept of constant randomness in time &#8212; that is, the unbounded, ordered analogue of a uniform density on a finite, unordered interval.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/09/18/response-times-the-exponential-distribution-and-poisson-processes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Escher and Redemption: Using Cyclical Preferences to Overcome Hedonic Treadmills</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/09/12/escher-and-redemption-using-cyclical-preferences-to-overcome-hedonic-treadmills/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/09/12/escher-and-redemption-using-cyclical-preferences-to-overcome-hedonic-treadmills/#comments</comments>
		<pubDate>Sun, 12 Sep 2010 16:28:37 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Economics]]></category>
		<category><![CDATA[Psychology]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=4055</guid>
		<description><![CDATA[This morning I started thinking about using violations of classical economic theory to increase well-being. The main idea is probably obvious to anyone familiar with the relevant literature on cyclical preferences and the hedonic treadmill, but I think it&#8217;s still worth articulating cleanly. Please let me know if you have a reference to existing literature [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I started thinking about using violations of classical economic theory to increase well-being. The main idea is probably obvious to anyone familiar with the relevant literature on cyclical preferences and the hedonic treadmill, but I think it&#8217;s still worth articulating cleanly. Please let me know if you have a reference to existing literature on this approach.</p>
<p>First, let&#8217;s assume the following things about our idealized human being:</p>
<ol>
<li>They possess cyclical preferences where A > B, B > C and C > A.</li>
<li>Their derived pleasure from experiencing A, B or C is based on an adaptive scale, i.e. they derive pleasure from having something better than their local average consumption.</li>
</ol>
<p>Under these assumptions, the best possible consumption stream is an ordered walk through the cycle of their preferences, such as (A, B, C, A, B, C, &#8230;). Assuming that their adaption is sufficiently local, they will able to continually derive pleasure from moving along the preference chain, despite the hedonic treadmill slowly eroding the gains made from any single movement along the preference graph.</p>
<p>The big idea here is that cyclical preferences, which are generally assumed to lower welfare, can be exploited to increase welfare for agents that suffer from a hedonic treadmill. Since there is some reason to believe in both violations of classical theory, this is potentially one trick for improving one&#8217;s own well-being.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/09/12/escher-and-redemption-using-cyclical-preferences-to-overcome-hedonic-treadmills/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Failures of Self-Control: More Data</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/09/07/failures-of-self-control-more-data/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/09/07/failures-of-self-control-more-data/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 23:55:56 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Economics]]></category>
		<category><![CDATA[Psychology]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=4049</guid>
		<description><![CDATA[Just when you worried that preference reversals weren&#8217;t real: Professional bookmakers rarely accept bets from individuals who directly control the outcome of the bet. We analyse a unique exception to this rule and a potential policy innovation in the battle against obesity: a weight loss betting market. If obese individuals have time-inconsistent preferences then commitment [...]]]></description>
			<content:encoded><![CDATA[<p>Just when you worried that <a href="http://www.google.com/url?sa=t&#038;source=web&#038;cd=1&#038;ved=0CBcQFjAA&#038;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.63.2938%26rep%3Drep1%26type%3Dpdf&#038;rct=j&#038;q=preference%20reversal%20intertemporal&#038;ei=uNOGTIqbNsSAlAfT6eXUDw&#038;usg=AFQjCNF6TXwgh2eJ3y1MXFQU0xBGqp9csA">preference reversals</a> weren&#8217;t real:</p>
<blockquote><p>
Professional bookmakers rarely accept bets from individuals who directly control the outcome of the bet. We analyse a unique exception to this rule and a potential policy innovation in the battle against obesity: a weight loss betting market. If obese individuals have time-inconsistent preferences then commitment mechanisms, such as personal gambles, should help them restrain their short-term impulses and lose weight. Correspondence with the bettors confirms that this is their primary motivation. However, it appears that the bettors in our sample are not particularly skilled at choosing effective commitment mechanisms. Despite payoffs of as high as $7350, approximately 80% of people who spend money to bet on their own behaviour end up losing their bets.<sup><a href="http://www.johnmyleswhite.com/notebook/2010/09/07/failures-of-self-control-more-data/#footnote_0_4049" id="identifier_0_4049" class="footnote-link footnote-identifier-link" title="Nicholas Burger and John Lynham : Betting on weight loss . . . and losing: personal gambles as commitment mechanisms">1</a></sup>
</p></blockquote>
<p>HT <a href="http://cheeptalk.wordpress.com/2010/09/07/bad-news-for-commitment-mechanisms/">Cheap Talk</a>. Reading this makes me glad to have finished my last Master&#8217;s degree requirement today by turning in a review of the literature on intertemporal choice.</p>
<ol class="footnotes"><li id="footnote_0_4049" class="footnote">Nicholas Burger and John Lynham : <a href="http://www2.hawaii.edu/~lynham/Welcome_files/913267828-2.pdf">Betting on weight loss . . . and losing: personal gambles as commitment mechanisms</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/09/07/failures-of-self-control-more-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paul Meehl via Andrew Gelman</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/01/paul-meehl-via-andrew-gelman/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/01/paul-meehl-via-andrew-gelman/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 16:30:15 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Psychology]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3628</guid>
		<description><![CDATA[Andrew Gelman points out that there is now a website devoted entirely to Paul Meehl. In my mind, Paul Meehl was one of the greatest psychologists of the 20th century. Few other people in our field were more level-headed or insightful. Every paper written by Meehl is a delight to read, so this is a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.stat.columbia.edu/~cook/movabletype/archives/2009/12/all_meehl_all_t.html">Andrew Gelman points out<a/> that there is now <a href="http://www.tc.umn.edu/~pemeehl/pubCategories.htm">a website devoted entirely to Paul Meehl</a>.</p>
<p>In my mind, Paul Meehl was one of the greatest psychologists of the 20th century. Few other people in our field were more level-headed or insightful. Every paper written by Meehl is a delight to read, so this is a veritable treasure trove of material.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/01/paul-meehl-via-andrew-gelman/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IQ, Intelligence and Hypocrisy</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/04/17/iq-intelligence-and-hypocrisy/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/04/17/iq-intelligence-and-hypocrisy/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 12:44:22 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Psychology]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3492</guid>
		<description><![CDATA[Razib&#8217;s recent post on IQ at Gene Expression contained a particularly poignant passage that I thought I should quote: Of course, there is &#8220;believe,&#8221; and then there is believe. The same people who don&#8217;t believe in intelligence are proud of their GRE scores, convinced that Republicans and religious people have lower IQ&#8217;s, and outraged when [...]]]></description>
			<content:encoded><![CDATA[<p>Razib&#8217;s <a href="http://www.gnxp.com/blog/2009/04/iq-and-conventional-wisdom.php">recent post</a> on IQ at Gene Expression contained a particularly poignant passage that I thought I should quote:</p>
<blockquote><p>
Of course, there is &#8220;believe,&#8221; and then there is <b>believe</b>. The same people who don&#8217;t believe in intelligence are proud of their GRE scores, convinced that Republicans and religious people have lower IQ&#8217;s, and outraged when the mentally deficient, as measured on IQ tests, are executed. This probably reflects some mental modularity. People might say <i>they don&#8217;t believe in IQ</i>, but the decisions they make are to some extent informed by the assumption that <i>intelligence exists, and individuals vary</i>.<sup><a href="http://www.johnmyleswhite.com/notebook/2009/04/17/iq-intelligence-and-hypocrisy/#footnote_0_3492" id="identifier_0_3492" class="footnote-link footnote-identifier-link" title="Razib : Gene Expression : IQ and &amp;#8220;conventional wisdom&amp;#8221;">1</a></sup>
</p></blockquote>
<p>While I frequently insist that measuring intelligence must, at least in principle, be a meaningful activity because all we do it implicitly in using words like intelligent or stupid to describe people &#8212; about which we are all in surprisingly good agreement &#8211;, I&#8217;d never thought of these very clear examples of hypocrisy in rejecting IQ only when it doesn&#8217;t serve one&#8217;s interests and prejudices.</p>
<ol class="footnotes"><li id="footnote_0_3492" class="footnote">Razib : Gene Expression : <a href="http://www.gnxp.com/blog/2009/04/iq-and-conventional-wisdom.php">IQ and &#8220;conventional wisdom&#8221;</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/04/17/iq-intelligence-and-hypocrisy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>And the Teslas Just Keep on Coming</title>
		<link>http://www.johnmyleswhite.com/notebook/2008/12/29/and-the-teslas-just-keep-on-coming/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2008/12/29/and-the-teslas-just-keep-on-coming/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 15:47:01 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Psychology]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3242</guid>
		<description><![CDATA[I think this Youtube video does a far better job of showcasing the dangers of MRI machines than &#8220;The Magnetic Zone&#8221; video that Siemens distributes. I particularly enjoy the &#8220;take off&#8221; sound that the air cylinder makes three seconds into the clip.]]></description>
			<content:encoded><![CDATA[<p>I think <a href="http://www.youtube.com/watch?v=RcNXinLX9GA">this Youtube video</a> does a far better job of showcasing the dangers of MRI machines than &#8220;The Magnetic Zone&#8221; video that Siemens distributes. I particularly enjoy the &#8220;take off&#8221; sound that the air cylinder makes three seconds into the clip.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2008/12/29/and-the-teslas-just-keep-on-coming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.455 seconds -->

