<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>gonium.net &#187; ruby</title>
	<atom:link href="http://gonium.net/md/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://gonium.net/md</link>
	<description>so much time, so little to do.</description>
	<lastBuildDate>Fri, 30 Jul 2010 13:47:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby for Chumby: HowTo</title>
		<link>http://gonium.net/md/2010/06/02/ruby-for-chumby-howto/</link>
		<comments>http://gonium.net/md/2010/06/02/ruby-for-chumby-howto/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 15:06:09 +0000</pubDate>
		<dc:creator>md</dc:creator>
				<category><![CDATA[digital]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[smartgrid]]></category>
		<category><![CDATA[chumby]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gonium.net/md/?p=261</guid>
		<description><![CDATA[
It took me a while to figure this out, so here are my notes:


Install Scratchbox as described here. This greatly simplifies cross-compiling things. It also provides you a way to run the compiled ruby interpreter on a bigger host machine using QEMU. This is essential because I rely on rubygems, and the Chumby is too [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://gonium.net/md/2010/06/02/ruby-for-chumby-howto/chumby/" rel="attachment wp-att-265"><img src="http://gonium.net/md/wp-content/uploads/2010/06/chumby.jpg" alt="" title="chumby" width="500" height="387" class="alignnone size-full wp-image-265" /></a></p>
<p>It took me a while to figure this out, so here are my notes:<br />
<span id="more-261"></span></p>
<ol>
<li>Install Scratchbox as described <a href="http://wiki.chumby.com/mediawiki/index.php/Scratchbox">here</a>. This greatly simplifies cross-compiling things. It also provides you a way to run the compiled ruby interpreter on a bigger host machine using QEMU. This is essential because I rely on rubygems, and the Chumby is too small to execute the gem install commands. Besides, at least for the sqlite3-ruby and mongrel gems, I need to compile native code.</li>
<li>Install dependencies. I found the following libraries necessary:
<ul>
<li>OpenSSL 0.9.8n &#8211; avoid the 1.0.0 version, it causes problems with the Ruby interpreter.</li>
<li>Zlib 1.5.3 &#8211; same as installed on the Chumby.</li>
<li>Readline 6.1</li>
<li>A recent version of sqlite, since I need it for my gems later on.</li>
</ul>
<p>Compile the libraries and install them into /mnt/storage (use &#8211;prefix where necessary).</li>
<li>Adjust the environment to use the new libraries:<br />
<code>BASEDIR=/mnt/storage/usr<br />
export PATH=$PATH:$BASEDIR/bin<br />
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BASEDIR/lib</code></li>
<li>Install Ruby and rubygems. I was not able to compile Ruby 1.8.7, but Ruby 1.8.6-p399 works nice. To configure everything, I use the following commands:<br />
<code>export ac_cv_func_getpgrp_void=yes<br />
export ac_cv_func_setpgrp_void=yes<br />
./configure --target=arm-linux --host=arm-linux --disable-pthread --enable-wide-getaddrinfo --enable-zlib --with-zlib-dir=/mnt/storage/usr --enable-readline --with-readline-dir=/mnt/storage/usr --prefix=/mnt/storage/usr --with-openssl-dir=/mnt/storage/usr &#038;&#038; make &#038;&#038; make install<br />
</code></li>
<li>Install rubygems:<br />
<code><br />
/mnt/storage/usr/bin/ruby setup.rb --prefix=/mnt/storage/usr<br />
mkdir -p /mnt/storage/usr/lib/ruby/gems/1.8<br />
</code></li>
<li>Afterwards, the necessary gems can be installed using the gem command. It might be necessary to provide additonal path information to the gems that compile native extensions, for example<br />
<code>gem install sqlite3-ruby -- --with-sqlite3-dir=/mnt/storage/usr</code>
</li>
</ol>
<p>The result is an image with Ruby installed, and all necessary gems for my project. The <a href="http://gonium.net/media/chumby-ruby-1.8.6-p399.tar.gz">tarball can be downloaded here</a>. To install, scp it to the Chumby and log in:<br />
<code><br />
scp *tarball* root@*chumby-ip*:/mnt/storage<br />
ssh -l root *chumby-ip*<br />
cd /mnt/storage<br />
tar xvzf *tarball*<br />
</code></p>
<p>Now you have a directory /mnt/storage/usr with all tools permanently installed on your Chumby. In order to use it, please adjust your PATH and LD_LIBRARY_PATH appropriately:<br />
<code><br />
export BASEDIR=/mnt/storage/usr<br />
export PATH=$PATH:$BASEDIR/bin<br />
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BASEDIR/lib<br />
</code></p>
<p>/me mumbles more automation needed. But not today.</p>
]]></content:encoded>
			<wfw:commentRss>http://gonium.net/md/2010/06/02/ruby-for-chumby-howto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9 Performance</title>
		<link>http://gonium.net/md/2009/04/17/ruby-19-performance/</link>
		<comments>http://gonium.net/md/2009/04/17/ruby-19-performance/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 09:51:15 +0000</pubDate>
		<dc:creator>md</dc:creator>
				<category><![CDATA[digital]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gonium.net/md/?p=146</guid>
		<description><![CDATA[I&#8217;m currently using Ruby to optimize schedules based on a simulated annealing approach. For my current intermediate version I rely on marshalling internal datastructures really frequently. Out of curiosity I compared the runtimes of Ruby 1.8 and 1.9:

Ruby 1.8.6: 21 minutes, 55 seconds
Ruby 1.9.0: 22 minutes, 13 seconds

I certainly did not expect any wonders, but [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently using Ruby to optimize schedules based on a simulated annealing approach. For my current intermediate version I rely on marshalling internal datastructures really frequently. Out of curiosity I compared the runtimes of Ruby 1.8 and 1.9:</p>
<ul>
<li>Ruby 1.8.6: 21 minutes, 55 seconds</li>
<li>Ruby 1.9.0: 22 minutes, 13 seconds</li>
</ul>
<p>I certainly did not expect any wonders, but in theis case, the runtimes are about the same. So I&#8217;m investigating further, using the ruby 1.8 profiler on a smaller test problem:</p>
<p><code><br />
    %   cumulative   self              self     total<br />
 time   seconds   seconds    calls  ms/call  ms/call  name<br />
 33.20    32.96     32.96      661    49.86    77.25  Marshal.load<br />
 17.80    50.63     17.67     6365     2.78    14.96  Array#each<br />
 17.52    68.03     17.40   601659     0.03     0.03  IO#getc<br />
  9.19    77.15      9.12      660    13.82    13.82  Marshal.dump<br />
  6.01    83.12      5.97   187158     0.03     0.03  Float#+<br />
</code></p>
<p>My code spends 42.39 percent of its runtime doing marshalling operations. For Ruby 1.9, the code spends 48.98 percent of its runtime doing marshalling. This is somewhat disappointing. In a second round I changed my code to not relying on marshalling &#8211; basically, I am using clone on essential internal datastructures. All other information can be reconstructed afterwards. This means that I need to recompute certain values, but evidently this is a much faster approach:</p>
<ul>
<li>Ruby 1.8.6: 1 minute, 34 seconds</li>
<li>Ruby 1.9.0: 1 minute, 17 seconds</li>
</ul>
<p>Note to self: always use a profiler before refactoring code for performance reasons. </p>
]]></content:encoded>
			<wfw:commentRss>http://gonium.net/md/2009/04/17/ruby-19-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating random numbers from any distribution</title>
		<link>http://gonium.net/md/2008/07/29/generating-random-numbers-from-any-distribution/</link>
		<comments>http://gonium.net/md/2008/07/29/generating-random-numbers-from-any-distribution/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 09:22:52 +0000</pubDate>
		<dc:creator>md</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[nerdism]]></category>
		<category><![CDATA[distribution]]></category>
		<category><![CDATA[gaussian]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gonium.net/md/?p=85</guid>
		<description><![CDATA[
At work I needed to generate random numbers following a combination of two gaussian distributions &#8211; which gave me some headache until someone pointed me to using a Monte Carlo approach. Here&#8217;s how.

Typically, any programming language provides some way to generate uniformly distributed random numbers, typically in the interval [0,1]. A histogram of the values [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://gonium.net/md/wp-content/uploads/2008/07/381184220_4cfce0c86a.jpg" alt="" title="random dices"  class="alignnone size-medium wp-image-86" /></p>
<p>At work I needed to generate random numbers following a combination of two gaussian distributions &#8211; which gave me some headache until someone pointed me to using a Monte Carlo approach. Here&#8217;s how.</p>
<p><span id="more-85"></span></p>
<p>Typically, any programming language provides some way to generate uniformly distributed random numbers, typically in the interval [0,1]. A histogram of the values looks like this:</p>
<p><img src="http://gonium.net/md/wp-content/uploads/2008/07/uniform.png" alt="" title="uniform" class="alignnone size-medium wp-image-87" /></p>
<p>However, I needed random numbers following the joint distribution of two gaussian distributions. The histogram should look like this:</p>
<p><img src="http://gonium.net/md/wp-content/uploads/2008/07/doublegaussian.png" alt="" title="doublegaussian" class="alignnone size-medium wp-image-88" /></p>
<p>It turns out that it is rather simple to generate random numbers from an given probability density function (PDF) f(x). The only restriction is that there exists another density function g(x) such that c*g(x) majorizes the density function f(x):</p>
<p>c*g(x) &gt;= f(x) for all x.</p>
<p>The steps are rather simple &#8211; to generate a random variate x with density f(x):</p>
<ol>
<li>Generate x with PDF g(x)</li>
<li>Generate y uniform on [0, cg(x)]</li>
<li>If y &lt;= f(x), then output x and return &#8211; otherwise repeat from step 1.</li>
</ol>
<p>Please note that this approach is applicable to many PDFs, but it can be rather slow, depending on the &#8220;hit rate&#8221;. The algorithm is simple: You generate two uniform random numbers, x and y. Then, you check if y &lt; f(x) &#8211; if true, add x to your collection of random numbers. Otherwise, discard it. Repeat the steps until you have enough random numbers. This method is also called Rejection Sampling and was first described by John von Neumann. Other techniques include Inverse Transformation, Composition and Convolution.</p>
<p>I highly recommend reading Chapters 28 and 29 of Rai Jain&#8217;s &#8220;The Art of Computer Systems Performance Analysis&#8221; which outline techniques for generating random numbers of several distributions.</p>
<p>My ruby implementation consists of a MC wrapper and blocks compute the random variate. Currently, I can create gaussian distributions, exponential distributions and a combination of two gaussian distributions. The toolbox also contains a method to scale the range of the random numbers using a linear transformation approach.</p>
<p class="vci_info">[viewcode]src=statistics.rb [/viewcode]</p>
<pre class="vci_code"></pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://gonium.net/media//statistics.rb">statistics.rb</a></p>
<p>The title picture was CCed by <a href="http://flickr.com/photos/mamabarns/381184220/">Saffana</a>, thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://gonium.net/md/2008/07/29/generating-random-numbers-from-any-distribution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
