Random variable library?

Folks,

Anyone know of a Ruby library that defines random variables[1]? I'd like
access to a variety of distributions. I haven't seen anything like this
in Ruby. Perhaps someone has, or can suggest a C library I might wrap.

Cheers,
Gavin

[1] By which I mean "an object that returns random numbers on request,
according to a configured distribution, bias, etc." Sorry if my
terminology is wrong; the Wikipedia articles on the matter are making my
head spin.

http://users.rcn.com/m3ha11/ruby/Rand.rb looks good to me

martin

···

Gavin Sinclair <gsinclair@soyabean.com.au> wrote:

Folks,

Anyone know of a Ruby library that defines random variables[1]? I'd like
access to a variety of distributions. I haven't seen anything like this
in Ruby. Perhaps someone has, or can suggest a C library I might wrap.

Selon Gavin Sinclair <gsinclair@soyabean.com.au>:

Folks,

Anyone know of a Ruby library that defines random variables[1]? I'd like
access to a variety of distributions. I haven't seen anything like this
in Ruby. Perhaps someone has, or can suggest a C library I might wrap.

Hello,
the gsl library[1] has a lot of random distribution generators. There are
already two ruby bindings [2] for gsl, but i am not sure if they are complete
(gsl is huge).

Ciao

[1] http://sources.redhat.com/gsl/
[2] http://raa.ruby-lang.org/search.rhtml?search=gsl

···

--
Denis Mertz

Oh neat. Is this in a tarball anwhere? I guess its
another good package for rpa-base :slight_smile: --David Ross

···

--- Martin DeMello <martindemello@yahoo.com> wrote:

Gavin Sinclair <gsinclair@soyabean.com.au> wrote:
> Folks,
>
> Anyone know of a Ruby library that defines random
variables[1]? I'd like
> access to a variety of distributions. I haven't
seen anything like this
> in Ruby. Perhaps someone has, or can suggest a C
library I might wrap.

http://users.rcn.com/m3ha11/ruby/Rand.rb looks good
to me

martin

__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

Martin DeMello wrote:

Folks,

Anyone know of a Ruby library that defines random variables[1]? I'd like
access to a variety of distributions. I haven't seen anything like this
in Ruby. Perhaps someone has, or can suggest a C library I might wrap.

http://users.rcn.com/m3ha11/ruby/Rand.rb looks good to me

Nice, but it is based on Kernel.rand.

I'd like to see something like that, but with multiple independent streams of random numbers, so it can be used for simulations with many random variables.

I have some code like that, but it's based on the PRNGs from Numerical Recipes, and I don't think I can distribute the ruby source except to people who have a license for NR.

···

Gavin Sinclair <gsinclair@soyabean.com.au> wrote:

Martin DeMello wrote:

Anyone know of a Ruby library that defines random variables[1]? I'd like
access to a variety of distributions.

http://users.rcn.com/m3ha11/ruby/Rand.rb looks good to me

Heh... I was going to say that. :slight_smile:

It's just a collection of various distributions that I've seen in different places.
Like Joel said, it starts with Kernel.rand, so it's ultimately no better than that.
But, it should give you different distributions that you need.

···

--
Mike Hall

I have a Ruby implementation of the ISAAC cryptographically secure random
number generator packaged with Iowa. Each Crypt::Iowa object is an
independent source of random numbers.

@random_numbers = Crypt::ISAAC.new

number = @random_numbers.rand(1000)

It does need a little work, but it wouldn't take much to polish it up.

Kirk Haines

···

On Fri, 13 Aug 2004 07:17:01 +0900, Joel VanderWerf wrote

I'd like to see something like that, but with multiple independent
streams of random numbers, so it can be used for simulations with
many random variables.

Mike wrote:

Martin DeMello wrote:

Anyone know of a Ruby library that defines random variables[1]? I'd like
access to a variety of distributions.

