<?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>Far away from Ankaa &#187; Programming</title>
	<atom:link href="http://prxq.wordpress.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://prxq.wordpress.com</link>
	<description></description>
	<lastBuildDate>Sun, 06 May 2007 17:43:30 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='prxq.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/d1cc1b9199b4b70dc9451ff85963f519?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Far away from Ankaa &#187; Programming</title>
		<link>http://prxq.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://prxq.wordpress.com/osd.xml" title="Far away from Ankaa" />
		<item>
		<title>ASDF system definition files for Fortran 77 codes</title>
		<link>http://prxq.wordpress.com/2007/05/06/asdf-system-definition-files-for-fortran-77-codes/</link>
		<comments>http://prxq.wordpress.com/2007/05/06/asdf-system-definition-files-for-fortran-77-codes/#comments</comments>
		<pubDate>Sun, 06 May 2007 17:42:19 +0000</pubDate>
		<dc:creator>prxq</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://prxq.wordpress.com/2007/05/06/asdf-system-definition-files-for-fortran-77-codes/</guid>
		<description><![CDATA[One of the Common Lisp packages that I find very useful is f2cl, originally written by Kevin A. Broughan and Diane M. K. Willcock [1], with recent contributions by Richard J. Fateman and Raymond Toy [2]. It can convert a lot of the Fortran 77 codes out there fairly reliably into quite fast Common Lisp [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=7&subd=prxq&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>One of the Common Lisp packages that I find very useful is f2cl, originally written by Kevin A. Broughan and Diane M. K. Willcock <a href="#f1">[1]</a>, with recent contributions by Richard J. Fateman and Raymond Toy <a href="#f2">[2]</a>. It can convert a lot of the Fortran 77 codes out there fairly reliably into quite fast Common Lisp code. The lack of a fancy home page or even a separate project (it is currently part of <a href="http://clocc.sourceforge.net/">CLOCC</a>) makes it somewhat hard to find, but should not be taken as a sign of abandon. A more prominent project that uses f2cl is the open source CAS system <a href="http://maxima.sourceforge.net/">Maxima</a>.</p>
<p>The advantages of using f2cl over a foreign function interface are, among other things, that the generated Common Lisp code has far better error handling, and is much more convenient to use (for a Common Lisp programmer, at least).</p>
<p>I think it would be nice to have ASDF system definitions for the Fortran 77 packages bundled with f2cl. This would complement the mk:defsystem definitions already available. A small difficulty is that the f2cl compiler has a lot of options, and it is desirable to want to set them globally, eventually overriding them on a file by file basis. At least, this is what is done in some of the .system files. I managed to convince ASDF to do this, but I am not sure if the way i did it is the best solution. From the point of view of OOP this can be (and will be) improved, of course. I&#8217;m referring more to the need of things like accessing ASDF symbols that are not exported. In any case, having global options that can be overriden on a component basis looks like a feature one would like to have every now and then.</p>
<p>A typical f2cl .system file looks like this (for example):</p>
<pre>
(mk:define-language :f2cl
    :compiler #'f2cl:f2cl-compile
    :source-extension "f")

(mk:defsystem minpack
    :source-pathname "clocc:src;f2cl;packages;minpack"
    :components
    ((:file "minpack"
		      :language :lisp)
     (:module "minpack"
	      :source-pathname ""
	      :source-extension "f"
	      :package :minpack
	      :language :f2cl
	      :compiler-options (:include-comments t
				 :keep-lisp-file t
				 :relaxed-array-decls nil
				 :array-type :array
				 :array-slicing t
				 :package :minpack)
;; etc</pre>
<p>Note that :file components can also have a :compiler-options, although I don&#8217;t know how the overriding behavior really is.</p>
<p>To convince ASDF to behave in a similar way, I subclassed the asdf:module class, adding an f2cl-options slot.</p>
<pre>
(defclass f2cl-module (module)
  ((f2cl-options :initform nil
		 :initarg :f2cl-options
		 :accessor f2cl-options)))</pre>
<p>To have an f2cl source file type with corresponding compilation, one does</p>
<pre>
(defclass f77-source-file (cl-source-file)
  ((f2cl-options :initform nil
		 :initarg :f2cl-options)))

(defmethod source-file-type ((c f77-source-file) (s module)) "f")

(defmethod perform ((o compile-op) (f f77-source-file))
  (apply #'f2cl:f2cl-compile
	 (cons (make-pathname :name (component-name f)
			      :type "f"
			      :defaults (component-pathname f))
	       (compiler-options f))))</pre>
<p>But here is the catch. Compiler options have to be computed from the module *and* the file. I did it like this</p>
<pre>
(defmethod f2cl-options ((c f77-source-file))
  (let* ((c-c-o (slot-value c 'f2cl-options))
	 (p-c-o (when (typep (parent c) 'f2cl-module)
		  (f2cl-options (parent c)))))

    ;; override global options.
    (append c-c-o p-c-o)))</pre>
<p>where</p>
<pre>
(defmethod parent ((c f77-source-file))
  (slot-value c 'asdf::parent)) ;; Oops</pre>
<p>Anyway, it works. Now I am wondering if I overlooked the apropriate feature in ASDF, or another way of doing this. Accessing &#8216;asdf::parent is good enough for <em>me</em>, but before attempting to contribute .asd files to f2cl, I would prefer to have a more solid solution.</p>
<p><span id="more-7"></span></p>
<p>P.S: The rest of the .asd file looks like this</p>
<pre>
(defsystem minpack
  :components
  ((:f2cl-module :minpack
	    :f2cl-options (:include-comments t
			       :keep-lisp-file t
			       :relaxed-array-decls nil
			       :array-type :array
			       :array-slicing t
			       :package :minpack)
	    :components
	    ((:file "minpack")
	     (:f77-source-file "dpmpar" :depends-on ("minpack"))
	     (:f77-source-file "enorm" :depends-on ("minpack"))
	     (:f77-source-file "fdjac1" :depends-on ("dpmpar"))
	     (:f77-source-file "fdjac2" :depends-on ("dpmpar"))
	     (:f77-source-file "qrsolv" :depends-on ("minpack"))
	     (:f77-source-file "lmpar" :depends-on ("dpmpar" "enorm" "qrsolv"))
	     (:f77-source-file "qrfac" :depends-on ("dpmpar" "enorm"))
	     (:f77-source-file "lmdif" :depends-on ("dpmpar" "enorm" "fdjac2" "lmpar" "qrfac"))
	     (:f77-source-file "lmdif1" :depends-on ("lmdif"))
	     (:f77-source-file "lmder" :depends-on ("dpmpar" "enorm" "lmpar" "qrfac"))
	     (:f77-source-file "lmder1" :depends-on ("lmder"))
	     (:f77-source-file "dogleg" :depends-on ("dpmpar" "enorm"))
	     (:f77-source-file "qform" :depends-on ("minpack"))
	     (:f77-source-file "r1mpyq" :depends-on ("minpack"))
	     (:f77-source-file "r1updt" :depends-on ("dpmpar"))
	     (:f77-source-file "hybrd" :depends-on ("dogleg" "dpmpar" "enorm" "fdjac1"
						  "qform" "qrfac" "r1mpyq" "r1updt"))
	     (:f77-source-file "hybrd1" :depends-on ("hybrd"))
	     (:f77-source-file "hybrj" :depends-on ("dogleg" "dpmpar" "enorm" "qform" "qrfac"
						  "r1mpyq" "r1updt"))
	     (:f77-source-file "hybrj1" :depends-on ("hybrj"))
	     ))))</pre>
<p><strong>Footnotes</strong></p>
<p><a title="f1" name="f1"></a>[1] K. A. Broughan and D. M. K. Willcock, <a href="http://portal.acm.org/citation.cfm?id=241701&amp;coll=GUIDE&amp;dl=GUIDE&amp;CFID=15151515&amp;CFTOKEN=6184618"><em>Fortran to Lisp translation using f2cl</em></a>, Software Practice &amp; Experience, 26(10) pp. 1127 &#8211; 1139 (October 1996)</p>
<p><a title="f2" name="f2"></a>[2] R. J. Fateman and R. Toy, <a href="http://portal.acm.org/citation.cfm?id=860884&amp;dl=GUIDE&amp;coll=GUIDE&amp;CFID=15151515&amp;CFTOKEN=6184618"><em>Converting call-by-reference to call-by-value: Fortran and Lisp coexisting</em></a>, Proceedings of the 2003 international symposium on Symbolic and algebraic computation, pp. 95 &#8211; 102, 2003.</p>
<p><strong>Note:</strong> If you want to comment, please take into account that the wordpress comment box is not friendly to code.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prxq.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prxq.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prxq.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prxq.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prxq.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prxq.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prxq.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prxq.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prxq.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prxq.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prxq.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prxq.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=7&subd=prxq&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://prxq.wordpress.com/2007/05/06/asdf-system-definition-files-for-fortran-77-codes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/132d66a99a4ab1529bb7d0254082fd21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">prxq</media:title>
		</media:content>
	</item>
		<item>
		<title>Grid Restrained Nelder-Mead</title>
		<link>http://prxq.wordpress.com/2006/11/05/grid-restrained-nelder-mead/</link>
		<comments>http://prxq.wordpress.com/2006/11/05/grid-restrained-nelder-mead/#comments</comments>
		<pubDate>Sun, 05 Nov 2006 17:39:56 +0000</pubDate>
		<dc:creator>prxq</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://prxq.wordpress.com/2006/11/05/grid-restrained-nelder-mead/</guid>
		<description><![CDATA[The Nelder-Mead algorithm is a rather popular algorithm for (low dimensional) nonlinear programming. The original version (from 1965, see [1]) has known and varied failure modes, but never really had to fear for its popularity. It doesn&#8217;t need derivatives, which can be quite convenient, and has a reputation to work well even with noisy and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=6&subd=prxq&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The Nelder-Mead algorithm is a rather popular algorithm for (low dimensional) nonlinear programming. The original version (from 1965, see [1]) has known and varied failure modes, but never really had to fear for its popularity. It doesn&#8217;t need derivatives, which can be quite convenient, and has a reputation to work well even with noisy and rough functions.</p>
<p>Recently, a few provably convergent variants have been devised (see [2], [3], and [4]). The one by A. Bürmen et al [4], the &#8220;Grid Restrained Nelder-Mead Algorithm&#8221; (GRNMA) is notable because it does not impose a &#8220;sufficient descent&#8221; condition at each iteration, which makes it closer to the original algorithm.</p>
<p>A reliable, simple-to-use derivative free minimization routine is a handy little tool to have around. It turns out that implementing the GRNMA is also a pleasant programming project.</p>
<p><span id="more-6"></span></p>
<p><a href="http://common-lisp.net/~mmommer/cl-grnm.tar.gz">Here</a> you can download a rather faithful implementation of the GRNMA (in Common Lisp). I also included the classical Nelder Mead algorithm for completeness.</p>
<p>The performance of these improved methods (judging from the numerical experiments in [4]) seems to be about equal in terms of number of function evaluations. But as a side effect of the additional reliability, they tend to be more efficient than the original algorithm even when it does not fail.</p>
<p>The code is under the MIT licence, with no warranty of any sort, etc.</p>
<p>References:</p>
<p>[1] J.A. Nelder and R. Mead, &#8220;A simplex method for function<br />
minimization,&#8221; The Computer Journal, vol. 7, pp. 308-313, 1965.</p>
<p>[2] P. Tseng, &#8220;Fortified-descent simplical search method: A general<br />
approach,&#8221; SIAM Journal on Optimization, vol. 10, pp. 269-288,<br />
1999.</p>
<p>[3] C.J. Price, I.D. Coope, and D. Byatt, &#8220;A convergent variant of the<br />
Nelder-Mead algorithm,&#8221; Journal of Optimization Theory and<br />
Applications, vol. 113, pp. 5-19, 2002.</p>
<p>[4] A. Bürmen, J. Puhan and T. Tuma, &#8220;Grid Restrained Nelder-Mead<br />
Algorithm&#8221;, Computational Optimization and Applications, vol.<br />
34, no. 3, pp. 359 &#8211; 375, 2006.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prxq.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prxq.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prxq.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prxq.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prxq.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prxq.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prxq.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prxq.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prxq.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prxq.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prxq.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prxq.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=6&subd=prxq&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://prxq.wordpress.com/2006/11/05/grid-restrained-nelder-mead/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/132d66a99a4ab1529bb7d0254082fd21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">prxq</media:title>
		</media:content>
	</item>
		<item>
		<title>More on the alias method</title>
		<link>http://prxq.wordpress.com/2006/04/23/more-on-the-alias-method/</link>
		<comments>http://prxq.wordpress.com/2006/04/23/more-on-the-alias-method/#comments</comments>
		<pubDate>Sun, 23 Apr 2006 20:21:20 +0000</pubDate>
		<dc:creator>prxq</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">https://prxq.wordpress.com/2006/04/23/more-on-the-alias-method/</guid>
		<description><![CDATA[It turns out that, as pointed out by Jens Axel Søgaard, the method I wrote about a few days ago is the alias method by Walker, as found in D. Knuth&#8217;s TAOCP, but with a modification by R. A. Kronmal and A. V. Peterson. Their contribution, it seems, was the algorithm to rearrange the intervals [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=5&subd=prxq&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>It turns out that, as pointed out by Jens Axel Søgaard, the method I <a href="http://prxq.wordpress.com/2006/04/17/the-alias-method/">wrote about</a> a few days ago is the alias method by Walker, as found in D. Knuth&#8217;s <a href="http://www-cs-faculty.stanford.edu/~uno/taocp.html">TAOCP</a>, but with a modification by R. A. Kronmal and A. V. Peterson. Their contribution, it seems, was the algorithm to rearrange the intervals in O(N) operations.</p>
<p>In any case, there is a very good and complete textbook, nowadays available freely on the net, where all these things are explained, disected, and thoroughly overengineered in every good sense of the word. The book is <a href="http://cg.scs.carleton.ca/~luc/rnbookindex.html">&#8220;Non-Uniform Random Variate Generation&#8221;</a>, by Luc Devroye (thanks to Brad Lucier for mailing me this great link).</p>
<p>Below you&#8217;ll find some additional notes about the method, prompted by reading part of the book, and a link to an updated version of the CL code. (For simplicity, I will write as if in the context of my <a href="http://prxq.wordpress.com/2006/04/17/the-alias-method/">first post</a>).</p>
<p><span id="more-5"></span></p>
<p>The first thing I found interesting is that, while in theory one random number is enough, in practice this doesn&#8217;t always hold, because one is using the higher bits to choose the slot, and the lower bits to compute the alternative. The more you need for the former, the less you have for the latter. This means that the simulated variable will produce the values with slightly different probabilities than those specified. However, I think it is unlikely for this deviation to be really important unless the number of possible values of the variable becomes large, but it is something to think about.</p>
<p>The other interesting point is that the method can be made even faster. The trick to speed up the generation of values of the random variable is to add more possible values but with probability zero. What happens then is that one has a variety of slots for whom only the alternaive needs to be considered, and thus one can avoid the second call to the random number generator. The more of these phantom values, the less calls are needed. And if these &#8216;dead&#8217; slots are rearanged in the correct order, which btw is very easy, it is also possible to reduce the number of array accesses.</p>
<p>In old days that seems to have been very important. I doubt that an array access will make that much difference nowadays, but being so easy to implement, it doesn&#8217;t cost you much either. Economizing calls to the random number generator, in particular if it is an expensive one, seems to me a bigger advantage.</p>
<p>I have updated my implementation, and included the option <tt>:phantom-values</tt> to specify the number of extra values with zero probability to consider. This version goes the safe route, using a second call to RANDOM should it be necessary, but also avoids array accesses when possible. You can get it <a href="http://common-lisp.net/~mmommer/alias_method_v2.lisp">here</a>.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prxq.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prxq.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prxq.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prxq.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prxq.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prxq.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prxq.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prxq.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prxq.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prxq.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prxq.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prxq.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=5&subd=prxq&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://prxq.wordpress.com/2006/04/23/more-on-the-alias-method/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/132d66a99a4ab1529bb7d0254082fd21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">prxq</media:title>
		</media:content>
	</item>
		<item>
		<title>Slime 2.0</title>
		<link>http://prxq.wordpress.com/2006/04/20/slime-20/</link>
		<comments>http://prxq.wordpress.com/2006/04/20/slime-20/#comments</comments>
		<pubDate>Thu, 20 Apr 2006 20:11:51 +0000</pubDate>
		<dc:creator>prxq</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">https://prxq.wordpress.com/2006/04/20/slime-20/</guid>
		<description><![CDATA[A new version of slime has been released. See here, or download it directly as a gzipped tar archive or as a zip file.
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=4&subd=prxq&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A new version of slime has been released. See <a href="http://common-lisp.net/project/slime/">here</a>, or download it directly as a <a href="http://common-lisp.net/project/slime/slime-2.0.tgz">gzipped tar archive</a> or as a <a href="http://common-lisp.net/project/slime/slime-2.0.zip">zip file</a>.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prxq.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prxq.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prxq.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prxq.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prxq.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prxq.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prxq.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prxq.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prxq.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prxq.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prxq.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prxq.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=4&subd=prxq&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://prxq.wordpress.com/2006/04/20/slime-20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/132d66a99a4ab1529bb7d0254082fd21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">prxq</media:title>
		</media:content>
	</item>
		<item>
		<title>The Alias Method</title>
		<link>http://prxq.wordpress.com/2006/04/17/the-alias-method/</link>
		<comments>http://prxq.wordpress.com/2006/04/17/the-alias-method/#comments</comments>
		<pubDate>Mon, 17 Apr 2006 12:59:38 +0000</pubDate>
		<dc:creator>prxq</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">https://prxq.wordpress.com/2006/04/17/the-alias-method/</guid>
		<description><![CDATA[Some days ago, there was some discussion in #lisp about how to program a random variable with nontrivial probability distribution. That is, given values x1,&#8230;,xn, and probabilities p1,&#8230;,pn, how to write a function that randomly returns one of those values with the corresponding probability each time it is called.
It turns out this problem has a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=3&subd=prxq&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Some days ago, there was some discussion in <a href="http://www.cliki.net/IRC">#lisp</a> about how to program a random variable with nontrivial probability distribution. That is, given values x<sub>1</sub>,&#8230;,x<sub>n</sub>, and probabilities p<sub>1</sub>,&#8230;,p<sub>n</sub>, how to write a function that randomly returns one of those values with the corresponding probability each time it is called.</p>
<p>It turns out this problem has a beautiful, simple, and efficient solution called the alias method (R. A. Kronmal and A. V. Peterson. <i>On the alias method for generating random variables from a discrete distribution</i>. The American Statistician, 33:214&#8211;218, 1979.), that requires only one call to the random number generator. While I haven&#8217;t read any complete explanation of it (only the one in  R. A. Kronmal, A. V. Peterson, <i>The alias and alias-rejection-mixture methods for generating random variables from probability distributions</i>. Proc. 1979 Winter Simulation Conference (ed. H.J. Highland et al.) 269&#8211;280, 1979, which isn&#8217;t complete), I think it is highly unlikely that it is different from the method I describe below.</p>
<p>An Common Lisp implementation can be found <a href="http://common-lisp.net/~mmommer/alias_method.lisp">here</a>.</p>
<p><span id="more-3"></span></p>
<p><b>The alias method</b></p>
<p>If you choose one real number between 0 and 1 at random with a uniform distribution, the probability that it lies between a and b, with<br />
0≤a&lt;b≤1, is b-a. So the obvious solution to our problem is to put intervals I<sub>i</sub> in [0,1) of length p<sub>i</sub> next to each other. Each time we want a value of our random variable, we choose a value v in [0,1) at random with a uniform distribution, and see in which interval it falls. If it falls in I<sub>k</sub>, the value is x<sub>k</sub>. The problem is that finding the interval is costly.</p>
<p>The fundamental idea behind the alias method is that this approach still works if each I<sub>i</sub> was subdivided in nonoverlaping, not necessarily contiguous intervals J<sub>i</sub><sup>1</sup>, J<sub>i</sub><sup>2</sup>,&#8230;,J<sub>i</sub><sup>k<sub>i</sub></sup> in [0,1) such that the sum of their lengths is p<sub>i</sub>. This can be used to rearange the intervals in such a way that it is easy to know in what interval a given point lies, and to which value the interval corresponds. To do this, one divides [0,1) in n intervals L<sub>k</sub> of equal length, and then packs each with at most two intervals belonging to different x<sub>k</sub>. Thus, finding out in what interval a point falls amounts to finding out, first, in which L<sub>k</sub> it falls, which can be obtained by division and truncation, and then finding out which one of the two possibilities that are left holds.</p>
<p>To get this redistribution right, pack L<sub>1</sub> with an interval whose length is less or equal than 1/n, and, if necessary, take away what is needed to fill up L<sub>1</sub> from one whose length is greater than 1/n. Now, filling up the rest of the L<sub>i</sub> is very close to the initial problem, but with one value (and one interval) less.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prxq.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prxq.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prxq.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prxq.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prxq.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prxq.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prxq.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prxq.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prxq.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prxq.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prxq.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prxq.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prxq.wordpress.com&blog=186581&post=3&subd=prxq&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://prxq.wordpress.com/2006/04/17/the-alias-method/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/132d66a99a4ab1529bb7d0254082fd21?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">prxq</media:title>
		</media:content>
	</item>
	</channel>
</rss>