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,
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);

View File

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

View File

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

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