Class: Statistical::Distribution::UniformDiscrete
- Inherits:
-
Object
- Object
- Statistical::Distribution::UniformDiscrete
- 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
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#lower ⇒ Object
readonly
Returns the value of attribute lower.
-
#support ⇒ Array, Numeric
readonly
The support set of valid values a.
-
#upper ⇒ Object
readonly
Returns the value of attribute upper.
Instance Method Summary collapse
-
#cdf(k) ⇒ Float
Returns value of cumulative density function at a point on the real line Uses a binary search on the support array internally.
-
#initialize(elems) ⇒ UniformDiscrete
constructor
Returns a model for the discrete uniform distribution on all elements present in the given set of elemets
elems. -
#mean ⇒ Float
Returns the mean value for the calling instance.
-
#pdf(k) ⇒ Float
Returns value of probability density function at a point on the real line.
-
#quantile(p) ⇒ Numeric
(also: #p_value)
Returns value of inverse CDF for a given probability.
-
#variance ⇒ Float
Returns the expected value of population variance for the calling instance.
Constructor Details
#initialize(elems) ⇒ UniformDiscrete
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
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
#count ⇒ Object (readonly)
Returns the value of attribute count.
15 16 17 |
# File 'lib/statistical/distribution/uniform_discrete.rb', line 15 def count @count end |
#lower ⇒ Object (readonly)
Returns the value of attribute lower.
15 16 17 |
# File 'lib/statistical/distribution/uniform_discrete.rb', line 15 def lower @lower end |
#support ⇒ Array, Numeric (readonly)
The support set of valid values a
12 13 14 |
# File 'lib/statistical/distribution/uniform_discrete.rb', line 12 def support @support end |
#upper ⇒ Object (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
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.
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 |
#mean ⇒ Float
Returns the mean value for the calling instance. Calculated mean, and
not inferred from simulations
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
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.
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 |
#variance ⇒ Float
Returns the expected value of population variance for the calling
instance.
114 115 116 |
# File 'lib/statistical/distribution/uniform_discrete.rb', line 114 def variance return @support.pvariance end |