http://users.rcn.com/m3ha11/ruby/Rand.rb looks good to me

Heh... I was going to say that. :slight_smile:

It's just a collection of various distributions that I've seen in different places.
Like Joel said, it starts with Kernel.rand, so it's ultimately no better than that.
But, it should give you different distributions that you need.

Not that there's anything wrong with Kernel.rand--it uses MT19937, which seems to be very good for all but crypto purposes.[1] That plus Rand.rb would be ideal for a single stream sampled from one of the many distributions that Rand.rb implements. With caution, you could use it for a small number of streams, too.

It's just that if you need arbitrarily many streams of numbers, then you want to be able to instantiate more generators. Actually, I suppose you could use Kernel#srand to switch between streams. That would be slow, because the state vector has to be recreated each time, but would save on the space needed for all those state vectors.

[1] Mersenne Twister - Wikipedia

Kirk Haines wrote:

···

On Fri, 13 Aug 2004 07:17:01 +0900, Joel VanderWerf wrote

I'd like to see something like that, but with multiple independent streams of random numbers, so it can be used for simulations with many random variables.

I have a Ruby implementation of the ISAAC cryptographically secure random number generator packaged with Iowa. Each Crypt::Iowa object is an independent source of random numbers.

@random_numbers = Crypt::ISAAC.new

number = @random_numbers.rand(1000)

It does need a little work, but it wouldn't take much to polish it up.

That's great! I hope it ends up in RAA :slight_smile:

Are there any disadvantages? License, memory usage, cpu usage? How about randomness metrics, compared to something like the MT generators? I'd guess they'd be better for a crytpo secure PRNG...

Joel VanderWerf wrote:

Mike wrote:

Martin DeMello wrote:

Anyone know of a Ruby library that defines random variables[1]? I'd like
access to a variety of distributions.

http://users.rcn.com/m3ha11/ruby/Rand.rb looks good to me

Heh... I was going to say that. :slight_smile:

It's just a collection of various distributions that I've seen in different places.
Like Joel said, it starts with Kernel.rand, so it's ultimately no better than that.
But, it should give you different distributions that you need.

Not that there's anything wrong with Kernel.rand--it uses MT19937, which seems to be very good for all but crypto purposes.[1] That plus Rand.rb would be ideal for a single stream sampled from one of the many distributions that Rand.rb implements. With caution, you could use it for a small number of streams, too.

It's just that if you need arbitrarily many streams of numbers, then you want to be able to instantiate more generators. Actually, I suppose you could use Kernel#srand to switch between streams. That would be slow, because the state vector has to be recreated each time, but would save on the space needed for all those state vectors.

[1] Mersenne Twister - Wikipedia

This has been discussed/proposed before so isn't it time someone patched random.c in Ruby sources to work on a MT19937 class instead of the static vars?

If matz is pro this change it would be a slim change; introduce

typedef struct Rmt {
  unsigned long state[N]; /* the array for the state vector */
  int left = 1;
  int initf = 0;
  unsigned long *next;
} RMT;

and then change the methods to access these instead of the static ones. Come on now, do it and hone your Ruby-c-extension writing skills!

The "old" rand + srand would simply work on a global var/constant RANDOM_VALUE which would be an object of the MMT19937 class, ie

def rand
  RANDOM_VALUE.rand
end

def srand(seed)
RANDOM_VALUE.reseed(seed)
end

etc.

Maybe there is already a RCR on this? It is so important in many scientific / simulation apps to be able to have multiple, independent streams of random values.

In the mean time one could use RandomR README

/R

Joel VanderWerf wrote:

I have a Ruby implementation of the ISAAC cryptographically secure
random number generator packaged with Iowa. Each Crypt::Iowa object
is an independent source of random numbers.

@random_numbers = Crypt::ISAAC.new

number = @random_numbers.rand(1000)

It does need a little work, but it wouldn't take much to polish it up.

That's great! I hope it ends up in RAA :slight_smile:

