linear-regression 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/lib/linear-regression.rb +3 -86
- data/lib/linear-regression/base.rb +46 -0
- data/lib/linear-regression/correlation_coefficient.rb +34 -0
- data/lib/linear-regression/linear.rb +28 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60dea3f33c89cc9e8c6fa2d5c2a4a0f3672c58d5
|
4
|
+
data.tar.gz: 6b6d65500b239519bba094b02c6eb9b87aec00fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39e673708c0d3b295012b7d1ce43615213a71439addd55133ea884a3c8f8c713b4da5fb1ce628d3f155e964ccbac2ef0a5fcb1d6a5cf0ea6dc7e810eeb325d99
|
7
|
+
data.tar.gz: 2bc0bcc477a7ab3d859586c9aece09eb615af565864590e0ccfa1ad78820f926208180562df4158a2259f1e24af059862b35f6a278f8c34c70099826afb942e1
|
data/lib/linear-regression.rb
CHANGED
@@ -1,86 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
# http://en.wikipedia.org/wiki/Expected_value
|
5
|
-
def ev(items)
|
6
|
-
raise "Items must be an array" unless items.is_a?(Array)
|
7
|
-
|
8
|
-
items.inject(0) {|sum, x| sum + x}.to_f / items.length
|
9
|
-
end
|
10
|
-
|
11
|
-
# Covariance
|
12
|
-
# http://en.wikipedia.org/wiki/Covariance
|
13
|
-
def cov(xs, ys)
|
14
|
-
raise "Length xs and ys must be equal" unless xs.length == ys.length
|
15
|
-
|
16
|
-
xys = (0..(xs.length-1)).map{|i| xs[i] * ys[i]}
|
17
|
-
ev(xys) - ev(xs)*ev(ys)
|
18
|
-
end
|
19
|
-
|
20
|
-
# Another way to implement covariance
|
21
|
-
def cov2(xs, ys)
|
22
|
-
raise "Length xs and ys must be equal" unless xs.length == ys.length
|
23
|
-
|
24
|
-
len = xs.length
|
25
|
-
sum = 0
|
26
|
-
ev_x = ev(xs)
|
27
|
-
ev_y = ev(ys)
|
28
|
-
|
29
|
-
0.upto(len - 1) do |i|
|
30
|
-
sum += (xs[i].to_f - ev_x) * (ys[i].to_f - ev_y)
|
31
|
-
end
|
32
|
-
sum / len
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
# Variance
|
37
|
-
# http://en.wikipedia.org/wiki/Variance
|
38
|
-
def var(items)
|
39
|
-
raise "Items must be an array" unless items.is_a?(Array)
|
40
|
-
|
41
|
-
ev(items.map{|i| i**2}) - ev(items)**2
|
42
|
-
end
|
43
|
-
|
44
|
-
# Standard Deviation
|
45
|
-
# http://en.wikipedia.org/wiki/Standard_deviation
|
46
|
-
def stdev(items)
|
47
|
-
raise "Items must be an array" unless items.is_a?(Array)
|
48
|
-
|
49
|
-
Math.sqrt(var(items))
|
50
|
-
end
|
51
|
-
|
52
|
-
# Pearson product-moment correlation coefficient
|
53
|
-
# http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
|
54
|
-
def pcc(xs, ys)
|
55
|
-
raise "Length xs and ys must be equal" unless xs.length == ys.length
|
56
|
-
|
57
|
-
cov(xs, ys) / (stdev(xs).to_f * stdev(ys).to_f)
|
58
|
-
end
|
59
|
-
|
60
|
-
# Spearman's rank correlation coefficient
|
61
|
-
# http://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient
|
62
|
-
def scc(xs, ys)
|
63
|
-
raise "Length xs and ys must be equal" unless xs.length == ys.length
|
64
|
-
|
65
|
-
len = xs.length
|
66
|
-
sum = 0.0
|
67
|
-
(0..(len-1)).each do |i|
|
68
|
-
sum += (xs[i] - ys[i]) ** 2
|
69
|
-
end
|
70
|
-
|
71
|
-
1 - (6 * sum)/(len * (len - 1))
|
72
|
-
end
|
73
|
-
|
74
|
-
# y = kx + b
|
75
|
-
def k(xs, ys)
|
76
|
-
raise "Length xs and ys must be equal" unless xs.length == ys.length
|
77
|
-
|
78
|
-
cov(xs, ys) / var(xs)
|
79
|
-
end
|
80
|
-
|
81
|
-
# y = kx + b
|
82
|
-
def b(xs, ys)
|
83
|
-
ev(ys) - k(xs, ys) * ev(xs)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
1
|
+
require 'linear-regression/base'
|
2
|
+
require 'linear-regression/correlation_coefficient'
|
3
|
+
require 'linear-regression/linear'
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Regression
|
2
|
+
class Base
|
3
|
+
# Expected value
|
4
|
+
# http://en.wikipedia.org/wiki/mean
|
5
|
+
def mean(values)
|
6
|
+
raise "values must be an array" unless values.is_a?(Array)
|
7
|
+
|
8
|
+
values.inject(0) {|sum, x| sum + x}.to_f / values.length
|
9
|
+
end
|
10
|
+
|
11
|
+
# Covariance
|
12
|
+
# http://en.wikipedia.org/wiki/Covariance
|
13
|
+
def covariance(xs, ys)
|
14
|
+
raise "Length xs and ys must be equal" unless xs.length == ys.length
|
15
|
+
|
16
|
+
xys = xs.zip(ys).map{|x,y| x * y }
|
17
|
+
mean(xys) - mean(xs) * mean(ys)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Another way to implement covariance
|
21
|
+
def covariance2(xs, ys)
|
22
|
+
raise "Length xs and ys must be equal" unless xs.length == ys.length
|
23
|
+
|
24
|
+
ev_x, ev_y = mean(xs), mean(ys)
|
25
|
+
xs.zip(ys)
|
26
|
+
.map{|x,y| (x-ev_x) * (y-ev_y)}
|
27
|
+
.inject(0) {|sum, x| sum += x} / xs.length
|
28
|
+
end
|
29
|
+
|
30
|
+
# Variance
|
31
|
+
# http://en.wikipedia.org/wiki/Variance
|
32
|
+
def variance(values)
|
33
|
+
raise "Values must be an array" unless values.is_a?(Array)
|
34
|
+
|
35
|
+
mean(values.map{|i| i**2}) - mean(values)**2
|
36
|
+
end
|
37
|
+
|
38
|
+
# Standard Deviation
|
39
|
+
# http://en.wikipedia.org/wiki/Standard_deviation
|
40
|
+
def standard_deviation(values)
|
41
|
+
raise "Values must be an array" unless values.is_a?(Array)
|
42
|
+
|
43
|
+
Math.sqrt(variance(values))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Regression
|
2
|
+
class CorrelationCoefficient < Base
|
3
|
+
def initialize(xs, ys)
|
4
|
+
raise "Length xs and ys must be equal" unless xs.length == ys.length
|
5
|
+
|
6
|
+
@xs = xs
|
7
|
+
@ys = ys
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
# Pearson product-moment correlation coefficient
|
12
|
+
# http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
|
13
|
+
def pearson
|
14
|
+
@pearson ||= covariance(@xs, @ys) / (standard_deviation(@xs).to_f * standard_deviation(@ys).to_f)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Spearman's rank correlation coefficient
|
18
|
+
# http://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient
|
19
|
+
def spearman
|
20
|
+
@spearman ||= calc_spearman
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def calc_spearman
|
25
|
+
len, sum = xs.length, 0.0
|
26
|
+
|
27
|
+
sum = xs.zip(ys)
|
28
|
+
.map{|x, y| (x - y) ** 2}
|
29
|
+
.inject(0.0) {|sum, x| sum += x}
|
30
|
+
|
31
|
+
1 - (6 * sum)/(len * (len - 1))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Regression
|
2
|
+
class Linear < Base
|
3
|
+
def initialize(xs, ys)
|
4
|
+
abort "Length xs and ys must be equal" unless xs.length == ys.length
|
5
|
+
|
6
|
+
@xs = xs
|
7
|
+
@ys = ys
|
8
|
+
end
|
9
|
+
|
10
|
+
def trend
|
11
|
+
@xs.map{|x| predict x}
|
12
|
+
end
|
13
|
+
|
14
|
+
def predict(x)
|
15
|
+
y = slope * x + intercept
|
16
|
+
end
|
17
|
+
|
18
|
+
# y = kx + b
|
19
|
+
def slope
|
20
|
+
@slope ||= covariance(@xs, @ys) / variance(@xs)
|
21
|
+
end
|
22
|
+
|
23
|
+
# y = kx + b
|
24
|
+
def intercept
|
25
|
+
@intercept ||= mean(@ys) - slope * mean(@xs)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linear-regression
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Alekseichenko
|
@@ -17,6 +17,9 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/linear-regression.rb
|
20
|
+
- lib/linear-regression/base.rb
|
21
|
+
- lib/linear-regression/correlation_coefficient.rb
|
22
|
+
- lib/linear-regression/linear.rb
|
20
23
|
homepage: https://github.com/slon1024/linear-regression
|
21
24
|
licenses:
|
22
25
|
- MIT
|