├── lib ├── index.js ├── memoryDataSource.js ├── mongoDataSource.js └── anomalyDetector.js ├── example ├── train.js ├── test.js └── example.js ├── .gitattributes ├── package.json ├── LICENSE ├── README.md ├── test └── anomalyDetectorTest.js └── .gitignore /lib/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./anomalyDetector'); -------------------------------------------------------------------------------- /example/train.js: -------------------------------------------------------------------------------- 1 | var detector = require('../lib/index.js'); 2 | 3 | var random_variables = { 4 | 'a' : [ 18, 15, 16, 17, 14, 15, 16 ], 5 | 'b' : [ 110, 130, 125, 124, 128, 118, 119 ], 6 | 'c' : [ 110, 115, 113, 114, 90, 116, 90 ] 7 | }; 8 | 9 | detector.init(detector.default_options, function(){ 10 | detector.train(random_variables, function(distributions){ 11 | console.log('training finished!'); 12 | console.log(distributions); 13 | detector.close(); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /example/test.js: -------------------------------------------------------------------------------- 1 | var detector = require('../lib/index.js'); 2 | 3 | var random_variable_id = 'b'; 4 | var testing_values = [50, 70, 90, 110, 130, 150, 170]; 5 | 6 | detector.init(detector.default_options, function(){ 7 | 8 | var counter = 0; 9 | for (var i = 0; i < testing_values.length; i++) { 10 | var value = testing_values[i]; 11 | 12 | // test whether values is ok or an outlier 13 | detector.test(random_variable_id, value, function(id, v, result) { 14 | console.log(v, result); 15 | 16 | // if all tests are done, close dataSource connection 17 | if (counter++ === (testing_values.length - 1)) { 18 | detector.close(); 19 | } 20 | }); 21 | } 22 | }); 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "anomaly-detector", 3 | "version": "0.1.4", 4 | "description": "Data anomaly detector for NodeJS", 5 | "main": "./lib/index.js", 6 | "keywords": [ 7 | "anomaly", 8 | "detector", 9 | "mongo" 10 | ], 11 | "contributors": [ 12 | { 13 | "name": "Lukasz Krawczyk", 14 | "email": "contact@lukaszkrawczyk.eu" 15 | } 16 | ], 17 | "license": "MIT", 18 | "readmeFilename": "README.md", 19 | "repository": { 20 | "type": "git", 21 | "url": "git://github.com/LukaszKrawczyk/anomaly-detector.git" 22 | }, 23 | "dependencies": { 24 | "mongodb": "*" 25 | }, 26 | "devDependencies": { 27 | "mocha": "*" 28 | }, 29 | "bugs": { 30 | "url": "https://github.com/LukaszKrawczyk/anomaly-detector/issues" 31 | }, 32 | "_id": "anomaly-detector@0.1.4", 33 | "dist": { 34 | "shasum": "a9674d090e35fc057b1a230c09eac62962348bdc" 35 | }, 36 | "_from": "anomaly-detector@", 37 | "_resolved": "https://registry.npmjs.org/anomaly-detector/-/anomaly-detector-0.1.4.tgz" 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | 4 | 5 | Copyright © 2013 Lukasz Krawczyk 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of 8 | this software and associated documentation files (the “Software”), to deal in the 9 | Software without restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 11 | Software, and to permit persons to whom the Software is furnished to do so, 12 | subject to the following conditions 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | The software is provided “as is”, without warranty of any kind, express or 18 | implied, including but not limited to the warranties of merchantability, fitness 19 | for a particular purpose and noninfringement. In no event shall the authors or 20 | copyright holders be liable for any claim, damages or other liability, whether 21 | in an action of contract, tort or otherwise, arising from, out of or in 22 | connection with the software or the use or other dealings in the software. -------------------------------------------------------------------------------- /example/example.js: -------------------------------------------------------------------------------- 1 | var detector = require('../lib/index.js'); 2 | 3 | var options = detector.default_options; 4 | 5 | // initialize detector 6 | detector.init(options, function(){ 7 | 8 | // training classifier for 3 separate random variables 9 | var random_variables = { 10 | 'a' : [ 18, 15, 16, 17, 14, 15, 16 ], 11 | 'b' : [ 110, 130, 125, 124, 128, 118, 119 ], 12 | 'c' : [ 110, 115, 113, 114, 90, 116, 90 ] 13 | }; 14 | detector.train(random_variables, function(){ 15 | 16 | // testing 17 | var variable_id = 'b'; 18 | var testing_values = [50, 70, 90, 110, 130, 150, 170]; 19 | 20 | var counter = 0; 21 | for (var i = 0; i < testing_values.length; i++) { 22 | var value = testing_values[i]; 23 | 24 | // test whether values is ok or an outlier 25 | detector.test(variable_id, value, function(id, v, result) { 26 | console.log(v, result); 27 | 28 | // if all tests are done, close dataSource connection 29 | if (counter++ === (testing_values.length - 1)) { 30 | detector.close(); 31 | } 32 | }); 33 | } 34 | }); 35 | }); -------------------------------------------------------------------------------- /lib/memoryDataSource.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Memory data source 3 | * 4 | * @package AnomalyDetector.DataSource 5 | */ 6 | var MemoryDataSource = { 7 | distributions : {}, 8 | 9 | /** 10 | * Initialize data source 11 | * 12 | * @param {object} options 13 | * @param {callable} cb 14 | * @returns {void} 15 | * @access public 16 | */ 17 | init: function(options, cb) { 18 | cb(); 19 | }, 20 | 21 | /** 22 | * Close data source 23 | * 24 | * @returns {void} 25 | * @access public 26 | */ 27 | close : function() {}, 28 | 29 | /** 30 | * Save multiple distribution parameters 31 | * 32 | * @param {object} distributions 33 | * @param {callable} cb 34 | * @returns {void} 35 | * @access public 36 | */ 37 | set : function(distributions, cb) { 38 | // overwrite distribution data 39 | for (var i in distributions) { 40 | this.distributions[i] = distributions[i]; 41 | } 42 | cb(); 43 | }, 44 | 45 | /** 46 | * Get distribution parameters by variable id 47 | * 48 | * @param {string|integer} id 49 | * @param {callable} cb 50 | * @returns {void} 51 | * @access public 52 | */ 53 | get : function(id, cb) { 54 | cb(this.distributions[id]); 55 | } 56 | }; 57 | 58 | exports = module.exports = MemoryDataSource; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Anomaly Detector [![NPM version](https://badge.fury.io/js/anomaly-detector.png)](http://badge.fury.io/js/anomaly-detector) 2 | Data anomaly detector for NodeJS 3 | 4 | ## Instalation 5 | 6 | ``` 7 | npm install anomaly-detector 8 | ``` 9 | 10 | ## Examples 11 | 12 | ### Training classifier 13 | ```js 14 | var detector = require('anomaly-detector'); 15 | 16 | var random_variables = { 17 | 'a' : [ 18, 15, 16, 17, 14, 15, 16 ], 18 | 'b' : [ 110, 130, 125, 124, 128, 118, 119 ], 19 | 'c' : [ 110, 115, 113, 114, 90, 116, 90 ] 20 | }; 21 | 22 | // by default, detector is storing training data in mongo database 23 | detector.init(detector.default_options, function(){ 24 | detector.train(random_variables, function(distributions){ 25 | console.log('training finished!'); 26 | console.log(distributions); 27 | detector.close(); 28 | }); 29 | }); 30 | ``` 31 | 32 | ### Testing classifier 33 | ```js 34 | var detector = require('anomaly-detector'); 35 | 36 | var random_variable_id = 1; 37 | var testing_values = [50, 70, 90, 110, 130, 150, 170]; 38 | 39 | // by default, detector is storing training data in mongo database 40 | detector.init(detector.default_options, function(){ 41 | 42 | var counter = 0; 43 | for (var i = 0; i < testing_values.length; i++) { 44 | var value = testing_values[i]; 45 | 46 | // test whether values is ok or an outlier 47 | detector.test(random_variable_id, value, function(id, v, result) { 48 | console.log(v, result); 49 | 50 | // if all tests are done, close dataSource connection 51 | if (counter++ === (testing_values.length - 1)) { 52 | detector.close(); 53 | } 54 | }); 55 | } 56 | }); 57 | ``` 58 | 59 | ### Using memory data source 60 | 61 | ```js 62 | var detector = require('anomaly-detector'); 63 | 64 | var options = { 65 | data_source : { 66 | name : 'memory' 67 | } 68 | }; 69 | 70 | detector.init(detector.default_options, function(){ 71 | ... 72 | }); 73 | 74 | ``` 75 | 76 | ## Next releases 77 | 78 | * File data source 79 | * Error handling 80 | * Different detection methods 81 | 82 | ## License 83 | 84 | Software is licensed under MIT license. 85 | For more information check LICENSE file. -------------------------------------------------------------------------------- /lib/mongoDataSource.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Mongo data source 3 | * 4 | * @package AnomalyDetector.DataSource 5 | */ 6 | var MongoDataSource = { 7 | collection : null, 8 | db : null, 9 | MongoClient : require('mongodb').MongoClient, 10 | 11 | /** 12 | * Initialize data source 13 | * 14 | * @param {object} options 15 | * @param {callable} cb 16 | * @returns {void} 17 | * @access public 18 | */ 19 | init: function(options, cb) { 20 | var self = this; 21 | 22 | if (!this.collection) { 23 | this.MongoClient.connect('mongodb://' + options.host + ':' + options.port + '/' + options.database, function(err, db) { 24 | if (err) throw err; 25 | self.db = db; 26 | self.collection = self.db.collection(options.collection); 27 | cb(); 28 | }); 29 | } else { 30 | cb(); 31 | } 32 | }, 33 | 34 | /** 35 | * Close data source 36 | * 37 | * @returns {void} 38 | * @access public 39 | */ 40 | close : function() { 41 | this.db.close(); 42 | }, 43 | 44 | /** 45 | * Save multiple distribution parameters 46 | * 47 | * @param {object} distributions 48 | * @param {callable} cb 49 | * @returns {void} 50 | * @access public 51 | */ 52 | set : function(distributions, cb) { 53 | var data = [], 54 | remove_ids = [], 55 | self = this; 56 | 57 | for (var id in distributions) { 58 | distributions[id]._id = id; 59 | data.push(distributions[id]); 60 | remove_ids.push(id); 61 | } 62 | 63 | // remove all data 64 | this.collection.remove({ _id : { $in : remove_ids } }, function(err, result){ 65 | if (err) throw err; 66 | 67 | // insert multiple distribution parameters 68 | self.collection.insert(data, function(err, result){ 69 | if (err) throw err; 70 | cb(result); 71 | }); 72 | }); 73 | }, 74 | 75 | /** 76 | * Get distribution parameters by variable id 77 | * 78 | * @param {string|integer} id 79 | * @param {callable} cb 80 | * @returns {void} 81 | * @access public 82 | */ 83 | get : function(id, cb) { 84 | // find distribution parameters of a random variable 85 | this.collection.findOne({_id : id}, function(err, data){ 86 | if (err) throw err; 87 | cb(data); 88 | }); 89 | } 90 | }; 91 | 92 | exports = module.exports = MongoDataSource; -------------------------------------------------------------------------------- /test/anomalyDetectorTest.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var detector = require('../lib/index'); 3 | 4 | detector.init({ 5 | data_source : { name : 'memory'} 6 | }, function(){}); 7 | 8 | describe('AnomalyDetector', function() { 9 | 10 | describe('#train()', function() { 11 | it('should correctly train classifier for small and big values', function(done) { 12 | detector.train({ 13 | 'a' : [4, 4, -4, -4], // positive / negative 14 | 'b' : [0, 6, 8, 14], // standard 15 | 'c' : [0, 600000, 800000, 1400000], // big 16 | 'd' : [0, 0.6, 0.8, 1.4], // small 17 | 'e' : [5, 5, 5] // eqal 18 | }, function(){ 19 | assert.deepEqual(detector.data_source.distributions, { 20 | 'a' : {e : 0, sigma : 4}, 21 | 'b' : {e : 7, sigma : 5}, 22 | 'c' : {e : 700000, sigma : 500000}, 23 | 'd' : {e : 0.7, sigma : 0.5}, 24 | 'e' : {e : 5, sigma : 0} 25 | }); 26 | done(); 27 | }); 28 | }); 29 | }); 30 | 31 | describe('#test()', function() { 32 | it('should return correct result for positive case', function(done) { 33 | // prepare data source to return sample data 34 | detector.data_source.distributions = {'a' : {e : 7, sigma : 5}}; 35 | 36 | detector.test('a', 22, function(i, v, result){ 37 | assert.equal(result, true); 38 | done(); 39 | }); 40 | }); 41 | 42 | it('should return correct result for negative case', function(done) { 43 | // prepare data source to return sample data 44 | detector.data_source.distributions = {'a' : {e : 7, sigma : 5}}; 45 | 46 | detector.test('a', 23, function(i, v, result){ 47 | assert.equal(result, false); 48 | done(); 49 | }); 50 | }); 51 | }); 52 | 53 | describe('#_expectedValue()', function() { 54 | it('should return expected value of a random variable (without arugment)', function() { 55 | var result = detector._expectedValue([0, 6, 8, 14]); 56 | assert.equal(result, 7); 57 | }); 58 | 59 | it('should return squared expected value of a random variable (with arugment)', function() { 60 | var result = detector._expectedValue([0, 6, 8, 14], 2); 61 | assert.equal(result, 74); 62 | }); 63 | 64 | it('should return error for a very small values', function() { 65 | var result = detector._expectedValue([0, 0.006, 0.008, 0.014], 1, 100); 66 | assert.notEqual(result, 0.007); 67 | }); 68 | 69 | it('should return correct value for a very small values with accuracy setting', function() { 70 | 71 | detector.init({ 72 | data_source : { name : 'memory'}, 73 | accuracy : 0.001 74 | }, function(){}); 75 | 76 | var result = detector._expectedValue([0, 0.006, 0.008, 0.014], 1, 1000); 77 | assert.equal(result, 0.007); 78 | }); 79 | }); 80 | 81 | describe('#_standardDeviation()', function() { 82 | it('should return standard deviation (without argument)', function() { 83 | var result = detector._standardDeviation([0, 6, 8, 14]); 84 | assert.equal(result, 5); 85 | }); 86 | 87 | it('should return standard deviation (with argument)', function() { 88 | var result = detector._standardDeviation([0, 6, 8, 14], 7); 89 | assert.equal(result, 5); 90 | }); 91 | }); 92 | }); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | 217 | ############# 218 | ## NetBeans 219 | ############# 220 | nbproject/ -------------------------------------------------------------------------------- /lib/anomalyDetector.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Data anomaly detector based on the 3-sigma rule. 3 | * @see http://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule 4 | * 5 | * @package AnomalyDetector 6 | */ 7 | var AnomalyDetector = { 8 | data_source : null, 9 | default_options : { 10 | data_source : { 11 | name : 'mongo', 12 | host : '127.0.0.1', 13 | port : 27017, 14 | database : 'anomaly', 15 | collection : 'distributions' 16 | }, 17 | // decimal accuracy for expected value and standard deviation 18 | // random variable should not be smaller than this value 19 | accuracy : 0.1 20 | }, 21 | 22 | /** 23 | * Init data source (if necessary) 24 | * 25 | * @param {object} options 26 | * @param {callable} cb 27 | * @return {void} 28 | * @access public 29 | */ 30 | init : function(options, cb) { 31 | this.accuracy = options.accuracy || this.default_options.accuracy; 32 | this.data_source = require('./' + options.data_source.name + 'DataSource'); 33 | this.data_source.init(options.data_source, function(){ 34 | cb(); 35 | }); 36 | }, 37 | 38 | /** 39 | * Close data source (if necessary) 40 | * 41 | * @return {void} 42 | * @access public 43 | */ 44 | close : function() { 45 | this.data_source.close(); 46 | }, 47 | 48 | /** 49 | * Training classifier 50 | * Calculating probability distribution parameters for each random variable 51 | * 52 | * @param {array} random_variables 53 | * @returns {Array} 54 | * @access public 55 | */ 56 | train : function (random_variables, cb) { 57 | var self = this; 58 | distributions = {}; // probability distribution parameters array 59 | 60 | for (var id in random_variables){ 61 | var X = random_variables[id], 62 | E = self._expectedValue(X), 63 | sigma = self._standardDeviation(X, E); 64 | 65 | // add values to distributions array 66 | distributions[id] = { e : E, sigma : sigma }; 67 | } 68 | 69 | self.data_source.set(distributions, cb); 70 | }, 71 | 72 | /** 73 | * Classifier 74 | * true = value seems to be correct 75 | * false = value is an outlier 76 | * 77 | * @param {integer} id - Random variable id 78 | * @param {float} v - Random variable value 79 | * @returns {callable} 80 | * @access public 81 | */ 82 | test : function (id, v, cb) { 83 | this.data_source.get(id, function(distribution){ 84 | var E = distribution.e, 85 | sigma = distribution.sigma; 86 | 87 | return cb(id, v, Math.abs(E - v) <= (3 * sigma), E, sigma); 88 | }); 89 | }, 90 | 91 | /** 92 | * Calculating expected value (E) of a random variable 93 | * 94 | * @param {array} X - random variable 95 | * @param {integer} pow - power used in summation operator (optional, default = 1) 96 | * @returns {float} 97 | * @access protected 98 | */ 99 | _expectedValue : function (X, pow) { 100 | var sum = 0, 101 | n = X.length; 102 | // set default value if not set 103 | pow = pow || 1; 104 | 105 | // if random variable is empty, return 0 106 | if (n == 0) return 0; 107 | 108 | for (var i = 0; i < n; i++) 109 | sum += Math.pow(X[i], pow) / this.accuracy; 110 | 111 | return sum / (n / this.accuracy); 112 | }, 113 | 114 | /** 115 | * Calculating standard deviation (sigma) of a random variable 116 | * 117 | * @param {array} X 118 | * @param {float} Ex - expexted value of X (optional) 119 | * @returns {float} 120 | * @access protected 121 | */ 122 | _standardDeviation : function (X, Ex) { 123 | var Ex2 = this._expectedValue(X, 2); 124 | // calculate expected value if not set 125 | Ex = Ex || this._expectedValue(X); 126 | 127 | // return squared root of the variation 128 | return Math.sqrt(Ex2 - Math.pow(Ex, 2)); 129 | } 130 | }; 131 | 132 | exports = module.exports = AnomalyDetector; --------------------------------------------------------------------------------