rstat 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +3 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +1 -1
- data/README.md +88 -6
- data/Rakefile +4 -4
- data/lib/rstat.rb +3 -2
- data/lib/rstat/core_ext.rb +1 -1
- data/lib/rstat/core_ext/array.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics/dispersion.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics/dispersion/percentile.rb +2 -2
- data/lib/rstat/core_ext/array/descriptive_statistics/dispersion/range.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics/index_of_dispersion.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics/location.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics/shape.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics/shape/kurtosis.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics/shape/moment.rb +3 -3
- data/lib/rstat/core_ext/array/descriptive_statistics/shape/skewness.rb +1 -1
- data/lib/rstat/core_ext/array/descriptive_statistics/shape/variance.rb +1 -1
- data/lib/rstat/core_ext/array/product.rb +1 -1
- data/lib/rstat/core_ext/array/sum.rb +1 -1
- data/lib/rstat/core_ext/fixnum.rb +1 -1
- data/lib/rstat/core_ext/fixnum/binomial_coefficient.rb +2 -2
- data/lib/rstat/regression_analysis.rb +3 -0
- data/lib/rstat/regression_analysis/linear_regression.rb +3 -0
- data/lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb +43 -0
- data/lib/rstat/regression_analysis/sample_correlation_coefficient.rb +20 -0
- data/lib/rstat/version.rb +1 -1
- data/rstat.gemspec +9 -9
- data/spec/rstat/array_spec.rb +59 -59
- data/spec/rstat/fixnum_spec.rb +3 -3
- data/spec/rstat/regession_analysis_spec.rb +38 -0
- data/spec/spec_helper.rb +1 -1
- metadata +10 -3
data/.rspec
ADDED
data/CHANGELOG.md
CHANGED
@@ -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
data/README.md
CHANGED
@@ -1,12 +1,94 @@
|
|
1
|
-
|
1
|
+
# Rstat
|
2
2
|
|
3
|
-
A very simple
|
3
|
+
A very simple statistics gem.
|
4
4
|
|
5
|
-
|
5
|
+
## Installation
|
6
6
|
|
7
|
-
|
7
|
+
$ gem install rstat
|
8
8
|
|
9
|
-
|
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
|
-
|
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
|
2
|
-
require
|
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
|
8
|
+
desc 'Open an irb session preloaded with this library'
|
9
9
|
task :console do
|
10
|
-
sh
|
10
|
+
sh 'irb -rubygems -I lib -r rstat.rb'
|
11
11
|
end
|
data/lib/rstat.rb
CHANGED
data/lib/rstat/core_ext.rb
CHANGED
data/lib/rstat/core_ext/array.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Array
|
2
|
-
def central_moment
|
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
|
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
|
@@ -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
|
data/lib/rstat/version.rb
CHANGED
data/rstat.gemspec
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path(
|
3
|
-
require
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'rstat/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
6
|
+
s.name = 'rstat'
|
7
7
|
s.version = Rstat::VERSION
|
8
|
-
s.authors = [
|
9
|
-
s.email = [
|
10
|
-
s.homepage =
|
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 =
|
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 = [
|
19
|
+
s.require_paths = ['lib']
|
20
20
|
|
21
|
-
s.add_development_dependency
|
21
|
+
s.add_development_dependency 'rspec'
|
22
22
|
end
|
data/spec/rstat/array_spec.rb
CHANGED
@@ -2,47 +2,47 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Rstat do
|
5
|
-
describe
|
6
|
-
it
|
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
|
12
|
-
it
|
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
|
18
|
-
describe
|
19
|
-
it
|
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
|
25
|
-
it
|
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
|
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
|
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
|
39
|
-
it
|
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
|
45
|
-
it
|
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
|
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
|
64
|
-
it
|
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
|
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
|
74
|
-
it
|
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
|
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
|
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
|
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
|
93
|
-
describe
|
94
|
-
it
|
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
|
100
|
-
it
|
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
|
106
|
-
it
|
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
|
112
|
-
it
|
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
|
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
|
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
|
126
|
-
it
|
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
|
133
|
-
describe
|
134
|
-
it
|
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
|
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
|
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
|
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
|
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
|
156
|
-
it
|
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
|
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
|
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
|
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
|
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
|
178
|
-
it
|
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
|
184
|
-
it
|
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
|
190
|
-
it
|
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
|
197
|
-
describe
|
198
|
-
it
|
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
|
data/spec/rstat/fixnum_spec.rb
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Rstat do
|
5
|
-
describe
|
6
|
-
it
|
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
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
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.
|
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:
|
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:
|
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
|