Class: RubyStatistics::Distribution::Geometric

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-statistics/distribution/geometric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p, always_success: false) ⇒ Geometric

Returns a new instance of Geometric.



6
7
8
9
# File 'lib/ruby-statistics/distribution/geometric.rb', line 6

def initialize(p, always_success: false)
  self.probability_of_success = p.to_r
  self.always_success_allowed = always_success
end

Instance Attribute Details

#always_success_allowedObject

Returns the value of attribute always_success_allowed.



4
5
6
# File 'lib/ruby-statistics/distribution/geometric.rb', line 4

def always_success_allowed
  @always_success_allowed
end

#probability_of_successObject

Returns the value of attribute probability_of_success.



4
5
6
# File 'lib/ruby-statistics/distribution/geometric.rb', line 4

def probability_of_success
  @probability_of_success
end

Instance Method Details

#cumulative_function(k) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby-statistics/distribution/geometric.rb', line 25

def cumulative_function(k)
  k = k.to_i

  if always_success_allowed
    return if k < 0

    1.0 - ((1.0 - probability_of_success) ** (k + 1.0))
  else
    return if k <= 0

    1.0 - ((1.0 - probability_of_success) ** k)
  end
end

#density_function(k) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby-statistics/distribution/geometric.rb', line 11

def density_function(k)
  k = k.to_i

  if always_success_allowed
    return if k < 0

    ((1.0 - probability_of_success) ** k) * probability_of_success
  else
    return if k <= 0

    ((1.0 - probability_of_success) ** (k - 1.0)) * probability_of_success
  end
end

#kurtosisObject



71
72
73
# File 'lib/ruby-statistics/distribution/geometric.rb', line 71

def kurtosis
  6.0 + ((probability_of_success ** 2) / (1.0 - probability_of_success))
end

#meanObject



39
40
41
42
43
44
45
# File 'lib/ruby-statistics/distribution/geometric.rb', line 39

def mean
  if always_success_allowed
    (1.0 - probability_of_success) / probability_of_success
  else
    1.0 / probability_of_success
  end
end

#medianObject



47
48
49
50
51
52
53
# File 'lib/ruby-statistics/distribution/geometric.rb', line 47

def median
  if always_success_allowed
    (-1.0 / Math.log2(1.0 - probability_of_success)).ceil - 1.0
  else
    (-1.0 / Math.log2(1.0 - probability_of_success)).ceil
  end
end

#modeObject



55
56
57
58
59
60
61
# File 'lib/ruby-statistics/distribution/geometric.rb', line 55

def mode
  if always_success_allowed
    0.0
  else
    1.0
  end
end

#skewnessObject



67
68
69
# File 'lib/ruby-statistics/distribution/geometric.rb', line 67

def skewness
  (2.0 - probability_of_success) / Math.sqrt(1.0 - probability_of_success)
end

#varianceObject



63
64
65
# File 'lib/ruby-statistics/distribution/geometric.rb', line 63

def variance
  (1.0 - probability_of_success) / (probability_of_success ** 2)
end