Class: Statistical::Distribution::Frechet

Inherits:
Object
  • Object
show all
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.

Author:

  • Vaibhav Yenamandra

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alpha = nil, location = 0, scale = 1) ⇒ Frechet

Returns a new Statistical::Distribution::Frechet instance

Parameters:

  • alpha (Numeric) (defaults to: nil)

    Mandatory shape parameter of the distribution

  • location (Numeric) (defaults to: 0)

    Optional location parameter

  • scale (Numeric) (defaults to: 1)

    Optional scale parameter

Raises:

  • (ArgumentError)


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

#alphaNumeric (readonly)

Mandatory shape parameter of the distribution

Returns:

  • (Numeric)

    the current value of alpha



14
15
16
# File 'lib/statistical/distribution/frechet.rb', line 14

def alpha
  @alpha
end

#locationNumeric (readonly)

Optional location parameter

Returns:

  • (Numeric)

    the current value of location



14
15
16
# File 'lib/statistical/distribution/frechet.rb', line 14

def location
  @location
end

#scaleNumeric (readonly)

Optional scale parameter

Returns:

  • (Numeric)

    the current value of scale



14
15
16
# File 'lib/statistical/distribution/frechet.rb', line 14

def scale
  @scale
end

#supportNumeric (readonly)

Support / domain of the distribution’s PDF / CDF

Returns:

  • (Numeric)

    the current value of support



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

Parameters:

  • x (Numeric)

    A real valued point

Returns:

  • (Float)

    Cumulative density function evaluated for F(u <= x)



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

#meanObject

Returns the mean value for the calling instance. Calculated mean, and

not inferred from simulations

Returns:

  • Mean of the distribution



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.

Parameters:

  • x (Numeric)

    A real valued point

Returns:

  • (Float)

    Probability density function evaluated at x



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

Parameters:

  • p (Numeric)

    a value within [0, 1]

Returns:

  • (Float)

    Inverse CDF for valid p

Raises:

  • (RangeError)

    if p > 1 or p < 0

See Also:



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

#varianceObject

Returns the expected value of variance for the calling instance.

Returns:

  • Variance of the distribution



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