simple_statistics 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,64 @@
1
+ = Statistics
2
+
3
+ Basic statistics including mean, median, weighted moving average and rate of change.
4
+
5
+ == INSTALL
6
+
7
+ gem install simple_statistics
8
+
9
+ == DEPENDENCIES
10
+
11
+ None
12
+
13
+ == USAGE
14
+
15
+ require 'simple_statistics'
16
+
17
+ To calculate mean, median, and weighted moving average:
18
+
19
+ [10,20,30,20,25,24,23,22].mean
20
+ # 21.75
21
+
22
+ [10,20,30,20,25,24,23,22].median
23
+ # 22.5
24
+
25
+ [10,20,30,20,25,24,23,22].wma
26
+ # 23
27
+
28
+ To calculate the weighted moving average growth of a numeric sequence.....
29
+
30
+ [95.5, 95.5, 99.1, 104.7, 121.2].growth.wma.round_to(4)
31
+ # 1.0875
32
+
33
+ ..to express this as a percent we can write:
34
+
35
+ [95.5, 95.5, 99.1, 104.7, 121.2].growth.wma.as_percent(3)
36
+ # 8.75%
37
+
38
+
39
+ == LICENSE:
40
+
41
+ (The MIT License)
42
+
43
+ Copyright (c) 2010 Craig Davidson <craig@craigdavidson.co.uk>
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ 'Software'), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
59
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
60
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
61
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
62
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63
+
64
+
data/lib/array_of.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Enumerable
2
+ def array_of method_name
3
+ self.collect {|element| element.method(method_name).call}
4
+ end
5
+ end
data/lib/average.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'rounding'
2
+ require 'percent'
3
+
4
+ class Array
5
+
6
+ def mean
7
+ self.inject(0) { |sum, x| sum += x } / self.size.to_f
8
+ end
9
+
10
+ def median already_sorted=false
11
+ return nil if self.empty?
12
+ array = self.sort unless already_sorted
13
+ m_pos = array.size / 2
14
+ return array.size % 2 == 1 ? array[m_pos] : array[m_pos-1..m_pos].mean
15
+ end
16
+
17
+ def wma
18
+ values = self.reverse
19
+ periodLength = values.length - 1
20
+ sum = 0;
21
+ weightedSum = 0;
22
+ for n in 0..periodLength
23
+ weightedSum = weightedSum + ((periodLength - n) * values[n]);
24
+ sum = sum + n;
25
+ end
26
+ return weightedSum / sum;
27
+ end
28
+
29
+ end
data/lib/percent.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rounding'
2
+
3
+ class Object
4
+
5
+ def as_percent(rounding = 0)
6
+ return "#{as_percent_number(rounding)}%"
7
+ rescue
8
+ self
9
+ end
10
+
11
+ def as_percent_number(rounding = 0)
12
+ percent = ((self-1.0) * 100).round_to(rounding)
13
+ rounding == 0 ? percent.to_i : percent
14
+ end
15
+
16
+ end
@@ -0,0 +1,20 @@
1
+ class Array
2
+
3
+ def growth
4
+ rate_of_change
5
+ end
6
+
7
+ def rate_of_change
8
+ previous = nil
9
+ result = Array.new
10
+ self.each{|this| result << multiplier(this,previous) and previous = this}
11
+ result
12
+ rescue
13
+ self
14
+ end
15
+
16
+ def multiplier(this, previous)
17
+ previous != nil && previous != 0 ? (this.to_f/previous.to_f) : 1.0
18
+ end
19
+
20
+ end
data/lib/rounding.rb ADDED
@@ -0,0 +1,8 @@
1
+ class Object
2
+ def round_to(x)
3
+ return (self * 10**x.to_f).round / 10**x.to_f
4
+ rescue
5
+ self
6
+ end
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_statistics
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Craig Davidson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-14 00:00:00 +10:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Basic statistics including mean, median, weighted moving average and growth.
22
+ email: craig@craigdavidson.co.uk
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - README.rdoc
31
+ - lib/array_of.rb
32
+ - lib/rate_of_change.rb
33
+ - lib/rounding.rb
34
+ - lib/percent.rb
35
+ - lib/average.rb
36
+ has_rdoc: true
37
+ homepage:
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Basic statistics including mean, median, weighted moving average and growth.
70
+ test_files: []
71
+