class Mathpack::Statistics
Public Class Methods
new(series)
click to toggle source
# File lib/mathpack/statistics.rb, line 3 def initialize(series) @series = series @data_set, @frequency = parse_series(series) end
Public Instance Methods
central_moment(power)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
kurtosis()
click to toggle source
# File lib/mathpack/statistics.rb, line 24 def kurtosis @kurtosis ||= central_moment(4) / variance**2 - 3.0 end
max()
click to toggle source
# File lib/mathpack/statistics.rb, line 28 def max @data_set.max end
mean()
click to toggle source
# File lib/mathpack/statistics.rb, line 12 def mean @mean ||= raw_moment(1) end
min()
click to toggle source
# File lib/mathpack/statistics.rb, line 32 def min @data_set.min end
number()
click to toggle source
# File lib/mathpack/statistics.rb, line 8 def number @series.length end
print_empirical_cdf(filename)
click to toggle source
# 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
print_empirical_pdf(filename)
click to toggle source
# 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)
click to toggle source
# 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
skewness()
click to toggle source
# File lib/mathpack/statistics.rb, line 20 def skewness @skewness ||= central_moment(3) / variance**1.5 end
trend(params = {})
click to toggle source
# 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
variance()
click to toggle source
# File lib/mathpack/statistics.rb, line 16 def variance @variance ||= central_moment(2) end
Private Instance Methods
parse_series(series)
click to toggle source
# File lib/mathpack/statistics.rb, line 83 def parse_series(series) data_set = [] frequency = [] series.each do |element| if data_set.include? element frequency[data_set.index(element)] += 1 else data_set << element frequency[data_set.index(element)] = 1 end end [data_set, frequency] end