Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed anomalous test

  • Loading branch information...
commit 74d5ef4377b6619fc5ea6900d99eb2f33f502f10 1 parent a21e408
@heikkiv authored
Showing with 13 additions and 5 deletions.
  1. +6 −5 lib/carl.js
  2. +7 −0 test/carl.test.js
View
11 lib/carl.js
@@ -1,13 +1,11 @@
var gauss = require('gauss');
var gaussian = require('gaussian');
-var epsilon = 0.05;
-
var Carl = module.exports = function(mean, variance, epsilon) {
this.mean = mean || 0;
this.variance = variance || 1;
this.distribution = gaussian(this.mean, this.variance);
- this.epsilon = epsilon;
+ this.epsilon = epsilon || 0.05;
};
Carl.prototype.train = function(data, callback) {
@@ -32,8 +30,11 @@ Carl.prototype.cdf = function(x) {
};
Carl.prototype.isAnomalous = function(x) {
- var p = 1 - this.cdf(Math.abs(x));
- return p < epsilon/2;
+ var p = this.cdf(x);
+ if(p > 0.5) {
+ p = 1 - p;
+ }
+ return p < this.epsilon/2;
};
Carl.prototype.isValid = function(x) {
View
7 test/carl.test.js
@@ -12,6 +12,7 @@ describe('Carl', function() {
var carl = new Carl();
carl.mean.should.equal(0);
carl.variance.should.equal(1);
+ carl.epsilon.should.equal(0.05);
round(carl.pdf(1)).should.eql(0.242);
});
@@ -38,6 +39,12 @@ describe('Carl', function() {
round(carl.cdf(-2)).should.eql(0.023);
});
+ it('should use mean, variance and epsilon from constructor parameters to detect anomaly', function() {
+ var carl = new Carl(0.161388, 0.007538945, 0.1);
+ carl.isAnomalous(0.1).should.be.false;
+ carl.isAnomalous(0.01).should.be.true;
+ });
+
it('should determine significance with epsilon .05', function() {
var carl = new Carl();
carl.isAnomalous(0).should.be.false;
Please sign in to comment.
Something went wrong with that request. Please try again.