spec/anomaly/detector_spec.rb in anomaly-0.0.3 vs spec/anomaly/detector_spec.rb in anomaly-0.1.0
- old
+ new
@@ -1,10 +1,10 @@
require "spec_helper"
describe Anomaly::Detector do
- let(:data) { [[-1,-2],[0,0],[1,2]] }
- let(:ad) { Anomaly::Detector.new(data) }
+ let(:examples) { [[-1,-2,0],[0,0,0],[1,2,0]] }
+ let(:ad) { Anomaly::Detector.new(examples) }
# mean = [0, 0], std = [1, 2]
it "computes the right probability" do
ad.probability([0,0]).should == 0.079577471545947667
end
@@ -12,48 +12,60 @@
it "marshalizes" do
expect{ Marshal.dump(ad) }.to_not raise_error
end
context "when standard deviation is 0" do
- let(:data) { [[0],[0]] }
+ let(:examples) { [[0,0],[0,0]] }
it "returns infinity for mean" do
ad.probability([0]).should == 1
end
it "returns 0 for not mean" do
ad.probability([1]).should == 0
end
end
- context "when data is an array" do
- let(:data) { [[-1,-2],[0,0],[1,2]] }
+ context "when examples is an array" do
+ let(:examples) { [[-1,-2,0],[0,0,0],[1,2,0]] }
let(:sample) { [rand, rand] }
it "returns the same probability as an NMatrix" do
prob = ad.probability(sample)
Object.send(:remove_const, :NMatrix)
- prob.should == Anomaly::Detector.new(data).probability(sample)
+ prob.should == Anomaly::Detector.new(examples).probability(sample)
end
end
context "when lots of samples" do
- let(:data) { m.times.map{[0]} }
+ let(:examples) { m.times.map{[0,0]} }
let(:m) { rand(100) + 1 }
- it { ad.samples.should == m }
it { ad.trained?.should be_true }
end
context "when no samples" do
- let(:data) { [] }
+ let(:examples) { nil }
- it { ad.samples.should == 0 }
it { ad.trained?.should be_false }
end
context "when pdf is greater than 1" do
- let(:data) { 100.times.map{[0]}.push([1]) }
+ let(:examples) { 100.times.map{[0,0]}.push([1,0]) }
it { ad.probability([0]).should == 1 }
+ end
+
+ context "when only anomalies" do
+ let(:examples) { [[0,1]] }
+
+ it "raises error" do
+ expect{ ad }.to raise_error RuntimeError, "Must have at least one non-anomaly"
+ end
+ end
+
+ context "when only one non-anomaly" do
+ let(:examples) { [[0,0]] }
+
+ it { ad.eps.should == 1e-1 }
end
end