tweak: add coefficient of variation
This commit is contained in:
parent
981b2ca568
commit
5f9225538c
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"cSpell.words": ["Stdev"]
|
||||
}
|
|
@ -5,6 +5,7 @@ import {
|
|||
formatLargeOrSmall,
|
||||
avg,
|
||||
hackyGeomMean,
|
||||
getCoefficientOfVariation,
|
||||
} from "../lib/utils.js";
|
||||
|
||||
/* Functions */
|
||||
|
@ -301,6 +302,7 @@ export function CreateTable({ tableRows }) {
|
|||
<th>Possible relative values</th>
|
||||
<th> </th>
|
||||
<th>geomMean(relative values)</th>
|
||||
<th>Coefficient of variation</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
@ -317,6 +319,9 @@ export function CreateTable({ tableRows }) {
|
|||
<td className="">
|
||||
{formatLargeOrSmall(hackyGeomMean(row.distances))}
|
||||
</td>
|
||||
<td className="">
|
||||
{formatLargeOrSmall(getCoefficientOfVariation(row.distances))}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
|
|
23
lib/utils.js
23
lib/utils.js
|
@ -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 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);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user