Module: Distribution::Poisson::Ruby_
- Defined in:
- lib/distribution/poisson/ruby.rb
Class Method Summary collapse
- .cdf(k, l) ⇒ Object
- .pdf(k, l) ⇒ Object
- .quantile(prob, l) ⇒ Object (also: p_value)
-
.rng(lambda_val = 1, seed = nil) ⇒ Object
Return a Proc object which returns a random number drawn from the poisson distribution with lambda.
Class Method Details
.cdf(k, l) ⇒ Object
34 35 36 |
# File 'lib/distribution/poisson/ruby.rb', line 34 def cdf(k, l) Math.exp(-l) * (0..k).inject(0) { |ac, i| ac + (l**i).quo(Math.factorial(i)) } end |
.pdf(k, l) ⇒ Object
30 31 32 |
# File 'lib/distribution/poisson/ruby.rb', line 30 def pdf(k, l) (l**k * Math.exp(-l)).quo(Math.factorial(k)) end |
.quantile(prob, l) ⇒ Object Also known as: p_value
38 39 40 41 42 43 44 |
# File 'lib/distribution/poisson/ruby.rb', line 38 def quantile(prob, l) ac = 0 (0..100).each do |i| ac += pdf(i, l) return i if prob <= ac end end |
.rng(lambda_val = 1, seed = nil) ⇒ Object
Return a Proc object which returns a random number drawn from the poisson distribution with lambda.
== Arguments
- +lambda_val+ - mean of the poisson distribution
- +seed+ - seed, an integer value to set the initial state
== Algorithm
- Donald Knuth
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/distribution/poisson/ruby.rb', line 16 def rng(lambda_val = 1, seed = nil) seed = Random.new_seed if seed.nil? r = Random.new(seed).rand x = 0 l = Math.exp(-lambda_val) s = l while r > s x += 1 l *= lambda_val / x.to_f s += l end x end |