Class: RubyStatistics::Distribution::LogSeries

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

Class Method Summary collapse

Class Method Details

.cumulative_function(k, p) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-statistics/distribution/logseries.rb', line 14

def self.cumulative_function(k, p)
  return if k <= 0

  # Sadly, the incomplete beta function is converging
  # too fast to zero and breaking the calculation on logs.
  # So, we default to the basic definition of the CDF which is
  # the integral (-Inf, K) of the PDF, with P(X <= x) which can
  # be solved as a summation of all PDFs from 1 to K. Note that the summation approach
  # only applies to discrete distributions.
  #
  # right = Math.incomplete_beta_function(p, (k + 1).floor, 0) / Math.log(1.0 - p)
  # 1.0 + right

  result = 0.0
  1.upto(k) do |number|
    result += self.density_function(number, p)
  end

  result
end

.density_function(k, p) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/ruby-statistics/distribution/logseries.rb', line 4

def self.density_function(k, p)
  return if k <= 0
  k = k.to_i

  left = (-1.0 / Math.log(1.0 - p))
  right = (p ** k).to_r

  left * right / k
end

.mean(p) ⇒ Object



39
40
41
# File 'lib/ruby-statistics/distribution/logseries.rb', line 39

def self.mean(p)
  (-1.0 / Math.log(1.0 - p)) * (p / (1.0 - p))
end

.modeObject



35
36
37
# File 'lib/ruby-statistics/distribution/logseries.rb', line 35

def self.mode
  1.0
end

.variance(p) ⇒ Object



43
44
45
46
47
48
# File 'lib/ruby-statistics/distribution/logseries.rb', line 43

def self.variance(p)
  up = p + Math.log(1.0 - p)
  down = ((1.0 - p) ** 2) * (Math.log(1.0 - p) ** 2)

  (-1.0 * p) * (up / down.to_r)
end