Are there any disadvantages? License, memory usage, cpu usage? How about
randomness metrics, compared to something like the MT generators? I'd
guess they'd be better for a crytpo secure PRNG...

The work I'm doing now doesn't strictly require, but would benefit from a
random number generator that:
- obeyed the distribution you want, with the parameters you want
- spat out numbers between a given minimum and maximum
- could be configured to return Integers or Floats

Obviously, it should be able to use any RNG you wish, defaulting to
Kernel.rand.

I haven't looked at the GSL stuff yet (and wish to avoid C dependencies if
possible), but Rand.rb seems to be a good start.

When it gets more important for me, I'll try to wrap something up for a
library release. Since there are a few people interested in this thread,
post your API ideas if you've got 'em!

Cheers,
Gavin

Joel VanderWerf wrote:

Mike wrote:

Martin DeMello wrote:

Anyone know of a Ruby library that defines random variables[1]? I'd like
access to a variety of distributions.

http://users.rcn.com/m3ha11/ruby/Rand.rb looks good to me

Heh... I was going to say that. :slight_smile:

It's just a collection of various distributions that I've seen in different places.
Like Joel said, it starts with Kernel.rand, so it's ultimately no better than that.
But, it should give you different distributions that you need.

Not that there's anything wrong with Kernel.rand--it uses MT19937, which seems to be very good for all but crypto purposes.[1] That plus Rand.rb would be ideal for a single stream sampled from one of the many distributions that Rand.rb implements. With caution, you could use it for a small number of streams, too.

It's just that if you need arbitrarily many streams of numbers, then you want to be able to instantiate more generators. Actually, I suppose you could use Kernel#srand to switch between streams. That would be slow, because the state vector has to be recreated each time, but would save on the space needed for all those state vectors.

Could you explain this more?

[1] Mersenne Twister - Wikipedia

This has been discussed/proposed before so isn't it time someone patched random.c in Ruby sources to work on a MT19937 class instead of the static vars?

If matz is pro this change it would be a slim change; introduce

typedef struct Rmt {
unsigned long state[N]; /* the array for the state vector */
int left = 1;
int initf = 0;
unsigned long *next;
} RMT;

I don't think that is legal C :slight_smile:

and then change the methods to access these instead of the static ones. Come on now, do it and hone your Ruby-c-extension writing skills!

The "old" rand + srand would simply work on a global var/constant RANDOM_VALUE which would be an object of the MMT19937 class, ie

def rand
RANDOM_VALUE.rand
end

def srand(seed)
RANDOM_VALUE.reseed(seed)
end

etc.

Then have something like the following?
s = RandomStreamClass.new(seed)
r = s.rand

-Charlie

···

On Aug 14, 2004, at 4:12 PM, Robert Feldt wrote:

Maybe there is already a RCR on this? It is so important in many scientific / simulation apps to be able to have multiple, independent streams of random values.

In the mean time one could use RandomR README

/R

The work I'm doing now doesn't strictly require, but would benefit from a
random number generator that:
- obeyed the distribution you want, with the parameters you want

You can do that easily by taking a uniform random variable and feeding
it into the distribution function you want.

- spat out numbers between a given minimum and maximum

like min + rand(max-min+1) ?

···

On Fri, Aug 13, 2004 at 11:26:22AM +0900, Gavin Sinclair wrote:

- could be configured to return Integers or Floats

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Charles Mills wrote:

Joel VanderWerf wrote:

It's just that if you need arbitrarily many streams of numbers, then you want to be able to instantiate more generators. Actually, I suppose you could use Kernel#srand to switch between streams. That would be slow, because the state vector has to be recreated each time, but would save on the space needed for all those state vectors.

Could you explain this more?

