Class: Statistical::Distribution::Exponential

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

Overview

Abstraction of common statistical properties of the exponential

distribution

Author:

  • Vaibhav Yenamandra

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate = 1) ⇒ Object

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

Parameters:

  • rate (Numeric) (defaults to: 1)

    Rate parameter of the exponential distribution. Same as ‘λ` in the canonical version



19
20
21
22
# File 'lib/statistical/distribution/exponential.rb', line 19

def initialize(rate = 1)
  @rate = rate
  @support = Domain[0.0, Float::INFINITY, :right_open]
end

Instance Attribute Details

#rateNumeric (readonly)

Rate parameter controlling the exponential distribution. Same as ‘λ` in the canonical version

Returns:

  • (Numeric)

    the current value of rate



11
12
13
# File 'lib/statistical/distribution/exponential.rb', line 11

def rate
  @rate
end

#supportObject (readonly)

Returns the value of attribute support.



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

def support
  @support
end

Instance Method Details

#cdf(x) ⇒ Float

Returns value of cumulative density function at a point.

Parameters:

  • x (Numeric)

    A real valued point

Returns:

  • (Float)

    Probability mass function evaluated at x



36
37
38
# File 'lib/statistical/distribution/exponential.rb', line 36

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

#meanObject

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

not inferred from simulations

Returns:

  • Mean of the distribution



56
57
58
# File 'lib/statistical/distribution/exponential.rb', line 56

def mean
  return 1.0 / @rate
end

#pdf(x) ⇒ Float

Returns value of probability density function at a point.

Parameters:

  • x (Numeric)

    A real valued point

Returns:

  • (Float)

    Probility density function evaluated at x



28
29
30
# File 'lib/statistical/distribution/exponential.rb', line 28

def pdf(x)
  return [@rate * Math.exp(-@rate * x), 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 (Float)

    a value within [0, 1]

Returns:

  • Inverse CDF for valid p

Raises:

  • (RangeError)

    if p > 1 or p < 0

See Also:



47
48
49
50
# File 'lib/statistical/distribution/exponential.rb', line 47

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

#varianceObject

Returns the expected value of variance for the calling instance.

Returns:

  • Variance of the distribution



63
64
65
# File 'lib/statistical/distribution/exponential.rb', line 63

def variance
  return (1.0 / @rate)**2
end