fix: exclude non-finite non-numeric args

This commit is contained in:
NunoSempere 2022-06-19 21:17:12 -04:00
parent 5215b54964
commit 0c78b74c3a
5 changed files with 54 additions and 38 deletions

View File

@ -84,6 +84,7 @@ export function aggregatePathsThroughMixtureOfMeans({
pathsArray, pathsArray,
orderedList, orderedList,
VERBOSE, VERBOSE,
DONT_EXCLUDE_INFINITIES_AND_NANS,
}) { }) {
let print = (x) => { let print = (x) => {
if (VERBOSE) { if (VERBOSE) {
@ -93,11 +94,21 @@ export function aggregatePathsThroughMixtureOfMeans({
let result = pathsArray.map((paths, i) => { let result = pathsArray.map((paths, i) => {
print(orderedList[i].name); print(orderedList[i].name);
let expectedRelativeValues = paths let expectedRelativeValues = paths.map(
.map((path) => path.expectedRelativeValue) (path) => path.expectedRelativeValue
.filter((x) => x != undefined); );
let hasPositive = expectedRelativeValues.filter((x) => x > 0);
let hasNegative = expectedRelativeValues.filter((x) => x < 0); 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; let answer;
if (hasPositive.length != 0 && hasNegative.length != 0) { if (hasPositive.length != 0 && hasNegative.length != 0) {
answer = avg(expectedRelativeValues); answer = avg(expectedRelativeValues);

View File

@ -43,7 +43,8 @@ async function main() {
pathsArray: paths, pathsArray: paths,
orderedList, orderedList,
aggregationType: "mean", // alternatively: aggregationType: "distribution" aggregationType: "mean", // alternatively: aggregationType: "distribution"
VERBOSE: false, VERBOSE: false, // optional arg
DONT_EXCLUDE_INFINITIES_AND_NANS: false, // optional arg
}); });
console.log(aggregatedPaths); console.log(aggregatedPaths);
} }

View File

@ -5,6 +5,7 @@ import { Separator } from "./separator.js";
import { truncateValueForDisplay } from "../lib/truncateNums.js"; import { truncateValueForDisplay } from "../lib/truncateNums.js";
import { cutOffLongNames } from "../lib/stringManipulations.js"; import { cutOffLongNames } from "../lib/stringManipulations.js";
import { getCoefficientOfVariation } from "../lib/coefficientOfVariation.js";
async function fullResultsTable({ listAfterMergeSort, links }) { async function fullResultsTable({ listAfterMergeSort, links }) {
console.log("listAfterMergeSort", listAfterMergeSort); console.log("listAfterMergeSort", listAfterMergeSort);
@ -22,38 +23,6 @@ async function fullResultsTable({ listAfterMergeSort, links }) {
return aggregatedPaths; 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) { function abridgeArrayAndDisplay(array) {
let newArray; let newArray;
let formatForDisplay; let formatForDisplay;

View File

@ -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);
}
};

View File

@ -21,6 +21,8 @@ export const truncateValueForDisplay = (value) => {
candidateNumSignificantDigits candidateNumSignificantDigits
); );
result = value.toFixed(numSignificantDigits); result = value.toFixed(numSignificantDigits);
} else if (value == 0) {
return 0;
} else if (-1 < value) { } else if (-1 < value) {
let candidateNumSignificantDigits = let candidateNumSignificantDigits =
-Math.floor(Math.log(Math.abs(value)) / Math.log(10)) + 1; -Math.floor(Math.log(Math.abs(value)) / Math.log(10)) + 1;