It was a bad idea. After looking more closely at ruby's random.c, I don't think it's possible. I naively thought the value returned by #srand might be a new seed that encodes the current position in the sequence, but of course is does not, because there are 2^19937-1 distinct positions. So you'd have to save and restore the whole state vector. It's better to follow Robert's suggestion to wrap that state in an object.

···

On Aug 14, 2004, at 4:12 PM, Robert Feldt wrote:

The work I'm doing now doesn't strictly require, but would benefit from a
random number generator that:
- obeyed the distribution you want, with the parameters you want

You can do that easily by taking a uniform random variable and feeding
it into the distribution function you want.

Thanks for the tip. How do you write a distribution function?

- spat out numbers between a given minimum and maximum

like min + rand(max-min+1) ?

Yes, but what about: "give me a normal distribution between 0 and 10"?
That probably doesn't make sense without some extra parameters, but
they can be defaults/derived.

Then I'd like to be able to bias it as well, if that makes sense. So
you can have a "normal" distribution between 0 and 10, biased so that
the center is on 3 instead of 5.

IF that makes sense... :confused:

Gavin

···

On Friday, August 13, 2004, 5:41:22 PM, Mauricio wrote:

On Fri, Aug 13, 2004 at 11:26:22AM +0900, Gavin Sinclair wrote:

Just as an FYI, I've requested a RubyForge project for Crypt::ISAAC. As
soon as it is setup I'll upload the library and drop an entry into RAA for
it.

Kirk Haines

Well, that's more or less what the rand.rb mentioned somewhere else in
this thread does, at least for some distributions. Basically, once you
know the probability density function you want, you integrate it to obtain
the distribution function. Note that in some cases the pdf. cannot be
integrated analytically, so you'd have to either generate an instance of
the random var through other means or use some numerical approximations.

