easystats 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@ pkg/*
2
2
  *.gem
3
3
  .bundle
4
4
  Gemfile.lock
5
+ coverage/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -2,7 +2,7 @@ rvm:
2
2
  - 1.8.7
3
3
  - 1.9.2
4
4
  - 1.9.3
5
- - rbx-2.0
5
+ - rbx
6
6
  - jruby
7
7
  - ruby-head
8
8
  - ree
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source :rubygems
2
2
 
3
3
  gemspec
4
4
 
data/Guardfile CHANGED
@@ -1,5 +1,6 @@
1
- guard :test do
2
- watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
3
- watch(%r{^test/.+_test\.rb$})
4
- watch('test/test_helper.rb') { "test" }
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
5
  end
6
+
@@ -0,0 +1,51 @@
1
+ # Easystats [![Build Status](https://secure.travis-ci.org/mgrigajtis/easystats.png)](https://secure.travis-ci.org/mgrigajtis/easystats)
2
+
3
+ > Created by [Matthew Grigajtis](http://www.matthewgrigajtis.com)
4
+
5
+ Provides easy to use statistical functions to use on an array
6
+
7
+ ## Install
8
+
9
+ In your shell:
10
+
11
+ ```sh
12
+ gem install easystats
13
+ ```
14
+
15
+ or in your Gemfile:
16
+
17
+ ```rb
18
+ gem 'easystats'
19
+ ```
20
+
21
+ ## Example
22
+
23
+ ```rb
24
+ require 'easystats'
25
+
26
+ array = [4, 8, 15, 16, 23, 42, 42]
27
+
28
+ %w[
29
+ average
30
+ median
31
+ mode
32
+ range
33
+ standard_deviation
34
+ sum
35
+ variance
36
+ ].each do |method|
37
+ puts "#{method}: #{array.send(method.to_sym)}"
38
+ end
39
+ ```
40
+
41
+ This will result in:
42
+
43
+ ```sh
44
+ average: 21.428571428571427
45
+ median: 16
46
+ mode: 42
47
+ range: 38
48
+ standard_deviation: 15.295501984321435
49
+ sum: 150
50
+ variance: 200.53061224489798
51
+ ```
data/Rakefile CHANGED
@@ -4,12 +4,11 @@ require 'rake/testtask'
4
4
 
5
5
  Bundler::GemHelper.install_tasks
6
6
 
7
- desc 'Default: run unit tests.'
8
- task :default => :test
9
-
10
- desc 'Run unit tests.'
11
- Rake::TestTask.new(:test) do |t|
12
- t.libs << 'lib'
13
- t.pattern = 'test/*_test.rb'
14
- t.verbose = true
15
- end
7
+ require 'rspec/core/rake_task'
8
+
9
+ desc 'Default: run specs.'
10
+ task :default => :spec
11
+
12
+ desc "Run specs"
13
+ RSpec::Core::RakeTask.new
14
+
@@ -1,8 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
3
  require "easystats/version"
4
- require "complex"
5
- include Math
6
4
 
7
5
  Gem::Specification.new do |s|
8
6
  s.name = "easystats"
@@ -21,5 +19,8 @@ Gem::Specification.new do |s|
21
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
20
  s.require_paths = ["lib"]
23
21
 
24
- s.add_development_dependency "guard-test"
22
+ s.add_development_dependency "guard-rspec"
23
+ s.add_development_dependency "rake"
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "simplecov"
25
26
  end
@@ -1,169 +1,143 @@
1
1
  class Array
2
+ # take in an array of numbers and calculate the mean (average)
3
+ def mean
4
+ return unless self.any?
2
5
 
3
- # take in an array of numbers and calculate the sum
4
- def sum
5
- data = self
6
- sum_of_numbers = 0
6
+ self.sum / self.count.to_f
7
+ end unless Array.instance_methods.include? "mean"
8
+ alias_method :average, :mean unless Array.instance_methods.include? "average"
7
9
 
8
- # Get the total sum only if there are actually numbers to total
9
- if data.count > 0
10
- data.each do |num|
11
- sum_of_numbers += num
12
- end
13
- else
14
- sum_of_numbers = 0
15
- end
10
+ # take in the array of numbers and calculate the median
11
+ def median
12
+ return unless self.any?
16
13
 
17
- sum_of_numbers
18
- end unless Array.instance_methods.include? "sum"
14
+ data = self
19
15
 
20
- # take in an array of numbers and calculate the mean (average)
21
- def mean
22
- data = self
23
-
24
- # Check to make sure there are numbers to avoid division by 0
25
- if data.count > 0
26
- self.sum / data.count.to_f
27
- else
28
- 0
29
- end
30
- end unless Array.instance_methods.include? "mean"
31
- alias_method :average, :mean unless Array.instance_methods.include? "average"
32
-
33
- # take in an array of numbers and calculate the standard deviation
34
- def standard_deviation
35
- data = self
36
- sum_of_deviations_squared = self.sum_of_deviations_squared
37
-
38
- if data.count > 1
39
- Math::sqrt(sum_of_deviations_squared / (data.count-1))
40
- else
41
- 0
42
- end
43
- end unless Array.instance_methods.include? "standard_deviation"
44
-
45
- def variance
46
- data = self
47
- average = self.mean
48
- sum_of_deviations = self.sum_of_deviations_squared
49
-
50
- if data.count > 0
51
- sum_of_deviations / data.count.to_f
52
- else
53
- 0
54
- end
55
- end unless Array.instance_methods.include? "variance"
16
+ halfway = data.count / 2
56
17
 
57
- # take in the array of numbers and calculate the median
58
- def median
59
- data = self
18
+ # Sort the array
19
+ data = data.sort
60
20
 
61
- halfway = data.count / 2
21
+ # The median will be different based on the number of numbers in the array
22
+ # If there is an even number in the array
23
+ if(data.count % 2) == 0
24
+ median = (data[halfway] + data[halfway-1]) / 2.0
62
25
 
63
- # Sort the array
64
- data = data.sort
26
+ # Else, there is an odd number of elements in the array
27
+ else
28
+ median = data[halfway]
29
+ end
65
30
 
66
- # The median will be different based on the number of numbers in the array
67
- # If there is an even number in the array
68
- if(data.count % 2) == 0
69
- median = (data[halfway] + data[halfway-1]) / 2.0
31
+ median
32
+ end unless Array.instance_methods.include? "median"
70
33
 
71
- # Else, there is an odd number of elements in the array
72
- else
73
- median = data[halfway]
74
- end
34
+ # take in an array of numbers and return the mode
35
+ def mode
36
+ return unless self.any?
75
37
 
76
- median
77
- end unless Array.instance_methods.include? "median"
38
+ # Sort the array
39
+ data = self.sort
78
40
 
79
- # take in an array of numbers and calculate the range
80
- def range
81
- data = self
82
- data = data.sort
83
- data[data.count-1] - data[0]
84
- end unless Array.instance_methods.include? "range"
41
+ # create a flag to tell the user if all the numbers only appear once
42
+ no_mode = true
85
43
 
86
- # take in an array of numbers and return the mode
87
- def mode
88
- data = self
44
+ # The variable that will hold the highest number
45
+ highest_value = 0
89
46
 
90
- # Sort the array
91
- data = data.sort
47
+ # The variable that holds the most time the value appears
48
+ most_times = 0
92
49
 
93
- # create a flag to tell the user if all the numbers only appear once
94
- no_mode = true
50
+ # Create a new hash to hold the numbers
51
+ tmp = Hash.new
95
52
 
96
- # The variable that will hold the highest number
97
- highest_value = 0
98
-
99
- # The variable that holds the most time the value appears
100
- most_times = 0
53
+ # Populate the hash
54
+ data.each do |num|
55
+ if tmp["#{num}"].nil? == false
56
+ tmp["#{num}"] = tmp["#{num}"].to_i + 1
57
+ else
58
+ tmp["#{num}"] = 1
59
+ end
60
+ end
101
61
 
102
- # Create a new hash to hold the numbers
103
- tmp = Hash.new
62
+ # Check to make sure that there is a mode
63
+ data.each do |num|
64
+ if tmp["#{num}"].to_i > 0
65
+ no_mode = false
66
+ end
67
+ end
104
68
 
105
- # Populate the hash
69
+ if no_mode == true
70
+ nil
71
+ else
106
72
  data.each do |num|
107
- if tmp["#{num}"].nil? == false
108
- tmp["#{num}"] = tmp["#{num}"].to_i + 1
109
- else
110
- tmp["#{num}"] = 1
73
+ if tmp["#{num}"].to_i > most_times
74
+ highest_value = num
75
+ most_times = tmp["#{num}"]
111
76
  end
112
77
  end
113
78
 
114
- # Check to make sure that there is a mode
79
+ # now loop through again just to make sure another number doesn't appear an equal number of times
115
80
  data.each do |num|
116
- if tmp["#{num}"].to_i > 0
117
- no_mode = false
81
+ if num != highest_value
82
+ if tmp["#{num}"].to_i == most_times
83
+ no_mode = true
84
+ end
118
85
  end
119
86
  end
120
-
87
+
121
88
  if no_mode == true
122
- 0
89
+ nil
123
90
  else
124
- data.each do |num|
125
- if tmp["#{num}"].to_i > most_times
126
- highest_value = num
127
- most_times = tmp["#{num}"]
128
- end
129
- end
130
-
131
- # now loop through again just to make sure another number doesn't appear an equal number of times
132
- data.each do |num|
133
- if num != highest_value
134
- if tmp["#{num}"].to_i == most_times
135
- no_mode = true
136
- end
137
- end
138
- end
139
-
140
- if no_mode == true
141
- nil
142
- else
143
- highest_value
144
- end
91
+ highest_value
145
92
  end
146
- end unless Array.instance_methods.include? "mode"
147
-
148
- protected
149
-
150
- # this function returns the sum of each squared difference of mean
151
- def sum_of_deviations_squared
152
- data = self
153
-
154
- deviations = Array.new
155
- average = self.mean
156
- sum_of_deviations_squared = 0
93
+ end
94
+ end unless Array.instance_methods.include? "mode"
157
95
 
158
- data.each do |num|
159
- deviations.push((num-average)**2)
160
- end
96
+ # take in an array of numbers and calculate the range
97
+ def range
98
+ return unless self.any?
161
99
 
162
- deviations.each do |num|
163
- sum_of_deviations_squared += num
164
- end
100
+ data = self.sort
101
+ data.last - data.first
102
+ end unless Array.instance_methods.include? "range"
103
+
104
+ # take in an array of numbers and calculate the standard deviation
105
+ def standard_deviation
106
+ return unless self.any?
107
+ return 0 if self.one?
165
108
 
166
- sum_of_deviations_squared
109
+ Math::sqrt(self.sum_of_deviations_squared / (self.count-1))
110
+ end unless Array.instance_methods.include? "standard_deviation"
111
+
112
+ # take in an array of numbers and calculate the sum
113
+ def sum
114
+ self.reduce { |total, number| total + number }
115
+ end unless Array.instance_methods.include? "sum"
116
+
117
+ def variance
118
+ return unless self.any?
119
+
120
+ self.sum_of_deviations_squared / self.count.to_f
121
+ end unless Array.instance_methods.include? "variance"
122
+
123
+ protected
124
+
125
+ # this function returns the sum of each squared difference of mean
126
+ def sum_of_deviations_squared
127
+ data = self
128
+
129
+ deviations = Array.new
130
+ average = self.mean
131
+ sum_of_deviations_squared = 0
132
+
133
+ data.each do |num|
134
+ deviations.push((num-average)**2)
167
135
  end
168
-
136
+
137
+ deviations.each do |num|
138
+ sum_of_deviations_squared += num
139
+ end
140
+
141
+ sum_of_deviations_squared
142
+ end
169
143
  end
@@ -1,3 +1,3 @@
1
1
  module Easystats
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+ describe "#mean" do
5
+ it { [].mean.should be_nil }
6
+ it { [1].mean.should == 1 }
7
+ it { [1,2].mean.should == 1.5 }
8
+ it { [1,2,3].mean.should == 2 }
9
+ it { [1,2,3,4].mean.should == 2.5 }
10
+ end
11
+
12
+ describe "#average" do
13
+ it { [1,2,3,4].average.should == 2.5 }
14
+ end
15
+
16
+ describe "#median" do
17
+ it { [].median.should be_nil }
18
+ it { [1].median.should == 1 }
19
+ it { [1,2].median.should == 1.5 }
20
+ it { [1,2,3].median.should == 2 }
21
+ it { [1,2,3,4].median.should == 2.5 }
22
+ it { [1,2,2,4].median.should == 2 }
23
+ it { [1,3,3,4].median.should == 3 }
24
+ end
25
+
26
+ describe "#mode" do
27
+ it { [].mode.should be_nil }
28
+ it { [1].mode.should == 1 }
29
+ it { [1,2].mode.should be_nil }
30
+ it { [1,2,3].mode.should be_nil }
31
+ it { [1,2,3,4].mode.should be_nil }
32
+ it { [1,2,2,4].mode.should == 2 }
33
+ it { [1,3,3,4].mode.should == 3 }
34
+ end
35
+
36
+ describe "#range" do
37
+ it { [].range.should be_nil }
38
+ it { [1].range.should == 0 }
39
+ it { [1,2].range.should == 1 }
40
+ it { [1,2,3].range.should == 2 }
41
+ it { [1,2,3,4].range.should == 3 }
42
+ it { [1,2,2,4].range.should == 3 }
43
+ it { [1,3,3,4].range.should == 3 }
44
+ end
45
+
46
+ describe "#standard_deviation" do
47
+ let(:delta) { 0.00000000000001 }
48
+
49
+ it { [].standard_deviation.should be_nil }
50
+ it { [1].standard_deviation.should == 0 }
51
+ it { [1,2].standard_deviation.should be_within(delta).of(0.707106781186548) }
52
+ it { [1,2,3].standard_deviation.should be_within(delta).of(1) }
53
+ it { [1,2,3,4].standard_deviation.should be_within(delta).of(1.29099444873581) }
54
+ end
55
+
56
+ describe "#sum" do
57
+ it { [].sum.should be_nil }
58
+ it { [1].sum.should == 1 }
59
+ it { [1,2].sum.should == 3 }
60
+ it { [1,2,3].sum.should == 6 }
61
+ it { [1,2,3,4].sum.should == 10 }
62
+ it { [1,2,2,4].sum.should == 9 }
63
+ it { [1,3,3,4].sum.should == 11 }
64
+ end
65
+
66
+ describe "#variance" do
67
+ it { [].variance.should be_nil }
68
+ it { [1].variance.should == 0.0 }
69
+ it { [1,2].variance.should == 0.25 }
70
+ it { [1,2,3].variance.should == 2.0/3.0 }
71
+ it { [1,2,3,4].variance.should == 1.25 }
72
+ end
73
+ end
74
+
@@ -0,0 +1,5 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'easystats'
5
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easystats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,8 +13,8 @@ cert_chain: []
13
13
  date: 2012-04-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: guard-test
17
- requirement: &70355012085360 !ruby/object:Gem::Requirement
16
+ name: guard-rspec
17
+ requirement: &70146983589700 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,40 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70355012085360
25
+ version_requirements: *70146983589700
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &70146983589240 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70146983589240
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &70146983588620 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70146983588620
48
+ - !ruby/object:Gem::Dependency
49
+ name: simplecov
50
+ requirement: &70146983588020 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70146983588020
26
59
  description: This gem contains statistics functions that are very easy to use. Much
27
60
  easier and much more complete than many of the other statistical gems available
28
61
  out there. If you need a feature added, send me a message on Github!
@@ -34,17 +67,18 @@ extensions: []
34
67
  extra_rdoc_files: []
35
68
  files:
36
69
  - .gitignore
70
+ - .rspec
37
71
  - .rvmrc
38
72
  - .travis.yml
39
73
  - Gemfile
40
74
  - Guardfile
41
- - README
75
+ - README.md
42
76
  - Rakefile
43
77
  - easystats.gemspec
44
78
  - lib/easystats.rb
45
79
  - lib/easystats/version.rb
46
- - test/easystats_test.rb
47
- - test/test_helper.rb
80
+ - spec/lib/easystats_spec.rb
81
+ - spec/spec_helper.rb
48
82
  homepage: https://github.com/mgrigajtis/easystats
49
83
  licenses: []
50
84
  post_install_message:
@@ -70,5 +104,5 @@ signing_key:
70
104
  specification_version: 3
71
105
  summary: Easy to use statistics functions
72
106
  test_files:
73
- - test/easystats_test.rb
74
- - test/test_helper.rb
107
+ - spec/lib/easystats_spec.rb
108
+ - spec/spec_helper.rb
data/README DELETED
@@ -1,45 +0,0 @@
1
- Gem Name: Easystats
2
- Gem Author: Matthew Grigajtis (http://www.matthewgrigajtis.com)
3
- Description: Provides easy to use statistical functions to use on an array
4
-
5
- Functions Provided:
6
- sum
7
- Returns: The sum of the numbers in the array
8
-
9
- mean
10
- Returns: The mean (average) of the numbers in the array
11
-
12
- average
13
- An alias of mean
14
-
15
- standard_deviation
16
- Returns: The standard deviation of the numbers in the array
17
-
18
- median
19
- Returns: The median of the numbers in the array
20
-
21
- range
22
- Returns: The range of the numbers
23
-
24
- mode
25
- Returns: The mode. If there is no mode returns 0.
26
-
27
- variance
28
- Returns: The variance
29
-
30
- Example usage:
31
-
32
- require "rubygems"
33
- require "easystats"
34
-
35
- # Create a test array of numbers
36
- myNumbers = Array.new
37
- myNumbers = [4, 8, 15, 16, 23, 42]
38
-
39
- puts "Range: " + myNumbers.range.to_s
40
- puts "Sum: " + myNumbers.sum.to_s
41
- puts "Average: " + myNumbers.mean.to_s
42
- puts "Median: " + myNumbers.median.to_s
43
- puts "Variance: " + myNumbers.variance.to_s
44
- puts "Standard Deviation: " + myNumbers.standard_deviation.to_s
45
- puts "Mode: " + myNumbers.mode.to_s
@@ -1,74 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- # TODO: autotest "Unable to map class EasystatsTest to a file"
4
- class EasystatsTest < Test::Unit::TestCase
5
-
6
- def test_mean
7
- assert_equal 1, [1].mean, "1"
8
- assert_equal 1.5, [1,2].mean, "1,2"
9
- assert_equal 2, [1,2,3].mean, "1,2,3"
10
- assert_equal 2.5, [1,2,3,4].mean, "1,2,3,4"
11
- end
12
-
13
- def test_average_synonym_for_mean
14
- assert_equal 2.5, [1,2,3,4].average, "1,2,3,4"
15
- end
16
-
17
- def test_median
18
- assert_equal 1, [1].median, "1"
19
- assert_equal 1.5, [1,2].median, "1,2"
20
- assert_equal 2, [1,2,3].median, "1,2,3"
21
- assert_equal 2.5, [1,2,3,4].median, "1,2,3,4"
22
- assert_equal 2, [1,2,2,4].median, "1,2,2,4"
23
- assert_equal 3, [1,3,3,4].median, "1,3,3,4"
24
- end
25
-
26
- def test_mode
27
- assert_equal 1, [1].mode, "1"
28
- assert_equal nil, [1,2].mode, "1,2"
29
- assert_equal nil, [1,2,3].mode, "1,2,3"
30
- assert_equal nil, [1,2,3,4].mode, "1,2,3,4"
31
- assert_equal 2, [1,2,2,4].mode, "1,2,2,4"
32
- assert_equal 3, [1,3,3,4].mode, "1,3,3,4"
33
- end
34
-
35
- def test_range
36
- assert_equal 0, [1].range, "1"
37
- assert_equal 1, [1,2].range, "1,2"
38
- assert_equal 2, [1,2,3].range, "1,2,3"
39
- assert_equal 3, [1,2,3,4].range, "1,2,3,4"
40
- assert_equal 3, [1,2,2,4].range, "1,2,2,4"
41
- assert_equal 3, [1,3,3,4].range, "1,3,3,4"
42
- end
43
-
44
- def test_sum
45
- assert_equal 1, [1].sum, "1"
46
- assert_equal 3, [1,2].sum, "1,2"
47
- assert_equal 6, [1,2,3].sum, "1,2,3"
48
- assert_equal 10, [1,2,3,4].sum, "1,2,3,4"
49
- assert_equal 9, [1,2,2,4].sum, "1,2,2,4"
50
- assert_equal 11, [1,3,3,4].sum, "1,3,3,4"
51
- end
52
-
53
- def test_variance
54
- assert_equal 0.0, [1].variance, "1"
55
- assert_equal 0.25, [1,2].variance, "1,2"
56
- assert_equal 2.0/3.0, [1,2,3].variance, "1,2,3"
57
- assert_equal 1.25, [1,2,3,4].variance, "1,2,3,4"
58
- end
59
-
60
- def test_standard_deviation
61
- delta = 0.00000000000001
62
- assert_in_delta 0, [1].standard_deviation, delta, "1"
63
- assert_in_delta 0.707106781186548, [1,2].standard_deviation, delta, "1,2"
64
- assert_in_delta 1, [1,2,3].standard_deviation, delta, "1,2,3"
65
- assert_in_delta 1.29099444873581, [1,2,3,4].standard_deviation, delta, "1,2,3,4"
66
- end
67
-
68
- def test_should_not_be_able_to_access_protected_method_sum_of_deviations_squared
69
- assert_raise(NoMethodError) do
70
- [1].sum_of_deviations_squared
71
- end
72
- end
73
-
74
- end
@@ -1,5 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '/../lib')
2
-
3
- require 'rubygems'
4
- require 'test/unit'
5
- require 'easystats'