rstat 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
6
+ ._*
7
+ .redcar/
8
+ .idea/
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rstat.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ ## Rstat
2
+
3
+ A very simple gem that adds some statistics methods to the core Array class.
4
+
5
+ Maybe in the future it will be something more.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ desc "Open an irb session preloaded with this library"
9
+ task :console do
10
+ sh "irb -rubygems -I lib -r rstat.rb"
11
+ end
data/lib/rstat.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "rstat/version"
2
+ require "rstat/core_ext"
3
+
4
+ module Rstat
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].sort.each do |path|
2
+ require "rstat/core_ext/#{File.basename(path, ".rb")}"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "rstat/core_ext/array/mean"
2
+ require "rstat/core_ext/array/median"
3
+ require "rstat/core_ext/array/mode"
@@ -0,0 +1,25 @@
1
+ class Array
2
+ def mean
3
+ self.inject(:+).to_f / self.length
4
+ end
5
+
6
+ def arithmetric_mean
7
+ self.mean
8
+ end
9
+
10
+ def geometric_mean
11
+ self.inject(:*).to_f ** (1.0 / self.length)
12
+ end
13
+
14
+ def harmonic_mean
15
+ self.length / self.map{ |x| 1.0 / x }.inject{ |sum, x| sum + x }.to_f
16
+ end
17
+
18
+ def quadratic_mean
19
+ self.power_mean(2)
20
+ end
21
+
22
+ def power_mean(p = 1)
23
+ ((1.0 / self.length) * self.map{ |x| x ** p }.inject{ |sum, x| sum + x }.to_f) ** (1.0 / p)
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ class Array
2
+ def median
3
+ unless self.length == 0
4
+ copy = self.sort
5
+
6
+ if copy.length % 2 == 0
7
+ (copy[copy.length / 2 - 1] + copy[copy.length / 2]) / 2.0
8
+ else
9
+ copy[copy.length / 2]
10
+ end
11
+ else
12
+ nil
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ class Array
2
+ def mode
3
+ if self.length <= 1
4
+ return self
5
+ end
6
+
7
+ values = Hash[*self.collect { |x| [x, 0] }.flatten]
8
+
9
+ self.map{ |x| values[x] += 1 }
10
+
11
+ q = values.sort_by{ |key, value| value }.reverse
12
+
13
+ if q[0][1] == q[1][1]
14
+ modes = []
15
+
16
+ q.each_cons(2) do |pair|
17
+ if !pair[1].nil? and pair[0][1] == pair[1][1]
18
+ modes << pair[0][0]
19
+ modes << pair[1][0]
20
+ else
21
+ break
22
+ end
23
+ end
24
+
25
+ modes.uniq.sort
26
+ else
27
+ [q[0][0]]
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Rstat
2
+ VERSION = "0.0.1"
3
+ end
data/rstat.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rstat/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rstat"
7
+ s.version = Rstat::VERSION
8
+ s.authors = ["Sean Eshbaugh"]
9
+ s.email = ["seaneshbaugh@gmail.com"]
10
+ s.homepage = "http://seaneshbaugh.com/"
11
+ s.summary = %q{A Simple statistics gem.}
12
+ s.description = %q{A Simple statistics gem.}
13
+
14
+ s.rubyforge_project = "rstat"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Rstat do
5
+ describe ".mean" do
6
+ it "calculates the mean of an array" do
7
+ [1, 2, 3, 4, 5].mean.should be_within(0.0001).of(3.0000)
8
+ end
9
+ end
10
+
11
+ describe ".geometric_mean" do
12
+ it "calculates the geometric mean of an array" do
13
+ [1, 2, 3, 4, 5].geometric_mean.should be_within(0.0001).of(2.6052)
14
+ end
15
+
16
+ it "calculates the geometric mean of an array with a zero element" do
17
+ [0, 2, 3, 4, 5].geometric_mean.should eql(0.0)
18
+ end
19
+
20
+ it "calculates the geometric mean of an array with a negative element" do
21
+ [-1, 2, 3, 4, 5].geometric_mean.nan?.should be_true
22
+ end
23
+ end
24
+
25
+ describe ".harmonic mean" do
26
+ it "calculates the harmonic mean of an array" do
27
+ [1, 2, 3, 4, 5].harmonic_mean.should be_within(0.0001).of(2.1897)
28
+ end
29
+ end
30
+
31
+ describe ".power_mean" do
32
+ it "power_mean(2) should equal quadratic_mean" do
33
+ p = [1, 2, 3, 4, 5].power_mean(2)
34
+ q = [1, 2, 3, 4, 5].quadratic_mean
35
+
36
+ p.should eql(q)
37
+ end
38
+ end
39
+
40
+ it "AM > GM > HM" do
41
+ a = [1, 2, 3, 4, 5]
42
+ am = a.arithmetric_mean
43
+ gm = a.geometric_mean
44
+ hm = a.harmonic_mean
45
+
46
+ am.should be > gm
47
+ gm.should be > hm
48
+ end
49
+
50
+ describe ".median" do
51
+ it "calculates the median of an array with an odd number of elements" do
52
+ [2, 3, 5, 1, 4].median.should eql(3)
53
+ end
54
+
55
+ it "calculates the median of an array with an even number of elements" do
56
+ [2, 3, 5, 1, 4, 6].median.should eql(3.5)
57
+ end
58
+ end
59
+
60
+ describe ".mode" do
61
+ it "calculates the mode of an empty array" do
62
+ [].mode.should eql([])
63
+ end
64
+
65
+ it "calculates the mode of an array with one element" do
66
+ [100].mode.should eql([100])
67
+ end
68
+
69
+ it "calculates the mode of an unimodal array" do
70
+ [1, 1, 1, 2, 3, 4, 5, 5, 6].mode.should eql([1])
71
+ end
72
+
73
+ it "calculates the mode of a bimodal array" do
74
+ [44, 45, 46, 44, 46, 50].mode.should eql([44, 46])
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "rstat"
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rstat
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sean Eshbaugh
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-24 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: A Simple statistics gem.
36
+ email:
37
+ - seaneshbaugh@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - CHANGELOG.md
47
+ - Gemfile
48
+ - README.md
49
+ - Rakefile
50
+ - lib/rstat.rb
51
+ - lib/rstat/core_ext.rb
52
+ - lib/rstat/core_ext/array.rb
53
+ - lib/rstat/core_ext/array/mean.rb
54
+ - lib/rstat/core_ext/array/median.rb
55
+ - lib/rstat/core_ext/array/mode.rb
56
+ - lib/rstat/version.rb
57
+ - rstat.gemspec
58
+ - spec/rstat/array_spec.rb
59
+ - spec/spec_helper.rb
60
+ has_rdoc: true
61
+ homepage: http://seaneshbaugh.com/
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project: rstat
90
+ rubygems_version: 1.5.2
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: A Simple statistics gem.
94
+ test_files: []
95
+