basic-stats 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e3801e30fbbe3d7b3e731e1a5feca120b5915de
4
+ data.tar.gz: b738414f640c231200c547ded2eeb7de13804231
5
+ SHA512:
6
+ metadata.gz: d963ad59b357a9dbee66f352cf5d447a7b1330a6bf7f1f66cfb103d672ee527e3037ee6a4cc348d4217b9e9ad64a5f29b813a15dabd6e330aabd8ff816771da7
7
+ data.tar.gz: 1118862cb6811aea2417a3e66c122cf320dc70bfc7e27294f44fa0d316ee5168ca73ee2f771d7ef2e60c8684773db26a8ccb95260e21f64e6c127fad0a6ef0c6
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in basic-stats.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Christopher Petersen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # Basic::Stats
2
+
3
+ Basic statistical functions for Ruby collections, such as mean, standard_deviation, z, and outlier detection
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'basic-stats'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install basic-stats
18
+
19
+ If you aren't using rails, you may have to require the ```basic/stats``` file.
20
+
21
+ require 'basic/stats'
22
+
23
+ ## Usage
24
+
25
+ To use basic stat's methods, include the module in your array
26
+
27
+ ```ruby
28
+ array = [1,2,3,4,5,6,7,8,9,10]
29
+ array.extend Basic::Stats
30
+ array.mean
31
+ array.standard_deviation
32
+ array.critical_z
33
+ array.z(5)
34
+ ```
35
+
36
+ Once you've included the module, you can also use outlier detection
37
+
38
+ ```ruby
39
+ array = [1,2,3,4,5,6,7,8,9,30]
40
+ array.extend Basic::Stats
41
+ array.select_outliers
42
+ array.reject_outliers
43
+ array.reject_outliers!
44
+ ```
45
+
46
+ ## Notes on outlier detection
47
+
48
+ ```basic-stats``` uses the [Grubb's test](http://en.wikipedia.org/wiki/Grubbs'_test_for_outliers) for outlier detection. Specically it assumes an alpha of 5%, meaning you will mistakenly identify an outlier in 5% of your samples. More information [here](http://graphpad.com/support/faqid/1598/).
49
+
50
+ The Grubb's test does not work well for detecting multiple outliers.
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'basic/stats/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "basic-stats"
8
+ spec.version = Basic::Stats::VERSION
9
+ spec.authors = ["Christopher Petersen"]
10
+ spec.email = ["chris@petersen.io"]
11
+ spec.description = %q{Basic statistical functions for Ruby collections, such as mean, standard_deviation, z, and outlier detection}
12
+ spec.summary = %q{Basic statistical functions for Ruby collections, such as mean, standard_deviation, z, and outlier detection}
13
+ spec.homepage = "http://github.com/cpetersen/basic-stats"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,106 @@
1
+ require "basic/stats/version"
2
+
3
+ module Basic
4
+ module Stats
5
+ def sum
6
+ self.inject(0){|accum, i| accum + i }
7
+ end
8
+
9
+ def mean
10
+ self.sum/self.length.to_f
11
+ end
12
+
13
+ def sample_variance
14
+ m = self.mean
15
+ sum = self.inject(0){|accum, i| accum +(i-m)**2 }
16
+ sum/(self.length - 1).to_f
17
+ end
18
+
19
+ def standard_deviation
20
+ return Math.sqrt(self.sample_variance)
21
+ end
22
+
23
+ def z(value)
24
+ (self.mean - value).abs/standard_deviation
25
+ end
26
+
27
+ def critical_z
28
+ Stats.critical_z(self.size)
29
+ end
30
+
31
+ def reject_outliers
32
+ cz = self.critical_z
33
+ self.reject { |value| z(value) > cz }
34
+ end
35
+
36
+ def reject_outliers!
37
+ cz = self.critical_z
38
+ self.reject! { |value| z(value) > cz }
39
+ end
40
+
41
+ def select_outliers
42
+ cz = self.critical_z
43
+ self.select { |value| z(value) > cz }
44
+ end
45
+
46
+ @@critical_z_table = {
47
+ 3 => 1.15,
48
+ 4 => 1.48,
49
+ 5 => 1.71,
50
+ 6 => 1.89,
51
+ 7 => 2.02,
52
+ 8 => 2.13,
53
+ 9 => 2.21,
54
+ 10 => 2.29,
55
+ 11 => 2.34,
56
+ 12 => 2.41,
57
+ 13 => 2.46,
58
+ 14 => 2.51,
59
+ 15 => 2.55,
60
+ 16 => 2.59,
61
+ 17 => 2.62,
62
+ 18 => 2.65,
63
+ 19 => 2.68,
64
+ 20 => 2.71,
65
+ 21 => 2.73,
66
+ 22 => 2.76,
67
+ 23 => 2.78,
68
+ 24 => 2.80,
69
+ 25 => 2.82,
70
+ 26 => 2.84,
71
+ 27 => 2.86,
72
+ 28 => 2.88,
73
+ 29 => 2.89,
74
+ 30 => 2.91,
75
+ 31 => 2.92,
76
+ 32 => 2.94,
77
+ 33 => 2.95,
78
+ 34 => 2.97,
79
+ 35 => 2.98,
80
+ 36 => 2.99,
81
+ 37 => 3.00,
82
+ 38 => 3.01,
83
+ 39 => 3.03,
84
+ 40 => 3.04,
85
+ 50 => 3.13,
86
+ 60 => 3.20,
87
+ 70 => 3.26,
88
+ 80 => 3.31,
89
+ 90 => 3.35,
90
+ 100 => 3.38,
91
+ 110 => 3.42,
92
+ 120 => 3.44,
93
+ 130 => 3.47,
94
+ 140 => 3.49
95
+ }
96
+
97
+ def self.critical_z(n)
98
+ result = nil
99
+ @@critical_z_table.keys.sort.each do |key|
100
+ break if key > n
101
+ result = @@critical_z_table[key]
102
+ end
103
+ result
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,5 @@
1
+ module Basic
2
+ module Stats
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ require "basic/stats"
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe Basic::Stats do
4
+ let(:array) { [1,2,3,4,5,6,7,8,9,10].extend Basic::Stats }
5
+ let(:array_with_outlier) { [1,2,3,4,5,6,7,8,9,30].extend Basic::Stats }
6
+
7
+ it "should have a mean of 5.5" do
8
+ array.mean.should == 5.5
9
+ end
10
+
11
+ it "should have a standard deviation of 3.02765..." do
12
+ array.standard_deviation.should == 3.0276503540974917
13
+ end
14
+
15
+ it "should have a sample variance of 9.1666..." do
16
+ array.sample_variance.should == 9.166666666666666
17
+ end
18
+
19
+ it "should have a critical z of 2.29" do
20
+ array.critical_z.should == 2.29
21
+ end
22
+
23
+ it "should reject an outlier (copy)" do
24
+ new_array = array_with_outlier.reject_outliers
25
+ new_array.size.should == (array_with_outlier.size-1)
26
+ end
27
+
28
+ it "should reject an outlier (mutate)" do
29
+ expect {
30
+ array_with_outlier.reject_outliers!
31
+ }.to change{array_with_outlier.size}.by(-1)
32
+ end
33
+
34
+ it "should select outliers" do
35
+ outliers = array_with_outlier.select_outliers
36
+ outliers.should == [30]
37
+ end
38
+
39
+ describe "z" do
40
+ it "of 3 should be 0.82572" do
41
+ array.z(3).should == 0.8257228238447705
42
+ end
43
+
44
+ it "of 10 should be 1.48630" do
45
+ array.z(10).should == 1.4863010829205867
46
+ end
47
+ end
48
+
49
+ describe "critical z" do
50
+ it "of 0 should be nil" do
51
+ Basic::Stats.critical_z(0).should be_nil
52
+ end
53
+
54
+ it "of 1 should be nil" do
55
+ Basic::Stats.critical_z(1).should be_nil
56
+ end
57
+
58
+ it "of 3 should be 1.15" do
59
+ Basic::Stats.critical_z(3).should == 1.15
60
+ end
61
+
62
+ it "of 20 should be 2.71" do
63
+ Basic::Stats.critical_z(20).should == 2.71
64
+ end
65
+
66
+ it "of 45 should be 3.04" do
67
+ Basic::Stats.critical_z(45).should == 3.04
68
+ end
69
+
70
+ it "of 200 should be 3.49" do
71
+ Basic::Stats.critical_z(200).should == 3.49
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basic-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Petersen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Basic statistical functions for Ruby collections, such as mean, standard_deviation,
56
+ z, and outlier detection
57
+ email:
58
+ - chris@petersen.io
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - .rspec
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - basic-stats.gemspec
70
+ - lib/basic/stats.rb
71
+ - lib/basic/stats/version.rb
72
+ - spec/spec_helper.rb
73
+ - spec/stats_spec.rb
74
+ homepage: http://github.com/cpetersen/basic-stats
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Basic statistical functions for Ruby collections, such as mean, standard_deviation,
98
+ z, and outlier detection
99
+ test_files:
100
+ - spec/spec_helper.rb
101
+ - spec/stats_spec.rb