Skip to content

Gamma Distribution

Esteban Zapata Rojas edited this page Jan 16, 2025 · 1 revision

Scale and Rate parameters

As described in the wikipedia article, the parameters used by the Gamma distribution are: shape and scale or shape and rate, where rate is defined as 1.0 / shape. This implementation supports both flavours, and its usage is based on how you initialise the object:

irb(main):005> rate, scale = Distribution::Gamma.new(shape: 2), Distribution::Gamma.new(shape: 2, scale: 2)
=> [#<RubyStatistics::Distribution::Gamma:0x000000011c45c050 @rate=0.5, @scale=nil, @shape=2>, #<RubyStatistics::Distribution::Gamma:0x000000011c45bfd8 @rate=nil, @scale=2, @shape=2>]
irb(main):006> rate
=> #<RubyStatistics::Distribution::Gamma:0x000000011c45c050 @rate=0.5, @scale=nil, @shape=2>
irb(main):007> scale
=> #<RubyStatistics::Distribution::Gamma:0x000000011c45bfd8 @rate=nil, @scale=2, @shape=2>
irb(main):008> scale.as_rate?
=> false
irb(main):009> rate.as_rate?
=> true

All instance methods check if the defined distribution uses the rate parameter or not to perform its calculations properly.

Instance methods

Cumulative function

It calculates the probability P(x <= X) for an specified X.

irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2)
=> [#<RubyStatistics::Distribution::Gamma:0x000000011c3b4e90 @rate=0.25, @scale=nil, @shape=4>, #<RubyStatistics::Distribution::Gamma:0x000000011c3b4e18 @rate=nil, @scale=2, @shape=4>]
irb(main):017> rate.cumulative_function(1)
=> 0.00013336965051406234
irb(main):018> scale.cumulative_function(1)
=> 0.0017516225562908235

Density function

It calculates the density for the specified value, based the shape and scale or rate parameters defined at initialisation.

irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2)
=> [#<RubyStatistics::Distribution::Gamma:0x000000011c3b4e90 @rate=0.25, @scale=nil, @shape=4>, #<RubyStatistics::Distribution::Gamma:0x000000011c3b4e18 @rate=nil, @scale=2, @shape=4>]
irb(main):019> rate.density_function(1)
=> 0.0005070317598121126
irb(main):020> scale.density_function(1)
=> 0.006318027705339931

Mean

it calculates the mean based on the specified shape and scale or rate parameters.

irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2)
=> [#<RubyStatistics::Distribution::Gamma:0x000000011c3b4e90 @rate=0.25, @scale=nil, @shape=4>, #<RubyStatistics::Distribution::Gamma:0x000000011c3b4e18 @rate=nil, @scale=2, @shape=4>]
irb(main):021> rate.mean
=> 16.0
irb(main):022> scale.mean
=> 8

Mode

It calculates the mode based on the specified shape and scale or rate parameters. If shape < 1.0 then it returns zero.

irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2)
=> [#<RubyStatistics::Distribution::Gamma:0x000000011c3b4e90 @rate=0.25, @scale=nil, @shape=4>, #<RubyStatistics::Distribution::Gamma:0x000000011c3b4e18 @rate=nil, @scale=2, @shape=4>]
irb(main):023> rate.mode
=> 12.0
irb(main):024> scale.mode
=> 6.0
irb(main):027> Distribution::Gamma.new(shape: -2).mode
=> 0.0

Variance

It calculates the variance based on the specified shae and scale or rate parameters.

irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2)
=> [#<RubyStatistics::Distribution::Gamma:0x000000011c3b4e90 @rate=0.25, @scale=nil, @shape=4>, #<RubyStatistics::Distribution::Gamma:0x000000011c3b4e18 @rate=nil, @scale=2, @shape=4>]
irb(main):028> rate.variance
=> 64.0
irb(main):029> scale.variance
=> 16.0

Skewness

It calculates the skewness of the distribution. It depends only on the shape parameter.

irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2)
=> [#<RubyStatistics::Distribution::Gamma:0x000000011c3b4e90 @rate=0.25, @scale=nil, @shape=4>, #<RubyStatistics::Distribution::Gamma:0x000000011c3b4e18 @rate=nil, @scale=2, @shape=4>]
irb(main):031> rate.skewness == scale.skewness
=> true
irb(main):032> rate.skewness
=> 1.0
irb(main):033> scale.skewness
=> 1.0

Behave as a rate gamma distribution?

This method returns true if the rate parameter is being used, false otherwise.

irb(main):016> rate, scale = Distribution::Gamma.new(shape: 4), Distribution::Gamma.new(shape: 4, scale: 2)
=> [#<RubyStatistics::Distribution::Gamma:0x000000011c3b4e90 @rate=0.25, @scale=nil, @shape=4>, #<RubyStatistics::Distribution::Gamma:0x000000011c3b4e18 @rate=nil, @scale=2, @shape=4>]
irb(main):034> rate.as_rate?
=> true
irb(main):035> scale.as_rate?
=> false