ruby-nuggets 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --colour
2
2
  --debug
3
+ --require spec/spec_helper
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.7.0
5
+ This documentation refers to ruby-nuggets version 0.7.1
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -25,14 +25,14 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require 'nuggets/array/variance_mixin'
28
+ require 'nuggets/array/standard_deviation_mixin'
29
29
 
30
30
  module Nuggets
31
31
  class Array
32
32
  module CorrelationMixin
33
33
 
34
34
  def self.included(base)
35
- base.send :include, Nuggets::Array::VarianceMixin
35
+ base.send :include, Nuggets::Array::StandardDeviationMixin
36
36
  end
37
37
 
38
38
  # call-seq:
@@ -40,18 +40,21 @@ module Nuggets
40
40
  #
41
41
  # Calculates the {Pearson product-moment correlation
42
42
  # coefficient}[http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient]
43
- # for the <tt>{x,y}</tt> pairs in _array_.
43
+ # for the <tt>{x,y}</tt> pairs in _array_. If _array_ only contains
44
+ # values instead of pairs, +y+ will be the value and +x+ will be each
45
+ # value's position (rank) in _array_.
44
46
  def correlation_coefficient
45
- sx, sy = 0.0, 0.0
47
+ return 0.0 if empty?
46
48
 
47
- return sx if empty?
49
+ target = first.respond_to?(:to_ary) ? self :
50
+ ::Array.new(size) { |i| i + 1 }.zip(self)
48
51
 
49
- each { |x, y|
50
- sx += x
51
- sy += y
52
- }
52
+ sx = target.std { |x, _| x }
53
+ sy = target.std { |_, y| y }
53
54
 
54
- (sx * cov) / (sy * var { |x, _| x })
55
+ c = target.cov
56
+
57
+ sx.zero? || sy.zero? ? c < 0 ? -1.0 : 1.0 : c / (sx * sy)
55
58
  end
56
59
 
57
60
  alias_method :corr, :correlation_coefficient
@@ -72,6 +72,21 @@ module Nuggets
72
72
  hist
73
73
  end
74
74
 
75
+ # call-seq:
76
+ # array.probability_mass_function => aHash
77
+ # array.probability_mass_function { |x| ... } => aHash
78
+ #
79
+ # Calculates the {probability mass function}[http://en.wikipedia.org/wiki/Probability_mass_function]
80
+ # (normalized histogram) of the values in _array_. Returns a Hash that
81
+ # maps any value, or the result of the value yielded to the block, to
82
+ # its probability (via #histogram).
83
+ def probability_mass_function(&block)
84
+ hist, n = histogram(&block), size.to_f
85
+ hist.each { |k, v| hist[k] = v / n }
86
+ end
87
+
88
+ alias_method :pmf, :probability_mass_function
89
+
75
90
  # call-seq:
76
91
  # array.annotated_histogram => anArray
77
92
  # array.annotated_histogram { |hist_item| ... } => aHash
@@ -25,35 +25,42 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
- require 'nuggets/array/correlation_mixin'
28
+ require 'nuggets/array/variance_mixin'
29
29
 
30
30
  module Nuggets
31
31
  class Array
32
32
  module RegressionMixin
33
33
 
34
34
  def self.included(base)
35
- base.send :include, Nuggets::Array::CorrelationMixin
35
+ base.send :include, Nuggets::Array::VarianceMixin
36
36
  end
37
37
 
38
38
  # call-seq:
39
39
  # array.linear_least_squares => anArray
40
40
  #
41
- # Calculates the {linear least squares regression}[http://en.wikipedia.org/wiki/Ordinary_least_squares]
42
- # for the <tt>{x,y}</tt> pairs in _array_.
41
+ # Calculates the {linear least squares regression}[http://en.wikipedia.org/wiki/Simple_linear_regression]
42
+ # for the <tt>{x,y}</tt> pairs in _array_. If _array_ only contains
43
+ # values instead of pairs, +y+ will be the value and +x+ will be each
44
+ # value's position (rank) in _array_.
43
45
  def linear_least_squares
44
- sx, sy = 0.0, 0.0
45
-
46
46
  return [] if empty?
