easystats 0.3.2 → 0.4.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1,2 +1 @@
1
- rvm 1.9.3@easystats --install --create
2
- rvm_project_rvmrc_default=1
1
+ rvm 1.9.3
data/.travis.yml CHANGED
@@ -2,7 +2,14 @@ rvm:
2
2
  - 1.8.7
3
3
  - 1.9.2
4
4
  - 1.9.3
5
- - rbx
6
- - jruby
7
- - ruby-head
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - jruby-head
8
+ - rbx-18mode
9
+ - rbx-19mode
8
10
  - ree
11
+ - ruby-head
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: jruby-head
15
+ - rvm: ruby-head
data/Guardfile CHANGED
@@ -1,4 +1,4 @@
1
- guard 'rspec', :version => 2, :rvm => %w[1.8.7 1.9.3].map { |v| v << "@easystats" } do
1
+ guard 'rspec', :version => 2, :rvm => %w[1.9.3] do
2
2
  watch(%r{^spec/.+_spec\.rb$})
3
3
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
4
  watch('spec/spec_helper.rb') { "spec" }
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Easystats [![Build Status](https://secure.travis-ci.org/mgrigajtis/easystats.png)](https://secure.travis-ci.org/mgrigajtis/easystats)
1
+ # Easystats [![Build Status](https://secure.travis-ci.org/mgrigajtis/easystats.png)](https://secure.travis-ci.org/mgrigajtis/easystats) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/mgrigajtis/easystats)
2
2
 
3
3
  > Created by [Matthew Grigajtis](http://www.matthewgrigajtis.com)
4
4
 
data/easystats.gemspec CHANGED
@@ -22,5 +22,5 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "guard-rspec"
23
23
  s.add_development_dependency "rake"
24
24
  s.add_development_dependency "rspec"
25
- s.add_development_dependency "simplecov"
25
+ s.add_development_dependency "simplecov" if RUBY_DESCRIPTION.start_with? "ruby 1.9"
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module Easystats
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0.rc1"
3
3
  end
data/lib/easystats.rb CHANGED
@@ -1,144 +1,77 @@
1
1
  class Array
2
- # take in an array of numbers and calculate the mean (average)
3
2
  def mean
4
- return if self.empty?
3
+ return if empty?
5
4
 
6
- self.sum / self.count.to_f
5
+ sum / count.to_f
7
6
  end unless method_defined? :mean
8
7
 
9
8
  alias_method :average, :mean unless method_defined? :average
10
9
 
11
- # take in the array of numbers and calculate the median
12
10
  def median
13
- return if self.empty?
11
+ return if empty?
14
12
 
15
- data = self
13
+ data = sort
16
14
 
17
15
  halfway = data.count / 2
18
16
 
19
- # Sort the array
20
- data = data.sort
21
-
22
- # The median will be different based on the number of numbers in the array
23
- # If there is an even number in the array
24
- if(data.count % 2) == 0
25
- median = (data[halfway] + data[halfway-1]) / 2.0
26
-
27
- # Else, there is an odd number of elements in the array
17
+ if data.count.even?
18
+ (data[halfway] + data[halfway - 1]) / 2.0
28
19
  else
29
- median = data[halfway]
20
+ data[halfway]
30
21
  end
31
-
32
- median
33
22
  end unless method_defined? :median
34
23
 
35
- # take in an array of numbers and return the mode
36
24
  def mode
37
- return if self.empty?
38
-
39
- # Sort the array
40
- data = self.sort
25
+ return if empty?
26
+ return first if one?
27
+ return if self == uniq
41
28
 
42
- # create a flag to tell the user if all the numbers only appear once
43
- no_mode = true
29
+ frequencies = inject(Hash.new(0)) { |k,v| k[v] += 1; k }
30
+ frequencies = frequencies.sort_by { |k,v| v }
44
31
 
45
- # The variable that will hold the highest number
46
- highest_value = 0
32
+ return if frequencies[-1][1] == frequencies[-2][1]
47
33
 
48
- # The variable that holds the most time the value appears
49
- most_times = 0
50
-
51
- # Create a new hash to hold the numbers
52
- tmp = Hash.new
34
+ frequencies.last[0]
35
+ end unless method_defined? :mode
53
36
 
54
- # Populate the hash
55
- data.each do |num|
56
- if tmp["#{num}"].nil? == false
57
- tmp["#{num}"] = tmp["#{num}"].to_i + 1
58
- else
59
- tmp["#{num}"] = 1
60
- end
61
- end
37
+ def probability_distribution
38
+ return if empty?
62
39
 
63
- # Check to make sure that there is a mode
64
- data.each do |num|
65
- if tmp["#{num}"].to_i > 0
66
- no_mode = false
67
- end
68
- end
40
+ total = count.to_f
69
41
 
70
- if no_mode == true
71
- nil
72
- else
73
- data.each do |num|
74
- if tmp["#{num}"].to_i > most_times
75
- highest_value = num
76
- most_times = tmp["#{num}"]
77
- end
78
- end
79
-
80
- # now loop through again just to make sure another number doesn't appear an equal number of times
81
- data.each do |num|
82
- if num != highest_value
83
- if tmp["#{num}"].to_i == most_times
84
- no_mode = true
85
- end
86
- end
87
- end
88
-
89
- if no_mode == true
90
- nil
91
- else
92
- highest_value
93
- end
94
- end
95
- end unless method_defined? :mode
42
+ inject({}) { |result, item|
43
+ result.update({ item => count(item) / total })
44
+ }
45
+ end unless method_defined? :probability_distribution
96
46
 
97
- # take in an array of numbers and calculate the range
98
47
  def range
99
- return if self.empty?
48
+ return if empty?
100
49
 
101
- data = self.sort
102
- data.last - data.first
50
+ max - min
103
51
  end unless method_defined? :range
104
52
 
105
- # take in an array of numbers and calculate the standard deviation
106
53
  def standard_deviation
107
- return if self.empty?
108
- return 0 if self.one?
54
+ return if empty?
55
+ return 0 if one?
109
56
 
110
- Math::sqrt(self.sum_of_deviations_squared / (self.count-1))
57
+ Math::sqrt sum_of_deviations_squared / (count - 1)
111
58
  end unless method_defined? :standard_deviation
112
59
 
113
- # take in an array of numbers and calculate the sum
114
60
  def sum
115
- self.reduce { |total, number| total + number }
61
+ reduce :+
116
62
  end unless method_defined? :sum
117
63
 
118
64
  def variance
119
- return if self.empty?
65
+ return if empty?
120
66
 
121
- self.sum_of_deviations_squared / self.count.to_f
67
+ sum_of_deviations_squared / count.to_f
122
68
  end unless method_defined? :variance
123
69
 
124
70
  protected
125
71
 
126
- # this function returns the sum of each squared difference of mean
127
72
  def sum_of_deviations_squared
128
- data = self
129
-
130
- deviations = Array.new
131
- average = self.mean
132
- sum_of_deviations_squared = 0
133
-
134
- data.each do |num|
135
- deviations.push((num-average)**2)
136
- end
137
-
138
- deviations.each do |num|
139
- sum_of_deviations_squared += num
140
- end
73
+ mean = self.mean
141
74
 
142
- sum_of_deviations_squared
75
+ inject(0) { |total, number| total + ((number - mean) ** 2) }
143
76
  end
144
77
  end
@@ -12,7 +12,15 @@ describe Array do
12
12
  :range => nil,
13
13
  :standard_deviation => nil,
14
14
  :sum => nil,
15
- :variance => nil
15
+ :variance => nil,
16
+ :probability_distribution => nil
17
+ }
18
+
19
+ build_stats_spec [1,1,1,2,2], {
20
+ :probability_distribution => {
21
+ 1 => 0.6,
22
+ 2 => 0.4
23
+ }
16
24
  }
17
25
 
18
26
  build_stats_spec [0], {
@@ -22,7 +30,8 @@ describe Array do
22
30
  :range => 0,
23
31
  :standard_deviation => 0,
24
32
  :sum => 0,
25
- :variance => 0.0
33
+ :variance => 0.0,
34
+ :probability_distribution => {0 => 1.0}
26
35
  }
27
36
 
28
37
  build_stats_spec [1], {
@@ -32,7 +41,8 @@ describe Array do
32
41
  :range => 0,
33
42
  :standard_deviation => 0,
34
43
  :sum => 1,
35
- :variance => 0.0
44
+ :variance => 0.0,
45
+ :probability_distribution => {1 => 1.0}
36
46
  }
37
47
 
38
48
  build_stats_spec [1,2], {
@@ -42,7 +52,8 @@ describe Array do
42
52
  :range => 1,
43
53
  :standard_deviation => 0.707106781186548,
44
54
  :sum => 3,
45
- :variance => 0.25
55
+ :variance => 0.25,
56
+ :probability_distribution => {1 => 0.5, 2 => 0.5}
46
57
  }
47
58
 
48
59
  build_stats_spec [1,2,3], {
@@ -52,7 +63,8 @@ describe Array do
52
63
  :range => 2,
53
64
  :standard_deviation => 1,
54
65
  :sum => 6,
55
- :variance => 2.0 / 3.0
66
+ :variance => 2.0 / 3.0,
67
+ :probability_distribution => {1 => 0.3333333333333333, 2 => 0.3333333333333333, 3 => 0.3333333333333333}
56
68
  }
57
69
 
58
70
  build_stats_spec [1,2,3,4], {
@@ -62,9 +74,21 @@ describe Array do
62
74
  :range => 3,
63
75
  :standard_deviation => 1.29099444873581,
64
76
  :sum => 10,
65
- :variance => 1.25
77
+ :variance => 1.25,
78
+ :probability_distribution => {1 => 0.25, 2=>0.25, 3=> 0.25, 4=>0.25}
79
+
66
80
  }
67
81
 
82
+ build_stats_spec [1,1,2,2], {
83
+ :mean => 1.5,
84
+ :median => 1.5,
85
+ :mode => nil,
86
+ :range => 1,
87
+ :standard_deviation => 0.5773502691896257,
88
+ :sum => 6,
89
+ :variance => 0.25,
90
+ :probability_distribution => {1=>0.5, 2=>0.5}
91
+ }
68
92
 
69
93
  build_stats_spec [1,2,2,4], {
70
94
  :mean => 2.25,
@@ -73,10 +97,10 @@ describe Array do
73
97
  :range => 3,
74
98
  :standard_deviation => 1.2583057392117916,
75
99
  :sum => 9,
76
- :variance => 1.1875
100
+ :variance => 1.1875,
101
+ :probability_distribution => { 1 => 0.25, 2 => 0.5, 4 => 0.25 }
77
102
  }
78
103
 
79
-
80
104
  build_stats_spec [1,3,3,4], {
81
105
  :mean => 2.75,
82
106
  :median => 3,
@@ -84,7 +108,8 @@ describe Array do
84
108
  :range => 3,
85
109
  :standard_deviation => 1.2583057392117916,
86
110
  :sum => 11,
87
- :variance => 1.1875
111
+ :variance => 1.1875,
112
+ :probability_distribution => { 1 => 0.25, 3 => 0.5, 4=>0.25 }
88
113
  }
89
114
 
90
115
  build_stats_spec (0..100).to_a, {
@@ -114,7 +139,8 @@ describe Array do
114
139
  :range => 2,
115
140
  :standard_deviation => 1,
116
141
  :sum => 0,
117
- :variance => 0.6666666666666666
142
+ :variance => 0.6666666666666666,
143
+ :probability_distribution => { -1 => 0.3333333333333333, 0 => 0.3333333333333333, 1 => 0.3333333333333333 }
118
144
  }
119
145
 
120
146
  build_stats_spec [1,2.5], {
@@ -124,7 +150,8 @@ describe Array do
124
150
  :range => 1.5,
125
151
  :standard_deviation => 1.0606601717798212,
126
152
  :sum => 3.5,
127
- :variance =>0.5625
153
+ :variance =>0.5625,
154
+ :probability_distribution => { 1 => 0.5, 2.5=>0.5 }
128
155
  }
129
156
  end
130
157
 
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  puts RUBY_DESCRIPTION
2
2
 
3
- if RUBY_VERSION.to_f >= 1.9
3
+ if RUBY_DESCRIPTION.start_with? "ruby 1.9"
4
4
  require 'simplecov'
5
5
  SimpleCov.start
6
6
  end
@@ -10,11 +10,17 @@ require 'easystats'
10
10
  def build_stats_spec(array, expectations)
11
11
  expectations.each do |method, expectation|
12
12
  describe "##{method}" do
13
- context "when #{array}" do
13
+ context "when #{array.inspect}" do
14
14
  if expectation && method == :standard_deviation
15
15
  it "should be within #{DELTA} of #{expectation.inspect}" do
16
16
  array.send(method).should be_within(DELTA).of(expectation)
17
17
  end
18
+ elsif expectation && method == :probability_distribution
19
+ it "should return hash #{expectation.inspect}" do
20
+ array.send(method).each do |key,value|
21
+ value.should be_within(DELTA).of(expectation[key])
22
+ end
23
+ end
18
24
  else
19
25
  it "should be #{expectation.inspect}" do
20
26
  array.send(method).should == expectation
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easystats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
5
- prerelease:
4
+ version: 0.4.0.rc1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Matthew Grigajtis
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-04-17 00:00:00.000000000 Z
13
+ date: 2012-09-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: guard-rspec
17
- requirement: &70193074432080 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,15 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70193074432080
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
26
31
  - !ruby/object:Gem::Dependency
27
32
  name: rake
28
- requirement: &70193074431400 !ruby/object:Gem::Requirement
33
+ requirement: !ruby/object:Gem::Requirement
29
34
  none: false
30
35
  requirements:
31
36
  - - ! '>='
@@ -33,10 +38,15 @@ dependencies:
33
38
  version: '0'
34
39
  type: :development
35
40
  prerelease: false
36
- version_requirements: *70193074431400
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
37
47
  - !ruby/object:Gem::Dependency
38
48
  name: rspec
39
- requirement: &70193074430520 !ruby/object:Gem::Requirement
49
+ requirement: !ruby/object:Gem::Requirement
40
50
  none: false
41
51
  requirements:
42
52
  - - ! '>='
@@ -44,10 +54,15 @@ dependencies:
44
54
  version: '0'
45
55
  type: :development
46
56
  prerelease: false
47
- version_requirements: *70193074430520
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
48
63
  - !ruby/object:Gem::Dependency
49
64
  name: simplecov
50
- requirement: &70193074429080 !ruby/object:Gem::Requirement
65
+ requirement: !ruby/object:Gem::Requirement
51
66
  none: false
52
67
  requirements:
53
68
  - - ! '>='
@@ -55,7 +70,12 @@ dependencies:
55
70
  version: '0'
56
71
  type: :development
57
72
  prerelease: false
58
- version_requirements: *70193074429080
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
59
79
  description: This gem contains statistics functions that are very easy to use. Much
60
80
  easier and much more complete than many of the other statistical gems available
61
81
  out there. If you need a feature added, send me a message on Github!
@@ -94,15 +114,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
114
  required_rubygems_version: !ruby/object:Gem::Requirement
95
115
  none: false
96
116
  requirements:
97
- - - ! '>='
117
+ - - ! '>'
98
118
  - !ruby/object:Gem::Version
99
- version: '0'
119
+ version: 1.3.1
100
120
  requirements: []
101
121
  rubyforge_project: easystats
102
- rubygems_version: 1.8.17
122
+ rubygems_version: 1.8.23
103
123
  signing_key:
104
124
  specification_version: 3
105
125
  summary: Easy to use statistics functions
106
126
  test_files:
107
127
  - spec/lib/easystats_spec.rb
108
128
  - spec/spec_helper.rb
129
+ has_rdoc: