gonium.net » ruby http://gonium.net/md so much time, so little to do. Sat, 11 Sep 2010 16:42:09 +0000 en hourly 1 http://wordpress.org/?v=3.0.1 Ruby for Chumby: HowTo http://gonium.net/md/2010/06/02/ruby-for-chumby-howto/ http://gonium.net/md/2010/06/02/ruby-for-chumby-howto/#comments Wed, 02 Jun 2010 15:06:09 +0000 md http://gonium.net/md/?p=261

It took me a while to figure this out, so here are my notes:

  1. 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 small to execute the gem install commands. Besides, at least for the sqlite3-ruby and mongrel gems, I need to compile native code.
  2. Install dependencies. I found the following libraries necessary:
    • OpenSSL 0.9.8n – avoid the 1.0.0 version, it causes problems with the Ruby interpreter.
    • Zlib 1.5.3 – same as installed on the Chumby.
    • Readline 6.1
    • A recent version of sqlite, since I need it for my gems later on.

    Compile the libraries and install them into /mnt/storage (use –prefix where necessary).

  3. Adjust the environment to use the new libraries:
    BASEDIR=/mnt/storage/usr
    export PATH=$PATH:$BASEDIR/bin
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BASEDIR/lib
  4. 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:
    export ac_cv_func_getpgrp_void=yes
    export ac_cv_func_setpgrp_void=yes
    ./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 && make && make install
  5. Install rubygems:

    /mnt/storage/usr/bin/ruby setup.rb --prefix=/mnt/storage/usr
    mkdir -p /mnt/storage/usr/lib/ruby/gems/1.8
  6. 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
    gem install sqlite3-ruby -- --with-sqlite3-dir=/mnt/storage/usr

The result is an image with Ruby installed, and all necessary gems for my project. The tarball can be downloaded here. To install, scp it to the Chumby and log in:

scp *tarball* root@*chumby-ip*:/mnt/storage
ssh -l root *chumby-ip*
cd /mnt/storage
tar xvzf *tarball*

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:

export BASEDIR=/mnt/storage/usr
export PATH=$PATH:$BASEDIR/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BASEDIR/lib

/me mumbles more automation needed. But not today.

]]>
http://gonium.net/md/2010/06/02/ruby-for-chumby-howto/feed/ 0
Ruby 1.9 Performance http://gonium.net/md/2009/04/17/ruby-19-performance/ http://gonium.net/md/2009/04/17/ruby-19-performance/#comments Fri, 17 Apr 2009 09:51:15 +0000 md http://gonium.net/md/?p=146 I’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 in theis case, the runtimes are about the same. So I’m investigating further, using the ruby 1.8 profiler on a smaller test problem:


% cumulative self self total
time seconds seconds calls ms/call ms/call name
33.20 32.96 32.96 661 49.86 77.25 Marshal.load
17.80 50.63 17.67 6365 2.78 14.96 Array#each
17.52 68.03 17.40 601659 0.03 0.03 IO#getc
9.19 77.15 9.12 660 13.82 13.82 Marshal.dump
6.01 83.12 5.97 187158 0.03 0.03 Float#+

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 – 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:

  • Ruby 1.8.6: 1 minute, 34 seconds
  • Ruby 1.9.0: 1 minute, 17 seconds

Note to self: always use a profiler before refactoring code for performance reasons.

]]>
http://gonium.net/md/2009/04/17/ruby-19-performance/feed/ 0
Generating random numbers from any distribution http://gonium.net/md/2008/07/29/generating-random-numbers-from-any-distribution/ http://gonium.net/md/2008/07/29/generating-random-numbers-from-any-distribution/#comments Tue, 29 Jul 2008 09:22:52 +0000 md http://gonium.net/md/?p=85

At work I needed to generate random numbers following a combination of two gaussian distributions – which gave me some headache until someone pointed me to using a Monte Carlo approach. Here’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 looks like this:

However, I needed random numbers following the joint distribution of two gaussian distributions. The histogram should look like this:

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):

c*g(x) >= f(x) for all x.

The steps are rather simple – to generate a random variate x with density f(x):

  1. Generate x with PDF g(x)
  2. Generate y uniform on [0, cg(x)]
  3. If y <= f(x), then output x and return – otherwise repeat from step 1.

Please note that this approach is applicable to many PDFs, but it can be rather slow, depending on the “hit rate”. The algorithm is simple: You generate two uniform random numbers, x and y. Then, you check if y < f(x) – 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.

I highly recommend reading Chapters 28 and 29 of Rai Jain’s “The Art of Computer Systems Performance Analysis” which outline techniques for generating random numbers of several distributions.

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.

[viewcode]src=statistics.rb [/viewcode]


HTML code generated by vim-color-improved v.0.4.0.Download this code: statistics.rb

The title picture was CCed by Saffana, thanks!

]]>
http://gonium.net/md/2008/07/29/generating-random-numbers-from-any-distribution/feed/ 0