Skip to content

Commit

Permalink
Fixed anomalous test
Browse files Browse the repository at this point in the history
  • Loading branch information
heikkiv committed Sep 27, 2012
1 parent a21e408 commit 74d5ef4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/carl.js
@@ -1,13 +1,11 @@
var gauss = require('gauss'); var gauss = require('gauss');
var gaussian = require('gaussian'); var gaussian = require('gaussian');


var epsilon = 0.05;

var Carl = module.exports = function(mean, variance, epsilon) { var Carl = module.exports = function(mean, variance, epsilon) {
this.mean = mean || 0; this.mean = mean || 0;
this.variance = variance || 1; this.variance = variance || 1;
this.distribution = gaussian(this.mean, this.variance); this.distribution = gaussian(this.mean, this.variance);
this.epsilon = epsilon; this.epsilon = epsilon || 0.05;
}; };


Carl.prototype.train = function(data, callback) { Carl.prototype.train = function(data, callback) {
Expand All @@ -32,8 +30,11 @@ Carl.prototype.cdf = function(x) {
}; };


Carl.prototype.isAnomalous = function(x) { Carl.prototype.isAnomalous = function(x) {
var p = 1 - this.cdf(Math.abs(x)); var p = this.cdf(x);
return p < epsilon/2; if(p > 0.5) {
p = 1 - p;
}
return p < this.epsilon/2;
}; };


Carl.prototype.isValid = function(x) { Carl.prototype.isValid = function(x) {
Expand Down
7 changes: 7 additions & 0 deletions test/carl.test.js
Expand Up @@ -12,6 +12,7 @@ describe('Carl', function() {
var carl = new Carl(); var carl = new Carl();
carl.mean.should.equal(0); carl.mean.should.equal(0);
carl.variance.should.equal(1); carl.variance.should.equal(1);
carl.epsilon.should.equal(0.05);
round(carl.pdf(1)).should.eql(0.242); round(carl.pdf(1)).should.eql(0.242);
}); });


Expand All @@ -38,6 +39,12 @@ describe('Carl', function() {
round(carl.cdf(-2)).should.eql(0.023); 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() { it('should determine significance with epsilon .05', function() {
var carl = new Carl(); var carl = new Carl();
carl.isAnomalous(0).should.be.false; carl.isAnomalous(0).should.be.false;
Expand Down

0 comments on commit 74d5ef4

Please sign in to comment.