summaryrefslogtreecommitdiff
path: root/lib/array.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/array.rb')
-rw-r--r--lib/array.rb30
1 files changed, 23 insertions, 7 deletions
diff --git a/lib/array.rb b/lib/array.rb
index 55186bd..32a8e5d 100644
--- a/lib/array.rb
+++ b/lib/array.rb
@@ -1,3 +1,4 @@
+# TODO increase speed with vectors, matrices?
class Array
# Sum the size of single arrays in an array of arrays
@@ -74,14 +75,29 @@ class Array
end
end
- # Collect array with index
- # in analogy to each_with_index
- def collect_with_index
- result = []
- self.each_with_index do |elt, idx|
- result << yield(elt, idx)
+ def remove indices
+ out = self
+ indices.sort.reverse_each do |i|
+ out.delete_at i
end
- result
+ out
+ end
+
+ # Correlation coefficient
+ # @param [Array]
+ # @return [Numeric]
+ def r(y)
+ raise "Argument is not a Array class!" unless y.class == Array
+ raise "Self array is nil!" if self.size == 0
+ raise "Argument array size is invalid!" unless self.size == y.size
+
+ mean_x = self.inject(0) { |s, a| s += a } / self.size.to_f
+ mean_y = y.inject(0) { |s, a| s += a } / y.size.to_f
+ cov = self.zip(y).inject(0) { |s, a| s += (a[0] - mean_x) * (a[1] - mean_y) }
+ var_x = self.inject(0) { |s, a| s += (a - mean_x) ** 2 }
+ var_y = y.inject(0) { |s, a| s += (a - mean_y) ** 2 }
+ r = cov / Math.sqrt(var_x)
+ r /= Math.sqrt(var_y)
end
end