Class: Statistical::Distribution::Frechet
- Inherits:
-
Object
- Object
- Statistical::Distribution::Frechet
- Defined in:
- lib/statistical/distribution/frechet.rb
Overview
This class models the Frechet (or inverse Weibull) distribution which is
a type-2 extreme value distribution.
Instance Attribute Summary collapse
-
#alpha ⇒ Numeric
readonly
Mandatory shape parameter of the distribution.
-
#location ⇒ Numeric
readonly
Optional location parameter.
-
#scale ⇒ Numeric
readonly
Optional scale parameter.
-
#support ⇒ Numeric
readonly
Support / domain of the distribution’s PDF / CDF.
Instance Method Summary collapse
-
#cdf(x) ⇒ Float
Returns value of cumulative density function upto the specified point.
-
#initialize(alpha = nil, location = 0, scale = 1) ⇒ Frechet
constructor
Returns a new
Statistical::Distribution::Frechetinstance. -
#mean ⇒ Object
Returns the mean value for the calling instance.
-
#pdf(x) ⇒ Float
Returns value of probability density function at a point.
-
#quantile(p) ⇒ Float
(also: #p_value)
Returns value of inverse CDF for a given probability.
-
#variance ⇒ Object
Returns the expected value of variance for the calling instance.
Constructor Details
#initialize(alpha = nil, location = 0, scale = 1) ⇒ Frechet
Returns a new Statistical::Distribution::Frechet instance
23 24 25 26 27 28 29 30 |
# File 'lib/statistical/distribution/frechet.rb', line 23 def initialize(alpha = nil, location = 0, scale = 1) raise ArgumentError if alpha.nil? @alpha = alpha.to_f @location = location.to_f @scale = scale.to_f @support = Domain[@location, Float::INFINITY, :full_open] end |
Instance Attribute Details
#alpha ⇒ Numeric (readonly)
Mandatory shape parameter of the distribution
14 15 16 |
# File 'lib/statistical/distribution/frechet.rb', line 14 def alpha @alpha end |
#location ⇒ Numeric (readonly)
Optional location parameter
14 15 16 |
# File 'lib/statistical/distribution/frechet.rb', line 14 def location @location end |
#scale ⇒ Numeric (readonly)
Optional scale parameter
14 15 16 |
# File 'lib/statistical/distribution/frechet.rb', line 14 def scale @scale end |
#support ⇒ Numeric (readonly)
Support / domain of the distribution’s PDF / CDF
14 15 16 |
# File 'lib/statistical/distribution/frechet.rb', line 14 def support @support end |
Instance Method Details
#cdf(x) ⇒ Float
Returns value of cumulative density function upto the specified point
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/statistical/distribution/frechet.rb', line 54 def cdf(x) xs = (x - @location) / @scale case @support <=> x when 0 return Math.exp(-(xs**-@alpha)) when 1 return 1 when -1 return 0 end end |
#mean ⇒ Object
Returns the mean value for the calling instance. Calculated mean, and
not inferred from simulations
83 84 85 86 87 88 89 |
# File 'lib/statistical/distribution/frechet.rb', line 83 def mean return [ Float::INFINITY, @location + @scale * Math.gamma(1 - 1 / @alpha), Float::INFINITY ][@alpha <=> 1.0] end |
#pdf(x) ⇒ Float
Returns value of probability density function at a point.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/statistical/distribution/frechet.rb', line 36 def pdf(x) xs = (x - @location) / @scale # Not following the usual array lookup idiom here since the PDF # expression is ill-defined (Complex) for x < location. Lazily evaluated # expressions would help retain the array lookup idiom, but that's for # later if (@support <=> x).zero? return (@alpha / @scale) * xs**(-1 - @alpha) * Math.exp(-(xs**-@alpha)) else return 0 end end |
#quantile(p) ⇒ Float Also known as: p_value
Returns value of inverse CDF for a given probability
74 75 76 77 |
# File 'lib/statistical/distribution/frechet.rb', line 74 def quantile(p) raise RangeError, "`p` must be in [0, 1], found: #{p}" if p < 0 || p > 1 return @location + @scale * ((-Math.log(p))**(-1 / @alpha)) end |
#variance ⇒ Object
Returns the expected value of variance for the calling instance.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/statistical/distribution/frechet.rb', line 94 def variance return [ Float::INFINITY, @scale * @scale * ( Math.gamma(1 - 2 / @alpha) - Math.gamma(1 - 1 / @alpha)**2 ), Float::INFINITY ][@alpha <=> 2.0] end |