Class: Mathpack::Statistics
- Inherits:
-
Object
- Object
- Mathpack::Statistics
- Defined in:
- lib/statistics.rb
Instance Method Summary collapse
- #central_moment(power) ⇒ Object
- #empirical_cdf(x) ⇒ Object
- #empirical_pdf(x) ⇒ Object
-
#initialize(series) ⇒ Statistics
constructor
A new instance of Statistics.
- #kurtosis ⇒ Object
- #max ⇒ Object
- #mean ⇒ Object
- #min ⇒ Object
- #number ⇒ Object
- #print_empirical_cdf_to_csv(filename) ⇒ Object
- #print_empirical_pdf_to_csv(filename) ⇒ Object
- #raw_moment(power) ⇒ Object
- #skewness ⇒ Object
- #variance ⇒ Object
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 |
#kurtosis ⇒ Object
23 24 25 |
# File 'lib/statistics.rb', line 23 def kurtosis central_moment(4) / variance**2 end |
#max ⇒ Object
27 28 29 |
# File 'lib/statistics.rb', line 27 def max @series.max end |
#mean ⇒ Object
11 12 13 |
# File 'lib/statistics.rb', line 11 def mean raw_moment(1) end |
#min ⇒ Object
31 32 33 |
# File 'lib/statistics.rb', line 31 def min @series.min end |
#number ⇒ Object
7 8 9 |
# File 'lib/statistics.rb', line 7 def number @series.length end |
#print_empirical_cdf_to_csv(filename) ⇒ Object
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 |
#print_empirical_pdf_to_csv(filename) ⇒ Object
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 |
#skewness ⇒ Object
19 20 21 |
# File 'lib/statistics.rb', line 19 def skewness central_moment(3) / variance**1.5 end |
#variance ⇒ Object
15 16 17 |
# File 'lib/statistics.rb', line 15 def variance central_moment(2) end |