Class: Mathpack::Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/statistics.rb

Instance Method Summary collapse

Constructor Details

#initialize(series) ⇒ Statistics

Returns a new instance of Statistics.



3
4
5
# File 'lib/statistics.rb', line 3

def initialize(series)
  @series = series
end

Instance Method Details

#central_moment(power) ⇒ Object



41
42
43
44
45
46
# File 'lib/statistics.rb', line 41

def central_moment(power)
  central_moment = 0.0
  m = mean
  @series.each{ |x| central_moment += (x - m)**power }
  central_moment / number
end

#empirical_cdf(x) ⇒ Object



48
49
50
51
52
# File 'lib/statistics.rb', line 48

def empirical_cdf(x)
  result = 0.0
  @series.each{ |val| result += e(x - val) }
  result / number
end

#empirical_pdf(x) ⇒ Object



66
67
68
69
70
71
# File 'lib/statistics.rb', line 66

def empirical_pdf(x)
  h = variance**0.5 * number**(-1.0/6)
  result = 0.0
  @series.each{ |val| result += (e(x - val + h) - e(x - val - h))/(2*h) }
  result / number
end

#kurtosisObject



23
24
25
# File 'lib/statistics.rb', line 23

def kurtosis
  central_moment(4) / variance**2
end

#maxObject



27
28
29
# File 'lib/statistics.rb', line 27

def max
  @series.max
end

#meanObject



11
12
13
# File 'lib/statistics.rb', line 11

def mean
  raw_moment(1)
end

#minObject



31
32
33
# File 'lib/statistics.rb', line 31

def min
  @series.min
end

#numberObject



7
8
9
# File 'lib/statistics.rb', line 7

def number
  @series.length
end


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/statistics.rb', line 54

def print_empirical_cdf_to_csv(filename)
  step = 0.25
  val = min - step
  File.open(filename, 'w+') do |file|
    while val <= max + step do
      file.write("#{val};")
      file.write("#{empirical_cdf(val)}\n")
      val += step
    end
  end
end


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/statistics.rb', line 73

def print_empirical_pdf_to_csv(filename)
  step = 0.25
  val = min - 2*step
  File.open(filename, 'w+') do |file|
    while val <= max + 2*step do
      file.write("#{val};")
      file.write("#{empirical_pdf(val)}\n")
      val += step
    end
  end
end

#raw_moment(power) ⇒ Object



35
36
37
38
39
# File 'lib/statistics.rb', line 35

def raw_moment(power)
  raw_moment = 0.0
  @series.each{ |x| raw_moment += x**power }
  raw_moment / number
end

#skewnessObject



19
20
21
# File 'lib/statistics.rb', line 19

def skewness
  central_moment(3) / variance**1.5
end

#varianceObject



15
16
17
# File 'lib/statistics.rb', line 15

def variance
  central_moment(2)
end