Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added api for mean and standard deviation

  • Loading branch information...
commit 3ba5a6e85e4f87f13aaa04ea255c8c9525de32b1 1 parent aff716d
Andrew Kane authored
Showing with 13 additions and 4 deletions.
  1. +5 −4 lib/anomaly/detector.rb
  2. +8 −0 spec/anomaly/detector_spec.rb
View
9 lib/anomaly/detector.rb
@@ -1,5 +1,6 @@
module Anomaly
class Detector
+ attr_reader :mean, :std
attr_accessor :eps
def initialize(examples = nil, opts = {})
@@ -45,8 +46,8 @@ def train(examples, opts = {})
else
# Default to Array, since built-in Matrix does not give us a big performance advantage.
cols = @n.times.map{|i| training_examples.map{|r| r[i]}}
- @mean = cols.map{|c| mean(c)}
- @std = cols.each_with_index.map{|c,i| std(c, @mean[i])}
+ @mean = cols.map{|c| alt_mean(c)}
+ @std = cols.each_with_index.map{|c,i| alt_std(c, @mean[i])}
end
@std.map!{|std| (std == 0 or std.nan?) ? Float::MIN : std}
@@ -120,11 +121,11 @@ def f1_score(tp, fp, fn)
# Not used for NArray
- def mean(x)
+ def alt_mean(x)
x.inject(0.0){|a, i| a + i}/x.size
end
- def std(x, mean)
+ def alt_std(x, mean)
Math.sqrt(x.inject(0.0){|a, i| a + (i - mean) ** 2}/(x.size - 1))
end
View
8 spec/anomaly/detector_spec.rb
@@ -9,6 +9,14 @@
ad.probability([0,0]).should == 0.079577471545947667
end
+ it "computes the right mean" do
+ ad.mean.should == [0,0]
+ end
+
+ it "computes the right standard deviation" do
+ ad.std.should == [1,2]
+ end
+
it "marshalizes" do
expect{ Marshal.dump(ad) }.to_not raise_error
end
Please sign in to comment.
Something went wrong with that request. Please try again.