rstat 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/.rspec +3 -0
  2. data/CHANGELOG.md +8 -0
  3. data/Gemfile +1 -1
  4. data/README.md +88 -6
  5. data/Rakefile +4 -4
  6. data/lib/rstat.rb +3 -2
  7. data/lib/rstat/core_ext.rb +1 -1
  8. data/lib/rstat/core_ext/array.rb +1 -1
  9. data/lib/rstat/core_ext/array/descriptive_statistics.rb +1 -1
  10. data/lib/rstat/core_ext/array/descriptive_statistics/dispersion.rb +1 -1
  11. data/lib/rstat/core_ext/array/descriptive_statistics/dispersion/percentile.rb +2 -2
  12. data/lib/rstat/core_ext/array/descriptive_statistics/dispersion/range.rb +1 -1
  13. data/lib/rstat/core_ext/array/descriptive_statistics/index_of_dispersion.rb +1 -1
  14. data/lib/rstat/core_ext/array/descriptive_statistics/location.rb +1 -1
  15. data/lib/rstat/core_ext/array/descriptive_statistics/shape.rb +1 -1
  16. data/lib/rstat/core_ext/array/descriptive_statistics/shape/kurtosis.rb +1 -1
  17. data/lib/rstat/core_ext/array/descriptive_statistics/shape/moment.rb +3 -3
  18. data/lib/rstat/core_ext/array/descriptive_statistics/shape/skewness.rb +1 -1
  19. data/lib/rstat/core_ext/array/descriptive_statistics/shape/variance.rb +1 -1
  20. data/lib/rstat/core_ext/array/product.rb +1 -1
  21. data/lib/rstat/core_ext/array/sum.rb +1 -1
  22. data/lib/rstat/core_ext/fixnum.rb +1 -1
  23. data/lib/rstat/core_ext/fixnum/binomial_coefficient.rb +2 -2
  24. data/lib/rstat/regression_analysis.rb +3 -0
  25. data/lib/rstat/regression_analysis/linear_regression.rb +3 -0
  26. data/lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb +43 -0
  27. data/lib/rstat/regression_analysis/sample_correlation_coefficient.rb +20 -0
  28. data/lib/rstat/version.rb +1 -1
  29. data/rstat.gemspec +9 -9
  30. data/spec/rstat/array_spec.rb +59 -59
  31. data/spec/rstat/fixnum_spec.rb +3 -3
  32. data/spec/rstat/regession_analysis_spec.rb +38 -0
  33. data/spec/spec_helper.rb +1 -1
  34. metadata +10 -3
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --tty
2
+ --colour
3
+ --format p
@@ -1,7 +1,15 @@
1
+ ## v0.1.1
2
+
3
+ * Made rstat.rb load all files automatically instead of listing them manually.
4
+ * Updated readme.
5
+ * Added sample correlation coefficient.
6
+ * Added simple linear regression.
7
+
1
8
  ## v0.1.0
2
9
 
3
10
  * Major reorganization! All files now in directories named after their corresponding discipline (as sort of defined by Wikipedia).
4
11
  * Added skewness.
12
+ * Added kurtosis.
5
13
  * Mean methods now now use the sum method.
6
14
  * Minor reorganization of tests.
7
15
  * Changed Gemfile source to https.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rstat.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -1,12 +1,94 @@
1
- ## Rstat
1
+ # Rstat
2
2
 
3
- A very simple gem that adds some statistics methods to the core Array class.
3
+ A very simple statistics gem.
4
4
 
5
- Maybe in the future it will be something more.
5
+ ## Installation
6
6
 
7
- Currently adds:
7
+ $ gem install rstat
8
8
 
9
- * Mean
9
+ ## Usage
10
+
11
+ ```ruby
12
+ require 'rstat'
13
+ => true
14
+ [1, 2, 3, 4, 5].mean
15
+ => 3.0
16
+ ```
17
+
18
+ ## Currently Adds
19
+
20
+ ### Arithmetic
21
+
22
+ * Sum
23
+ * Product
24
+ * Binomial Coefficient
25
+
26
+ ### Descriptive Statistics
27
+
28
+ #### Location
29
+
30
+ * Arithmetic Mean
31
+ * Geometric Mean
32
+ * Harmonic Mean
33
+ * Power Mean
10
34
  * Median
