Class: ERV::GpdDistribution

Inherits:
Distribution show all
Defined in:
lib/erv/generalized_pareto_distribution.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GpdDistribution

Returns a new instance of GpdDistribution.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/erv/generalized_pareto_distribution.rb', line 11

def initialize(opts={})
  super(opts)

  raise ArgumentError unless opts[:scale] and opts[:shape]
  @scale    = opts[:scale].to_f
  @shape    = opts[:shape].to_f
  @location = opts[:location].try(:to_f) || 0.0

  @mean = if @shape < 1.0
    @location + @scale / (1.0 - @shape)
  else
    Infinity
  end

  @variance = if @shape < 0.5
    (@scale ** 2.0) / (((1.0 - @shape) ** 2) * (1.0 - 2 * @shape))
  else
    Infinity
  end
end

Instance Attribute Details

#meanObject

Returns the value of attribute mean.



9
10
11
# File 'lib/erv/generalized_pareto_distribution.rb', line 9

def mean
  @mean
end

#varianceObject

Returns the value of attribute variance.



9
10
11
# File 'lib/erv/generalized_pareto_distribution.rb', line 9

def variance
  @variance
end

Instance Method Details

#sampleObject



32
33
34
35
36
37
38
39
# File 'lib/erv/generalized_pareto_distribution.rb', line 32

def sample
  u = 1.0 - @rng.rand
  if @shape == 0.0
    @location - @scale * Math::log(u)
  else
    @location + (@scale * ((u ** (- @shape)) - 1.0) / @shape)
  end
end