measurable 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Measurable
2
- VERSION = "0.0.7" # :nodoc:
2
+ VERSION = "0.0.8" # :nodoc:
3
3
  end
@@ -26,4 +26,23 @@ describe "Chebyshev distance" do
26
26
  it "shouldn't work with vectors of different length" do
27
27
  expect { Measurable.chebyshev(@u, [1, 3, 5, 7]) }.to raise_error(ArgumentError)
28
28
  end
29
- end
29
+
30
+ it "can be extended separately" do
31
+ klass = Class.new do
32
+ extend Measurable::Chebyshev
33
+ end
34
+
35
+ x = klass.chebyshev(@u, @v)
36
+ x.should be_within(TOLERANCE).of(3.1)
37
+ end
38
+
39
+ it "can be included separately" do
40
+ klass = Class.new do
41
+ include Measurable::Chebyshev
42
+ end
43
+
44
+ x = klass.new.chebyshev(@u, @v)
45
+ x.should be_within(TOLERANCE).of(3.1)
46
+ end
47
+
48
+ end
data/spec/cosine_spec.rb CHANGED
@@ -27,6 +27,22 @@ describe "Cosine" do
27
27
  it "shouldn't work with vectors of different length" do
28
28
  expect { Measurable.cosine_similarity(@u, [1, 3, 5, 7]) }.to raise_error(ArgumentError)
29
29
  end
30
+
31
+ it "can be extended separately" do
32
+ klass = Class.new do
33
+ extend Measurable::Cosine
34
+ end
35
+ x = klass.cosine_similarity(@u, @v)
36
+ x.should be_within(TOLERANCE).of(0.992277877)
37
+ end
38
+
39
+ it "can be extended separately" do
40
+ klass = Class.new do
41
+ include Measurable::Cosine
42
+ end
43
+ x = klass.new.cosine_similarity(@u, @v)
44
+ x.should be_within(TOLERANCE).of(0.992277877)
45
+ end
30
46
  end
31
47
 
32
48
  context "Distance" do
@@ -29,6 +29,22 @@ describe "Euclidean" do
29
29
  it "shouldn't work with vectors of different length" do
30
30
  expect { Measurable.euclidean(@u, [2, 2, 2, 2]) }.to raise_error(ArgumentError)
31
31
  end
32
+
33
+ it "can be extended separately" do
34
+ klass = Class.new do
35
+ extend Measurable::Euclidean
36
+ end
37
+
38
+ klass.euclidean([3, 4]).should == 5
39
+ end
40
+
41
+ it "can be included separately" do
42
+ klass = Class.new do
43
+ include Measurable::Euclidean
44
+ end
45
+
46
+ klass.new.euclidean([3, 4]).should == 5
47
+ end
32
48
  end
33
49
 
34
50
  context "Squared Distance" do
@@ -58,4 +74,4 @@ describe "Euclidean" do
58
74
  expect { Measurable.euclidean_squared(@u, [2, 2, 2, 2]) }.to raise_error(ArgumentError)
59
75
  end
60
76
  end
61
- end
77
+ end
data/spec/hamming_spec.rb CHANGED
@@ -27,4 +27,20 @@ describe "Hamming distance" do
27
27
  expect { Measurable.hamming(@u, "smallstring") }.to raise_error(ArgumentError)
28
28
  expect { Measurable.hamming(@u, "largestring" * 20) }.to raise_error(ArgumentError)
29
29
  end
30
- end
30
+
31
+ it "can be extended separately" do
32
+ klass = Class.new do
33
+ extend Measurable::Hamming
34
+ end
35
+
36
+ klass.hamming(@u, @v).should == 17
37
+ end
38
+
39
+ it "can be included separately" do
40
+ klass = Class.new do
41
+ include Measurable::Hamming
42
+ end
43
+
44
+ klass.new.hamming(@u, @v).should == 17
45
+ end
46
+ end
@@ -34,4 +34,24 @@ describe "Haversine distance" do
34
34
  it "should only work with [lat, long] vectors" do
35
35
  expect { Measurable.haversine([2, 4], [1, 3, 5, 7]) }.to raise_error(ArgumentError)
36
36
  end