11
35
  * Mode
12
- * Variance / Standard Deviation
36
+
37
+ #### Dispersion
38
+
39
+ * Range
40
+ * Standard Deviation
41
+ * Coefficient of Variation
42
+ * Percentile
43
+ * Interquartile Range
44
+
45
+ #### Shape
46
+
47
+ * Variance
48
+ * Skewness
49
+ * Kurtosis
50
+ * Central Moments
51
+ * Raw Moments
52
+
53
+ #### Count Data
54
+
55
+ * Index of Dispersion
56
+
57
+ ### Regression Analysis
58
+
59
+ #### Linear Regression
60
+
61
+ * Simple Linear Regression
62
+
63
+ ## Running Tests
64
+
65
+ Rstat depends upon RSpec for its tests.
66
+
67
+ To run all the tests for the gem:
68
+
69
+ $ rspec spec
70
+
71
+ To run one particular spec file:
72
+
73
+ $ rspec spec/rstat/array_spec.rb
74
+
75
+ To run one particular line of a spec file:
76
+
77
+ $ rspec spec/rstat/array_spec.rb:177
78
+
79
+ Rstat has been tested with Ruby 1.8.7 and 1.9.3. There's little reason it shouldn't work with other versions of Ruby though.
80
+
81
+ ## Console
82
+
83
+ Rstat includes a rake task to open up an irb session with the gem source preloaded. This is particularly useful if you're working with a local development copy of the gem's source and want to experiment with your changes. To use the preloaded console:
84
+
85
+ $ rake console
86
+
87
+ ## Contributing
88
+
89
+ If you feel like you can add something useful to Rstat then don't hesitate to send a pull request.
90
+
91
+
92
+ ## A Note of Warning
93
+
94
+ This gem extends the core Array class and Fixnum class. In isolation this is usually pretty harmless. But, in combination with other gems that do the same, unpredictable behavior may result. As always, use caution, and be aware of what this gem and any others you use actually do before including it in an important project.
data/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
6
  task :default => :spec
7
7
 
8
- desc "Open an irb session preloaded with this library"
8
+ desc 'Open an irb session preloaded with this library'
9
9
  task :console do
10
- sh "irb -rubygems -I lib -r rstat.rb"
10
+ sh 'irb -rubygems -I lib -r rstat.rb'
11
11
  end
@@ -1,5 +1,6 @@
1
- require "rstat/version"
2
- require "rstat/core_ext"
1
+ Dir["#{File.dirname(__FILE__)}/rstat/*.rb"].sort.each do |path|
2
+ require "rstat/#{File.basename(path, '.rb')}"
3
+ end
3
4
 
4
5
  module Rstat
5
6
  end
@@ -1,3 +1,3 @@
1
1
  Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].sort.each do |path|
2
- require "rstat/core_ext/#{File.basename(path, ".rb")}"
2
+ require "rstat/core_ext/#{File.basename(path, '.rb')}"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  Dir["#{File.dirname(__FILE__)}/array/*.rb"].sort.each do |path|
2
- require "rstat/core_ext/array/#{File.basename(path, ".rb")}"
2
+ require "rstat/core_ext/array/#{File.basename(path, '.rb')}"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  Dir["#{File.dirname(__FILE__)}/descriptive_statistics/*.rb"].sort.each do |path|
2
- require "rstat/core_ext/array/descriptive_statistics/#{File.basename(path, ".rb")}"
2
+ require "rstat/core_ext/array/descriptive_statistics/#{File.basename(path, '.rb')}"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  Dir["#{File.dirname(__FILE__)}/dispersion/*.rb"].sort.each do |path|
2
- require "rstat/core_ext/array/descriptive_statistics/dispersion/#{File.basename(path, ".rb")}"
2
+ require "rstat/core_ext/array/descriptive_statistics/dispersion/#{File.basename(path, '.rb')}"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  class Array
2
- def percentile p
2
+ def percentile(p)
3
3
  if p < 0 || p > 100
4
4
  nil
5
5
  else
@@ -22,4 +22,4 @@ class Array
22
22
  def middle_fifty
