davidrichards-just_enumerable_stats 0.0.6 → 0.0.7
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/VERSION.yml +1 -1
- data/lib/just_enumerable_stats.rb +30 -0
- data/spec/just_enumerable_stats_spec.rb +12 -0
- metadata +2 -2
data/VERSION.yml
CHANGED
@@ -601,4 +601,34 @@ module Enumerable
|
|
601
601
|
def min_of_lists(*enums)
|
602
602
|
yield_transpose(*enums) {|e| e.min}
|
603
603
|
end
|
604
|
+
|
605
|
+
# Returns the covariance of two lists.
|
606
|
+
def covariance(other)
|
607
|
+
self.to_f!
|
608
|
+
other.to_f!
|
609
|
+
n = [self.size, other.size].min
|
610
|
+
self_average = self.mean
|
611
|
+
other_average = other.mean
|
612
|
+
total_expected = self.sigma_pairs(other) {|a, b| (a - self_average) * (b - other_average)}
|
613
|
+
total_expected / n
|
614
|
+
end
|
615
|
+
|
616
|
+
# The covariance / product of standard deviations
|
617
|
+
# http://en.wikipedia.org/wiki/Correlation
|
618
|
+
def pearson_correlation(other)
|
619
|
+
self.to_f!
|
620
|
+
other.to_f!
|
621
|
+
denominator = self.standard_deviation * other.standard_deviation
|
622
|
+
self.covariance(other) / denominator
|
623
|
+
end
|
624
|
+
|
625
|
+
protected
|
626
|
+
|
627
|
+
# Some calculations have to have at least floating point numbers. This
|
628
|
+
# generates a cached version of the operation--only runs once per object.
|
629
|
+
def to_f!
|
630
|
+
return true if @to_f
|
631
|
+
@to_f = self.map! {|e| e.to_f}
|
632
|
+
end
|
633
|
+
|
604
634
|
end
|
@@ -517,4 +517,16 @@ describe "JustEnumerableStats" do
|
|
517
517
|
@a.min_of_lists(@b).should eql([1,2,2])
|
518
518
|
end
|
519
519
|
|
520
|
+
it "should return the covariance of two lists" do
|
521
|
+
a = [1,2,3,4]
|
522
|
+
b = [3,3,4,3]
|
523
|
+
a.covariance(b).should eql(0.125)
|
524
|
+
end
|
525
|
+
|
526
|
+
it "should be able to return the Pearson correlation" do
|
527
|
+
a = [1,2,3,4]
|
528
|
+
b = [3,3,4,3]
|
529
|
+
a.pearson_correlation(b).should be_close(0.193649167310371, 1.0e-15)
|
530
|
+
end
|
531
|
+
|
520
532
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: davidrichards-just_enumerable_stats
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Richards
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-11 00:00:00 -07:00
|
13
13
|
default_executable: jes
|
14
14
|
dependencies: []
|
15
15
|
|