Class: Statistical::Distribution::UniformDiscrete

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

Overview

This class abstracts the discrete uniform distribution over a given set

of elements

random variate from the distribution can take. Must have at least 1 value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elems) ⇒ UniformDiscrete

Note:

The constructor sorts the array of elements given to it, as this is a key assumption of the discrete uniform distribution. This set must also be homogenous

Returns a model for the discrete uniform distribution on all elements present in the given set of elemets elems

Parameters:

  • elems (Array)

    The elements over which the distribution exists in [lower, upper]

Raises:

  • (RangeError)

    if elems isn’t one of Array, Range, Fixnum or Bignum



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/statistical/distribution/uniform_discrete.rb', line 27

def initialize(elems)
  case elems
  when Fixnum, Bignum
    @support = [elems]
  when Array
    @support = elems.sort
  when Range
    @support = elems.to_a
  else
    raise ArgumentError,
          "Expected Fixnum, Bignum, Array or Range, found #{elems.class}"
  end
  @count = @support.length
  @lower = @support[0]
  @upper = @support[-1]
  self
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



15
16
17
# File 'lib/statistical/distribution/uniform_discrete.rb', line 15

def count
  @count
end

#lowerObject (readonly)

Returns the value of attribute lower.



15
16
17
# File 'lib/statistical/distribution/uniform_discrete.rb', line 15

def lower
  @lower
end

#supportArray, Numeric (readonly)

The support set of valid values a

Returns:

  • (Array, Numeric)

    the current value of support



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

def support
  @support
end

#upperObject (readonly)

Returns the value of attribute upper.



15
16
17
# File 'lib/statistical/distribution/uniform_discrete.rb', line 15

def upper
  @upper
end

Instance Method Details

#cdf(k) ⇒ Float

Note:

This suffers from some floating point comparison issues. Errors start appearing when dealing with precision > 1E-18

Returns value of cumulative density function at a point on the real line Uses a binary search on the support array internally.

Parameters:

  • k (Fixnum, Bignum)

    Point at which cdf value is desired

Returns:

  • (Float)

    0 if k is on the left of the support, 1 if k on the right support and the evaluates CDF for any other legal value



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/statistical/distribution/uniform_discrete.rb', line 67

def cdf(k)
  return 0.0 if k < @lower
  return 1.0 if k >= @upper

  # Ruby has a Array#bsearch_index already but it supports find-min mode
  # What we need is a find-max mode. This can be achieved by reversing
  # and then searching, but reverse is O(N) so it defeats the purpose
  low = 0
  high = @count - 1
  while low < high
    mid = (low + high) / 2
    if @support[mid] <= k
      low = mid + 1
    else
      high = mid
    end
  end
  # This should be true for all i > low
  return low.fdiv(@count)
end

#meanFloat

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

not inferred from simulations

Returns:

  • (Float)

    Mean of the distribution



106
107
108
# File 'lib/statistical/distribution/uniform_discrete.rb', line 106

def mean
  return @support.mean
end

#pdf(k) ⇒ Float

Returns value of probability density function at a point on the real

line

Parameters:

  • k (Fixnum, Bignum)

    Point at which pdf is desired

Returns:

  • (Float)

    0 if k doesn’t belong to the elements over which the current instance is distributed. 1/n otherwise where n is number of elements



52
53
54
55
# File 'lib/statistical/distribution/uniform_discrete.rb', line 52

def pdf(k)
  return 1.0 / @count if @support.include?(k)
  return 0.0
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)

    Returns inverse CDF for valid p

Raises:

  • (RangeError)

    if p > 1 or p < 0

See Also:



95
96
97
98
99
100
# File 'lib/statistical/distribution/uniform_discrete.rb', line 95

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

#varianceFloat

Returns the expected value of population variance for the calling

instance.

Returns:

  • (Float)

    Variance of the distribution



114
115
116
# File 'lib/statistical/distribution/uniform_discrete.rb', line 114

def variance
  return @support.pvariance
end