47
47
 
48
- each { |x, y|
48
+ target = first.respond_to?(:to_ary) ? self :
49
+ ::Array.new(size) { |i| i + 1 }.zip(self)
50
+
51
+ sx, sy = 0.0, 0.0
52
+
53
+ target.each { |x, y|
49
54
  sx += x
50
55
  sy += y
51
56
  }
52
57
 
53
- b = corr * sy / sx
54
- a = (sy - b * sx) / size.to_f
58
+ v = target.var { |x, _| x }
59
+
60
+ b = v.zero? ? 0.0 : target.cov / v
61
+ a = (sy - b * sx) / size
55
62
 
56
- map { |x, _| [x, a + b * x] }
63
+ target.map { |x, _| [x, a + b * x] }
57
64
  end
58
65
 
59
66
  alias_method :llsq, :linear_least_squares
@@ -54,14 +54,19 @@ module Nuggets
54
54
  # call-seq:
55
55
  # array.covariance => aFloat
56
56
  #
57
- # Calculates the covariance[http://en.wikipedia.org/wiki/Covariance] of the
58
- # <tt>{x,y}</tt> pairs in _array_.
57
+ # Calculates the covariance[http://en.wikipedia.org/wiki/Covariance] of
58
+ # the <tt>{x,y}</tt> pairs in _array_. If _array_ only contains values
59
+ # instead of pairs, +y+ will be the value and +x+ will be each value's
60
+ # position (rank) in _array_.
59
61
  def covariance
60
62
  sx, sy, sp = 0.0, 0.0, 0.0
61
63
 
62
64
  return sx if empty?
63
65
 
64
- each { |x, y|
66
+ target = first.respond_to?(:to_ary) ? self :
67
+ ::Array.new(size) { |i| i + 1 }.zip(self)
68
+
69
+ target.each { |x, y|
65
70
  sx += x
66
71
  sy += y
67
72
  sp += x * y
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 7
7
- TINY = 0
7
+ TINY = 1
8
8
 
9
9
  class << self
10
10
 
@@ -0,0 +1,83 @@
1
+ require 'nuggets/array/correlation'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::CorrelationMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::CorrelationMixin) }
6
+
7
+ example do
8
+ [].correlation_coefficient.should == 0.0
9
+ end
10
+
11
+ example do
12
+ [1].correlation_coefficient.should == 1.0
13
+ end
14
+
15
+ example do
16
+ [1, 1, 1].correlation_coefficient.should == 1.0
17
+ end
18
+
19
+ example do
20
+ [1, 2, 3].correlation_coefficient.should == 1.0
21
+ end
22
+
23
+ example do
24
+ [-1, -2, -3].correlation_coefficient.should == -1.0
25
+ end
26
+
27
+ example do
28
+ [1, 1.5, 2].correlation_coefficient.should == 1.0
29
+ end
30
+
31
+ example do
32
+ [3, 2, 1].correlation_coefficient.should == -1.0
33
+ end
34
+
35
+ example do
36
+ [-3, -2, -1].correlation_coefficient.should == -[3, 2, 1].correlation_coefficient
37
+ end
38
+
39
+ example do
40
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].correlation_coefficient.should equal_float(0.478471417102228)
41
+ end
42
+
43
+ example do
44
+ [[1, 1]].correlation_coefficient.should == 1.0
45
+ end
46
+
47
+ example do
48
+ [[1, 1], [1, 1], [1, 1]].correlation_coefficient.should == 1.0
49
+ end
50
+
51
+ example do
52
+ [[-1, 1], [-1, 1], [-1, 1]].correlation_coefficient.should == 1.0
53
+ end
54
+
55
+ example do
56
+ [[1, -1], [1, -1], [1, -1]].correlation_coefficient.should == 1.0
57
+ end
58
+
59
+ example do
60
+ [[1, 1], [1, 2], [1, 3]].correlation_coefficient.should == 1.0
61
+ end
62
+
63
+ example do
64
+ [[1, 1], [2, 1], [3, 1]].correlation_coefficient.should == 1.0
65
+ end
66
+
67
+ example do
68
+ [[-1, 1], [-2, 1], [-3, 1]].correlation_coefficient.should == 1.0
69
+ end
70
+
71
+ example do
72
+ [[1, -1], [2, -1], [3, -1]].correlation_coefficient.should == 1.0
73
+ end
74
+
75
+ example do
76
+ [[3, 1], [2, 2], [1, 3]].correlation_coefficient.should == -1.0
77
+ end
78
+
79
+ example do
80
+ [[-3, 3], [-2, 2], [-1, 1]].correlation_coefficient.should == -[[3, 3], [2, 2], [1, 1]].correlation_coefficient
81
+ end
82
+
83
+ end
@@ -0,0 +1,89 @@
1
+ require 'nuggets/array/histogram'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::HistogramMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::HistogramMixin) }
6
+
7
+ example do
8
+ [].histogram.should == {}
9
+ end
10
+
11
+ example do
12
+ [1].histogram.should == { 1 => 1 }
13
+ end
14
+
15
+ example do
16
+ [1, 1, 1].histogram.should == { 1 => 3 }
17
+ end
18
+
19
+ example do
20
+ [1, 2, 2, 3].histogram.should == { 1 => 1, 2 => 2, 3 => 1 }
21
+ end
22
+
23
+ example do
24
+ [3, 2, 2, 1].histogram.should == [1, 2, 2, 3].histogram
25
+ end
26
+
27
+ example do
28
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].histogram.should == {
29
+ -4 => 1, -2 => 1, 0 => 2, 1 => 4, 2 => 2, 3 => 2, 24 => 1
30
+ }
31
+ end
32
+
33
+ example do
34
+ %w[one two three three].histogram.should == { 'one' => 1, 'two' => 1, 'three' => 2 }
35
+ end
36
+
37
+ example do
38
+ %w[three two one three].histogram.should == %w[one two three three].histogram
39
+ end
40
+
41
+ example do
42
+ [1, 2, 2, 3, 4].histogram { |x| x * 2 }.should == { 2 => 1, 4 => 2, 6 => 1, 8 => 1 }
43
+ end
44
+
45
+ example do
46
+ [1, 2, 3, 4].histogram { |x| x / 2 }.should == { 0 => 1, 1 => 2, 2 => 1 }
47
+ end
48
+
49
+ example do
50
+ [].probability_mass_function.should == {}
51
+ end
52
+
53
+ example do
54
+ [1].probability_mass_function.should == { 1 => 1 }
55
+ end
56
+
57
+ example do
58
+ [1, 1, 1].probability_mass_function.should == { 1 => 1 }
59
+ end
60
+
61
+ example do
62
+ [1, 2, 2, 3].probability_mass_function.should == { 1 => 0.25, 2 => 0.5, 3 => 0.25 }
63
+ end
64
+
65
+ example do
66
+ [3, 2, 2, 1].probability_mass_function.should == [1, 2, 2, 3].probability_mass_function
67
+ end
68
+
69
+ example do
70
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].probability_mass_function.values.inject(:+).should == 1.0
71
+ end
72
+
73
+ example do
74
+ %w[one two three three].probability_mass_function.should == { 'one' => 0.25, 'two' => 0.25, 'three' => 0.5 }
75
+ end
76
+
77
+ example do
78
+ %w[three two one three].probability_mass_function.should == %w[one two three three].probability_mass_function
79
+ end
80
+
81
+ example do
82
+ [1, 2, 2, 3, 4].probability_mass_function { |x| x * 2 }.should == { 2 => 0.2, 4 => 0.4, 6 => 0.2, 8 => 0.2 }
83
+ end
84
+
85
+ example do
86
+ [1, 2, 3, 4].probability_mass_function { |x| x / 2 }.should == { 0 => 0.25, 1 => 0.5, 2 => 0.25 }
87
+ end
88
+
89
+ end
@@ -120,8 +120,4 @@ describe Array, 'when extended by', Nuggets::Array::MeanMixin do
120
120
  # TODO: other methods