23
23
  self.interquartile_range
24
24
  end
25
- end
25
+ end
@@ -2,4 +2,4 @@ class Array
2
2
  def range
3
3
  self.max - self.min
4
4
  end
5
- end
5
+ end
@@ -22,4 +22,4 @@ class Array
22
22
  def fano_factor
23
23
  self.index_of_dispersion
24
24
  end
25
- end
25
+ end
@@ -1,3 +1,3 @@
1
1
  Dir["#{File.dirname(__FILE__)}/location/*.rb"].sort.each do |path|
2
- require "rstat/core_ext/array/descriptive_statistics/location/#{File.basename(path, ".rb")}"
2
+ require "rstat/core_ext/array/descriptive_statistics/location/#{File.basename(path, '.rb')}"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  Dir["#{File.dirname(__FILE__)}/shape/*.rb"].sort.each do |path|
2
- require "rstat/core_ext/array/descriptive_statistics/shape/#{File.basename(path, ".rb")}"
2
+ require "rstat/core_ext/array/descriptive_statistics/shape/#{File.basename(path, '.rb')}"
3
3
  end
@@ -2,4 +2,4 @@ class Array
2
2
  def kurtosis
3
3
  (self.central_moment(4) / self.variance ** 2) - 3
4
4
  end
5
- end
5
+ end
@@ -1,5 +1,5 @@
1
1
  class Array
2
- def central_moment k
2
+ def central_moment(k)
3
3
  if k < 0
4
4
  nil
5
5
  else
@@ -9,7 +9,7 @@ class Array
9
9
  end
10
10
  end
11
11
 
12
- def raw_moment k
12
+ def raw_moment(k)
13
13
  if k < 0
14
14
  nil
15
15
  else
@@ -20,4 +20,4 @@ class Array
20
20
  # def l_moment k
21
21
  #
22
22
  # end
23
- end
23
+ end
@@ -2,4 +2,4 @@ class Array
2
2
  def skewness
3
3
  self.central_moment(3) / (self.variance ** 1.5)
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ class Array
2
2
  def variance
3
3
  self.central_moment 2
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ class Array
2
2
  def product
3
3
  self.inject(:*)
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ class Array
2
2
  def sum
3
3
  self.inject(:+)
4
4
  end
5
- end
5
+ end
@@ -1,3 +1,3 @@
1
1
  Dir["#{File.dirname(__FILE__)}/fixnum/*.rb"].sort.each do |path|
2
- require "rstat/core_ext/fixnum/#{File.basename(path, ".rb")}"
2
+ require "rstat/core_ext/fixnum/#{File.basename(path, '.rb')}"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  class Fixnum
2
- def binomial_coefficient bottom
2
+ def binomial_coefficient(bottom)
3
3
  (self - bottom + 1 .. self).inject(1, &:*) / (2 .. bottom).inject(1, &:*)
4
4
  end
