tweak: add coefficient of variation

This commit is contained in:
NunoSempere 2022-04-11 14:39:29 -04:00
parent 981b2ca568
commit 5f9225538c
3 changed files with 31 additions and 0 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"cSpell.words": ["Stdev"]
}

View File

@ -5,6 +5,7 @@ import {
formatLargeOrSmall, formatLargeOrSmall,
avg, avg,
hackyGeomMean, hackyGeomMean,
getCoefficientOfVariation,
} from "../lib/utils.js"; } from "../lib/utils.js";
/* Functions */ /* Functions */
@ -301,6 +302,7 @@ export function CreateTable({ tableRows }) {
<th>Possible relative values</th> <th>Possible relative values</th>
<th> &nbsp;&nbsp;&nbsp;</th> <th> &nbsp;&nbsp;&nbsp;</th>
<th>geomMean(relative values)</th> <th>geomMean(relative values)</th>
<th>Coefficient of variation</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -317,6 +319,9 @@ export function CreateTable({ tableRows }) {
<td className=""> <td className="">
{formatLargeOrSmall(hackyGeomMean(row.distances))} {formatLargeOrSmall(hackyGeomMean(row.distances))}
</td> </td>
<td className="">
{formatLargeOrSmall(getCoefficientOfVariation(row.distances))}
</td>
</tr> </tr>
))} ))}
</tbody> </tbody>

View File

@ -117,3 +117,26 @@ export function conservativeNumMergeSortSteps(n) {
// export const geomMean = (arr) => arr.reduce((a, b) => a * b, 1); // ^ (1 / arr.length); // didn't work so well for low numbers. // export const geomMean = (arr) => arr.reduce((a, b) => a * b, 1); // ^ (1 / arr.length); // didn't work so well for low numbers.
export const increasingList = (n) => Array.from(Array(n).keys()); export const increasingList = (n) => Array.from(Array(n).keys());
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 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);
}
};