Question

In Ruby, I need to draw from an exponential distribution with mean m. Please show me how to do it quickly and efficiently. Eg. let's have:

m = 4.2

def exponential_distribution
  rand( m * 2 )
end

But of course, this code is wrong, and also, it only returns whole number results. I'm already tired today, please hint me towards a good solution.

Was it helpful?

Solution 2

How about using the distribution gem? Here's an example:

require 'distribution'

mean = 4.2
lambda = mean**-1

# generate a rng with exponential distribution
rng = Distribution::Exponential.rng(lambda)

# sample a value
sample = rng.call

If you need to change the value of lambda very often it might be useful to use the p_value method directly. A good sample can be found in the source code for Distribution::Exponential#rng, which basically just uses p_value internally. Here's an example of how to do it:

require 'distribution'

# use the same rng for each call
rng = Random

1.step(5, 0.1) do |mean|
  lambda = mean**-1

  # sample a value
  sample = Distribution::Exponential.p_value(rng.rand, lambda)
end

OTHER TIPS

If you want to do it from scratch you can use inversion.

def exponential(mean)
  -mean * Math.log(rand) if mean > 0
end

If you want to parameterize it with rate lambda, the mean and rate are inverses of each other. Divide by -lambda rather than multiplying by -mean.

Technically it should be log(1.0 - rand), but since 1.0 - rand has a uniform distribution you can save one arithmetic operation by just using rand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top