From a29eb3e38414cd252850c9c4fb356f8b2bef6fb4 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 12 Feb 2021 19:54:07 +0100 Subject: model.rb refactored, mp2d models updated --- lib/array.rb | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'lib/array.rb') 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 -- cgit v1.2.3