descriptive-statistics 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -29,6 +29,21 @@ stats.median #=> 2
29
29
  stats.mode #=> 1
30
30
  ```
31
31
 
32
+ ### Dispersion:
33
+ ```ruby
34
+ stats = DescriptiveStatistics.new([1,1,2,3,10])
35
+ stats.range #=> 9
36
+ stats.percentile_from_value(10) #=> 80
37
+ stats.value_from_percentile(60) #=> 3
38
+ ```
39
+
40
+ ### Spread:
41
+ ```ruby
42
+ stats = DescriptiveStatistics.new([1,1,2,3,10])
43
+ stats.variance #=> 14.299999999999999
44
+ stats.standard_deviation #=> 3.7815340802378072
45
+ ```
46
+
32
47
  ## Contributing
33
48
 
34
49
  1. Fork it
@@ -1,9 +1,13 @@
1
1
  require "descriptive-statistics/version"
2
2
  require 'descriptive-statistics/central-tendency'
3
+ require 'descriptive-statistics/dispersion'
4
+ require 'descriptive-statistics/spread'
3
5
 
4
6
  class DescriptiveStatistics
5
7
  extend Forwardable
6
8
  include CentralTendency
9
+ include Dispersion
10
+ include Spread
7
11
 
8
12
  def initialize(data)
9
13
  @data = data
@@ -32,15 +32,5 @@ class DescriptiveStatistics
32
32
  top_2.first.first # Most frequent is mode.
33
33
  end
34
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
35
  end
46
36
  end
@@ -0,0 +1,20 @@
1
+ class DescriptiveStatistics
2
+ module Dispersion
3
+ def range
4
+ return if length < 1
5
+ sorted = sort
6
+ sorted.last - sorted.first
7
+ end
8
+
9
+ def percentile_from_value(value)
10
+ return if length < 1
11
+ (sort.index(value) / length.to_f * 100).ceil
12
+ end
13
+
14
+ def value_from_percentile(percentile)
15
+ return if length < 1
16
+ value_index = (percentile.to_f / 100 * length).ceil
17
+ sort[value_index]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ class DescriptiveStatistics
2
+ module Spread
3
+ def variance
4
+ return if length < 1
5
+ sum = self.inject(0) {|accumulator, value| accumulator + (value - mean) ** 2 }
6
+ sum / (length.to_f - 1)
7
+ end
8
+
9
+ def population_variance
10
+ return if length < 1
11
+ sum = self.inject(0) {|accumulator, value| accumulator + (value - mean) ** 2 }
12
+ sum / length.to_f
13
+ end
14
+
15
+ def standard_deviation
16
+ return if length < 2
17
+ Math.sqrt(variance)
18
+ end
19
+
20
+ def population_standard_deviation
21
+ return if length < 2
22
+ Math.sqrt(population_variance)
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  module Descriptive
2
2
  module Statistics
3
- VERSION = "0.0.2"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe DescriptiveStatistics::Dispersion do
4
+ describe '#range' do
5
+ it 'returns the mean squared deviation of the sample (n - 1 denominator)' do
6
+ DescriptiveStatistics.new([1,2,6]).range.should == 5
7
+ end
8
+
9
+ it 'returns nil if empty' do
10
+ DescriptiveStatistics.new([]).range.should be_nil
11
+ end
12
+ end
13
+
14
+ describe '#percentile_from_value' do
15
+ it 'returns the precise percentile of each value' do
16
+ data = [95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.061, 95.1591, 95.1195,95.1065, 95.0925, 95.199, 95.1682]
17
+ percentiles = [0,9,17,25,34,42,50,59,67,75,84,92]
18
+ stats = DescriptiveStatistics.new(data)
19
+ data.sort.each_with_index do |datum, i|
20
+ stats.percentile_from_value(datum).should == percentiles[i]
21
+ end
22
+ end
23
+
24
+ it 'returns nil if empty' do
25
+ DescriptiveStatistics.new([]).percentile_from_value(1).should be_nil
26
+ end
27
+ end
28
+
29
+ describe '#value_from_percentile' do
30
+ it 'returns the precise percentile of each value' do
31
+ data = [95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.061, 95.1591, 95.1195,95.1065, 95.0925, 95.199, 95.1682]
32
+ percentiles = [0,10,20,30,40,50,60,70,80,90]
33
+ values = [95.061,95.1065,95.1195,95.1442,95.1567,95.1591,95.1772,95.1937,95.1959,95.199]
34
+ stats = DescriptiveStatistics.new(data)
35
+ percentiles.sort.each_with_index do |percentile, i|
36
+ stats.value_from_percentile(percentile).should == values[i]
37
+ end
38
+ end
39
+
40
+ it 'returns nil if empty' do
41
+ DescriptiveStatistics.new([]).value_from_percentile(1).should be_nil
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe DescriptiveStatistics::Spread do
4
+ describe '#variance' do
5
+ it 'returns the mean squared deviation of the sample (n - 1 denominator)' do
6
+ DescriptiveStatistics.new([1,2,6]).variance.should == 7
7
+ end
8
+
9
+ it 'returns nil if empty' do
10
+ DescriptiveStatistics.new([]).variance.should be_nil
11
+ end
12
+ end
13
+
14
+ describe '#population_variance' do
15
+ it 'returns the mean squared deviation of the sample' do
16
+ DescriptiveStatistics.new([1,2,3,4]).population_variance.should == 1.25
17
+ end
18
+
19
+ it 'returns nil if empty' do
20
+ DescriptiveStatistics.new([]).population_variance.should be_nil
21
+ end
22
+ end
23
+
24
+ describe '#standard_deviation' do
25
+ it 'returns the square root of the variance' do
26
+ DescriptiveStatistics.new([1,2,6]).standard_deviation.should == 2.6457513110645907
27
+ end
28
+
29
+ it 'returns nil if empty' do
30
+ DescriptiveStatistics.new([]).standard_deviation.should be_nil
31
+ end
32
+ end
33
+
34
+ describe '#population_standard_deviation' do
35
+ it 'returns the square root of the population_variance' do
36
+ DescriptiveStatistics.new([1,2,6]).population_standard_deviation.should == 2.160246899469287
37
+ end
38
+
39
+ it 'returns nil if empty' do
40
+ DescriptiveStatistics.new([]).population_standard_deviation.should be_nil
41
+ end
42
+ end
43
+ 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: 0.0.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,8 +59,12 @@ files:
59
59
  - descriptive-statistics.gemspec
60
60
  - lib/descriptive-statistics.rb
61
61
  - lib/descriptive-statistics/central-tendency.rb
62
+ - lib/descriptive-statistics/dispersion.rb
63
+ - lib/descriptive-statistics/spread.rb
62
64
  - lib/descriptive-statistics/version.rb
63
65
  - spec/lib/descriptive-statistics/central_tendency_spec.rb
66
+ - spec/lib/descriptive-statistics/dispersion_spec.rb
67
+ - spec/lib/descriptive-statistics/spread_spec.rb
64
68
  - spec/spec_helper.rb
65
69
  homepage: https://github.com/jtescher/descriptive-statistics
66
70
  licenses: []
@@ -76,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
80
  version: '0'
77
81
  segments:
78
82
  - 0
79
- hash: 3988520921431193355
83
+ hash: -2781553124124914132
80
84
  required_rubygems_version: !ruby/object:Gem::Requirement
81
85
  none: false
82
86
  requirements:
@@ -85,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
89
  version: '0'
86
90
  segments:
87
91
  - 0
88
- hash: 3988520921431193355
92
+ hash: -2781553124124914132
89
93
  requirements: []
90
94
  rubyforge_project:
91
95
  rubygems_version: 1.8.24
@@ -95,4 +99,6 @@ summary: Simply calculate measures of central tendency (e.g. mean, median mode),
95
99
  (e.g. range and quartiles), and spread (e.g variance and standard deviation)
96
100
  test_files:
97
101
  - spec/lib/descriptive-statistics/central_tendency_spec.rb
102
+ - spec/lib/descriptive-statistics/dispersion_spec.rb
103
+ - spec/lib/descriptive-statistics/spread_spec.rb
98
104
  - spec/spec_helper.rb