Class: Statistical::Distribution::Uniform
- Inherits:
-
Object
- Object
- Statistical::Distribution::Uniform
- Defined in:
- lib/statistical/distribution/uniform.rb
Overview
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
Instance Attribute Summary collapse
-
#lower ⇒ Numeric
readonly
The lower bound of the uniform distribution.
-
#support ⇒ Object
readonly
Returns the value of attribute support.
-
#upper ⇒ Numeric
readonly
The upper bound of the uniform distrbution.
Instance Method Summary collapse
-
#cdf(x) ⇒ Float
Returns value of cumulative density function at a point.
-
#initialize(start = 0.0, finish = 1.0) ⇒ Object
constructor
Returns a new ‘Statistical::Distribution::Uniform` instance.
-
#mean ⇒ Float
Returns the expected value of mean value for the calling instance.
-
#pdf(x) ⇒ Float
Returns value of probability density function at a point.
-
#quantile(p) ⇒ Numeric
(also: #p_value)
Returns value of inverse CDF for a given probability.
-
#variance ⇒ Float
Returns the expected value of variance for the calling instance.
Constructor Details
#initialize(start = 0.0, finish = 1.0) ⇒ Object
if given lower > upper, it swaps them internally
Returns a new ‘Statistical::Distribution::Uniform` instance
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
#lower ⇒ Numeric (readonly)
The lower bound of the uniform distribution. Defaults to 0.0.
18 19 20 |
# File 'lib/statistical/distribution/uniform.rb', line 18 def lower @lower end |
#support ⇒ Object (readonly)
Returns the value of attribute support.
19 20 21 |
# File 'lib/statistical/distribution/uniform.rb', line 19 def support @support end |
#upper ⇒ Numeric (readonly)
The upper bound of the uniform distrbution. Defaults to 1.0.
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
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 |
#mean ⇒ Float
Returns the expected value of mean value for the calling instance.
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
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
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 |
#variance ⇒ Float
Returns the expected value of variance for the calling instance.
74 75 76 |
# File 'lib/statistical/distribution/uniform.rb', line 74 def variance return ((@upper - @lower)**2) / 12.0 end |