descriptive-statistics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -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/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in descriptive-statistics.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Julian Tescher
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.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # DescriptiveStatistics
2
+
3
+ This gem calculates measures of central tendency (e.g. mean, median mode), dispersion (e.g. range, and quartiles), and
4
+ spread (e.g variance and standard deviation)
5
+
6
+ [![Build Status](https://secure.travis-ci.org/jtescher/descriptive-statistics.png)]
7
+ (http://travis-ci.org/jtescher/descriptive-statistics)
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'descriptive-statistics'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install descriptive-statistics
21
+
22
+ ## Usage
23
+
24
+ ### Central Tendency:
25
+ ```ruby
26
+ stats = DescriptiveStatistics.new([1,1,2,3,10])
27
+ stats.mean #=> 3.4
28
+ stats.median #=> 2
29
+ stats.mode #=> 1
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'descriptive-statistics/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "descriptive-statistics"
8
+ gem.version = Descriptive::Statistics::VERSION
9
+ gem.authors = ["Julian Tescher"]
10
+ gem.email = ["virulent@gmail.com"]
11
+ gem.description = %q{Descriptive Statistics Calculator}
12
+ gem.summary = %q{Simply calculate measures of central tendency (e.g. mean, median mode), dispersion (e.g. range
13
+ and quartiles), and spread (e.g variance and standard deviation)}
14
+ gem.homepage = "https://github.com/jtescher/descriptive-stats"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency "rspec"
22
+ gem.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,13 @@
1
+ require "descriptive-statistics/version"
2
+ require 'descriptive-statistics/central-tendency'
3
+
4
+ class DescriptiveStatistics
5
+ extend Forwardable
6
+ include CentralTendency
7
+
8
+ def initialize(data)
9
+ @data = data
10
+ end
11
+
12
+ def_delegators :@data, :length, :inject, :sort, :each_with_object
13
+ end
@@ -0,0 +1,46 @@
1
+ class DescriptiveStatistics
2
+ module CentralTendency
3
+ def sum
4
+ inject(0, :+)
5
+ end
6
+
7
+ def mean
8
+ return if length < 1
9
+ sum / length.to_f
10
+ end
11
+
12
+ def median
13
+ return if length < 1
14
+ sorted = self.sort
15
+
16
+ if length % 2 == 0
17
+ (sorted[(length/2) -1 ] + sorted[length / 2]) / 2.0
18
+ else
19
+ sorted[length / 2]
20
+ end
21
+ end
22
+
23
+ def mode
24
+ return if length < 1
25
+ frequency_distribution = each_with_object(Hash.new(0)) { |value, hash| hash[value] += 1 }
26
+ top_2 = frequency_distribution.sort { |a,b| b[1] <=> a[1] } .take(2)
27
+ if top_2.length == 1
28
+ top_2.first.first # Only one value in distribution, so it's the mode.
29
+ elsif top_2.first.last == top_2.last.last
30
+ nil # Equal frequency, no mode.
31
+ else
32
+ top_2.first.first # Most frequent is mode.
33
+ end
34
+ end
35
+
36
+ def sample_variance
37
+ sum = self.inject(0) {|accumulator, value| accumulator + (value - mean) ** 2 }
38
+ sum / (length.to_f - 1)
39
+ end
40
+
41
+ def standard_deviation
42
+ return if length < 2
43
+ Math.sqrt(sample_variance)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module Descriptive
2
+ module Statistics
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe DescriptiveStatistics::CentralTendency do
4
+ describe '#sum' do
5
+ it 'returns the amount attained by adding all numbers in the array' do
6
+ DescriptiveStatistics.new([1,2,6]).sum.should == 9
7
+ end
8
+
9
+ it 'returns 0 if empty' do
10
+ DescriptiveStatistics.new([]).sum.should == 0
11
+ end
12
+ end
13
+
14
+ describe '#mean' do
15
+ it 'returns the sum of the values over the count of the values' do
16
+ DescriptiveStatistics.new([1,2,6]).mean.should == 3
17
+ end
18
+
19
+ it 'returns nil if empty' do
20
+ DescriptiveStatistics.new([]).mean.should be_nil
21
+ end
22
+ end
23
+
24
+ describe '#median' do
25
+ it 'returns the value lying at the midpoint of the array' do
26
+ DescriptiveStatistics.new([1,2,6]).median.should == 2
27
+ end
28
+
29
+ it 'returns the mean of the two midpoint values if the array length is even' do
30
+ DescriptiveStatistics.new([1,2,3,6]).median.should == 2.5
31
+ end
32
+
33
+ it 'returns nil if empty' do
34
+ DescriptiveStatistics.new([]).median.should be_nil
35
+ end
36
+ end
37
+
38
+ describe '#mode' do
39
+ it 'returns the most frequent value' do
40
+ DescriptiveStatistics.new([1,1,2,46]).mode.should == 1
41
+ end
42
+
43
+ it 'returns nil if there is no most frequent value' do
44
+ DescriptiveStatistics.new([1,2,3]).mode.should be_nil
45
+ end
46
+
47
+ it 'returns nil if empty' do
48
+ DescriptiveStatistics.new([]).mode.should be_nil
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+ require 'descriptive-statistics'
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: descriptive-statistics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Julian Tescher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Descriptive Statistics Calculator
47
+ email:
48
+ - virulent@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .travis.yml
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - descriptive-statistics.gemspec
60
+ - lib/descriptive-statistics.rb
61
+ - lib/descriptive-statistics/central-tendency.rb
62
+ - lib/descriptive-statistics/version.rb
63
+ - spec/lib/descriptive-statistics/central_tendency_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: https://github.com/jtescher/descriptive-stats
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: 2378606324476284759
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: 2378606324476284759
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.24
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Simply calculate measures of central tendency (e.g. mean, median mode), dispersion
95
+ (e.g. range and quartiles), and spread (e.g variance and standard deviation)
96
+ test_files:
97
+ - spec/lib/descriptive-statistics/central_tendency_spec.rb
98
+ - spec/spec_helper.rb