Class: RubyStatistics::Distribution::Gamma

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shape:, scale: nil) ⇒ Gamma



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby-statistics/distribution/gamma.rb', line 8

def initialize(shape:, scale: nil)
  @shape = shape
  @scale = scale

  # If the scale is nil, it means we want the distribution to behave with a rate parameter
  # instead of a scale parameter
  @rate = if scale.nil?
            1.0 / shape
          else
            nil
          end
end

Instance Attribute Details

#rateObject (readonly)

Returns the value of attribute rate.



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

def rate
  @rate
end

#scaleObject (readonly)

Returns the value of attribute scale.



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

def scale
  @scale
end

#shapeObject (readonly)

Returns the value of attribute shape.



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

def shape
  @shape
end

Instance Method Details

#as_rate?Boolean



21
22
23
# File 'lib/ruby-statistics/distribution/gamma.rb', line 21

def as_rate?
  scale.nil?
end

#cumulative_function(x) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby-statistics/distribution/gamma.rb', line 71

def cumulative_function(x)
  upper = if as_rate?
            self.rate * x.to_r
          else
            x / self.scale.to_r
          end

  # left = 1.0 / Math.gamma(self.shape)
  # right = Math.lower_incomplete_gamma_function(self.shape, upper)
  # left * right
  Math.normalised_lower_incomplete_gamma_function(self.shape, upper)
end

#density_function(x) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby-statistics/distribution/gamma.rb', line 55

def density_function(x)
  euler = if as_rate?
            Math.exp(- self.rate * x)
          else
            Math.exp(-x / self.scale.to_r)
          end

  left = if as_rate?
           (self.rate ** self.shape).to_r / Math.gamma(self.shape).to_r
         else
           1r / (Math.gamma(self.shape).to_r * (self.scale ** self.shape).to_r)
         end

  left * (x ** (self.shape - 1)) * euler
end

#meanObject



25
26
27
28
29
30
31
# File 'lib/ruby-statistics/distribution/gamma.rb', line 25

def mean
  if as_rate?
    self.shape / self.rate
  else
    self.shape * self.scale
  end
end

#modeObject



33
34
35
36
37
38
39
40
41
# File 'lib/ruby-statistics/distribution/gamma.rb', line 33

def mode
  return 0.0 if self.shape < 1.0

  if as_rate?
    (self.shape - 1.0) / self.rate
  else
    (self.shape - 1.0) * self.scale
  end
end

#skewnessObject



51
52
53
# File 'lib/ruby-statistics/distribution/gamma.rb', line 51

def skewness
  2.0 / Math.sqrt(self.shape)
end

#varianceObject



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

def variance
  if as_rate?
    self.shape / (self.rate ** 2.0)
  else
    self.shape * (self.scale ** 2.0)
  end
end