class Statistical::Distribution::Laplace

Say something useful about this class.

@note Any caveats you want to talk about go here…

@author Vaibhav Yenamandra @attr_reader [Numeric] scale Scale parameter of the Laplace distribution. @attr_reader [Numeric] location Location parameter to determine where the

distribution is centered / where the mean lies at

Attributes

location[R]
scale[R]
support[R]

Public Class Methods

new(location = 0, scale = 1) click to toggle source

Returns a new instance of the Laplace distribution; also known as

the two-sided exponential distribution

@param [Numeric] scale Scale parameter of the Laplace distribution. @param [Numeric] location Location parameter to determine where the

distribution is centered / where the mean lies at

@return `Statistical::Distribution::Laplace` instance

# File lib/statistical/distribution/laplace.rb, line 23
def initialize(location = 0, scale = 1)
  @scale = scale
  @location = location
  @support = Domain[-Float::INFINITY, Float::INFINITY, :full_open]
  self
end

Public Instance Methods

==(other)
Alias for: eql?
cdf(x) click to toggle source

Returns value of cumulative density function at a point.

@param [Numeric] x A real valued point @return [Float] Cumulative density function evaluated at x

# File lib/statistical/distribution/laplace.rb, line 43
def cdf(x)
  return [0.5,
          1.0 - 0.5 * Math.exp((@location - x).fdiv(@scale)),
          0.5 * Math.exp((x - @location).fdiv(@scale))][x <=> @location]
end
mean() click to toggle source

Returns the expected mean value for the calling instance.

@return Mean of the distribution

# File lib/statistical/distribution/laplace.rb, line 67
def mean
  return @location
end
p_value(p)
Alias for: quantile
pdf(x) click to toggle source

Returns value of probability density function at a point.

@param [Numeric] x A real valued point @return [Float] Probility density function evaluated at x

# File lib/statistical/distribution/laplace.rb, line 34
def pdf(x)
  x_ = (x - @location).abs.fdiv(@scale)
  return Math.exp(- x_).fdiv(2 * @scale)
end
quantile(p) click to toggle source

Returns value of inverse CDF for a given probability

@see p_value

@param [Numeric] p a value within [0, 1] @return Inverse CDF for valid p @raise [RangeError] if p > 1 or p < 0

# File lib/statistical/distribution/laplace.rb, line 56
def quantile(p)
  raise RangeError, "`p` must be in [0, 1], found: #{p}" if p < 0 || p > 1

  return [@location,
          @location - @scale * Math.log(2 * (1.0 - p)),
          @scale * Math.log(2 * p) + @location][p <=> 0.5]
end
Also aliased as: p_value
variance() click to toggle source

Returns the expected value of variance for the calling instance.

@return Variance of the distribution

# File lib/statistical/distribution/laplace.rb, line 74
def variance
  return 2 * @scale * @scale
end

Private Instance Methods

eql?(other) click to toggle source

Compares two distribution instances and returns a boolean outcome

Available publicly as #==

@private

@param other A distribution object (preferred) @return [Boolean] true if-and-only-if two instances are of the same

class and have the same parameters.
# File lib/statistical/distribution/laplace.rb, line 86
def eql?(other)
  return false unless other.is_a?(self.class) &&
                      other.scale == @scale &&
                      other.location == @location
  return true
end
Also aliased as: ==