ar_result_calculations 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/README.textile +2 -0
- data/lib/ar_result_calculations/ar_result_calculations.rb +25 -2
- data/lib/ar_result_calculations/version.rb +1 -1
- metadata +5 -4
data/CHANGELOG
ADDED
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
|
-
|
26
|
-
|
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
|
#
|
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:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
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-
|
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
|