5
- end
5
+ end
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/regression_analysis/*.rb"].sort.each do |path|
2
+ require "rstat/regression_analysis/#{File.basename(path, '.rb')}"
3
+ end
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/linear_regression/*.rb"].sort.each do |path|
2
+ require "rstat/regression_analysis/linear_regression/#{File.basename(path, '.rb')}"
3
+ end
@@ -0,0 +1,43 @@
1
+ module Rstat
2
+ def self.simple_linear_regression(x, y)
3
+ if x.length != y.length
4
+ return nil
5
+ end
6
+
7
+ n = x.length
8
+
9
+ xy = x.zip(y).map { |a| a[0] * a[1] }
10
+
11
+ xx = x.map { |a| a * a }
12
+
13
+ sumx = x.sum
14
+
15
+ sumy = y.sum
16
+
17
+ sumxy = xy.sum
18
+
19
+ sumxx = xx.sum
20
+
21
+ slope = ((n * sumxy) - (sumx * sumy)) / ((n * sumxx) - (sumx ** 2))
22
+
23
+ intercept = (sumy - (slope * sumx)) / n
24
+
25
+ { :slope => slope, :intercept => intercept }
26
+ end
27
+
28
+ def self.simple_linear_regression_slope(x, y)
29
+ if x.length != y.length
30
+ return nil
31
+ end
32
+
33
+ Rstat.simple_linear_regression(x, y)[:slope]
34
+ end
35
+
36
+ def self.simple_linear_regression_intercept(x, y)
37
+ if x.length != y.length
38
+ return nil
39
+ end
40
+
41
+ Rstat.simple_linear_regression(x, y)[:intercept]
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ module Rstat
2
+ def self.sample_correlation_coefficient(x, y)
3
+ if x.length != y.length
4
+ return nil
5
+ end
6
+
7
+ xm = x.mean
8
+ ym = y.mean
9
+
10
+ top = x.zip(y).map { |a, b| (a - xm) * (b - ym) }.inject(:+)
11
+
12
+ bottom = Math.sqrt(x.map { |a| (a - xm) ** 2 }.inject(:+)) * Math.sqrt(y.map { |b| (b - ym) ** 2 }.inject(:+))
13
+
14
+ top / bottom
15
+ end
16
+
17
+ def self.pearsons_correlation_coefficient(x, y)
18
+ Rstat.sample_correlation_coefficient x, y
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Rstat
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
@@ -1,22 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "rstat/version"
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'rstat/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "rstat"
6
+ s.name = 'rstat'
7
7
  s.version = Rstat::VERSION
8
- s.authors = ["Sean Eshbaugh"]
9
- s.email = ["seaneshbaugh@gmail.com"]
10
- s.homepage = "http://seaneshbaugh.com/"
8
+ s.authors = ['Sean Eshbaugh']
9
+ s.email = ['seaneshbaugh@gmail.com']
10
+ s.homepage = 'https://github.com/seaneshbaugh/rstat'
11
11
  s.summary = %q{A Simple statistics gem.}
12
12
  s.description = %q{A Simple statistics gem.}
13
13
 
14
- s.rubyforge_project = "rstat"
14
+ s.rubyforge_project = 'rstat'
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
19
+ s.require_paths = ['lib']
20
20
 
21
- s.add_development_dependency "rspec"
21
+ s.add_development_dependency 'rspec'
22
22
  end
@@ -2,47 +2,47 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Rstat do
5
- describe ".sum" do
6
- it "calculates the sum of an array" do
5
+ describe '.sum' do
6
+ it 'calculates the sum of an array' do
7
7
  [1, 2, 3, 4, 5, 6].sum.should be(21)
8
8
  end
9
9
  end
10
10
 
11
- describe ".product" do
12
- it "calculates the product of an array" do
11
+ describe '.product' do
12
+ it 'calculates the product of an array' do
13
13
  [1, 2, 3, 4, 5, 6].product.should be(720)
14
14
  end
15
15
  end
16
16
 
17
- context "Location" do
18
- describe ".mean" do
19
- it "calculates the mean of an array" do
17
+ context 'Location' do
18
+ describe '.mean' do
19
+ it 'calculates the mean of an array' do
20
20
  [1, 2, 3, 4, 5].mean.should be_within(0.0001).of(3.0000)
21
21
  end
22
22
  end
23
23
 
24
- describe ".geometric_mean" do
25
- it "calculates the geometric mean of an array" do
24
+ describe '.geometric_mean' do
25
+ it 'calculates the geometric mean of an array' do
26
26
  [1, 2, 3, 4, 5].geometric_mean.should be_within(0.0001).of(2.6052)
27
27
  end
28
28
 
29
- it "calculates the geometric mean of an array with a zero element" do
29
+ it 'calculates the geometric mean of an array with a zero element' do
30
30
  [0, 2, 3, 4, 5].geometric_mean.should eql(0.0)
31
31
  end
32
32
 
33
- it "calculates the geometric mean of an array with a negative element" do
33
+ it 'calculates the geometric mean of an array with a negative element' do
34
34
  [-1, 2, 3, 4, 5].geometric_mean.real?.should be_false
35
35
  end
36
36
  end
37
37
 
38
- describe ".harmonic mean" do
39
- it "calculates the harmonic mean of an array" do
38
+ describe '.harmonic mean' do
39
+ it 'calculates the harmonic mean of an array' do
40
40
  [1, 2, 3, 4, 5].harmonic_mean.should be_within(0.0001).of(2.1897)
41
41
  end
42
42
  end
43
43
 
44
- describe ".power_mean" do
45
- it "power_mean(2) should equal quadratic_mean" do
44
+ describe '.power_mean' do
45
+ it 'power_mean(2) should equal quadratic_mean' do
46
46
  p = [1, 2, 3, 4, 5].power_mean(2)
47
47
  q = [1, 2, 3, 4, 5].quadratic_mean
48
48
 
@@ -50,7 +50,7 @@ describe Rstat do
50
50
  end
51
51
  end
52
52
 
53
- it "AM > GM > HM" do
53
+ it 'AM > GM > HM' do
54
54
  a = [1, 2, 3, 4, 5]
55
55
  am = a.arithmetric_mean
56
56
  gm = a.geometric_mean
@@ -60,142 +60,142 @@ describe Rstat do
60
60
  gm.should be > hm
61
61
  end
62
62
 
63
- describe ".median" do
64
- it "calculates the median of an array with an odd number of elements" do
63
+ describe '.median' do
64
+ it 'calculates the median of an array with an odd number of elements' do
65
65
  [2, 3, 5, 1, 4].median.should eql(3)
66
66
  end
67
67
 
68
- it "calculates the median of an array with an even number of elements" do
68
+ it 'calculates the median of an array with an even number of elements' do
69
69
  [2, 3, 5, 1, 4, 6].median.should eql(3.5)
70
70
  end
71
71
  end
72
72
 
73
- describe ".mode" do
74
- it "calculates the mode of an empty array" do
73
+ describe '.mode' do
74
+ it 'calculates the mode of an empty array' do
75
75
  [].mode.should eql([])
76
76
  end
77
77
 
78
- it "calculates the mode of an array with one element" do
78
+ it 'calculates the mode of an array with one element' do
79
79
  [100].mode.should eql([100])
80
80
  end
81
81
 
82
- it "calculates the mode of an unimodal array" do
82
+ it 'calculates the mode of an unimodal array' do
83
83
  [1, 1, 1, 2, 3, 4, 5, 5, 6].mode.should eql([1])
84
84
  end
85
85
 
86
- it "calculates the mode of a bimodal array" do
86
+ it 'calculates the mode of a bimodal array' do
87
87
  [44, 45, 46, 44, 46, 50].mode.should eql([44, 46])
88
88
  end
89
89
  end
90
90
  end
91
91
 
92
- context "Dispersion" do
93
- describe ".range" do
94
- it "finds the range of an array" do
92
+ context 'Dispersion' do
93
+ describe '.range' do
94
+ it 'finds the range of an array' do
95
95
  [0, 34, 656, 400, 1000].range.should be(1000)
96
96
  end
97
97
  end
98
98
 
99
- describe ".standard_deviation" do
100
- it "calculates the standard deviation of an array" do
99
+ describe '.standard_deviation' do
100
+ it 'calculates the standard deviation of an array' do
101
101
  [1, 2, 3, 4, 5, 6].standard_deviation.should be_within(0.00001).of(1.70783)
102
102
  end
103
103
  end
104
104
 
105
- describe ".coefficient_of_variation" do
106
- it "calculates the coefficient of variation of an array" do
105
+ describe '.coefficient_of_variation' do
106
+ it 'calculates the coefficient of variation of an array' do
107
107
  [1, 2, 3, 4, 5, 6].coefficient_of_variation.should be_within(0.00001).of(0.48795)
108
108
  end
109
109
  end
110
110
 
111
- describe ".percentile" do
112
- it "finds the 30th percentile of an array" do
111
+ describe '.percentile' do
112
+ it 'finds the 30th percentile of an array' do
113
113
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].percentile(30).should be(3)
114
114
  end
115
115
 
116
- it "finds the 50th percentile of an array" do
116
+ it 'finds the 50th percentile of an array' do
117
117
  [1, 2, 3, 4, 5].percentile(50).should be(3)
118
118
  end
119
119
 
120
- it "finds the 10th percentile of an array" do
120
+ it 'finds the 10th percentile of an array' do
121
121
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17].percentile(10).should be(2)
122
122
  end
123
123
  end
124
124
 
125
- describe ".interquartile_range" do
126
- it "finds the interquartile range of an array" do
125
+ describe '.interquartile_range' do
126
+ it 'finds the interquartile range of an array' do
127
127
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].interquartile_range.should be(5)
128
128
  end
129
129
  end
130
130
  end
131
131
 
132
- context "Shape" do
133
- describe ".central_moment" do
134
- it "calculates the 0th central moment of an array" do
132
+ context 'Shape' do
133
+ describe '.central_moment' do
134
+ it 'calculates the 0th central moment of an array' do
135
135
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(0).should be_within(0.00001).of(1.0)
136
136
  end
137
137
 
138
- it "calculates the 1st central moment of an array" do
138
+ it 'calculates the 1st central moment of an array' do
139
139
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(1).should be_within(0.00001).of(0.0)
140
140
  end
141
141
 
142
- it "calculates the 2nd central moment of an array" do
142
+ it 'calculates the 2nd central moment of an array' do
143
143
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(2).should be_within(0.00001).of(3459.44000)
144
144
  end
145
145
 
146
- it "calculates the 3rd central moment of an array" do
146
+ it 'calculates the 3rd central moment of an array' do
147
147
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(3).should be_within(0.00001).of(321046.00800)
148
148
  end
149
149
 
150
- it "calculates the 4th central moment of an array" do
150
+ it 'calculates the 4th central moment of an array' do
151
151
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].central_moment(4).should be_within(0.00001).of(56245095.69920)
152
152
  end
153
153
  end
154
154
 
155
- describe ".raw_moment" do
156
- it "calculates the 0th central moment of an array" do
155
+ describe '.raw_moment' do
156
+ it 'calculates the 0th central moment of an array' do
157
157
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(0).should be_within(0.00001).of(1.0)
158
158
  end
159
159
 
160
- it "calculates the 1st central moment of an array" do
160
+ it 'calculates the 1st central moment of an array' do
161
161
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(1).should be_within(0.00001).of([2, 43, 5, 65, 56, 7, 85, 3, -1, 199].mean)
162
162
  end
163
163
 
164
- it "calculates the 2nd central moment of an array" do
164
+ it 'calculates the 2nd central moment of an array' do
165
165
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(2).should be_within(0.00001).of(5612.40000)
166
166
  end
167
167
 
168
- it "calculates the 3rd central moment of an array" do
168
+ it 'calculates the 3rd central moment of an array' do
169
169
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(3).should be_within(0.00001).of(902497.40000)
170
170
  end
171
171
 
172
- it "calculates the 4th central moment of an array" do
172
+ it 'calculates the 4th central moment of an array' do
173
173
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].raw_moment(4).should be_within(0.00001).of(165154687.20000)
174
174
  end
175
175
  end
176
176
 
177
- describe ".variance" do
178
- it "calculates the variance of an array" do
177
+ describe '.variance' do
178
+ it 'calculates the variance of an array' do
179
179
  [1, 2, 3, 4, 5, 6].variance.should be_within(0.00001).of(2.91667)
180
180
  end
181
181
  end
182
182
 
183
- describe ".skewness" do
184
- it "finds the skewness of an array" do
183
+ describe '.skewness' do
184
+ it 'finds the skewness of an array' do
185
185
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].skewness.should be_within(0.00001).of(1.57782)
186
186
  end
187
187
  end
188
188
 
189
- describe ".kurtosis" do
190
- it "finds the kurtosis of an array" do
189
+ describe '.kurtosis' do
190
+ it 'finds the kurtosis of an array' do
191
191
  [2, 43, 5, 65, 56, 7, 85, 3, -1, 199].kurtosis.should be_within(0.00001).of(1.69973)
192
192
  end
193
193
  end
194
194
  end
195
195
 
196
- context "Count Data" do
197
- describe ".index_of_dispersion" do
198
- it "finds the index of dispersion of an array" do
196
+ context 'Count Data' do
197
+ describe '.index_of_dispersion' do
198
+ it 'finds the index of dispersion of an array' do
199
199
  [1, 2, 3, 4, 5, 6].index_of_dispersion.should be_within(0.00001).of(0.83333)
200
200
  end
201
201
  end
@@ -2,11 +2,11 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Rstat do
5
- describe ".binomial_coefficient" do
6
- it "computes the binomial coeffecient of itself and another number" do
5
+ describe '.binomial_coefficient' do
6
+ it 'computes the binomial coeffecient of itself and another number' do
7
7
  10.binomial_coefficient(4).should eq(210)
8
8
 
9
9
  50.binomial_coefficient(34).should eq(4_923_689_695_575)
10
10
  end
11
11
  end
12
- end
12
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Rstat do
5
+ describe '.sample_correlation_coefficient' do
6
+ it 'computes the sample correlation coefficient of two arrays' do
7
+ Rstat.sample_correlation_coefficient([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]).should be_within(0.00001).of(1.0)
8
+
9
+ Rstat.sample_correlation_coefficient([1, 2, 3, 4, 5], [5, 4, 3, 8, 1]).should be_within(0.00001).of(-0.24434)
10
+
11
+ Rstat.sample_correlation_coefficient([2345, 43, 346, 45, -4234], [354, 66, 100, 443, 655]).should be_within(0.00001).of(-0.59889)
12
+ end
13
+ end
14
+
15
+ describe '.simple_linear_regression' do
16
+ it 'should return a Hash with the slope and intercept of the linear regression equation for two arrays' do
17
+ result = Rstat.simple_linear_regression([60, 61, 62, 63, 65], [3.1, 3.6, 3.8, 4, 4.1])
18
+
19
+ result.class.should eq(Hash)
20
+
21
+ result[:slope].should be_within(0.00001).of(0.18784)
22
+
23
+ result[:intercept].should be_within(0.00001).of(-7.96351)
24
+ end
25
+ end
26
+
27
+ describe '.simple_linear_regression_slope' do
28
+ it 'should return the slope of the linear regression equation for two arrays' do
29
+ Rstat.simple_linear_regression_slope([60, 61, 62, 63, 65], [3.1, 3.6, 3.8, 4, 4.1]).should be_within(0.00001).of(0.18784)
30
+ end
31
+ end
32
+
33
+ describe '.simple_linear_regression_intercept' do
34
+ it 'should return the slope of the linear regression equation for two arrays' do
35
+ Rstat.simple_linear_regression_intercept([60, 61, 62, 63, 65], [3.1, 3.6, 3.8, 4, 4.1]).should be_within(0.00001).of(-7.96351)
36
+ end
37
+ end
38
+ end
@@ -1,2 +1,2 @@
1
1
  # encoding: utf-8
2
- require "rstat"
2
+ require 'rstat'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rstat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-16 00:00:00.000000000 Z
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -35,6 +35,7 @@ extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
37
  - .gitignore
38
+ - .rspec
38
39
  - .rvmrc
39
40
  - CHANGELOG.md
40
41
  - Gemfile
@@ -62,12 +63,17 @@ files:
62
63
  - lib/rstat/core_ext/array/sum.rb
63
64
  - lib/rstat/core_ext/fixnum.rb
64
65
  - lib/rstat/core_ext/fixnum/binomial_coefficient.rb
66
+ - lib/rstat/regression_analysis.rb
67
+ - lib/rstat/regression_analysis/linear_regression.rb
68
+ - lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb
69
+ - lib/rstat/regression_analysis/sample_correlation_coefficient.rb
65
70
  - lib/rstat/version.rb
66
71
  - rstat.gemspec
67
72
  - spec/rstat/array_spec.rb
68
73
  - spec/rstat/fixnum_spec.rb
74
+ - spec/rstat/regession_analysis_spec.rb
69
75
  - spec/spec_helper.rb
70
- homepage: http://seaneshbaugh.com/
76
+ homepage: https://github.com/seaneshbaugh/rstat
71
77
  licenses: []
72
78
  post_install_message:
73
79
  rdoc_options: []
@@ -94,4 +100,5 @@ summary: A Simple statistics gem.
94
100
  test_files:
95
101
  - spec/rstat/array_spec.rb
96
102
  - spec/rstat/fixnum_spec.rb
103
+ - spec/rstat/regession_analysis_spec.rb
97
104
  - spec/spec_helper.rb