Class: Statistical::Distribution::Uniform

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

Overview

Note:

If initialized with lower and upper parameters in reverse order, it swaps them. Eg. initializing with lower = 10 and upper = 2 is the same as lower = 2 and upper = 10, due to the swap during call to new(,)

An abstraction of the common statistical properties of the uniform distribution. Provides a PDF, CDF, Inverse-CDF, mean, variance

Author:

  • Vaibhav Yenamandra

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start = 0.0, finish = 1.0) ⇒ Object

Note:

if given lower > upper, it swaps them internally

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

Parameters:

  • start (Numeric) (defaults to: 0.0)

    lower bound of the distribution.

  • finish (Numeric) (defaults to: 1.0)

    upper bound of the distribution.



28
29
30
31
32
# File 'lib/statistical/distribution/uniform.rb', line 28

def initialize(start = 0.0, finish = 1.0)
  @lower = [start, finish].min
  @upper = [start, finish].max
  @support = Domain[@lower, @upper, :closed]
end

Instance Attribute Details

#lowerNumeric (readonly)

The lower bound of the uniform distribution. Defaults to 0.0.

Returns:

  • (Numeric)

    the current value of lower



18
19
20
# File 'lib/statistical/distribution/uniform.rb', line 18

def lower
  @lower
end

#supportObject (readonly)

Returns the value of attribute support.



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

def support
  @support
end

#upperNumeric (readonly)

The upper bound of the uniform distrbution. Defaults to 1.0.

Returns:

  • (Numeric)

    the current value of upper



18
19
20
# File 'lib/statistical/distribution/uniform.rb', line 18

def upper
  @upper
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)

    1 if x is within [lower, upper], 0 otherwise



46
47
48
# File 'lib/statistical/distribution/uniform.rb', line 46

def cdf(x)
  return [(x - @lower).fdiv(@upper - @lower), 1.0, 0.0][@support <=> x]
end

#meanFloat

Returns the expected value of mean value for the calling instance.

Returns:

  • (Float)

    Mean of the distribution

Author:

  • Vaibhav Yenamandra



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

def mean
  return 0.5 * (@upper + @lower)
end

#pdf(x) ⇒ Float

Returns value of probability density function at a point

Parameters:

  • x (Numeric)

    A real valued point

Returns:

  • (Float)

    1 if x is within [lower, upper], 0 otherwise



38
39
40
# File 'lib/statistical/distribution/uniform.rb', line 38

def pdf(x)
  return [1.0 / (@upper - @lower),  0.0, 0.0][@support <=> x]
end

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

Returns value of inverse CDF for a given probability

Parameters:

  • p (Numeric)

    a value within [0, 1]

Returns:

  • (Numeric)

    Inverse CDF for valid p

Raises:

  • (RangeError)

    if p > 1 or p < 0

See Also:



57
58
59
60
# File 'lib/statistical/distribution/uniform.rb', line 57

def quantile(p)
  raise RangeError, "`p` must be in [0, 1], found: #{p}" if p < 0 || p > 1
  return @lower + p * (@upper - @lower)
end

#varianceFloat

Returns the expected value of variance for the calling instance.

Returns:

  • (Float)

    Variance of the distribution



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

def variance
  return ((@upper - @lower)**2) / 12.0
end