Last active
November 22, 2020 02:06
-
-
Save Restuta/5cbf1d186f17febe5e899febb63e1b86 to your computer and use it in GitHub Desktop.
Outliers: z-score and modified z-score method in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// z-score | |
const { mean, deviation } = require('d3-array') | |
const zscore = input => { | |
const arrMean = mean(input) | |
// here the n-1 : http://duramecho.com/Misc/WhyMinusOneInSd.html | |
const arrDeviation = deviation(input) | |
return input.map(i => ({ | |
zscore: (i - arrMean) / arrDeviation, | |
item: i, | |
})) | |
} | |
const findOutliersZ = input => { | |
return zscore(input).filter(x => x.zscore >= 3 || x.zscore <= -3).map(x => x.item) | |
} | |
/** MODIFIED ZSCORE ***/ | |
const R = require('ramda') | |
const zscorem = input => { | |
// const arrMean = mean(input) | |
// here the n-1 : http://duramecho.com/Misc/WhyMinusOneInSd.html | |
// const arrDeviation = deviation(input) | |
const medianY = R.median(input) | |
medianY //? | |
// MAD | |
const medianAbsDeviationY = R.median( | |
input.map(y => Math.abs(y - medianY)) | |
) | |
medianAbsDeviationY //? | |
const MODIFIED_Z_SCORE_CONST = 0.6745 | |
return input.map(y => ({ | |
zscore: MODIFIED_Z_SCORE_CONST * (y - medianY) / medianAbsDeviationY, | |
item: y, | |
})) | |
} | |
const findOutliersZ = input => { | |
return zscorem(input).filter(x => x.zscore >= 3.5 || x.zscore <= -3.5).map(x => x.item) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment