mathstats 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +11 -6
- data/Rakefile +0 -6
- data/lib/mathstats.rb +37 -36
- data/mathstats.gemspec +11 -9
- data/test/mathstats_test.rb +24 -32
- metadata +12 -5
data/README.txt
CHANGED
@@ -8,20 +8,25 @@ Mathstats is a simple statistics library.
|
|
8
8
|
2. No dependencies
|
9
9
|
3. Rails integration
|
10
10
|
4. Usable as a mixin: [4,8,15,16,23,42].variance
|
11
|
-
5. Usable as a library: Mathstats
|
11
|
+
5. Usable as a library: Mathstats.variance([4,8,15,16,23,42])
|
12
12
|
|
13
13
|
== Installation
|
14
14
|
|
15
|
-
$ gem
|
16
|
-
$ gem install bmarini-mathstats
|
15
|
+
$ gem install mathstats
|
17
16
|
|
18
17
|
== Usage
|
19
18
|
|
20
|
-
require 'rubygems'
|
21
19
|
require 'mathstats'
|
22
20
|
|
23
|
-
Array.send :include, Mathstats
|
21
|
+
Mathstats.attach_to(Array) # => Array.send :include, Mathstats::Mixin
|
24
22
|
[4,8,15,16,23,42].variance
|
25
23
|
|
26
24
|
# Or if you don't want to mess with the Array class
|
27
|
-
Mathstats
|
25
|
+
Mathstats.variance([4,8,15,16,23,42])
|
26
|
+
|
27
|
+
== Methods available
|
28
|
+
|
29
|
+
* mean, average
|
30
|
+
* standard_deviation
|
31
|
+
* sum
|
32
|
+
* variance
|
data/Rakefile
CHANGED
data/lib/mathstats.rb
CHANGED
@@ -1,42 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
end
|
5
|
-
alias_method :average, :mean
|
6
|
-
|
7
|
-
def standard_deviation(options = {}, &block)
|
8
|
-
Lib.standard_deviation(self, options, &block)
|
9
|
-
end
|
10
|
-
|
11
|
-
def sum(identity = 0, &block)
|
12
|
-
Lib.sum(self, identity, &block)
|
13
|
-
end
|
14
|
-
|
15
|
-
def variance(options = {}, &block)
|
16
|
-
Lib.variance(self, options, &block)
|
17
|
-
end
|
18
|
-
|
19
|
-
class Lib
|
20
|
-
|
21
|
-
def self.mean(array, identity = 0, &block)
|
1
|
+
class Mathstats
|
2
|
+
class << self
|
3
|
+
def mean(array, identity = 0, &block)
|
22
4
|
array.size > 0 ? sum(array, identity, &block) / array.size.to_f : identity
|
23
5
|
end
|
24
|
-
|
25
|
-
|
26
|
-
def
|
27
|
-
|
28
|
-
def self.standard_deviation(array, options = {}, &block)
|
6
|
+
alias_method :average, :mean
|
7
|
+
|
8
|
+
def standard_deviation(array, options = {}, &block)
|
29
9
|
options = {:default => 0}.merge(options)
|
30
10
|
return options[:default] unless array.size > 0
|
31
|
-
|
11
|
+
|
32
12
|
if block_given?
|
33
13
|
return standard_deviation( array.map(&block), options )
|
34
14
|
end
|
35
|
-
|
15
|
+
|
36
16
|
Math.sqrt( variance(array, options) )
|
37
17
|
end
|
38
|
-
|
39
|
-
def
|
18
|
+
|
19
|
+
def sum(array, identity = 0, &block)
|
40
20
|
return identity unless array.size > 0
|
41
21
|
|
42
22
|
if block_given?
|
@@ -48,23 +28,44 @@ module Mathstats
|
|
48
28
|
|
49
29
|
# Two pass algorithm is currently the only algo supported
|
50
30
|
# http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
|
51
|
-
def
|
31
|
+
def variance(array, options = {}, &block)
|
52
32
|
options = {:default => 0, :algo => :two_pass, :population => :infinite}.merge(options)
|
53
33
|
return options[:default] unless array.size > 0
|
54
|
-
|
34
|
+
|
55
35
|
if block_given?
|
56
36
|
return variance( array.map(&block), options )
|
57
37
|
end
|
58
|
-
|
38
|
+
|
59
39
|
variance_two_pass(array, options)
|
60
40
|
end
|
61
|
-
|
62
|
-
def
|
41
|
+
|
42
|
+
def variance_two_pass(array, options)
|
63
43
|
n = array.size
|
64
44
|
denom = options[:population] == :infinite ? n - 1 : n
|
65
45
|
mean = mean(array)
|
66
46
|
variance = array.inject(0) {|memo, element| memo + (element - mean)**2 } / denom
|
67
47
|
end
|
68
48
|
|
49
|
+
def attach_to(klass)
|
50
|
+
klass.send :include, Mixin
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# All the class methods from Mathstats above are available in the Mixin
|
55
|
+
# module, via delegation. This module can be included in any class that
|
56
|
+
# has a method `#to_a` like Array
|
57
|
+
|
58
|
+
module Mixin
|
59
|
+
def self.delegate_to_mathstats(*symbols)
|
60
|
+
Array(symbols).each do |symbol|
|
61
|
+
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
62
|
+
def #{symbol}(*args, &block)
|
63
|
+
Mathstats.#{symbol}(self.to_a, *args, &block)
|
64
|
+
end
|
65
|
+
EOS
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
delegate_to_mathstats :mean, :average, :standard_deviation, :sum, :variance
|
69
70
|
end
|
70
|
-
end
|
71
|
+
end
|
data/mathstats.gemspec
CHANGED
@@ -1,20 +1,22 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "mathstats"
|
3
|
-
s.version = "0.9.
|
4
|
-
s.date = "
|
5
|
-
s.summary = "Basic statistics methods for ruby available as a
|
3
|
+
s.version = "0.9.2"
|
4
|
+
s.date = "2010-03-20"
|
5
|
+
s.summary = "Basic statistics methods for ruby available as a \
|
6
|
+
mixin or standalone class"
|
6
7
|
s.email = "bmarini@gmail.com"
|
7
8
|
s.homepage = "http://github.com/bmarini/mathstats"
|
8
|
-
s.description = "Mathstats is a basic statistics library. It is meant
|
9
|
+
s.description = "Mathstats is a basic statistics library. It is meant \
|
10
|
+
to be mixed into Array, but you can also use it as a standalone class."
|
9
11
|
s.has_rdoc = true
|
10
12
|
s.rdoc_options = ["--main", "README.txt"]
|
11
13
|
s.authors = ["Ben Marini"]
|
12
14
|
s.files = [
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
"README.txt",
|
16
|
+
"Rakefile",
|
17
|
+
"mathstats.gemspec",
|
18
|
+
"lib/mathstats.rb"
|
19
|
+
]
|
18
20
|
s.test_files = ["test/mathstats_test.rb"]
|
19
21
|
s.extra_rdoc_files = ["README.txt"]
|
20
22
|
end
|
data/test/mathstats_test.rb
CHANGED
@@ -1,30 +1,28 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'mathstats'
|
3
3
|
|
4
|
-
class
|
5
|
-
def setup
|
6
|
-
Array.send :include, Mathstats
|
7
|
-
end
|
8
|
-
|
4
|
+
class MathStatsTest < Test::Unit::TestCase
|
9
5
|
def test_average_as_module
|
6
|
+
Mathstats.attach_to(Array)
|
10
7
|
assert_equal 3, [1,2,3,4,5].average
|
11
8
|
assert_equal(6, [1,2,3,4,5].average {|n| n * 2 })
|
12
9
|
assert_equal(0, [].average(0))
|
13
10
|
assert_equal('empty', [].average('empty'))
|
14
|
-
|
11
|
+
|
15
12
|
assert_equal(1.5, [1,2].average)
|
16
13
|
end
|
17
|
-
|
14
|
+
|
18
15
|
def test_average_as_class
|
19
|
-
assert_equal 3, Mathstats
|
20
|
-
assert_equal(6, Mathstats
|
21
|
-
assert_equal(0, Mathstats
|
22
|
-
assert_equal('empty', Mathstats
|
23
|
-
|
24
|
-
assert_equal(1.5, Mathstats
|
16
|
+
assert_equal 3, Mathstats.average([1,2,3,4,5])
|
17
|
+
assert_equal(6, Mathstats.average([1,2,3,4,5]) {|n| n * 2 })
|
18
|
+
assert_equal(0, Mathstats.average([], 0))
|
19
|
+
assert_equal('empty', Mathstats.average([], 'empty'))
|
20
|
+
|
21
|
+
assert_equal(1.5, Mathstats.average([1,2]))
|
25
22
|
end
|
26
23
|
|
27
24
|
def test_sum_as_module
|
25
|
+
Mathstats.attach_to(Array)
|
28
26
|
assert_equal 10, [1,2,3,4].sum
|
29
27
|
assert_equal(20, [1,2,3,4].sum {|n| n * 2 })
|
30
28
|
assert_equal(0, [].sum(0))
|
@@ -32,37 +30,31 @@ class TC_MyTest < Test::Unit::TestCase
|
|
32
30
|
end
|
33
31
|
|
34
32
|
def test_sum_as_class
|
35
|
-
assert_equal 15, Mathstats
|
36
|
-
assert_equal(30, Mathstats
|
37
|
-
assert_equal(0, Mathstats
|
38
|
-
assert_equal('empty', Mathstats
|
39
|
-
|
40
|
-
assert_equal(3, Mathstats
|
33
|
+
assert_equal 15, Mathstats.sum([1,2,3,4,5])
|
34
|
+
assert_equal(30, Mathstats.sum([1,2,3,4,5]) {|n| n * 2 })
|
35
|
+
assert_equal(0, Mathstats.sum([], 0))
|
36
|
+
assert_equal('empty', Mathstats.sum([], 'empty'))
|
37
|
+
|
38
|
+
assert_equal(3, Mathstats.sum([1,2]))
|
41
39
|
end
|
42
|
-
|
40
|
+
|
43
41
|
def test_variance_as_module
|
42
|
+
Mathstats.attach_to(Array)
|
44
43
|
assert_equal 30, [4, 7, 13, 16].variance
|
45
44
|
assert_equal 30, [4, 7, 13, 16].map {|n| 10**8 + n }.variance
|
46
45
|
assert_equal 30, [4, 7, 13, 16].map {|n| 10**9 + n }.variance
|
47
46
|
end
|
48
47
|
|
49
48
|
def test_variance_as_class
|
50
|
-
assert_equal 30, Mathstats
|
51
|
-
assert_equal 30, Mathstats
|
52
|
-
assert_equal 30, Mathstats
|
49
|
+
assert_equal 30, Mathstats.variance([4, 7, 13, 16])
|
50
|
+
assert_equal 30, Mathstats.variance([4, 7, 13, 16].map {|n| 10**8 + n })
|
51
|
+
assert_equal 30, Mathstats.variance([4, 7, 13, 16].map {|n| 10**9 + n })
|
53
52
|
end
|
54
|
-
|
53
|
+
|
55
54
|
def test_standard_deviation_as_module
|
55
|
+
Mathstats.attach_to(Array)
|
56
56
|
assert_equal 6, [3,7,7,19].standard_deviation({:population => :finite})
|
57
57
|
assert_equal 6.93, ([3,7,7,19].standard_deviation * 100).round / 100.0
|
58
58
|
end
|
59
59
|
|
60
|
-
def test_standard_deviation_as_class
|
61
|
-
assert_equal 6, Mathstats::Lib.standard_deviation([3,7,7,19], {:population => :finite})
|
62
|
-
assert_equal 6.93, ( Mathstats::Lib.standard_deviation([3,7,7,19]) * 100 ).round / 100.0
|
63
|
-
end
|
64
|
-
|
65
|
-
# def teardown
|
66
|
-
# end
|
67
|
-
|
68
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mathstats
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 2
|
9
|
+
version: 0.9.2
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Ben Marini
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-03-20 00:00:00 -07:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -40,18 +45,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
45
|
requirements:
|
41
46
|
- - ">="
|
42
47
|
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 0
|
43
50
|
version: "0"
|
44
|
-
version:
|
45
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
52
|
requirements:
|
47
53
|
- - ">="
|
48
54
|
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
49
57
|
version: "0"
|
50
|
-
version:
|
51
58
|
requirements: []
|
52
59
|
|
53
60
|
rubyforge_project:
|
54
|
-
rubygems_version: 1.3.
|
61
|
+
rubygems_version: 1.3.6
|
55
62
|
signing_key:
|
56
63
|
specification_version: 3
|
57
64
|
summary: Basic statistics methods for ruby available as a mixin or standalone class
|