descriptive-statistics 1.3.4 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +10 -1
- data/README.md +7 -3
- data/lib/descriptive-statistics.rb +1 -1
- data/lib/descriptive-statistics/central-tendency.rb +1 -1
- data/lib/descriptive-statistics/shape.rb +22 -6
- data/lib/descriptive-statistics/version.rb +1 -1
- data/spec/lib/descriptive-statistics/shape_spec.rb +1 -1
- data/spec/lib/descriptive_statistics_spec.rb +9 -7
- metadata +4 -4
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,8 +3,12 @@
|
|
3
3
|
This gem calculates descriptive statistics including measures of central tendency (e.g. mean, median mode), dispersion
|
4
4
|
(e.g. range, and quartiles), and spread (e.g variance and standard deviation).
|
5
5
|
|
6
|
+
Tested against ruby 1.8.7, 1.9.2, 1.9.3, ruby-head, jruby-18mode, jruby-19mode, jruby-head, rbx-18mode, rbx-19mode, and
|
7
|
+
ree
|
8
|
+
|
6
9
|
[![Build Status](https://secure.travis-ci.org/jtescher/descriptive-statistics.png)]
|
7
10
|
(http://travis-ci.org/jtescher/descriptive-statistics)
|
11
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/jtescher/descriptive-statistics)
|
8
12
|
## Installation
|
9
13
|
|
10
14
|
Add this line to your application's Gemfile:
|
@@ -61,12 +65,12 @@ If you want to monkey patch descriptive statistics methods into Enumerable, you
|
|
61
65
|
require 'descriptive-statistics'
|
62
66
|
|
63
67
|
module Enumerable
|
64
|
-
include DescriptiveStatistics::
|
68
|
+
include DescriptiveStatistics::AllMethods
|
65
69
|
|
66
70
|
# Warning: hacky evil meta programming. Required because classes that have already included
|
67
71
|
# Enumerable will not otherwise inherit the statistics methods.
|
68
|
-
DescriptiveStatistics::
|
69
|
-
define_method(m, DescriptiveStatistics::
|
72
|
+
DescriptiveStatistics::AllMethods.instance_methods.each do |m|
|
73
|
+
define_method(m, DescriptiveStatistics::AllMethods.instance_method(m))
|
70
74
|
end
|
71
75
|
end
|
72
76
|
```
|
@@ -22,7 +22,7 @@ class DescriptiveStatistics
|
|
22
22
|
|
23
23
|
def mode
|
24
24
|
return if length < 1
|
25
|
-
frequency_distribution =
|
25
|
+
frequency_distribution = inject(Hash.new(0)) { |hash, value| hash[value] += 1; hash }
|
26
26
|
top_2 = frequency_distribution.sort { |a,b| b[1] <=> a[1] } .take(2)
|
27
27
|
if top_2.length == 1
|
28
28
|
top_2.first.first # Only one value in distribution, so it's the mode.
|
@@ -1,19 +1,35 @@
|
|
1
1
|
class DescriptiveStatistics
|
2
2
|
module Shape
|
3
|
+
|
3
4
|
def skewness
|
4
|
-
return if length
|
5
|
+
return if length == 0
|
5
6
|
return 0 if length == 1
|
6
|
-
sum_cubed_deviation = inject(0) {|sum, value| sum + (value - mean) ** 3}
|
7
|
-
cubed_standard_deviation = standard_deviation ** 3
|
8
7
|
sum_cubed_deviation / ((length - 1) * cubed_standard_deviation.to_f)
|
9
8
|
end
|
10
9
|
|
11
10
|
def kurtosis
|
12
|
-
return if length
|
11
|
+
return if length == 0
|
13
12
|
return 0 if length == 1
|
14
|
-
sum_quarted_deviation = inject(0) {|sum, value| sum + (value - mean) ** 4}
|
15
|
-
quarted_standard_deviation = standard_deviation ** 4
|
16
13
|
sum_quarted_deviation / ((length - 1) * quarted_standard_deviation.to_f)
|
17
14
|
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def sum_cubed_deviation
|
19
|
+
inject(0) {|sum, value| sum + (value - mean) ** 3}
|
20
|
+
end
|
21
|
+
|
22
|
+
def cubed_standard_deviation
|
23
|
+
standard_deviation ** 3
|
24
|
+
end
|
25
|
+
|
26
|
+
def sum_quarted_deviation
|
27
|
+
inject(0) {|sum, value| sum + (value - mean) ** 4}
|
28
|
+
end
|
29
|
+
|
30
|
+
def quarted_standard_deviation
|
31
|
+
standard_deviation ** 4
|
32
|
+
end
|
33
|
+
|
18
34
|
end
|
19
35
|
end
|
@@ -25,7 +25,7 @@ describe DescriptiveStatistics::Shape do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'returns the measure of skewness of the data as high when skewed' do
|
28
|
-
DescriptiveStatistics.new([
|
28
|
+
DescriptiveStatistics.new([99,10,1,1,1]).kurtosis.should == 2.56586117539186
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'returns 0 if only one element' do
|
@@ -2,15 +2,17 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe DescriptiveStatistics do
|
4
4
|
it 'should allow hacky monkey patches to add methods to structures like arrays' do
|
5
|
-
|
6
|
-
|
5
|
+
if RUBY_VERSION == '1.9.3' and RUBY_ENGINE == 'ruby' # Ignore older versions of ruby and Rubinius
|
6
|
+
module Enumerable
|
7
|
+
include DescriptiveStatistics::AllMethods
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
# Warning: hacky evil meta programming. Required to have classes that include array get the methods too.
|
10
|
+
DescriptiveStatistics::AllMethods.instance_methods.each do |m|
|
11
|
+
define_method(m, DescriptiveStatistics::AllMethods.instance_method(m))
|
12
|
+
end
|
11
13
|
end
|
12
|
-
end
|
13
14
|
|
14
|
-
|
15
|
+
[1,2,3].mean.should == 2
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: descriptive-statistics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
85
|
version: '0'
|
86
86
|
segments:
|
87
87
|
- 0
|
88
|
-
hash:
|
88
|
+
hash: 4156932844491246834
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
90
|
none: false
|
91
91
|
requirements:
|
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
version: '0'
|
95
95
|
segments:
|
96
96
|
- 0
|
97
|
-
hash:
|
97
|
+
hash: 4156932844491246834
|
98
98
|
requirements: []
|
99
99
|
rubyforge_project:
|
100
100
|
rubygems_version: 1.8.24
|