121
121
  # TODO: more examples: http://people.revoledu.com/kardi/tutorial/BasicMath/Average/mean.html
122
122
 
123
- def equal_float(value, precision = 1.0e-14)
124
- be_within(precision).of(value)
125
- end
126
-
127
123
  end
@@ -0,0 +1,40 @@
1
+ require 'nuggets/array/regression'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::RegressionMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::RegressionMixin) }
6
+
7
+ example {
8
+ [].linear_least_squares.should == []
9
+ }
10
+
11
+ example {
12
+ [0].linear_least_squares.should == [[1, 0]]
13
+ }
14
+
15
+ example {
16
+ [1].linear_least_squares.should == [[1, 1]]
17
+ }
18
+
19
+ example {
20
+ [1, 1].linear_least_squares.should == [[1, 1], [2, 1]]
21
+ }
22
+
23
+ example {
24
+ [1, 2, 3, 4].linear_least_squares.should == [[1, 1], [2, 2], [3, 3], [4, 4]]
25
+ }
26
+
27
+ example {
28
+ [1, 2, 4, 1].linear_least_squares.should == [[1, 1.7], [2, 1.9], [3, 2.1], [4, 2.3]]
29
+ }
30
+
31
+ example {
32
+ r = [2.3, 1.6, 0.9, 0.2]
33
+
34
+ [1, 2, 4, -2].linear_least_squares.each_with_index { |(x, y), i|
35
+ x.should == i + 1
36
+ y.should equal_float(r[i])
37
+ }
38
+ }
39
+
40
+ end
@@ -17,11 +17,11 @@ describe Array, 'when extended by', Nuggets::Array::StandardDeviationMixin do
17
17
  end
