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

<channel>
	<title>James Hensman's Weblog</title>
	<atom:link href="http://jameshensman.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jameshensman.wordpress.com</link>
	<description></description>
	<lastBuildDate>Mon, 23 Jan 2012 19:55:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jameshensman.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>James Hensman's Weblog</title>
		<link>http://jameshensman.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jameshensman.wordpress.com/osd.xml" title="James Hensman&#039;s Weblog" />
	<atom:link rel='hub' href='http://jameshensman.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Multiple Matrix Multiplication in numpy</title>
		<link>http://jameshensman.wordpress.com/2010/06/14/multiple-matrix-multiplication-in-numpy/</link>
		<comments>http://jameshensman.wordpress.com/2010/06/14/multiple-matrix-multiplication-in-numpy/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 10:45:55 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=89</guid>
		<description><![CDATA[There are two ways to deal with matrices in numpy. The standard numpy array in it 2D form can do all kinds of matrixy stuff, like dot products, transposes, inverses, or factorisations, though the syntax can be a little clumsy. For those who just can&#8217;t let go of matlab, there&#8217;s a matrix object which prettifies [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=89&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are two ways to deal with matrices in numpy. The standard numpy array in it 2D form can do all kinds of matrixy stuff, like dot products, transposes, inverses, or factorisations, though the syntax can be a little clumsy. For those who just can&#8217;t let go of matlab, there&#8217;s a matrix object which prettifies the syntax somewhat. For example, to do <img src='http://s0.wp.com/latex.php?latex=A+%3D+B%5E%5Ctop+C%5E%7B-1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='A = B^&#92;top C^{-1}' title='A = B^&#92;top C^{-1}' class='latex' /></p>
<p><pre class="brush: python;">
import numpy as np

#using arrays
B = np.random.randn(2,2) # random array
C = np.random.randn(2,2)

A = np.dot(B.T, np.linalg.inv(C))

#using matrices
B = np.mat(B) #cast as matrix
C = np.mat(C)

A = B.T*C.I
</pre></p>
<p>Despite the nicer syntax in the matrix version, I prefer to use arrays. This is in part because they play nicer with the rest of numpy, but mostly because they behave better in higher dimensions. This is really useful when you have to deal with lots of matrices, which seems to occur all the time in my work:</p>
<p><pre class="brush: python;">
A = np.random.randn(100,2,2) # a hundred 2x2 arrays
A2 = [np.mat(np.random.randn(2,2)) for i in range(100)] # a hundered 2x2 matrices
</pre></p>
<h2>Transposing</h2>
<p>Transposing the list of matrices is easy: just use the .T operator. This doesn&#8217;t work for the 100x2x2 array though, since it switches the axis in a way we don;t want. The solution is to manually specify which axes to switch.</p>
<p><pre class="brush: python;">
A2T = [a.T for a in A2] # matrix version
AT = np.transpose(A,(0,2,1)) #array version 1
#or
AT = np.rollaxis(A,-2,-1)# array version 2
</pre></p>
<h2>Multiplication</h2>
<p>Suppose you have a series of matrices which you want to (right) multiply by another matrix. This is messy with the matrix object, where you need to do list comprehension, but nice as pie with the array object.</p>
<p><pre class="brush: python;">
#matrix version
A = [np.mat(np.random.randn(2,2)) for i in range(100)]
B = np.mat(np.random.randn(2,2))
AB = [a*B for a in A]

#array version
A = np.random.randn(100,2,2)
B = np.random.randn(2,2)
AB = np.dot(A,B)
</pre></p>
<p>Left-multiplication is a little harder, but possible using a transpose trick: <img src='http://s0.wp.com/latex.php?latex=%28BA%29%5E%5Ctop+%3D+A%5E%5Ctop+B%5E%5Ctop&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='(BA)^&#92;top = A^&#92;top B^&#92;top' title='(BA)^&#92;top = A^&#92;top B^&#92;top' class='latex' /></p>
<p><pre class="brush: python;">
#matrix version
BA = [Ba for a in A]

#array version
BA = np.transpose(np.dot(np.transpose(A,(0,2,1)),B.T),(0,2,1))
</pre></p>
<p>Okay, the syntax is getting ugly there, I&#8217;ll admit. Suppose now that you had two sets of matrices, and wanted the product of each element, as in <img src='http://s0.wp.com/latex.php?latex=C_i+%3D+A_i+B_i%2C+%5C%2C+i%3D1+%5Cldots+100&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='C_i = A_i B_i, &#92;, i=1 &#92;ldots 100' title='C_i = A_i B_i, &#92;, i=1 &#92;ldots 100' class='latex' /></p>
<p><pre class="brush: python;">
#matrix version
A = [np.mat(np.random.randn(2,2)) for i in range(100)]
B = [np.mat(np.random.randn(2,2)) for i in range(100)]
AB = [a*b for a,b in zip(A,B)]

#array version
A = np.random.randn(100,2,2)
B = np.random.randn(100,2,2)
np.sum(np.transpose(A,(0,2,1)).reshape(100,2,2,1)*B.reshape(100,2,1,2),-3)
</pre></p>
<p>I&#8217;ll admit that the syntax of this is very weird. To see how it works, consider taking the (matrix) product of two arrays in a similar manner:</p>
<p><pre class="brush: python;">
A = np.random.randn(2,2)
B = np.random.randn(2,2)
np.sum(A.T.reshape(2,2,1)*B.reshape(2,1,2),0)
</pre></p>
<p>The reshaping persuades numpy to broadcast the multiplication, which results in a 2x2x2 cube of numbers. Summing the numbers along the first dimension of the cube results in matrix multiplication. I have some scribbles which illustrate this, which I&#8217;ll post if anyone wants.</p>
<h2>Speed</h2>
<p>The main motivation for using arrays in this manner is speed. I find for loops in python to be rather slow (including within list comps), so I prefer to use numpy array methods whenever possible. The following runs a quick test, multiplying 1000 3&#215;3 matrices together. It&#8217;s a little crude, but it shows the numpy.array method to be 10 times faster than the list comp of np.matrix.</p>
<p><pre class="brush: python;">
import numpy as np
import timeit

#compare multiple matrix multiplication using list coms of matrices and deep arrays

#1) the matrix method
setup1 = &quot;&quot;&quot;
import numpy as np
A = [np.mat(np.random.randn(3,3)) for i in range(1000)]
B = [np.mat(np.random.randn(3,3)) for i in range(1000)]
&quot;&quot;&quot;

test1 = &quot;&quot;&quot;
AB = [a*b for a,b in zip(A,B)]
&quot;&quot;&quot;

timer1 = timeit.Timer(test1,setup1)
print timer1.timeit(100)

#2) the array method
setup2 = &quot;&quot;&quot;
import numpy as np
A = np.random.randn(1000,3,3)
B = np.random.randn(1000,3,3)
&quot;&quot;&quot;

test2 = &quot;&quot;&quot;
AB = np.sum(np.transpose(A,(0,2,1)).reshape(1000,3,3,1)*B.reshape(1000,3,1,3),0)
&quot;&quot;&quot;

timer2 = timeit.Timer(test2,setup2)
print timer2.timeit(100)
</pre></p>
<p>In the likely event that there&#8217;s a better way to do this, or I&#8217;ve screwed up some code somewhere, please leave me a comment <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/89/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=89&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2010/06/14/multiple-matrix-multiplication-in-numpy/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>Tracking a finger</title>
		<link>http://jameshensman.wordpress.com/2009/05/15/tracking-a-finger/</link>
		<comments>http://jameshensman.wordpress.com/2009/05/15/tracking-a-finger/#comments</comments>
		<pubDate>Fri, 15 May 2009 11:29:53 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=77</guid>
		<description><![CDATA[Right. Having broken my avi file into pngs, how do I keep track of an object in the vid? Check this out to see what I mean: I&#8217;m not sure what the tapping of the finger is about (at the start of the vid), but I have data from the experiment which I&#8217;d like to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=77&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Right. Having broken my avi file into pngs, how do I keep track of an object in the vid? Check this out to see what I mean:</p>
<span style="text-align:center; display: block;"><a href="http://jameshensman.wordpress.com/2009/05/15/tracking-a-finger/"><img src="http://img.youtube.com/vi/aA6WqoGGIjQ/2.jpg" alt="" /></a></span>
<p>I&#8217;m not sure what the tapping of the finger is about (at the start of the vid), but I have data from the experiment which I&#8217;d like to correlate to the position of the finger on the rig. the idea is to follow that black blob.</p>
<p>Having broken the vid into png images, I loaded them into python using PIL:<br />
<pre class="brush: python;">
from PIL import Image
images = [Image.open('./tmp/'+f) for f in imagenames]
</pre></p>
<p>and then made them into numpy arrays, slicing off only the red channel and the middle part of the image(by inspection, the dot was most visible on the red channel):<br />
<pre class="brush: python;">
images = [np.asarray(image)[upper:lower,:,0] for image in images]#0 indexes red
</pre></p>
<p>Now, scipy.ndimage has an awesome tool for identifying regions of an image. I used a threshold (set by inspection) first:<br />
<pre class="brush: python;">
images = [image&lt;threshold for image in images]
regions = [scipy.ndimage.label(image)[0] for image in images]#[0]:region info. only
</pre></p>
<p>Each of the &#8216;regions&#8217; then is a numpy array of the same size and shape as the images, but with 0&#8242;s in the biggest region, 1s in the next biggest, etc. I wrote a simple set of rules to cut out the unwanted regions, and calculated the center of the remaining region. there are occasions where two or more regions pass my simple set of rules, in this case I look at the region identified in the previous image in an attempt to &#8216;track&#8217; the blob. Crude, but works okay. </p>
<p>I subplotted various stages in the process and stitches the plots together to make this vid:<br />
<span style="text-align:center; display: block;"><a href="http://jameshensman.wordpress.com/2009/05/15/tracking-a-finger/"><img src="http://img.youtube.com/vi/Cu08aLAUVuY/2.jpg" alt="" /></a></span></p>
<p>the top image is the (sliced) original image, with a green dot where I think the block pen mark is. The middle plot is a pylab.imshow() of the regions, and the final subplot is the same after applying my set of rules.</p>
<p>Thanks to Sarah T. for letting me post the videos. </p>
<p>Edit: corrected some code</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/77/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=77&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/05/15/tracking-a-finger/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>Making and Breaking Videos into pngs</title>
		<link>http://jameshensman.wordpress.com/2009/05/15/making-and-breaking-videos-into-pngs/</link>
		<comments>http://jameshensman.wordpress.com/2009/05/15/making-and-breaking-videos-into-pngs/#comments</comments>
		<pubDate>Fri, 15 May 2009 10:59:19 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=72</guid>
		<description><![CDATA[I&#8217;ve got some videos that I&#8217;d like to do a little bit of processing on. nothing spectacular, I just want to track a black blob. Luckily numpy/Scipy has all the tools I need to get started&#8230; on images. I need to break the (avi) video file into a series of png files (i could use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=72&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got some videos that I&#8217;d like to do a little bit of processing on. nothing spectacular, I just want to track a black blob.</p>
<p>Luckily numpy/Scipy has all the tools I need to get started&#8230; on images. I need to break the (avi) video file into a series of png files (i could use jpeg, I suppose, but it&#8217;s not like hdd space is limited here&#8230;). The tool I&#8217;ve been using for this is ffmpeg.</p>
<p>Now, ffmpeg is a phenomenal beast. It can re-scale videos, re-encode them, crop them, and all kinds of other things that I&#8217;m not interested in. The command I&#8217;ve used to break a avi file into pngs is:</p>
<p>ffmpeg -i  ./tmp/%03d.png</p>
<p>It&#8217;s nice that ffmpeg recognises the %03d format string, huh? Now that I&#8217;ve got some images, I can load them into numpy and process them (future blog post!)</p>
<p>After doing said processing, ffmpeg should be able to re-code my processed images into a video file for convenience. However, I&#8217;m really lazy, and can&#8217;t be bothered to read the whole of the ffmpeg doc. So I&#8217;m copying Mike here and using:</p>
<p>mencoder &#8216;mf://tmp/*.png&#8217; -mf type=png:fps=10 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o &lt;whatever&gt;.mpg</p>
<p>I&#8217;m sure ffmpeg can do the job too, but it just isn&#8217;t working for me.</p>
<p>In fact, I&#8217;m wrapping both of those commands up in python, using the os module.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/72/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=72&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/05/15/making-and-breaking-videos-into-pngs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>Expected Values (Matrices)</title>
		<link>http://jameshensman.wordpress.com/2009/02/12/expected-values-matrices/</link>
		<comments>http://jameshensman.wordpress.com/2009/02/12/expected-values-matrices/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 09:41:11 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=49</guid>
		<description><![CDATA[I&#8217;ve been fiddling around in python, implementing a Variational Bayesian Principal Component Analysis (VBPCA) algorithm. It&#8217;s true, I could do this happily in vibes , but where&#8217;s the fun in that? Besides, I think I learned more doing it this way. Happily, there&#8217;s a nice paper by Chris Bishop, which explains clearly what&#8217;s going on, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=49&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been fiddling around in python, implementing a Variational Bayesian Principal Component Analysis (VBPCA) algorithm. It&#8217;s true, I could do this happily in <a href="http://vibes.sourceforge.net/"> vibes </a>, but where&#8217;s the fun in that? Besides, I think I learned more doing it this way. </p>
<p>Happily, there&#8217;s a nice <a href="http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=819772">paper</a> by Chris Bishop, which explains clearly what&#8217;s going on, and gives you the update equations. For example:</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%7B%5Cbf+m_x%7D%5E%7B%28n%29%7D+%3D+%5Clangle+%5Ctau+%5Crangle+%7B%5Cbf+%5CSigma_x%7D+%5Clangle+%7B%5Cbf+W%7D%5E%5Ctop+%5Crangle+%28t_n+-+%5Clangle+%7B%5Cmathbf+%5Cmu%7D+%5Crangle%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bf m_x}^{(n)} = &#92;langle &#92;tau &#92;rangle {&#92;bf &#92;Sigma_x} &#92;langle {&#92;bf W}^&#92;top &#92;rangle (t_n - &#92;langle {&#92;mathbf &#92;mu} &#92;rangle)' title='{&#92;bf m_x}^{(n)} = &#92;langle &#92;tau &#92;rangle {&#92;bf &#92;Sigma_x} &#92;langle {&#92;bf W}^&#92;top &#92;rangle (t_n - &#92;langle {&#92;mathbf &#92;mu} &#92;rangle)' class='latex' /></p></blockquote>
<p>which involves taking the expected value of <img src='http://s0.wp.com/latex.php?latex=%5Ctau&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;tau' title='&#92;tau' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbf+W%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bf W}' title='{&#92;bf W}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cmathbf+%5Cmu&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;mathbf &#92;mu' title='&#92;mathbf &#92;mu' class='latex' />.  These three are straightforward:</p>
<p>The distribution for <img src='http://s0.wp.com/latex.php?latex=%5Ctau&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;tau' title='&#92;tau' class='latex' /> is <img src='http://s0.wp.com/latex.php?latex=p%28%5Ctau%29+%3D+%5Ctextit%7BGamma%7D%28%5Ctau+%5Cmid+a%2Cb%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;tau) = &#92;textit{Gamma}(&#92;tau &#92;mid a,b)' title='p(&#92;tau) = &#92;textit{Gamma}(&#92;tau &#92;mid a,b)' class='latex' />, and so the expected value of <img src='http://s0.wp.com/latex.php?latex=%5Ctau&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;tau' title='&#92;tau' class='latex' /> is <img src='http://s0.wp.com/latex.php?latex=a%2Fb&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='a/b' title='a/b' class='latex' />.  </p>
<p>The distribution for <img src='http://s0.wp.com/latex.php?latex=%5Cmu&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;mu' title='&#92;mu' class='latex' /> is <img src='http://s0.wp.com/latex.php?latex=p%28%5Cmu%29+%3D+%5Cmathcal%7BN%7D%28%5Cmu+%5Cmid+%7B%5Cbf+m_%5Cmu%7D%2C%7B%5Cbf+%5CSigma_%5Cmu%7D%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;mu) = &#92;mathcal{N}(&#92;mu &#92;mid {&#92;bf m_&#92;mu},{&#92;bf &#92;Sigma_&#92;mu})' title='p(&#92;mu) = &#92;mathcal{N}(&#92;mu &#92;mid {&#92;bf m_&#92;mu},{&#92;bf &#92;Sigma_&#92;mu})' class='latex' />, and so the expected value of <img src='http://s0.wp.com/latex.php?latex=%5Cmu&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;mu' title='&#92;mu' class='latex' /> is simply <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbf+m_%5Cmu%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bf m_&#92;mu}' title='{&#92;bf m_&#92;mu}' class='latex' />.  </p>
<p>The distribution for <img src='http://s0.wp.com/latex.php?latex=%5Cbf+W+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf W ' title='&#92;bf W ' class='latex' /> is a slightly more complex beast: <img src='http://s0.wp.com/latex.php?latex=%5Cbf+W+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf W ' title='&#92;bf W ' class='latex' /> is a non-square matrix, and we have a Gaussian distribution for each <em>row</em>.  That is: <img src='http://s0.wp.com/latex.php?latex=p%28%7B%5Cbf+W%7D%29+%3D+%5Cprod_%7Bi%3D1%7D%5Ed+%5Cmathcal%7BN%7D%28%7B%5Cbf+w%7D_i+%5Cmid+%7B%5Cbf+m%7D_w%5E%7B%28i%29%7D%2C+%7B%5Cbf+%5CSigma%7D_w%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p({&#92;bf W}) = &#92;prod_{i=1}^d &#92;mathcal{N}({&#92;bf w}_i &#92;mid {&#92;bf m}_w^{(i)}, {&#92;bf &#92;Sigma}_w)' title='p({&#92;bf W}) = &#92;prod_{i=1}^d &#92;mathcal{N}({&#92;bf w}_i &#92;mid {&#92;bf m}_w^{(i)}, {&#92;bf &#92;Sigma}_w)' class='latex' />.  Bizarrely, the rows share a covariance matrix.  The expected value of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbf+W%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bf W}' title='{&#92;bf W}' class='latex' /> is simple: is just a matrix made of a stack of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbf+m%7D_w%5E%7B%28i%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bf m}_w^{(i)}' title='{&#92;bf m}_w^{(i)}' class='latex' />s.  </p>
<p>The tricky bit comes in a different update equation, where we need to evaluate <img src='http://s0.wp.com/latex.php?latex=%5Clangle+%7B%5Cbf+W%7D%5E%5Ctop+%7B%5Cbf+W%7D+%5Crangle+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;langle {&#92;bf W}^&#92;top {&#92;bf W} &#92;rangle ' title='&#92;langle {&#92;bf W}^&#92;top {&#92;bf W} &#92;rangle ' class='latex' />.  The first thing to notice is that (where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbf+W%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bf W}' title='{&#92;bf W}' class='latex' /> is a d by q matrix): </p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%7B%5Cbf+W%5E%5Ctop+W%7D+%3D+%5Csum_%7Bi%3D1%7D%5Ed+%7B%5Cbf+w%7D_i+%7B%5Cbf+w%7D_i%5E%5Ctop&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bf W^&#92;top W} = &#92;sum_{i=1}^d {&#92;bf w}_i {&#92;bf w}_i^&#92;top' title='{&#92;bf W^&#92;top W} = &#92;sum_{i=1}^d {&#92;bf w}_i {&#92;bf w}_i^&#92;top' class='latex' />.  </p></blockquote>
<p>Since <img src='http://s0.wp.com/latex.php?latex=p%28%7B%5Cbf+w%7D_i%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p({&#92;bf w}_i)' title='p({&#92;bf w}_i)' class='latex' /> is Gaussian, <img src='http://s0.wp.com/latex.php?latex=%5Clangle+%7B%5Cbf+w%7D_i+%7B%5Cbf+w%7D_i%5E%5Ctop+%5Crangle+%3D+%7B%5Cbf+m%7D_i%7B%5Cbf+m%7D_i%5E%5Ctop+%2B+%7B%5Cbf+%5CSigma%7D_w&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;langle {&#92;bf w}_i {&#92;bf w}_i^&#92;top &#92;rangle = {&#92;bf m}_i{&#92;bf m}_i^&#92;top + {&#92;bf &#92;Sigma}_w' title='&#92;langle {&#92;bf w}_i {&#92;bf w}_i^&#92;top &#92;rangle = {&#92;bf m}_i{&#92;bf m}_i^&#92;top + {&#92;bf &#92;Sigma}_w' class='latex' />. Now we can write:</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=%5Clangle+%7B%5Cbf+W%5E%5Ctop+W%7D+%5Crangle+%3D+%5Clangle+%5Csum_%7Bi%3D1%7D%5Ed+%7B%5Cbf+w%7D_i+%7B%5Cbf+w%7D_i%5E%5Ctop+%5Crangle+%3D++%5Csum_%7Bi%3D1%7D%5Ed+%5Clangle+%7B%5Cbf+w%7D_i+%7B%5Cbf+w%7D_i%5E%5Ctop+%5Crangle+%3D+%5Csum_%7Bi%3D1%7D%5Ed+%5Cleft%28%7B%5Cbf+m%7D_i%7B%5Cbf+m%7D_i%5E%5Ctop+%2B+%7B%5Cbf+%5CSigma%7D_w+%5Cright%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;langle {&#92;bf W^&#92;top W} &#92;rangle = &#92;langle &#92;sum_{i=1}^d {&#92;bf w}_i {&#92;bf w}_i^&#92;top &#92;rangle =  &#92;sum_{i=1}^d &#92;langle {&#92;bf w}_i {&#92;bf w}_i^&#92;top &#92;rangle = &#92;sum_{i=1}^d &#92;left({&#92;bf m}_i{&#92;bf m}_i^&#92;top + {&#92;bf &#92;Sigma}_w &#92;right)' title='&#92;langle {&#92;bf W^&#92;top W} &#92;rangle = &#92;langle &#92;sum_{i=1}^d {&#92;bf w}_i {&#92;bf w}_i^&#92;top &#92;rangle =  &#92;sum_{i=1}^d &#92;langle {&#92;bf w}_i {&#92;bf w}_i^&#92;top &#92;rangle = &#92;sum_{i=1}^d &#92;left({&#92;bf m}_i{&#92;bf m}_i^&#92;top + {&#92;bf &#92;Sigma}_w &#92;right)' class='latex' /></p></blockquote>
<p>Simple when you know how.</p>
<p>Edit: Anyone know how to get a bold <img src='http://s0.wp.com/latex.php?latex=%5Cmu&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;mu' title='&#92;mu' class='latex' />? I&#8217;ve tried {\bf \mu} and \mathbf{\mu}.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=49&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/02/12/expected-values-matrices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>Back to school for me&#8230;</title>
		<link>http://jameshensman.wordpress.com/2009/02/04/back-to-school-for-me/</link>
		<comments>http://jameshensman.wordpress.com/2009/02/04/back-to-school-for-me/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 18:32:45 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=30</guid>
		<description><![CDATA[or: a well known and loved toy stats problem, bludgeoned by an engineer.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=30&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been having a preliminary flick through 	<a href="http://www.stat.columbia.edu/~gelman/"> Andrew Gelman&#8217;s</a> book, (<a href="http://www.amazon.co.uk/Bayesian-Analysis-Texts-statistical-science/dp/158488388X/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1233769937&amp;sr=8-1">amazon</a>) which so far seems excellent. I thought I&#8217;d have a shot at the questions in the <em> introductory</em> chapter. </p>
<p>First question. Easy, no problem. </p>
<p>Second question. Ooh, bit trickier. Got there in the end. </p>
<p>Third question. This has taken me 40 minutes, which seems like justification for posting a solution on this &#8216;ere weblog.</p>
<p>so the question is asked:</p>
<blockquote><p>Suppose that in each individual in a population there is a pair of genes, each of which can be either X or x, that controls eye colour: those with xx have blue eyes, those with XX or Xx or xX have brown eyes. Those with Xx are known as heterozygotes.</p>
<p>The proportion of individuals with blue eyes (xx) is <img src='http://s0.wp.com/latex.php?latex=a%5E2&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='a^2' title='a^2' class='latex' />, and the proportion of heterozygotes is <img src='http://s0.wp.com/latex.php?latex=2a%281-a%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='2a(1-a)' title='2a(1-a)' class='latex' />.  </p>
<p>Each parent transmits one gene to the child: if the parent is a heterozygote, the probability that they transmit X is <img src='http://s0.wp.com/latex.php?latex=0.5&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='0.5' title='0.5' class='latex' />.  Assuming random mating, show that amongst brown eyed parents with brown eyed children, the proportion of heterozygotes is <img src='http://s0.wp.com/latex.php?latex=2a%2F%281%2B2a%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='2a/(1+2a)' title='2a/(1+2a)' class='latex' />.
</p></blockquote>
<p>Okay says I, it&#8217;s just Bayes&#8217; rule, no? Let&#8217;s denote all heterozygotes as Xx (this should save significant keypresses&#8230;), children as ch and parents as pa. </p>
<p>Under Bayes rule we need a likelihood <img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bch%7D%3DXx+%7C+%5Ctext%7Bpa%7D%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{ch}=Xx | &#92;text{pa})' title='p(&#92;text{ch}=Xx | &#92;text{pa})' class='latex' />, prior <img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bpa%7D%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{pa})' title='p(&#92;text{pa})' class='latex' /> and a &#8216;marginal likelihood&#8217; term, which we get by summing the above. So we&#8217;re going to get something like:</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bch%7D%3DXx%29+%3D+%5Cfrac%7Bp%28%5Ctext%7Bch%7D%3DXx+%7C+%5Ctext%7Bpa%7D%29+p%28%5Ctext%7Bpa%7D%29%7D+%7B%5Csum_%5Ctext%7Bch%7D+p%28%5Ctext%7Bch%7D%3DXx+%7C+%5Ctext%7Bpa%7D%29+p%28%5Ctext%7Bpa%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{ch}=Xx) = &#92;frac{p(&#92;text{ch}=Xx | &#92;text{pa}) p(&#92;text{pa})} {&#92;sum_&#92;text{ch} p(&#92;text{ch}=Xx | &#92;text{pa}) p(&#92;text{pa})}' title='p(&#92;text{ch}=Xx) = &#92;frac{p(&#92;text{ch}=Xx | &#92;text{pa}) p(&#92;text{pa})} {&#92;sum_&#92;text{ch} p(&#92;text{ch}=Xx | &#92;text{pa}) p(&#92;text{pa})}' class='latex' /></p></blockquote>
<p>We also need to make sure that we only consider brown eyed individuals (XX, Xx, and xX. not xx). let&#8217;s have a look at the priors: </p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=p%28xx%29+%3D+a%5E2&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(xx) = a^2' title='p(xx) = a^2' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=p%28Xx%29+%3D+2a%281-a%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(Xx) = 2a(1-a)' title='p(Xx) = 2a(1-a)' class='latex' /><br />
and a little manipulation yeilds:<br />
<img src='http://s0.wp.com/latex.php?latex=p%28XX%29+%3D+%281-a%29%5E2&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(XX) = (1-a)^2' title='p(XX) = (1-a)^2' class='latex' /></p></blockquote>
<p>So looking at combinations of parents who have brown eyes:</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bpa%7D%3DXX%2CXX%29+%3D+%281-a%29%5E4&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{pa}=XX,XX) = (1-a)^4' title='p(&#92;text{pa}=XX,XX) = (1-a)^4' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bpa%7D%3DXX%2CXx%29+%3D+2%2A2a%281-a%29%2A%281-a%29%5E2&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{pa}=XX,Xx) = 2*2a(1-a)*(1-a)^2' title='p(&#92;text{pa}=XX,Xx) = 2*2a(1-a)*(1-a)^2' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bpa%7D%3DXx%2Cxx%29+%3D+4a%5E2%281-a%29%5E2&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{pa}=Xx,xx) = 4a^2(1-a)^2' title='p(&#92;text{pa}=Xx,xx) = 4a^2(1-a)^2' class='latex' /></p></blockquote>
<p>Each of which can be considered a <em>prior</em>, with correspondings likelihoods (of the child being Xx):</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bch%7D%3DXx+%7C+%5Ctext%7Bpa%7D%3DXX%2CXX%29+%3D+0&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{ch}=Xx | &#92;text{pa}=XX,XX) = 0' title='p(&#92;text{ch}=Xx | &#92;text{pa}=XX,XX) = 0' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bch%7D%3DXx+%7C+%5Ctext%7Bpa%7D%3DXX%2CXx%29+%3D+0.5&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{ch}=Xx | &#92;text{pa}=XX,Xx) = 0.5' title='p(&#92;text{ch}=Xx | &#92;text{pa}=XX,Xx) = 0.5' class='latex' /><br />
<img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bch%7D%3DXx+%7C+%5Ctext%7Bpa%7D%3DXx%2Cxx%29+%3D+0.5&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{ch}=Xx | &#92;text{pa}=Xx,xx) = 0.5' title='p(&#92;text{ch}=Xx | &#92;text{pa}=Xx,xx) = 0.5' class='latex' /></p></blockquote>
<p>Now, the top line of Thomas Bayes&#8217; famous rule looks like:</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=0.5%2A4a%281-a%29%5E3+%2B+0.5%2A4a%5E2%281-a%29%5E2&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='0.5*4a(1-a)^3 + 0.5*4a^2(1-a)^2' title='0.5*4a(1-a)^3 + 0.5*4a^2(1-a)^2' class='latex' /></p></blockquote>
<p>remembering that the bottom line must consist of all brown eyed children of brown eyed parents (not just the heterozygotes), the bottom line looks like:<br />
<blockquote><img src='http://s0.wp.com/latex.php?latex=%281-a%29%5E4+%2B+4a%281-a%29%5E3+%2B+0.75%2A4a%5E2%281-a%29%5E2&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='(1-a)^4 + 4a(1-a)^3 + 0.75*4a^2(1-a)^2' title='(1-a)^4 + 4a(1-a)^3 + 0.75*4a^2(1-a)^2' class='latex' /></p></blockquote>
<p>Phew. Cancelling a few terms does indeed leave</p>
<blockquote><p><img src='http://s0.wp.com/latex.php?latex=p%28%5Ctext%7Bch%7D%3DXx+%7C+%5Ctext%7Bpa%7D%3D%28Xx%2CXX%29%2C%28XX%2CXX%29%2C%28Xx%2CXx%29%29+%3D+%5Cfrac%7B2a%7D%7B1%2B2a%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;text{ch}=Xx | &#92;text{pa}=(Xx,XX),(XX,XX),(Xx,Xx)) = &#92;frac{2a}{1+2a}' title='p(&#92;text{ch}=Xx | &#92;text{pa}=(Xx,XX),(XX,XX),(Xx,Xx)) = &#92;frac{2a}{1+2a}' class='latex' /></p></blockquote>
<p>Not going to school for years switches your brain off: It took me as long to figure out how to do this as it did to blog it&#8230;</p>
<p>There&#8217;s actually a second part to the problem, (first set by Lindley, 1965, apparently), which turns out to have some rather messy terms in. I think I&#8217;ll save blogging that for another day.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=30&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/02/04/back-to-school-for-me/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>ipython %bg coolness</title>
		<link>http://jameshensman.wordpress.com/2009/01/29/ipython-bg-coolness/</link>
		<comments>http://jameshensman.wordpress.com/2009/01/29/ipython-bg-coolness/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 15:17:23 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=26</guid>
		<description><![CDATA[in ipython, I just found the magic function &#8216;%bg&#8217;, or background. If you want to run a long process, but keep the window active, this threads the task and stores the result for you to pick up later. e.g. In [1]: from numpy import matlib as ml In [2]: %bg ml.rand(2,2) Starting job # 0 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=26&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>in ipython, I just found the magic function &#8216;%bg&#8217;, or background. If you want to run a long process, but keep the window active, this threads the task and stores the result for you to pick up later. e.g.</p>
<blockquote><p>
In [1]: from numpy import matlib as ml</code></p>
<p>In [2]: %bg ml.rand(2,2)<br />
Starting job # 0 in a separate thread.</p>
<p>In [3]: %bg ml.zeros(3)<br />
Starting job # 1 in a separate thread.</p>
<p>In [4]: jobs[0].result<br />
Out[4]:<br />
matrix([[ 0.97556473,  0.67794221],<br />
[ 0.9331659 ,  0.78887001]])</p>
<p>In [5]: jobs[1].result<br />
Out[5]: matrix([[ 0.,  0.,  0.]])</p></blockquote>
<p>Mucho coolness, especially if you have a long process (or lots of them) to complete. I wonder if it's actually multi-threaded, as in using both of my CPUs?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/26/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=26&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/01/29/ipython-bg-coolness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>python.copy()</title>
		<link>http://jameshensman.wordpress.com/2009/01/29/pythoncopy/</link>
		<comments>http://jameshensman.wordpress.com/2009/01/29/pythoncopy/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 10:48:32 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[copy]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=21</guid>
		<description><![CDATA[Assignment in python is exactly that. Assignment, not copying. For those of us who have switched to the language of the gods from some inferior mortal language (Matlab), this can lead to some frustration. For example, within my Variational Factor Analysis (VBFA) class, I need to keep a record of something I&#8217;m calling b_phi_hat. One [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=21&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Assignment in python is exactly that. Assignment, not copying. For those of us who have switched to the language of the gods from some inferior mortal language (Matlab), this can lead to some frustration. </p>
<p>For example, within my Variational Factor Analysis (VBFA) class, I need to keep a record of something I&#8217;m calling <code>b_phi_hat</code>. One of the the methods in the class involves the update of this little vector, which depends on its initial (prior) value, <code>b_phi</code>.  Like this:<br />
<pre class="brush: python;">
class VBFA:
import numpy as np
    def __init__(self):
        self.b_phi = np.mat(np.zeros((5,1)))
        #blah blah...
    
    def update_phi(self):
        self.b_phi_hat = self.b_phi
        for i in range(5):
            self.b_phi_hat[i] = self.something()
</pre></p>
<p><code>update_phi()</code> get called 100s of times when the class is used. Spot the problem? It&#8217;s on line 8, where <code>b_phi_hat</code> is <em>assigned</em> to <code>b_phi</code>. When the loop runs on the next two lines, it&#8217;s modifying the original, not just a copy of the original, i.e. after the first iteration line 8 doesn&#8217;t &#8216;refresh&#8217; <code>b_phi_hat</code>, it keeps it at its current value.</p>
<p>What I should have written is:<br />
<pre class="brush: python;">
import numpy as np
class VBFA:
    def __init__(self):
        self.b_phi = np.mat(np.zeros((5,1)))
        #blah blah...
    
    def update_phi(self):
        self.b_phi_hat = self.b_phi.copy()
        for i in range(5):
            self.b_phi_hat[i] = self.something()
</pre></p>
<p>which explicitly makes a copy of the original on line 8, refreshing <code> b_phi_hat</code> with every iteration.  </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=21&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/01/29/pythoncopy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>Some notes on Factor Analysis (FA)</title>
		<link>http://jameshensman.wordpress.com/2009/01/28/some-notes-on-factor-analysis-fa/</link>
		<comments>http://jameshensman.wordpress.com/2009/01/28/some-notes-on-factor-analysis-fa/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 09:21:53 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=13</guid>
		<description><![CDATA[A latent variable model<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=13&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Factor analysis is a statistical technique which can uncover (linear) latent structures in a set of data.  It is very similar to PCA, but with a different noise model.  The model conssits of a set of observed parameters <img src='http://s0.wp.com/latex.php?latex=%5C%7B%5Cbf%7Bx%7D_n+%5C%7D_%7Bn%3D1%7D%5EN&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;{&#92;bf{x}_n &#92;}_{n=1}^N' title='&#92;{&#92;bf{x}_n &#92;}_{n=1}^N' class='latex' /> (this is your collected data), some latent paramenters <img src='http://s0.wp.com/latex.php?latex=%5C%7B%5Cbf%7Bz%7D_n+%5C%7D_%7Bn%3D1%7D%5EN&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;{&#92;bf{z}_n &#92;}_{n=1}^N' title='&#92;{&#92;bf{z}_n &#92;}_{n=1}^N' class='latex' />, with distribution <img src='http://s0.wp.com/latex.php?latex=p%28%5Cbf%7Bz_n%7D%29+%3D+%5Cmathcal%7BN%7D%28%5Cbf%7B0%7D%2C+%5Cbf%7BI%7D%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;bf{z_n}) = &#92;mathcal{N}(&#92;bf{0}, &#92;bf{I})' title='p(&#92;bf{z_n}) = &#92;mathcal{N}(&#92;bf{0}, &#92;bf{I})' class='latex' />.  There exists a noisy linear map <img src='http://s0.wp.com/latex.php?latex=%5Cbf%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf{A}' title='&#92;bf{A}' class='latex' /> from the latent space to the observed variables: <img src='http://s0.wp.com/latex.php?latex=p%28%5Cbf%7Bx%7D_n%29+%3D+%5Cmathcal%7BN%7D%5Cleft%28%5Cbf%7BAz%7D_n%2C+%5Cbf%7B%5CPsi%7D%5Cright%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;bf{x}_n) = &#92;mathcal{N}&#92;left(&#92;bf{Az}_n, &#92;bf{&#92;Psi}&#92;right)' title='p(&#92;bf{x}_n) = &#92;mathcal{N}&#92;left(&#92;bf{Az}_n, &#92;bf{&#92;Psi}&#92;right)' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%5Cbf%7B%5CPsi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf{&#92;Psi}' title='&#92;bf{&#92;Psi}' class='latex' /> is a diagonal matrix.</p>
<p>It&#8217;s also possible to assume a mean vector for the observed variables, but I&#8217;m going to ignore that for a moment for clarity.</p>
<p>Some simple algebra yields <img src='http://s0.wp.com/latex.php?latex=p%28%5Cbf%7Bx_n%7D%29+%3D+%5Cmathcal%7BN%7D%5Cleft%28%5Cbf%7B0%7D%2C+%5Cbf%7BAA%7D%5E%5Ctop+%2B+%5Cbf%7B%5CPsi%7D%5Cright%29&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='p(&#92;bf{x_n}) = &#92;mathcal{N}&#92;left(&#92;bf{0}, &#92;bf{AA}^&#92;top + &#92;bf{&#92;Psi}&#92;right)' title='p(&#92;bf{x_n}) = &#92;mathcal{N}&#92;left(&#92;bf{0}, &#92;bf{AA}^&#92;top + &#92;bf{&#92;Psi}&#92;right)' class='latex' />. It should now be clear that we are modeling the distribution of <img src='http://s0.wp.com/latex.php?latex=%5Cbf%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf{X}' title='&#92;bf{X}' class='latex' /> as a Gaussian distribution with limited degrees of freedom.</p>
<p><strong>Variational Approach</strong></p>
<p>The variational approach involved placing conjugate priors over the model parameters (<img src='http://s0.wp.com/latex.php?latex=%5Cbf%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf{A}' title='&#92;bf{A}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cbf%7B%5CPsi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf{&#92;Psi}' title='&#92;bf{&#92;Psi}' class='latex' />), and finding a factorised approximation to the posterior. </p>
<p>The variational approach to FA yields a distinct advantage: by placing an ARD prior over the columns of <img src='http://s0.wp.com/latex.php?latex=A&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='A' title='A' class='latex' />, unnecessary components get &#8216;switched off&#8217;, and the corresponding column of A goes to zero. This is dead useful: you don&#8217;t need to know the dimension of the latent space beforehand: it just drops out of the model.  </p>
<p><strong> Papers </strong><br />
There is a super Masters thesis on variational FA <a href="http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=3182">here</a> by a chap called Frederik Brink Nielsen. </p>
<p>An immediate extension of factor analysis springs to mind: if the distribution of the data is just a Gaussian, why not have a <em>mixture</em> of them? It seems Ghahramani was there first: <a> GIYF</a></p>
<p>More recently, Zhao and Yu proposed an alteration to the Variational FA model, which apparently achieves a tighter bound by making <img src='http://s0.wp.com/latex.php?latex=%5Cbf%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf{A}' title='&#92;bf{A}' class='latex' /> dependent on <img src='http://s0.wp.com/latex.php?latex=%5Cbf%7B%5CPsi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;bf{&#92;Psi}' title='&#92;bf{&#92;Psi}' class='latex' />. this apparently makes the model less prone to under-fitting (i.e. it drops factors more easily). <a href="http://www.sciencedirect.com/science?_ob=ArticleURL&amp;_udi=B6T08-4V0TD22-1&amp;_user=128590&amp;_coverDate=11%2F25%2F2008&amp;_rdoc=8&amp;_fmt=high&amp;_orig=browse&amp;_srch=doc-info(%23toc%234856%239999%23999999999%2399999%23FLA%23display%23Articles)&amp;_cdi=4856&amp;_sort=d&amp;_docanchor=&amp;_ct=13&amp;_acct=C000010619&amp;_version=1&amp;_urlVersion=0&amp;_userid=128590&amp;md5=7910d06365fe2b62fb4cfdeca9b9c858">Neural Networks Journal</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=13&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/01/28/some-notes-on-factor-analysis-fa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>Working with log likelihoods</title>
		<link>http://jameshensman.wordpress.com/2009/01/26/working-with-log-likelihoods/</link>
		<comments>http://jameshensman.wordpress.com/2009/01/26/working-with-log-likelihoods/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 13:41:58 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[probability]]></category>

		<guid isPermaLink="false">http://jameshensman.wordpress.com/?p=5</guid>
		<description><![CDATA[Sometimes, when you have a series of number representing the log-probability of something, you need to add up the probabilities. Perhaps to normalise them, or perhaps to weight them&#8230; whatever.  You end up writing (or Mike ends up writing): Which is going to suck when them members of x are small. Small enough that the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=5&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, when you have a series of number representing the log-probability of something, you need to add up the probabilities. Perhaps to normalise them, or perhaps to weight them&#8230; whatever.  You end up writing (or <a href="http://mikedewar.wordpress.com">Mike</a> ends up writing):</p>
<p><pre class="brush: python;">logsumexp = lambda x: np.log(sum([np.exp(xi) for xi in x]))</pre></p>
<p>Which is going to suck when them members of x are small. Small enough that the precision of the 64 bit float you holding them runs out, and they exponentiate to zero (somewhere near -700).  Your code is going to barf when it get to the <code>np.log</code> part, and finds it can&#8217;t log zero.</p>
<p>One solution is to add a constant to each member of x, so that you don&#8217;t work so close to the limits of the precision, and remove the constant later:</p>
<p><pre class="brush: python;">
def logsumexp(x):
    x += 700
    x = np.sum(np.exp(x))
    return np.log(x) - 700
</pre></p>
<p>Okay, so my choice of 700 is a little arbitrary, but that (-700) is where the precision starts to run out, and it works for me. Of course, if your numbers are way smaller than that, you may have a problem.</p>
<p>Edit: grammar. And I&#8217;m getting used to this whole weblog shenanigan. Oh, and <code>&lt;code&gt;blah&lt;/code&gt; looks rubbish: I'm going to stop doing that.<br />
</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=5&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/01/26/working-with-log-likelihoods/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
		<item>
		<title>A place for putting things</title>
		<link>http://jameshensman.wordpress.com/2009/01/23/hello-world/</link>
		<comments>http://jameshensman.wordpress.com/2009/01/23/hello-world/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 17:19:47 +0000</pubDate>
		<dc:creator>jameshensman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[This is a place for putting things, so that they can be found.  Some of these things include thoughts on what I&#8217;m doing at the moment, which consists largely of python probabilistic models of data learning teaching I&#8217;m hoping that by storing my thoughts, they&#8217;ll begin to make more sense. I may even start writing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=1&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is a place for putting things, so that they can be found.  Some of these things include thoughts on what I&#8217;m doing at the moment, which consists largely of</p>
<ul>
<li><a href="http://www.scipy.org">python</a></li>
<li>probabilistic models of data</li>
<li>learning</li>
<li>teaching</li>
</ul>
<p>I&#8217;m hoping that by storing my thoughts, they&#8217;ll begin to make more sense. I may even start writing thoughts on my thoughts, but mostly I&#8217;m just going to put them here for safekeeping.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshensman.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshensman.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshensman.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshensman.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshensman.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshensman.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshensman.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshensman.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshensman.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshensman.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshensman.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshensman.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshensman.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshensman.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshensman.wordpress.com&amp;blog=6292237&amp;post=1&amp;subd=jameshensman&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshensman.wordpress.com/2009/01/23/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9e3d6890dc433ff2337a1a15208712d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jameshensman</media:title>
		</media:content>
	</item>
	</channel>
</rss>
