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

Author:

  • Vaibhav Yenamandra

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/statistical/distribution/uniform_discrete.rb', line 23

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.



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

def count
  @count
end

#lowerObject (readonly)

Returns the value of attribute lower.



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

def lower
  @lower
end

#supportArray, Numeric (readonly)

The support set of valid values a

Returns:

  • (Array, Numeric)

    the current value of support



10
11
12
# File 'lib/statistical/distribution/uniform_discrete.rb', line 10

def support
  @support
end

#upperObject (readonly)

Returns the value of attribute upper.



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

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/statistical/distribution/uniform_discrete.rb', line 63

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



102
103
104
# File 'lib/statistical/distribution/uniform_discrete.rb', line 102

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



48
49
50
51
# File 'lib/statistical/distribution/uniform_discrete.rb', line 48

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:



91
92
93
94
95
96
# File 'lib/statistical/distribution/uniform_discrete.rb', line 91

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



110
111
112
# File 'lib/statistical/distribution/uniform_discrete.rb', line 110

def variance
  return @support.variance
end