After a surprisingly long search (compared to what one is used to
regarding google :slight_smile: I found the following:

···

On Sat, Aug 14, 2004 at 03:43:36PM +0900, Gavin Sinclair wrote:

On Friday, August 13, 2004, 5:41:22 PM, Mauricio wrote:

> On Fri, Aug 13, 2004 at 11:26:22AM +0900, Gavin Sinclair wrote:
>> The work I'm doing now doesn't strictly require, but would benefit from a
>> random number generator that:
>> - obeyed the distribution you want, with the parameters you want

> You can do that easily by taking a uniform random variable and feeding
> it into the distribution function you want.

Thanks for the tip. How do you write a distribution function?

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Gavin Sinclair wrote:

The work I'm doing now doesn't strictly require, but would benefit from a
random number generator that:
- obeyed the distribution you want, with the parameters you want

You can do that easily by taking a uniform random variable and feeding
it into the distribution function you want.

Actually that's the inverse of the distribution function.

Thanks for the tip. How do you write a distribution function?

Every probability, statistics, and simulation text out there has descriptions and formulas for various distributions. However, not all distribution functions can be written in closed form or are invertible.

- spat out numbers between a given minimum and maximum

like min + rand(max-min+1) ?

Yes, but what about: "give me a normal distribution between 0 and 10"?
That probably doesn't make sense without some extra parameters, but
they can be defaults/derived.

Don't take this personally, but this is where I start getting nervous. A normal distribution is characterized _entirely_ by its mean and variance, and has an infinite range. If what you want falls between 0 and 10, then it ain't normal.

Then I'd like to be able to bias it as well, if that makes sense. So
you can have a "normal" distribution between 0 and 10, biased so that
the center is on 3 instead of 5.

IF that makes sense... :confused:

Sorry, but no it doesn't. The normal distribution is symmetric about its mean. If you want an assymetric distribution (the term is "skewed", not "biased"), then once again it ain't normal.

If you're going to do probability modeling, you need to understand the models or you're going to get in trouble. Somebody already suggested Ross's book, I'll put in my 2-cents for taking a look at "Simulation Modeling and Analysis" by Law & Kelton. It has a good review of prob & stats and a very comprehensive discussion of the theory and algorithms for random variate generation.

I know that usenet posts can come across as harsh, and that's not my intention. Please accept this response as a strong caution rather than as a put-down.

--paul

···

On Friday, August 13, 2004, 5:41:22 PM, Mauricio wrote:

On Fri, Aug 13, 2004 at 11:26:22AM +0900, Gavin Sinclair wrote:

The work I'm doing now doesn't strictly require, but would benefit from a
random number generator that:
- obeyed the distribution you want, with the parameters you want

You can do that easily by taking a uniform random variable and feeding
it into the distribution function you want.

Thanks for the tip. How do you write a distribution function?

If your really interested in how to use a uniform random variable to generate other random variables 'Simulation' by Sheldon M Ross is a good book on the subject. You could also google the "inverse transform method" and that will probably turn up something.
-Charlie

···

On Aug 14, 2004, at 1:07 AM, Mauricio Fernández wrote:

On Sat, Aug 14, 2004 at 03:43:36PM +0900, Gavin Sinclair wrote:

On Friday, August 13, 2004, 5:41:22 PM, Mauricio wrote:

On Fri, Aug 13, 2004 at 11:26:22AM +0900, Gavin Sinclair wrote:

Well, that's more or less what the rand.rb mentioned somewhere else in
this thread does, at least for some distributions. Basically, once you
know the probability density function you want, you integrate it to obtain
the distribution function. Note that in some cases the pdf. cannot be
integrated analytically, so you'd have to either generate an instance of
the random var through other means or use some numerical approximations.

After a surprisingly long search (compared to what one is used to
regarding google :slight_smile: I found the following:
http://www.causascientia.org/math_stat/Dists/Compendium.pdf

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Yes, but what about: "give me a normal distribution between 0 and 10"?
That probably doesn't make sense without some extra parameters, but
they can be defaults/derived.

Don't take this personally, but this is where I start getting nervous.
A normal distribution is characterized _entirely_ by its mean and
variance, and has an infinite range. If what you want falls between 0
and 10, then it ain't normal.

That's fine and understood. I'm not looking for distributed random
numbers for probability analysis, just for node placement and cluster
sizes in a network simulation. There's no scientific validity riding
on the values pumped out of the RNG.

To use the 0-10 example, if the distribution has mean 5 and std-dev 2,
then only a tiny portion of generated numbers will fall outside of
(0..10). Those numbers are insignificant to me (not to others, I
appreciate), so I can just regenerate until I get a value I want.

That makes me realise: the "window" aspect of this (only seeing values
within a certain range) is a superficial operation that can be applied
seperately from any actual generation and distribution, with
themselves are two different layers.

Then I'd like to be able to bias it as well, if that makes sense. So
you can have a "normal" distribution between 0 and 10, biased so that
the center is on 3 instead of 5.

IF that makes sense... :confused:

Sorry, but no it doesn't. The normal distribution is symmetric about
its mean. If you want an assymetric distribution (the term is "skewed",
not "biased"), then once again it ain't normal.

So I have two choices: either apply a (0..10) window to a Normal(3,2)
distribution, or go and learn about asymmetric distributions. (I
suspect you can have a plain "skewed distribution" applied to any
other distribution to get the result I want.)

If you're going to do probability modeling, you need to understand the
models or you're going to get in trouble. Somebody already suggested
Ross's book, I'll put in my 2-cents for taking a look at "Simulation
Modeling and Analysis" by Law & Kelton. It has a good review of prob &
stats and a very comprehensive discussion of the theory and algorithms
for random variate generation.

Like I said, I'm not doing probability modeling, but I'll still be
interested to read about these things in more detail.

I know that usenet posts can come across as harsh, and that's not my
intention. Please accept this response as a strong caution rather than
as a put-down.

Not at all; thanks very much for the input. You've educated me and
helped to clarify my aims.

Gavin

···

On Sunday, August 15, 2004, 9:11:08 AM, Paul wrote: