From 0c78b74c3adcb02bdeff1643b1ed8beed1a0647d Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 19 Jun 2022 21:17:12 -0400 Subject: [PATCH] fix: exclude non-finite non-numeric args --- packages/utility-tools/src/aggregatePaths.js | 21 +++++++++--- packages/utility-tools/src/example.js | 3 +- .../components/resultsTable.js | 33 +------------------ .../lib/coefficientOfVariation.js | 33 +++++++++++++++++++ packages/webpage-refactor/lib/truncateNums.js | 2 ++ 5 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 packages/webpage-refactor/lib/coefficientOfVariation.js diff --git a/packages/utility-tools/src/aggregatePaths.js b/packages/utility-tools/src/aggregatePaths.js index 933cbaa..605283f 100644 --- a/packages/utility-tools/src/aggregatePaths.js +++ b/packages/utility-tools/src/aggregatePaths.js @@ -84,6 +84,7 @@ export function aggregatePathsThroughMixtureOfMeans({ pathsArray, orderedList, VERBOSE, + DONT_EXCLUDE_INFINITIES_AND_NANS, }) { let print = (x) => { if (VERBOSE) { @@ -93,11 +94,21 @@ export function aggregatePathsThroughMixtureOfMeans({ let result = pathsArray.map((paths, i) => { print(orderedList[i].name); - let expectedRelativeValues = paths - .map((path) => path.expectedRelativeValue) - .filter((x) => x != undefined); - let hasPositive = expectedRelativeValues.filter((x) => x > 0); - let hasNegative = expectedRelativeValues.filter((x) => x < 0); + let expectedRelativeValues = paths.map( + (path) => path.expectedRelativeValue + ); + + let expectedRelativeValuesFiltered; + if (!DONT_EXCLUDE_INFINITIES_AND_NANS) { + expectedRelativeValuesFiltered + .filter((x) => x != undefined) + .filter((x) => !isNaN(x)) + .filter((x) => isFinite(x)) + .filter((x) => x != null); + } + + let hasPositive = expectedRelativeValuesFiltered.filter((x) => x > 0); + let hasNegative = expectedRelativeValuesFiltered.filter((x) => x < 0); let answer; if (hasPositive.length != 0 && hasNegative.length != 0) { answer = avg(expectedRelativeValues); diff --git a/packages/utility-tools/src/example.js b/packages/utility-tools/src/example.js index 92e5518..5a8f7cb 100644 --- a/packages/utility-tools/src/example.js +++ b/packages/utility-tools/src/example.js @@ -43,7 +43,8 @@ async function main() { pathsArray: paths, orderedList, aggregationType: "mean", // alternatively: aggregationType: "distribution" - VERBOSE: false, + VERBOSE: false, // optional arg + DONT_EXCLUDE_INFINITIES_AND_NANS: false, // optional arg }); console.log(aggregatedPaths); } diff --git a/packages/webpage-refactor/components/resultsTable.js b/packages/webpage-refactor/components/resultsTable.js index afaf969..f76ec62 100644 --- a/packages/webpage-refactor/components/resultsTable.js +++ b/packages/webpage-refactor/components/resultsTable.js @@ -5,6 +5,7 @@ import { Separator } from "./separator.js"; import { truncateValueForDisplay } from "../lib/truncateNums.js"; import { cutOffLongNames } from "../lib/stringManipulations.js"; +import { getCoefficientOfVariation } from "../lib/coefficientOfVariation.js"; async function fullResultsTable({ listAfterMergeSort, links }) { console.log("listAfterMergeSort", listAfterMergeSort); @@ -22,38 +23,6 @@ async function fullResultsTable({ listAfterMergeSort, links }) { return aggregatedPaths; } -const sum = (arr) => arr.reduce((a, b) => a + b, 0); - -function getStdev(arr) { - if (Array.isArray(arr) && arr.length > 0) { - const n = arr.length; - const mean = arr.reduce((a, b) => a + b) / n; - return Math.sqrt( - arr.map((x) => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n - ); - } else { - return 0; - } -} - -export const geomMean = (arr) => { - let n = arr.length; - let logavg = sum(arr.map((x) => Math.log(x))); // works for low numbers much better, numerically - let result = Math.exp(logavg / n); - return result; -}; - -const getCoefficientOfVariation = (arr) => { - let nonPositiveNumbers = arr.filter((x) => x <= 0); - if (nonPositiveNumbers.length == 0) { - let gm = geomMean(arr); - let stdev = getStdev(arr); - return stdev / gm; - } else { - return getStdev(arr) / avg(arr); - } -}; - function abridgeArrayAndDisplay(array) { let newArray; let formatForDisplay; diff --git a/packages/webpage-refactor/lib/coefficientOfVariation.js b/packages/webpage-refactor/lib/coefficientOfVariation.js new file mode 100644 index 0000000..3a94108 --- /dev/null +++ b/packages/webpage-refactor/lib/coefficientOfVariation.js @@ -0,0 +1,33 @@ +const sum = (arr) => arr.reduce((a, b) => a + b, 0); + +function getStdev(arr) { + if (Array.isArray(arr) && arr.length > 0) { + const n = arr.length; + const mean = arr.reduce((a, b) => a + b) / n; + return Math.sqrt( + arr.map((x) => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n + ); + } else { + return 0; + } +} + +export const avg = (arr) => sum(arr) / arr.length; + +export const geomMean = (arr) => { + let n = arr.length; + let logavg = sum(arr.map((x) => Math.log(x))); // works for low numbers much better, numerically + let result = Math.exp(logavg / n); + return result; +}; + +export const getCoefficientOfVariation = (arr) => { + let nonPositiveNumbers = arr.filter((x) => x <= 0); + if (nonPositiveNumbers.length == 0) { + let gm = geomMean(arr); + let stdev = getStdev(arr); + return stdev / gm; + } else { + return getStdev(arr) / avg(arr); + } +}; diff --git a/packages/webpage-refactor/lib/truncateNums.js b/packages/webpage-refactor/lib/truncateNums.js index 5dc53e3..d657604 100644 --- a/packages/webpage-refactor/lib/truncateNums.js +++ b/packages/webpage-refactor/lib/truncateNums.js @@ -21,6 +21,8 @@ export const truncateValueForDisplay = (value) => { candidateNumSignificantDigits ); result = value.toFixed(numSignificantDigits); + } else if (value == 0) { + return 0; } else if (-1 < value) { let candidateNumSignificantDigits = -Math.floor(Math.log(Math.abs(value)) / Math.log(10)) + 1;