diff options
author | Christoph Helma <helma@in-silico.ch> | 2021-02-12 19:54:07 +0100 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2021-02-12 19:54:07 +0100 |
commit | a29eb3e38414cd252850c9c4fb356f8b2bef6fb4 (patch) | |
tree | a957d9ac455e7345c51f3ab6075698f552c497d1 /lib/array.rb | |
parent | 158e9a7ecbc467c3db77c354f203b1176b0fc3f2 (diff) |
model.rb refactored, mp2d models updated
Diffstat (limited to 'lib/array.rb')
-rw-r--r-- | lib/array.rb | 30 |
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 |