Random variable library?

G

Gavin Sinclair

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.
 
D

David Ross

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

--- Martin DeMello said:
Gavin Sinclair said:
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
 
D

Denis Mertz

Selon Gavin Sinclair said:
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
 
J

Joel VanderWerf

Martin said:
Gavin Sinclair said:
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.
 
K

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.

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
 
J

Joel VanderWerf

Kirk said:
On Fri, 13 Aug 2004 07:17:01 +0900, 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 :)

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...
 
G

Gavin Sinclair

Joel said:
That's great! I hope it ends up in RAA :)

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
 
M

Mike

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

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

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.
 
M

Mauricio Fernández

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) ?
 
G

Gavin Sinclair

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?
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... :/

Gavin
 
M

Mauricio Fernández

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

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 :) I found the following:
http://www.causascientia.org/math_stat/Dists/Compendium.pdf
 
C

Charles Mills

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
 
J

Joel VanderWerf

Mike said:
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. :)

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] http://en.wikipedia.org/wiki/Mersenne_twister
 
R

Robert Feldt

Joel said:
Mike said:
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. :)

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] http://en.wikipedia.org/wiki/Mersenne_twister
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
http://rubyvm.sourceforge.net/subprojects/randomr/

/R
 
C

Charles Mills

Joel said:
Mike said:
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. :)

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

Joel VanderWerf

Charles said:
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.
 
K

Kirk Haines

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
 
G

Gavin Sinclair

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.
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
 
M

Mike Hall

Gavin said:
So I have two choices: either apply a (0..10) window to a Normal(3,2)
distribution, or go and learn about asymmetric distributions.

Several things:

- run Rand.rb and it will show you sample distributions of all the distributions it knows. Maybe one will be close to what you're looking for.

- there's an Arb distribution -- you give it a piece-wise linear (step-wise??? I forgot) description of the distribution you want, and that's what you'll get

- track down the sources listed at the top of the file -- the two computer music
sources are easy to read and show graphs of the distributions. Perhaps one
of the standard ones are skewed to your liking.

Here's another site with some images:
http://www.itl.nist.gov/div898/handbook/eda/section3/eda366.htm

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top