least_squares 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +3 -0
- data/.gitignore +23 -0
- data/LICENSE +20 -0
- data/README.md +36 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/lib/least_squares.rb +69 -0
- data/spec/least_squares_spec.rb +42 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +100 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Shane Emmons
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
Least Squares
|
2
|
+
=============
|
3
|
+
|
4
|
+
This gem adds methods to the Math module to aid in calculating the Least
|
5
|
+
Squares Regression Line given two arrays.
|
6
|
+
|
7
|
+
Example Usage:
|
8
|
+
xs = [9300, 10565, 15000, 15000, 17764, 57000, 65940, 73676, 77006, 93739, 146088, 153260,]
|
9
|
+
ys = [7100, 15500, 4400, 4400, 5900, 4600, 8800, 2000, 2750, 2550, 960, 1025,]
|
10
|
+
|
11
|
+
lsqr = Math.least_squares(xs,ys)
|
12
|
+
puts (10_000..150_000).step(10_000).map{|n| lsqr.call(n)}
|
13
|
+
|
14
|
+
Results:
|
15
|
+
7623.45990767669
|
16
|
+
7110.76951749864
|
17
|
+
6598.07912732059
|
18
|
+
6085.38873714254
|
19
|
+
5572.69834696449
|
20
|
+
5060.00795678644
|
21
|
+
4547.31756660839
|
22
|
+
4034.62717643034
|
23
|
+
3521.93678625229
|
24
|
+
3009.24639607424
|
25
|
+
2496.55600589619
|
26
|
+
1983.86561571814
|
27
|
+
1471.17522554009
|
28
|
+
958.484835362034
|
29
|
+
445.794445183983
|
30
|
+
|
31
|
+
Included methods are `#mean`, `#stdev`, `#pearson` and `#least_squares`.
|
32
|
+
|
33
|
+
Copyright
|
34
|
+
---------
|
35
|
+
|
36
|
+
Copyright (c) 2010 Shane Emmons. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
|
5
|
+
CLOBBER.include('.yardoc', 'doc')
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'jeweler'
|
9
|
+
Jeweler::Tasks.new do |gem|
|
10
|
+
gem.name = "least_squares"
|
11
|
+
gem.summary = %Q{Calulate the Least Squares Regression Line}
|
12
|
+
gem.description = %Q{This gem adds methods to the Math module to aid in calculating the Least Squares Regression Line given two arrays.}
|
13
|
+
gem.email = "semmons99@gmail.com"
|
14
|
+
gem.homepage = "http://github.com/semmons99/least_squares"
|
15
|
+
gem.authors = ["Shane Emmons"]
|
16
|
+
gem.add_development_dependency "rspec", ">= 1.3.0"
|
17
|
+
gem.add_development_dependency "yard", ">= 0.5.4"
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'spec/rake/spectask'
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
31
|
+
spec.libs << 'lib' << 'spec'
|
32
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
33
|
+
spec.rcov = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :spec => :check_dependencies
|
37
|
+
|
38
|
+
task :default => :spec
|
39
|
+
|
40
|
+
begin
|
41
|
+
require 'yard'
|
42
|
+
YARD::Rake::YardocTask.new
|
43
|
+
rescue LoadError
|
44
|
+
task :yardoc do
|
45
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
46
|
+
end
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Math
|
2
|
+
##
|
3
|
+
# Takes an array of numbers and returns the Mean.
|
4
|
+
#
|
5
|
+
# @param [Array<Numeric>] xs Array of numbers
|
6
|
+
# @return [Float] Mean
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
10
|
+
# Math.mean(xs) #=> 5.5
|
11
|
+
def Math.mean(xs)
|
12
|
+
xs.inject{|s,n| s+n} / xs.size.to_f
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Takes an array of numbers and returns the Standard Deviation.
|
17
|
+
#
|
18
|
+
# @param [Array<Numeric>] xs Array of numbers
|
19
|
+
# @return [Float] Standard Deviation
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
23
|
+
# Math.stdev(xs) #=> 3.02765035409749
|
24
|
+
def Math.stdev(xs)
|
25
|
+
mean = Math.mean(xs)
|
26
|
+
Math.sqrt((1/(xs.size.to_f - 1)) * xs.inject(0){|s,n| s + (n - mean)**2})
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Takes two arrays of numbers and returns the Pearson's Correlation
|
31
|
+
# Coefficient.
|
32
|
+
#
|
33
|
+
# @param [Array<Numeric>] xs Array of numbers
|
34
|
+
# @param [Array<numeric>] ys Array of numbers
|
35
|
+
# @return [Float] Pearson's Correlation Coefficient
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
39
|
+
# ys = [9, 1, 0, 5, 4, 7, 7, 0, 9, 3]
|
40
|
+
# Math.pearson(xs,ys) #=> 0.0581327470763432
|
41
|
+
def Math.pearson(xs, ys)
|
42
|
+
numerator = 0
|
43
|
+
xs_mean = Math.mean(xs)
|
44
|
+
ys_mean = Math.mean(ys)
|
45
|
+
(0...xs.size).each do |i|
|
46
|
+
numerator += (xs[i] - xs_mean) * (ys[i] - ys_mean)
|
47
|
+
end
|
48
|
+
numerator / (Math.sqrt(xs.inject(0){|s,x| s + (x - xs_mean) ** 2}) * Math.sqrt(ys.inject(0){|s,y| s + (y - ys_mean) ** 2}))
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# Takes two arrays of numbers and returns the Least Squares Regression Line
|
53
|
+
# as a Proc.
|
54
|
+
#
|
55
|
+
# @param [Array<Numeric>] xs Array of numbers
|
56
|
+
# @param [Array<Numeric>] ys Array of numbers
|
57
|
+
# @return [Proc] Least Squares Regression Line
|
58
|
+
#
|
59
|
+
# @example
|
60
|
+
# xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
61
|
+
# ys = [9, 1, 0, 5, 4, 7, 7, 0, 9, 3]
|
62
|
+
# ls = Math.least_squares(xs,ys)
|
63
|
+
# (1..10).map{|i| ls.call(i)} #=> [4.2, 4.2667, 4.333, 4.4, 4.4667, 4.5333, 4.6, 4.667, 4.7333, 4.8]
|
64
|
+
def Math.least_squares(xs, ys)
|
65
|
+
b = Math.pearson(xs,ys) * (Math.stdev(ys) / Math.stdev(xs))
|
66
|
+
a = Math.mean(ys) - (b * Math.mean(xs))
|
67
|
+
lambda{|x| a + (b * x)}
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "LeastSquares" do
|
4
|
+
before(:each) do
|
5
|
+
@xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
6
|
+
@ys = [9, 1, 0, 5, 4, 7, 7, 0, 9, 3]
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#mean' do
|
10
|
+
specify 'returns the mean (average) of an array of numbers' do
|
11
|
+
Math.mean(@xs).should == 5.5
|
12
|
+
Math.mean(@ys).should == 4.5
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#stdev' do
|
17
|
+
specify 'returns the standard deviation of an array of numbers' do
|
18
|
+
Math.stdev(@xs).should be_close(3.0277, 0.0001)
|
19
|
+
Math.stdev(@ys).should be_close(3.4721, 0.0001)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#pearson' do
|
24
|
+
specify 'returns the Pearson Correlation Coefficient of two arrays of numbers' do
|
25
|
+
Math.pearson(@xs,@ys).should be_close(0.0581, 0.0001)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#least_squares' do
|
30
|
+
specify 'returns the Least Squares Regression Line of two arrays of numbers as a Proc' do
|
31
|
+
Math.least_squares(@xs,@ys).should be_a(Proc)
|
32
|
+
end
|
33
|
+
|
34
|
+
specify 'return the Least Squares Regression Line of two arrays of numbers' do
|
35
|
+
rs = [4.2, 4.2667, 4.3333, 4.4, 4.4667, 4.5333, 4.6, 4.6667, 4.7333, 4.8]
|
36
|
+
ls = Math.least_squares(@xs,@ys)
|
37
|
+
(1..10).map{|i| ls.call(i)}.each_with_index do |x,i|
|
38
|
+
x.should be_close(rs[i], 0.0001)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: least_squares
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Shane Emmons
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-05 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: 1.3.0
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: yard
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 5
|
44
|
+
- 4
|
45
|
+
version: 0.5.4
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: This gem adds methods to the Math module to aid in calculating the Least Squares Regression Line given two arrays.
|
49
|
+
email: semmons99@gmail.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .gitignore
|
60
|
+
- LICENSE
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- VERSION
|
64
|
+
- lib/least_squares.rb
|
65
|
+
- spec/least_squares_spec.rb
|
66
|
+
- spec/spec.opts
|
67
|
+
- spec/spec_helper.rb
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: http://github.com/semmons99/least_squares
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options:
|
74
|
+
- --charset=UTF-8
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.3.6
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: Calulate the Least Squares Regression Line
|
98
|
+
test_files:
|
99
|
+
- spec/least_squares_spec.rb
|
100
|
+
- spec/spec_helper.rb
|