18
18
 
19
19
  example do
20
- [1, 2, 3].standard_deviation.should be_within(0.0001).of(0.8165)
20
+ [1, 2, 3].standard_deviation.should equal_float(0.816496580927726)
21
21
  end
22
22
 
23
23
  example do
24
- [3, 2, 1].standard_deviation.should be_within(0.0001).of(0.8165)
24
+ [3, 2, 1].standard_deviation.should equal_float(0.816496580927726)
25
25
  end
26
26
 
27
27
  example do
@@ -29,7 +29,7 @@ describe Array, 'when extended by', Nuggets::Array::StandardDeviationMixin do
29
29
  end
30
30
 
31
31
  example do
32
- [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].standard_deviation.should be_within(0.01).of(6.49)
32
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].standard_deviation.should equal_float(6.48804088737217)
33
33
  end
34
34
 
35
35
  end
@@ -29,7 +29,64 @@ describe Array, 'when extended by', Nuggets::Array::VarianceMixin do
29
29
  end
30
30
 
31
31
  example do
32
- [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].variance.should be_within(0.1).of(42.0)
32
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].variance.should equal_float(42.0946745562130218)
33
+ end
34
+
35
+ example do
36
+ [].covariance.should == 0.0
37
+ end
38
+
39
+ example do
40
+ [1].covariance.should == 0.0
41
+ end
42
+
43
+ example do
44
+ [1, 1, 1].covariance.should == 0.0
45
+ end
46
+
47
+ example do
48
+ [1, 2, 3].covariance.should == 2 / 3.0
49
+ end
50
+
51
+ example do
52
+ [3, 2, 1].covariance.should == -2 / 3.0
53
+ end
54
+
55
+ example do
56
+ [-3, -2, -1].covariance.should == -[3, 2, 1].covariance
57
+ end
58
+
59
+ example do
60
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].covariance.should equal_float(11.615384615384615)
61
+ end
62
+
63
+ example do
64
+ a = [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24]
65
+ a.zip(Array.new(a.size) { 42 }).covariance.should == 0.0
66
+ end
67
+
68
+ example do
69
+ a = [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24]
70
+ a.zip(a).covariance.should == a.variance
71
+ end
72
+
73
+ example do
74
+ a, b = [1, -2, 1, 2, 3, -4], [3, 1, 2, 1, 0, 24]
75
+ a.zip(b).covariance.should == b.zip(a).covariance
76
+ end
77
+
78
+ example do
79
+ a, b = [1, -2, 1, 2, 3, -4], [3, 1, 2, 1, 0, 24]
80
+ m, n = 23, 42
81
+
82
+ a.map { |i| m * i }.zip(b.map { |i| n * i }).covariance.should == m * n * a.zip(b).covariance
83
+ end
84
+
85
+ example do
86
+ a, b = [1, -2, 1, 2, 3, -4], [3, 1, 2, 1, 0, 24]
87
+ m, n = 23, 42
88
+
89
+ a.map { |i| i + m }.zip(b.map { |i| i + n }).covariance.should equal_float(a.zip(b).covariance, 1.0e-13)
33
90
  end
