|
| 1 | +var stdev = require('compute-stdev'); |
| 2 | +var average = require('average'); |
| 3 | +var criticalValueTable = require('./criticalValueTable'); |
| 4 | + |
| 5 | +function test(originDataSet, originOptions) { |
| 6 | + if (typeof originDataSet === 'undefined') { |
| 7 | + throw new Error('dataSet MUST be passed'); |
| 8 | + } |
| 9 | + if (originDataSet.filter(isValidData).length > 100) { |
| 10 | + throw new Error('dataSet.length MUST less than 100'); |
| 11 | + } |
| 12 | + if (originDataSet.filter(isValidData).length <= 2) { |
| 13 | + throw new Error('dataSet.length MUST greater than 2'); |
| 14 | + } |
| 15 | + // defaultOptions |
| 16 | + var options = { |
| 17 | + alpha: 0.01, |
| 18 | + recursion: true |
| 19 | + }; |
| 20 | + // Merge options |
| 21 | + if (typeof originOptions !== 'undefined') { |
| 22 | + if (typeof originOptions.alpha !== 'undefined') { |
| 23 | + options.alpha = originOptions.alpha; |
| 24 | + } |
| 25 | + // TODO no recursion mode is not support yet |
| 26 | + // if (typeof options_.recursion !== 'undefined') { |
| 27 | + // options.recursion = options_.recursion; |
| 28 | + // } |
| 29 | + } |
| 30 | + var criticalValue = criticalValueTable[options.alpha]; |
| 31 | + if (typeof criticalValue === 'undefined') { |
| 32 | + throw new Error('alpha ' + options.alpha + ' is not support'); |
| 33 | + } |
| 34 | + var digit = getDigit(originDataSet); |
| 35 | + var powDigit = Math.pow(10, digit); |
| 36 | + |
| 37 | + // Main algorithm |
| 38 | + var result = []; |
| 39 | + var done = false; |
| 40 | + var dataSet = originDataSet.slice(); |
| 41 | + var currentRound = {}; |
| 42 | + var i; |
| 43 | + var gResult; |
| 44 | + // If no outlier, done |
| 45 | + while (!done) { |
| 46 | + done = true; |
| 47 | + currentRound = {}; |
| 48 | + currentRound.dataSet = dataSet.slice(); |
| 49 | + currentRound.stdev = stdev(currentRound.dataSet.filter(isValidData)); |
| 50 | + currentRound.average = |
| 51 | + Math.round(average(currentRound.dataSet.filter(isValidData)) * powDigit) / powDigit; |
| 52 | + currentRound.criticalValue = criticalValue[currentRound.dataSet.filter(isValidData).length]; |
| 53 | + currentRound.gSet = []; |
| 54 | + // true if pass, false if unpass, undefined if no data |
| 55 | + currentRound.gPass = []; |
| 56 | + currentRound.outliers = []; |
| 57 | + currentRound.outlierIndexes = []; |
| 58 | + for (i = 0; i < currentRound.dataSet.length; i++) { |
| 59 | + if (typeof currentRound.dataSet[i] === 'undefined') { |
| 60 | + currentRound.gSet.push(undefined); |
| 61 | + currentRound.gPass.push(undefined); |
| 62 | + continue; |
| 63 | + } |
| 64 | + if (typeof currentRound.dataSet[i] !== 'number') { |
| 65 | + throw new Error('data MUST be number'); |
| 66 | + } |
| 67 | + gResult = (currentRound.dataSet[i] - currentRound.average) / currentRound.stdev; |
| 68 | + currentRound.gSet.push(gResult); |
| 69 | + if (Math.abs(gResult) > currentRound.criticalValue) { |
| 70 | + done = false; |
| 71 | + currentRound.gPass.push(false); |
| 72 | + currentRound.outliers.push(currentRound.dataSet[i]); |
| 73 | + currentRound.outlierIndexes.push(i); |
| 74 | + dataSet[i] = undefined; |
| 75 | + } else { |
| 76 | + currentRound.gPass.push(true); |
| 77 | + } |
| 78 | + } |
| 79 | + result.push(currentRound); |
| 80 | + } |
| 81 | + return result; |
| 82 | +} |
| 83 | + |
| 84 | +function isValidData(data) { |
| 85 | + return ( |
| 86 | + typeof data !== 'undefined' && |
| 87 | + !isNaN(data) && |
| 88 | + data !== null |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +function getDigit(dataSet) { |
| 93 | + if (!dataSet) return 0; |
| 94 | + var filteredDataSet = dataSet.filter(isValidData); |
| 95 | + var filteredDataSetLength = filteredDataSet.length; |
| 96 | + if (filteredDataSetLength === 0) return 0; |
| 97 | + var digit = 0; |
| 98 | + filteredDataSet.forEach(function (data) { |
| 99 | + var dataString = data.toString(); |
| 100 | + var dotIndex = dataString.indexOf('.'); |
| 101 | + if (dotIndex === -1) return; |
| 102 | + var currentDigit = dataString.length - dotIndex - 1; |
| 103 | + if (currentDigit > digit) { |
| 104 | + digit = currentDigit; |
| 105 | + } |
| 106 | + }); |
| 107 | + return digit; |
| 108 | +} |
| 109 | + |
| 110 | +module.exports = { |
| 111 | + test: test, |
| 112 | + isValidData: isValidData |
| 113 | +}; |
0 commit comments