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`
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
#count ⇒ Object (readonly)
Returns the value of attribute count.
11 12 13 |
# File 'lib/statistical/distribution/uniform_discrete.rb', line 11 def count @count end |
#lower ⇒ Object (readonly)
Returns the value of attribute lower.
11 12 13 |
# File 'lib/statistical/distribution/uniform_discrete.rb', line 11 def lower @lower end |
#support ⇒ Array, Numeric (readonly)
The support set of valid values a
10 11 12 |
# File 'lib/statistical/distribution/uniform_discrete.rb', line 10 def support @support end |
#upper ⇒ Object (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
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.
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 |
#mean ⇒ Float
Returns the mean value for the calling instance. Calculated mean, and
not inferred from simulations
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
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.
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 |
#variance ⇒ Float
Returns the expected value of population variance for the calling
instance.
110 111 112 |
# File 'lib/statistical/distribution/uniform_discrete.rb', line 110 def variance return @support.variance end |