Class: Bernoulli::Distribution::Geometric

Inherits:
Object
  • Object
show all
Includes:
Bernoulli::Distribution
Defined in:
lib/bernoulli/distribution/geometric.rb

Overview

Implements geometrically distributed random variable See en.wikipedia.org/wiki/Geometric_distribution for more information

Instance Method Summary collapse

Methods included from Bernoulli::Distribution

#[], #probability_range, #standard_deviation

Constructor Details

#initialize(p) ⇒ Geometric

Returns a new instance of Geometric.

Parameters:

  • p (Float)

    the success probability (>= 0, <= 1)



13
14
15
16
17
18
# File 'lib/bernoulli/distribution/geometric.rb', line 13

def initialize(p)
  if p > 1.0 or p < 0.0
    raise 'Expecting 0.0 <= p <= 1.0'
  end
  @p = p.to_f
end

Instance Method Details

#excessObject



49
50
51
# File 'lib/bernoulli/distribution/geometric.rb', line 49

def excess
  6 + @p**2 / (1 - @p)
end

#expected_valueObject Also known as: ev



35
36
37
# File 'lib/bernoulli/distribution/geometric.rb', line 35

def expected_value
  1 / @p
end

#probability(k) ⇒ Float

Returns the probability that the first occurrence of success require ‘k` independent trials.

Parameters:

  • k (Integer)

    number of independent trials (> 0)

Returns:

  • (Float)

    the probability that the first occurrence of success require ‘k` independent trials



27
28
29
30
31
32
33
# File 'lib/bernoulli/distribution/geometric.rb', line 27

def probability(k)
  if k == 0
    0
  else
    (1 - @p)**(k - 1) * @p
  end
end

#skewnessObject



45
46
47
# File 'lib/bernoulli/distribution/geometric.rb', line 45

def skewness
  (2 - @p) / Math.sqrt(1 - @p)
end

#to_sObject



20
21
22
# File 'lib/bernoulli/distribution/geometric.rb', line 20

def to_s
  "#<Distribution::Geometric @p=#@p>"
end

#varianceObject Also known as: v



40
41
42
# File 'lib/bernoulli/distribution/geometric.rb', line 40

def variance
  (1 - @p) / @p**2
end