linear-regression 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/linear-regression.rb +86 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be7ede767c9e42da21e84dfcde225962a65b33c4
4
+ data.tar.gz: e4468fc7c026a3517f75626d6c3521e215e42307
5
+ SHA512:
6
+ metadata.gz: 46819763f4306dbd5734890dbbdcb2151aa0bd222c5dd32d0afd17f7544cab4d70f02e033ba377ca24ebae5695561e30658baadacb06f5d75c40471a30409dfb
7
+ data.tar.gz: 22e14c6f10bb0c9581b80d4a082ddc6a8b199dc2c3b321c3ce11d0149b749a9c1cc4abe2a640081a24bffd404c046d8e56165152ae6bc6b0f89270339f213e8d
@@ -0,0 +1,86 @@
1
+ module Regression
2
+ class Linear
3
+ # Expected value
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
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linear-regression
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Alekseichenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A linear regression gem
14
+ email: vova@vova.me
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/linear-regression.rb
20
+ homepage: https://github.com/slon1024/linear-regression
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.0
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Linear regression
44
+ test_files: []
45
+ has_rdoc: