ar_result_calculations 0.0.2 → 0.0.3

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/CHANGELOG ADDED
@@ -0,0 +1,6 @@
1
+ ar_result_calculations (0.0.3) December 2, 2010
2
+
3
+ * Add calculations for sample_variance and standard_deviation
4
+
5
+
6
+
data/README.textile CHANGED
@@ -28,6 +28,8 @@ p. All methods take one parameter (column name).
28
28
  |regression|Ordinary Least Squares regression on the given column. Returns the regression as an array. Argument should be either an ActiveRecord result set or an array of numbers|
29
29
  |slope|Returns the slope of a regression on a given column. Argument should be either an ActiveRecord result set or an array of numbers|
30
30
  |make_numeric|Coerces a column to be numeric. Useful if you have derived columns returned from a query that are not in the model definition and hence are otherwise returned as strings. Returns **self** so is composable|
31
+ |sample_variance|Calculates the sample variance of a column (for active record result sets) or self (for an array)|
32
+ |standard_deviation|Calculates the standard deviation of a column (for active record result sets) or self (for an array)|
31
33
 
32
34
  h1. License
33
35
 
@@ -22,8 +22,12 @@ module ArResultCalculations
22
22
  #
23
23
  # column: The column name to average
24
24
  def mean(column = nil)
25
- return super() unless column && first && first.class.respond_to?(:descends_from_active_record?)
26
- (length > 0) ? sum(column) / length : 0
25
+ total = if column && first && first.class.respond_to?(:descends_from_active_record?)
26
+ sum(column)
27
+ else
28
+ sum
29
+ end
30
+ (length > 0) ? total / length : 0
27
31
  end
28
32
  alias :avg :mean
29
33
  alias :average :mean
@@ -84,6 +88,25 @@ module ArResultCalculations
84
88
  end
85
89
  alias :trend :slope
86
90
 
91
+ # Return the variance of a column from an active record result set (or self)
92
+ #
93
+ # column: The column name to regress
94
+ def sample_variance(column = nil)
95
+ data = column ? map(&column.to_sym) : self
96
+ return nil unless data.first
97
+ avg = data.average
98
+ sum = data.inject(0) {|acc, i| acc + (i - avg)**2 }
99
+ 1 / data.length.to_f * sum
100
+ end
101
+
102
+ # Return the standard deviation of a column in an active record result set (or self)
103
+ #
104
+ # column: The column of which you want the standard deviation
105
+ def standard_deviation(column = nil)
106
+ return nil unless variance = sample_variance(column)
107
+ Math.sqrt(variance)
108
+ end
109
+
87
110
  # Force a column to be numeric. Useful if you have derived
88
111
  # columns from a query that is not part of the base model.
89
112
  #
@@ -1,3 +1,3 @@
1
1
  module ArResultCalculations
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_result_calculations
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kip Cole
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-30 00:00:00 +08:00
18
+ date: 2010-12-02 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,6 +30,7 @@ extra_rdoc_files: []
30
30
 
31
31
  files:
32
32
  - .gitignore
33
+ - CHANGELOG
33
34
  - Gemfile
34
35
  - MIT-LICENSE
35
36
  - README.textile