34
91
 
35
92
  end
@@ -94,11 +94,4 @@ BEFORE HE REACHED THE TOWN O
94
94
  end
95
95
  end
96
96
 
97
- def tempfile
98
- t = Tempfile.open('nuggets_file_replace_spec_temp') { |f| f.puts @txt }
99
- block_given? ? yield(t.path) : t.path
100
- ensure
101
- t.close(true) if t
102
- end
103
-
104
97
  end
@@ -148,11 +148,4 @@ Before he reached the town o
148
148
  lambda { File.gsub!(tempfile, /ight/, 'XIGHTX') }.should raise_error(Errno::ENOENT)
149
149
  end
150
150
 
151
- def tempfile
152
- t = Tempfile.open('nuggets_file_sub_spec_temp') { |f| f.puts @txt }
153
- block_given? ? yield(t.path) : t.path
154
- ensure
155
- t.close(true) if t
156
- end
157
-
158
151
  end
data/spec/spec_helper.rb CHANGED
@@ -1,14 +1,16 @@
1
- $:.unshift('lib') unless $:[0] == 'lib'
1
+ $:.unshift('lib') unless $:.first == 'lib'
2
2
 
3
- #class << Spec::Matchers
4
- #
5
- # def generated_description
6
- # return nil if last_should.nil?
7
- #
8
- # operator = last_should.to_s.tr('_', ' ')
9
- # target = last_matcher.instance_variable_get(:@actual)
10
- #
11
- # "#{target.inspect} #{operator} #{last_description}"
12
- # end
13
- #
14
- #end
3
+ RSpec.configure { |config|
4
+ config.include(Module.new {
5
+ def equal_float(value, precision = 1.0e-14)
6
+ be_within(precision).of(value)
7
+ end
8
+
9
+ def tempfile(txt = @txt)
10
+ t = Tempfile.open("nuggets_spec_#{object_id}_temp") { |f| f.puts txt }
11
+ block_given? ? yield(t.path) : t.path
12
+ ensure
13
+ t.close(true) if t
14
+ end
15
+ })
16
+ }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 0
10
- version: 0.7.0
9
+ - 1
10
+ version: 0.7.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jens Wille
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-05 00:00:00 Z
18
+ date: 2011-04-06 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Some extensions to the Ruby programming language.
@@ -155,7 +155,10 @@ files:
155
155
  - spec/nuggets/integer/length_spec.rb
156
156
  - spec/nuggets/array/mode_spec.rb
157
157
  - spec/nuggets/array/variance_spec.rb
158
+ - spec/nuggets/array/correlation_spec.rb
158
159
  - spec/nuggets/array/runiq_spec.rb
160
+ - spec/nuggets/array/histogram_spec.rb
161
+ - spec/nuggets/array/regression_spec.rb
159
162
  - spec/nuggets/array/median_spec.rb
160
163
  - spec/nuggets/array/mean_spec.rb
161
164
  - spec/nuggets/array/limit_spec.rb
@@ -193,9 +196,9 @@ rdoc_options:
193
196
  - README
194
197
  - --charset
195
198
  - UTF-8
196
- - --all
197
199
  - --title
198
- - ruby-nuggets Application documentation (v0.7.0)
200
+ - ruby-nuggets Application documentation (v0.7.1)
201
+ - --all
199
202
  require_paths:
200
203
  - lib
201
204
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -219,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
222
  requirements: []
220
223
 
221
224
  rubyforge_project: prometheus
222
- rubygems_version: 1.7.1
225
+ rubygems_version: 1.7.2
223
226
  signing_key:
224
227
  specification_version: 3
225
228
  summary: Some extensions to the Ruby programming language.