37
- end
37
+
38
+ it "can be extended seperately" do
39
+ klass = Class.new do
40
+ extend Measurable::Haversine
41
+ end
42
+
43
+ x = klass.haversine(@u, @v, :km)
44
+
45
+ x.should be_within(@haversine_tolerance).of(18533)
46
+ end
47
+
48
+ it "can be included seperately" do
49
+ klass = Class.new do
50
+ include Measurable::Haversine
51
+ end
52
+
53
+ x = klass.new.haversine(@u, @v, :km)
54
+
55
+ x.should be_within(@haversine_tolerance).of(18533)
56
+ end
57
+ end
data/spec/jaccard_spec.rb CHANGED
@@ -28,6 +28,27 @@ describe "Jaccard" do
28
28
  it "shouldn't work with vectors of different length" do
29
29
  expect { Measurable.jaccard_index(@u, [1, 2, 3, 4]) }.to raise_error(ArgumentError)
30
30
  end
31
+
32
+ it "can be extended separately" do
33
+ klass = Class.new do
34
+ extend Measurable::Jaccard
35
+ end
36
+
37
+ x = klass.jaccard_index(@u, @v)
38
+
39
+ x.should be_within(TOLERANCE).of(1.0 / 2.0)
40
+ end
41
+
42
+ it "can be included separately" do
43
+ klass = Class.new do
44
+ include Measurable::Jaccard
45
+ end
46
+
47
+ x = klass.new.jaccard_index(@u, @v)
48
+
49
+ x.should be_within(TOLERANCE).of(1.0 / 2.0)
50
+ end
51
+
31
52
  end
32
53
 
33
54
  context "Distance" do
@@ -0,0 +1,46 @@
1
+ describe "Kullback-Leibler" do
2
+ before :all do
3
+ @p = [0.10, 0.15, 0.35, 0.25, 0.15]
4
+ @q = [0.09, 0.13, 0.39, 0.22, 0.17]
5
+ @uniform = [0.20, 0.20, 0.20, 0.20, 0.20]
6
+ end
7
+
8
+ context "Divergence" do
9
+ it "accepts two arguments" do
10
+ expect { Measurable.kullback_leibler(@p, @q) }.to_not raise_error
11
+ expect { Measurable.kullback_leibler(@p, @q, []) }.to raise_error(ArgumentError)
12
+ end
13
+
14
+ it "shouldn't work with vectors of different length" do
15
+ expect { Measurable.kullback_leibler(@p, [0.50, 0.50]) }.to raise_error(ArgumentError)
16
+ end
17
+
18
+ it "shouldn't be symmetric" do
19
+ Measurable.kullback_leibler(@p, @q).should_not == Measurable.kullback_leibler(@q, @p)
20
+ end
21
+
22
+ it "should increase with dissimilarity" do
23
+ Measurable.kullback_leibler(@p, @uniform).should > Measurable.kullback_leibler(@p, @q)
24
+ end
25
+
26
+ it "should return the correct value" do
27
+ Measurable.kullback_leibler(@p, @uniform).should be_within(TOLERANCE).of 0.0960320738
28
+ end
29
+
30
+ it "can be extended separately" do
31
+ klass = Class.new do
32
+ extend Measurable::KullbackLeibler
33
+ end
34
+
35
+ klass.kullback_leibler(@p, @q).should be_within(TOLERANCE).of 0.007310294
36
+ end
37
+
38
+ it "can be included separately" do
39
+ klass = Class.new do
40
+ include Measurable::KullbackLeibler
41
+ end
42
+
43
+ klass.new.kullback_leibler(@p, @q).should be_within(TOLERANCE).of 0.007310294
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,21 @@
1
1
  describe Measurable::Levenshtein do
2
2
 
3
+ it "can be extended seperately" do
4
+ klass = Class.new do
5
+ extend Measurable::Levenshtein
6
+ end
7
+
8
+ klass.levenshtein("ab", "abc").should == 1
9
+ end
10
+
11
+ it "can be included seperately" do
12
+ klass = Class.new do
13
+ include Measurable::Levenshtein
14
+ end
15
+
16
+ klass.new.levenshtein("ab", "abc").should == 1
17
+ end
18
+
3
19
  context "strings" do
4
20
 
5
21
  it "handles empty" do
