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
51
# File 'lib/mathpack/statistics.rb', line 44

def central_moment(power)
  central_moment = 0.0
  m = mean
  @data_set.each_index do |i|
    central_moment += @frequency[i] * (@data_set[i] - m)**power
  end
  central_moment / number
end

#empirical_cdf(x) ⇒ Object



53
54
55
56
57
# File 'lib/mathpack/statistics.rb', line 53

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

#empirical_pdf(x) ⇒ Object



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

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

#kurtosisObject



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

def 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
  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


59
60
61
62
63
64
# File 'lib/mathpack/statistics.rb', line 59

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


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

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.each_index do |i|
    raw_moment += @frequency[i] * @data_set[i]**power
  end
  raw_moment / number
end

#skewnessObject



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

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

#trend(params = {}) ⇒ Object



80
81
82
83
84
# File 'lib/mathpack/statistics.rb', line 80

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
  central_moment(2)
end