Class: RubyStatistics::Distribution::Uniform

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-statistics/distribution/uniform.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Uniform

Returns a new instance of Uniform.



6
7
8
9
# File 'lib/ruby-statistics/distribution/uniform.rb', line 6

def initialize(a, b)
  self.left = a.to_r
  self.right = b.to_r
end

Instance Attribute Details

#leftObject

Returns the value of attribute left.



4
5
6
# File 'lib/ruby-statistics/distribution/uniform.rb', line 4

def left
  @left
end

#rightObject

Returns the value of attribute right.



4
5
6
# File 'lib/ruby-statistics/distribution/uniform.rb', line 4

def right
  @right
end

Instance Method Details

#cumulative_function(value) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/ruby-statistics/distribution/uniform.rb', line 19

def cumulative_function(value)
  if value < left
    0
  elsif value >= left && value <= right
    (value - left)/(right - left)
  else
    1
  end
end

#density_function(value) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ruby-statistics/distribution/uniform.rb', line 11

def density_function(value)
  if value >= left && value <= right
    1/(right - left)
  else
    0
  end
end

#meanObject Also known as: median



29
30
31
# File 'lib/ruby-statistics/distribution/uniform.rb', line 29

def mean
  (1/2.0) * ( left + right )
end

#varianceObject



35
36
37
# File 'lib/ruby-statistics/distribution/uniform.rb', line 35

def variance
  (1/12.0) * ( right - left ) ** 2
end