corr 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +41 -1
- data/corr.gemspec +1 -1
- data/lib/corr.rb +24 -33
- data/lib/corr/version.rb +1 -1
- metadata +4 -5
- data/.corr.gemspec.swp +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5cabc86085a928d1055531e629a397d23c31b88e2c9fbb00196b3b87b21796f4
|
|
4
|
+
data.tar.gz: af7a67243c5dd57a9928f0847b9ef9878927256558f963ce3ce16d1d23bacae1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e701a4c17e8624fe867820923d129e00661f28c52e644987272c4aeb6cde710107917d3480cfbc618928731232d471c240b385797b25b68c94b084b12f89c927
|
|
7
|
+
data.tar.gz: 99e846107e2644dfa35e357e6574a6a1248f20c1c6b1a8da9cca31a729778a016e39795570a75d3861dd564be9d25aa0839de69f6d98b084255dd8d49343f384
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -22,7 +22,47 @@ Or install it yourself as:
|
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
### Correlation
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
x = [43, 21, 25, 42, 57, 59] # Age
|
|
29
|
+
y = [99, 65, 79, 75, 87, 81] # Glucose level
|
|
30
|
+
|
|
31
|
+
Corr.correlation(x, y) # 0.5298089018901745
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Sum
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
x = [43, 21, 25, 42, 57, 59]
|
|
38
|
+
|
|
39
|
+
Corr.sum(x) # 247
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Mean
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
x = [1, 3, 2, 5, 8, 7, 12, 2, 4]
|
|
46
|
+
|
|
47
|
+
Corr.mean(x) # 4.888888888888889
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Standard deviation
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
x = [9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4]
|
|
54
|
+
|
|
55
|
+
Corr.standard_deviation(x) # 2.9832867780352594
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Covariance
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
x = [2.1, 2.5, 3.6, 4.0]
|
|
62
|
+
y = [8, 10, 12, 14]
|
|
63
|
+
|
|
64
|
+
Corr.covariance(x, y) # 1.7
|
|
65
|
+
```
|
|
26
66
|
|
|
27
67
|
## Development
|
|
28
68
|
|
data/corr.gemspec
CHANGED
|
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
|
8
8
|
|
|
9
9
|
spec.summary = %q{Calculate the correlation of two variables}
|
|
10
10
|
spec.description = %q{This gem allows to calculate the correlation of two variables (called vectors), the standard deviation and the covariance.}
|
|
11
|
-
spec.homepage = "https://
|
|
11
|
+
spec.homepage = "https://github.com/GerryLarios/corr"
|
|
12
12
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
13
13
|
|
|
14
14
|
spec.metadata["homepage_uri"] = spec.homepage
|
data/lib/corr.rb
CHANGED
|
@@ -1,44 +1,35 @@
|
|
|
1
1
|
require "corr/version"
|
|
2
2
|
|
|
3
3
|
module Corr
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
end
|
|
4
|
+
def self.correlation(vector_x, vector_y)
|
|
5
|
+
covariance(vector_x, vector_y) / (standard_deviation(vector_x) * standard_deviation(vector_y))
|
|
6
|
+
end
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
def self.sum(vector)
|
|
9
|
+
vector.reduce(0.0) { |result, value| result + value }
|
|
10
|
+
end
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
def self.mean(vector)
|
|
13
|
+
sum(vector) / vector.length
|
|
14
|
+
end
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
def self.standard_deviation(vector)
|
|
17
|
+
vector_mean = mean(vector)
|
|
18
|
+
sum_result = vector.reduce(0.0) {|result, value| result + (value - vector_mean)**2}
|
|
19
|
+
Math.sqrt((1.0 / vector.length) * sum_result)
|
|
20
|
+
end
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
raise 'The variable lengths are not the same.'
|
|
28
|
-
end
|
|
29
|
-
end
|
|
22
|
+
def self.covariance(vector_x, vector_y)
|
|
23
|
+
raise 'The variable lengths are not the same.' if vector_x.length != vector_y.length
|
|
24
|
+
sum_cov(vector_x, vector_y) / vector_x.length
|
|
25
|
+
end
|
|
30
26
|
|
|
31
|
-
|
|
27
|
+
private
|
|
32
28
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
for i in 0...n do
|
|
39
|
-
result += (vector_x[i] - mean_x) * (vector_y[i] - mean_y)
|
|
40
|
-
end
|
|
41
|
-
result
|
|
42
|
-
end
|
|
29
|
+
def self.sum_cov(vector_x, vector_y)
|
|
30
|
+
n = vector_x.length
|
|
31
|
+
mean_x = mean(vector_x)
|
|
32
|
+
mean_y = mean(vector_y)
|
|
33
|
+
(0...n).reduce(0.0) { |sum, i| sum + ((vector_x[i] - mean_x) * (vector_y[i] - mean_y))}
|
|
43
34
|
end
|
|
44
35
|
end
|
data/lib/corr/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: corr
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gerardo Manuel Chavez Larios
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-02-
|
|
11
|
+
date: 2020-02-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: This gem allows to calculate the correlation of two variables (called
|
|
14
14
|
vectors), the standard deviation and the covariance.
|
|
@@ -18,7 +18,6 @@ executables: []
|
|
|
18
18
|
extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
|
20
20
|
files:
|
|
21
|
-
- ".corr.gemspec.swp"
|
|
22
21
|
- ".gitignore"
|
|
23
22
|
- ".rspec"
|
|
24
23
|
- ".travis.yml"
|
|
@@ -33,10 +32,10 @@ files:
|
|
|
33
32
|
- corr.gemspec
|
|
34
33
|
- lib/corr.rb
|
|
35
34
|
- lib/corr/version.rb
|
|
36
|
-
homepage: https://
|
|
35
|
+
homepage: https://github.com/GerryLarios/corr
|
|
37
36
|
licenses: []
|
|
38
37
|
metadata:
|
|
39
|
-
homepage_uri: https://
|
|
38
|
+
homepage_uri: https://github.com/GerryLarios/corr
|
|
40
39
|
post_install_message:
|
|
41
40
|
rdoc_options: []
|
|
42
41
|
require_paths:
|
data/.corr.gemspec.swp
DELETED
|
Binary file
|