Module: RandomVariable::Samples

Defined in:
lib/samples.rb

Overview

# File: samples.rb #

#

#

Author: Jorge F.M. Rinaldi # Contact: [email protected] #

#

#

Date: 2012/12/02 #

#

Instance Method Summary collapse

Instance Method Details

#iterate_samplesObject



17
18
19
20
21
22
23
# File 'lib/samples.rb', line 17

def iterate_samples
  self.each_with_index do |sample, i|
    if block_given? then
      yield(sample, i)
    end 
  end
end

#maxObject



25
26
27
28
29
30
31
# File 'lib/samples.rb', line 25

def max
  cur_max = self[0]
  self.iterate_samples do |sample|
    cur_max = sample if sample > cur_max
  end
  cur_max
end

#meanObject



41
42
43
44
45
46
47
48
# File 'lib/samples.rb', line 41

def mean
  return nil if 0 == self.size
  acc = 0.0
  self.iterate_samples do |sample|
    acc += sample
  end
  acc / self.size
end

#medianObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/samples.rb', line 50

def median
  return nil if 0 == self.size
  return self[0] if 1 == self.size
  ary = self.sort
  i = ary.len / 2
  if ary.len.even? then
    return (ary[i] + ary[i-1]) / 2.0
  else
    return ary[i]
  end
end

#minObject



33
34
35
36
37
38
39
# File 'lib/samples.rb', line 33

def min
  cur_min = self[0]
  self.iterate_samples do |sample|
    cur_min = sample if sample < cur_min
  end
  cur_min
end