Skip to content

Commit

Permalink
Use cumulative density to calculate p-values
Browse files Browse the repository at this point in the history
  • Loading branch information
heikkiv committed Sep 27, 2012
1 parent 57e2a0b commit a21e408
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/carl.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ Carl.prototype.pdf = function(x) {
return this.distribution.pdf(x);
};

Carl.prototype.cdf = function(x) {
var z = (x - this.mean) / Math.sqrt(this.variance);
return this.distribution.cdf(x);
};

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

Carl.prototype.isValid = function(x) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "carl",
"description": "Probability density based anomaly detection library",
"version": "0.0.1",
"version": "0.0.2",
"repository": {
"type": "git",
"url": "git://github.com/heikkiv/carl.git"
Expand Down
12 changes: 12 additions & 0 deletions test/carl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ describe('Carl', function() {
round(carl.pdf(3)).should.eql(0.004);
});

it('should calculate pdf correctly', function() {
var carl = new Carl();
round(carl.pdf(1)).should.eql(0.242);
round(carl.pdf(-1)).should.eql(0.242);
});

it('should calculate cdf correctly', function() {
var carl = new Carl();
round(carl.cdf(2)).should.eql(0.977);
round(carl.cdf(-2)).should.eql(0.023);
});

it('should determine significance with epsilon .05', function() {
var carl = new Carl();
carl.isAnomalous(0).should.be.false;
Expand Down

0 comments on commit a21e408

Please sign in to comment.