Class: Statistical::Distribution::Weibull

Inherits:
Object
  • Object
show all
Defined in:
lib/statistical/distribution/weibull.rb

Overview

Note:

Any caveats you want to talk about go here…

Say something useful about this class.

Author:

  • Vaibhav Yenamandra

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scale = 1, shape = 1) ⇒ Object

Returns a new ‘Statistical::Distribution::Weibull` instance

Parameters:

  • scale (Numeric) (defaults to: 1)

    The distribution’s scale parameter

  • shape (Numeric) (defaults to: 1)

    The distribution’s shape parameter



20
21
22
23
24
25
# File 'lib/statistical/distribution/weibull.rb', line 20

def initialize(scale = 1, shape = 1)
  @scale = scale.to_f
  @shape = shape.to_f
  @support = Domain[0.0, Float::INFINITY, :right_open]
  self
end

Instance Attribute Details

#scaleFloat (readonly)

The distribution’s scale parameter

Returns:

  • (Float)

    the current value of scale



12
13
14
# File 'lib/statistical/distribution/weibull.rb', line 12

def scale
  @scale
end

#shapeFloat (readonly)

The distribution’s shape parameter

Returns:

  • (Float)

    the current value of shape



12
13
14
# File 'lib/statistical/distribution/weibull.rb', line 12

def shape
  @shape
end

#supportObject (readonly)

Returns the value of attribute support.



13
14
15
# File 'lib/statistical/distribution/weibull.rb', line 13

def support
  @support
end

Instance Method Details

#cdf(x) ⇒ Object

Returns value of cumulative density function at a point. Calculated

using some technique that you might want to name

Parameters:

  • x (Numeric)

    A real valued point

Returns:



45
46
47
48
49
50
# File 'lib/statistical/distribution/weibull.rb', line 45

def cdf(x)
  return [1 - Math.exp(-((x / @scale)**@shape)),
          1.0,
          0.0
  ][@support <=> x]
end

#meanObject

Returns the expected value of mean for the calling instance.

Returns:

  • Mean of the distribution



67
68
69
# File 'lib/statistical/distribution/weibull.rb', line 67

def mean
  return @scale * Math.gamma(1 + 1 / @shape)
end

#pdf(x) ⇒ Object

Returns value of probability density function at a point. Calculated

using some technique that you might want to name

Parameters:

  • x (Numeric)

    A real valued point

Returns:



32
33
34
35
36
37
38
# File 'lib/statistical/distribution/weibull.rb', line 32

def pdf(x)
  return [(@shape / @scale) * ((x / @scale)**(@shape - 1)) *
          Math.exp(-((x / @scale)**@shape)),
          0.0,
          0.0
  ][@support <=> x]
end

#quantile(p) ⇒ Object Also known as: p_value

Returns value of inverse CDF for a given probability

Parameters:

  • p (Numeric)

    a value within [0, 1]

Returns:

  • Inverse CDF for valid p

Raises:

  • (RangeError)

    if p > 1 or p < 0

See Also:



59
60
61
62
# File 'lib/statistical/distribution/weibull.rb', line 59

def quantile(p)
  raise RangeError, "`p` must be in [0, 1], found: #{p}" if p < 0 || p > 1
  return @scale * ((-Math.log(1 - p))**(1 / @shape))
end

#varianceObject

Returns the expected value of variance for the calling instance.

Returns:

  • Variance of the distribution



74
75
76
77
# File 'lib/statistical/distribution/weibull.rb', line 74

def variance
  m = mean
  (@scale * @scale) * Math.gamma(1 + 2 / @shape) - (m * m)
end