mathstats 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ = Mathstats
2
+
3
+ Mathstats is a simple statistics library.
4
+
5
+ == Goals
6
+
7
+ 1. Support for basic statistics equations
8
+ 2. No dependencies
9
+ 3. Rails integration
10
+ 4. Usable as a mixin: [4,8,15,16,23,42].variance
11
+ 5. Usable as a library: Mathstats::Lib.variance([4,8,15,16,23,42])
12
+
13
+ == Installation
14
+
15
+ $ gem sources -a http://gems.github.com
16
+ $ gem install bmarini-mathstats
17
+
18
+ == Usage
19
+
20
+ require 'rubygems'
21
+ require 'mathstats'
22
+
23
+ Array.send :include, Mathstats
24
+ [4,8,15,16,23,42].variance
25
+
26
+ # Or if you don't want to mess with the Array class
27
+ Mathstats::Lib.variance([4,8,15,16,23,42])
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'rubygems'
3
+ rescue Exception
4
+ nil
5
+ end
6
+
7
+ require 'rake/clean'
8
+ require 'rake/testtask'
9
+
10
+ task :default => [:test]
11
+
12
+ task :test do
13
+ Rake::TestTask.new do |t|
14
+ t.libs << "test"
15
+ t.pattern = 'test/*_test.rb'
16
+ t.verbose = true
17
+ end
18
+ end
@@ -0,0 +1,70 @@
1
+ module Mathstats
2
+ def mean(identity = 0, &block)
3
+ Lib.average(self, identity, &block)
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)
22
+ array.size > 0 ? sum(array, identity, &block) / array.size.to_f : identity
23
+ end
24
+
25
+ # Poor man's alias_method until I figure out how to do this right
26
+ def self.average(array, identity = 0, &block); mean(array, identity, &block); end
27
+
28
+ def self.standard_deviation(array, options = {}, &block)
29
+ options = {:default => 0}.merge(options)
30
+ return options[:default] unless array.size > 0
31
+
32
+ if block_given?
33
+ return standard_deviation( array.map(&block), options )
34
+ end
35
+
36
+ Math.sqrt( variance(array, options) )
37
+ end
38
+
39
+ def self.sum(array, identity = 0, &block)
40
+ return identity unless array.size > 0
41
+
42
+ if block_given?
43
+ sum( array.map(&block) )
44
+ else
45
+ array.inject { |sum, element| sum + element }
46
+ end
47
+ end
48
+
49
+ # Two pass algorithm is currently the only algo supported
50
+ # http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
51
+ def self.variance(array, options = {}, &block)
52
+ options = {:default => 0, :algo => :two_pass, :population => :infinite}.merge(options)
53
+ return options[:default] unless array.size > 0
54
+
55
+ if block_given?
56
+ return variance( array.map(&block), options )
57
+ end
58
+
59
+ variance_two_pass(array, options)
60
+ end
61
+
62
+ def self.variance_two_pass(array, options)
63
+ n = array.size
64
+ denom = options[:population] == :infinite ? n - 1 : n
65
+ mean = mean(array)
66
+ variance = array.inject(0) {|memo, element| memo + (element - mean)**2 } / denom
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "mathstats"
3
+ s.version = "0.9.1"
4
+ s.date = "2008-11-07"
5
+ s.summary = "Basic statistics methods for ruby available as a mixin or standalone class"
6
+ s.email = "bmarini@gmail.com"
7
+ s.homepage = "http://github.com/bmarini/mathstats"
8
+ s.description = "Mathstats is a basic statistics library. It is meant to be mixed into Array, but you can also use it as a standalone class."
9
+ s.has_rdoc = true
10
+ s.rdoc_options = ["--main", "README.txt"]
11
+ s.authors = ["Ben Marini"]
12
+ s.files = [
13
+ "README.txt",
14
+ "Rakefile",
15
+ "mathstats.gemspec",
16
+ "lib/mathstats.rb"
17
+ ]
18
+ s.test_files = ["test/mathstats_test.rb"]
19
+ s.extra_rdoc_files = ["README.txt"]
20
+ end
@@ -0,0 +1,68 @@
1
+ require 'test/unit'
2
+ require 'mathstats'
3
+
4
+ class TC_MyTest < Test::Unit::TestCase
5
+ def setup
6
+ Array.send :include, Mathstats
7
+ end
8
+
9
+ def test_average_as_module
10
+ assert_equal 3, [1,2,3,4,5].average
11
+ assert_equal(6, [1,2,3,4,5].average {|n| n * 2 })
12
+ assert_equal(0, [].average(0))
13
+ assert_equal('empty', [].average('empty'))
14
+
15
+ assert_equal(1.5, [1,2].average)
16
+ end
17
+
18
+ def test_average_as_class
19
+ assert_equal 3, Mathstats::Lib.average([1,2,3,4,5])
20
+ assert_equal(6, Mathstats::Lib.average([1,2,3,4,5]) {|n| n * 2 })
21
+ assert_equal(0, Mathstats::Lib.average([], 0))
22
+ assert_equal('empty', Mathstats::Lib.average([], 'empty'))
23
+
24
+ assert_equal(1.5, Mathstats::Lib.average([1,2]))
25
+ end
26
+
27
+ def test_sum_as_module
28
+ assert_equal 10, [1,2,3,4].sum
29
+ assert_equal(20, [1,2,3,4].sum {|n| n * 2 })
30
+ assert_equal(0, [].sum(0))
31
+ assert_equal('empty', [].sum('empty'))
32
+ end
33
+
34
+ def test_sum_as_class
35
+ assert_equal 15, Mathstats::Lib.sum([1,2,3,4,5])
36
+ assert_equal(30, Mathstats::Lib.sum([1,2,3,4,5]) {|n| n * 2 })
37
+ assert_equal(0, Mathstats::Lib.sum([], 0))
38
+ assert_equal('empty', Mathstats::Lib.sum([], 'empty'))
39
+
40
+ assert_equal(3, Mathstats::Lib.sum([1,2]))
41
+ end
42
+
43
+ def test_variance_as_module
44
+ assert_equal 30, [4, 7, 13, 16].variance
45
+ assert_equal 30, [4, 7, 13, 16].map {|n| 10**8 + n }.variance
46
+ assert_equal 30, [4, 7, 13, 16].map {|n| 10**9 + n }.variance
47
+ end
48
+
49
+ def test_variance_as_class
50
+ assert_equal 30, Mathstats::Lib.variance([4, 7, 13, 16])
51
+ assert_equal 30, Mathstats::Lib.variance([4, 7, 13, 16].map {|n| 10**8 + n })
52
+ assert_equal 30, Mathstats::Lib.variance([4, 7, 13, 16].map {|n| 10**9 + n })
53
+ end
54
+
55
+ def test_standard_deviation_as_module
56
+ assert_equal 6, [3,7,7,19].standard_deviation({:population => :finite})
57
+ assert_equal 6.93, ([3,7,7,19].standard_deviation * 100).round / 100.0
58
+ end
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
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mathstats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Marini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-07 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Mathstats is a basic statistics library. It is meant to be mixed into Array, but you can also use it as a standalone class.
17
+ email: bmarini@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - README.txt
26
+ - Rakefile
27
+ - mathstats.gemspec
28
+ - lib/mathstats.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/bmarini/mathstats
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --main
36
+ - README.txt
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Basic statistics methods for ruby available as a mixin or standalone class
58
+ test_files:
59
+ - test/mathstats_test.rb