Class: Mathpack::Statistics

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

Instance Method Summary collapse

Constructor Details

#initialize(series) ⇒ Statistics

Returns a new instance of Statistics.



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

def initialize(series)
  @series = series
  @data_set, @frequency = parse_series(series)
end

Instance Method Details

#central_moment(power) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/mathpack/statistics.rb', line 44

def central_moment(power)
  central_moment = 0.0
  @data_set.zip(@frequency).each do |value, frequency|
    central_moment += frequency * (value - mean)**power
  end
  central_moment / number
end

#empirical_cdf(x) ⇒ Object



52
53
54
# File 'lib/mathpack/statistics.rb', line 52

def empirical_cdf(x)
  1.0 / number * @series.count { |value| Mathpack::Functions.heaviside(x - value) > 0 }
end

#empirical_pdf(x) ⇒ Object



63
64
65
66
# File 'lib/mathpack/statistics.rb', line 63

def empirical_pdf(x)
  h = variance**0.5 * number**(-1.0 / 6)
  1.0 / number * @series.inject(0) { |sum, value| sum + (Mathpack::Functions.heaviside(x - value + h) - Mathpack::Functions.heaviside(x - value - h)) / (2 * h) }
end

#kurtosisObject



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

def kurtosis
  @kurtosis ||= central_moment(4) / variance**2 - 3.0
end

#maxObject



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

def max
  @data_set.max
end

#meanObject



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

def mean
  @mean ||= raw_moment(1)
end

#minObject



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

def min
  @data_set.min
end

#numberObject



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

def number
  @series.length
end


56
57
58
59
60
61
# File 'lib/mathpack/statistics.rb', line 56

def print_empirical_cdf(filename)
  step = 0.5 * (max - min) / number
  nodes = Mathpack::Approximation.generate_nodes(from: min - step, to: max + step, step: step)
  values = nodes.map { |x| empirical_cdf(x) }
  Mathpack::IO.print_table_function(filename: filename, x: nodes, y: values)
end


68
69
70
71
72
73
# File 'lib/mathpack/statistics.rb', line 68

def print_empirical_pdf(filename)
  step = 0.5 * (max - min) / number
  nodes = Mathpack::Approximation.generate_nodes(from: min - 10 * step, to: max + 10 * step, step: step)
  values = nodes.map { |x| empirical_pdf(x) }
  Mathpack::IO.print_table_function(filename: filename, x: nodes, y: values)
end

#raw_moment(power) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mathpack/statistics.rb', line 36

def raw_moment(power)
  raw_moment = 0.0
  @data_set.zip(@frequency).each do |value, frequency|
    raw_moment += frequency * value**power
  end
  raw_moment / number
end

#skewnessObject



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

def skewness
  @skewness ||= central_moment(3) / variance**1.5
end

#trend(params = {}) ⇒ Object



75
76
77
78
79
# File 'lib/mathpack/statistics.rb', line 75

def trend(params = {})
  numbers = Array.new(number){ |i| i + 1 }
  polynom = Mathpack::Approximation::approximate_by_polynom(x: numbers, f: @series, polynom_power: params[:polynom_power])
  Mathpack::Approximation.print_polynom(polynom)
end

#varianceObject



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

def variance
  @variance ||= central_moment(2)
end