data/spec/maxmin_spec.rb CHANGED
@@ -27,4 +27,23 @@ describe "Max-min distance" do
27
27
  it "shouldn't work with vectors of different length" do
28
28
  expect { Measurable.maxmin(@u, [1, 3, 5, 7]) }.to raise_error(ArgumentError)
29
29
  end
30
- end
30
+
31
+ it "can be extended separately" do
32
+ klass = Class.new do
33
+ extend Measurable::Maxmin
34
+ end
35
+ x = klass.maxmin(@u, @v)
36
+
37
+ x.should be_within(TOLERANCE).of(0.9523809523)
38
+ end
39
+
40
+ it "can be included separately" do
41
+ klass = Class.new do
42
+ include Measurable::Maxmin
43
+ end
44
+ x = klass.new.maxmin(@u, @v)
45
+
46
+ x.should be_within(TOLERANCE).of(0.9523809523)
47
+ end
48
+
49
+ end
@@ -24,5 +24,21 @@ describe "Minkowski" do
24
24
  it "shouldn't work with vectors of different length" do
25
25
  expect { Measurable.minkowski(@u, [2, 2, 2, 2]) }.to raise_error(ArgumentError)
26
26
  end
27
+
28
+ it "can be extended separately" do
29
+ klass = Class.new do
30
+ extend Measurable::Minkowski
31
+ end
32
+
33
+ klass.minkowski(@u, @u).should == 0
34
+ end
35
+
36
+ it "can be included separately" do
37
+ klass = Class.new do
38
+ include Measurable::Minkowski
39
+ end
40
+
41
+ klass.new.minkowski(@u, @u).should == 0
42
+ end
27
43
  end
28
- end
44
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,4 +3,4 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
4
  require 'measurable'
5
5
 
6
- TOLERANCE = 10e-9
6
+ TOLERANCE = 10e-9
@@ -27,4 +27,24 @@ describe "Tanimoto distance" do
27
27
  it "shouldn't work with vectors of different length" do
28
28
  expect { Measurable.tanimoto(@u, [1, 3, 5, 7]) }.to raise_error(ArgumentError)
29
29
  end
30
+
31
+ it "can be extended separately" do
32
+ klass = Class.new do
33
+ extend Measurable::Tanimoto
34
+ end
35
+
36
+ x = klass.tanimoto(@u, @v)
37
+
38
+ x.should be_within(TOLERANCE).of(-Math.log2(1.0 / 2.0))
39
+ end
40
+
41
+ it "can be included separately" do
42
+ klass = Class.new do
43
+ include Measurable::Tanimoto
44
+ end
45
+
46
+ x = klass.new.tanimoto(@u, @v)
47
+
48
+ x.should be_within(TOLERANCE).of(-Math.log2(1.0 / 2.0))
49
+ end
30
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measurable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Agarie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ files:
90
90
  - ".rspec"
91
91
  - ".travis.yml"
92
92
  - Gemfile
93
+ - History.txt
93
94
  - LICENSE
94
95
  - README.md
95
96
  - Rakefile
@@ -100,6 +101,7 @@ files:
100
101
  - lib/measurable/hamming.rb
101
102
  - lib/measurable/haversine.rb
102
103
  - lib/measurable/jaccard.rb
104
+ - lib/measurable/kullback_leibler.rb
103
105
  - lib/measurable/levenshtein.rb
104
106
  - lib/measurable/maxmin.rb
105
107
  - lib/measurable/minkowski.rb
@@ -112,6 +114,7 @@ files:
112
114
  - spec/hamming_spec.rb
113
115
  - spec/haversine_spec.rb
114
116
  - spec/jaccard_spec.rb
117
+ - spec/kullback_leibler_spec.rb
115
118
  - spec/levenshtein_spec.rb
116
119
  - spec/maxmin_spec.rb
117
120
  - spec/minkowski_spec.rb
@@ -148,6 +151,7 @@ test_files:
148
151
  - spec/hamming_spec.rb
149
152
  - spec/haversine_spec.rb
150
153
  - spec/jaccard_spec.rb
154
+ - spec/kullback_leibler_spec.rb
151
155
  - spec/levenshtein_spec.rb
152
156
  - spec/maxmin_spec.rb
153
157
  - spec/minkowski_spec.rb