Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geometric mean #259

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Returns the sum of the given *iterable* of numbers. If the iterable contains no

Returns the mean of the given *iterable* of numbers. If the iterable contains no numbers, returns undefined. An optional *accessor* function may be specified, which is equivalent to calling Array.from before computing the mean. This method ignores undefined and NaN values; this is useful for ignoring missing data.

<a name="gmean" href="#gmean">#</a> d3.<b>gmean</b>(<i>iterable</i>[, <i>accessor</i>]) · [Source](https://github.com/d3/d3-array/blob/main/src/gmean.js)

Returns the geometric mean of the given *iterable* of numbers. If the iterable contains no numbers, returns undefined. An optional *accessor* function may be specified, which is equivalent to calling Array.from before computing the mean. This method ignores undefined and NaN values; this is useful for ignoring missing data.

<a name="median" href="#median">#</a> d3.<b>median</b>(<i>iterable</i>[, <i>accessor</i>]) · [Source](https://github.com/d3/d3-array/blob/main/src/median.js), [Examples](https://observablehq.com/@d3/d3-mean-d3-median-and-friends)

Returns the median of the given *iterable* of numbers using the [R-7 method](https://en.wikipedia.org/wiki/Quantile#Estimating_quantiles_from_a_sample). If the iterable contains no numbers, returns undefined. An optional *accessor* function may be specified, which is equivalent to calling Array.from before computing the median. This method ignores undefined and NaN values; this is useful for ignoring missing data.
Expand Down
34 changes: 34 additions & 0 deletions src/gmean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export default function gmean(values, valueof) {
const r = 64;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using tabs is inconsistent with the rest of the codebase, which uses spaces.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this commit a few times before pushing and didn't notice the last change was using tabs instead of spaces. Making the necessary changes..

Using tabs is inconsistent with the rest of the codebase, which uses spaces.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that tabs are used in the new file, whereas the rest of the codebase uses spaces.

Also curious, is this implementation derived from any existing work, or inspired from any particular source?

Yes, this PR was inspired by d3.mean(), used to calculate the arithmetic mean because I noticed similarities between the two. Will also work on showing differences between the geometric and arithmetic mean calculations and update this PR.

const K = 2 ** r;
const K1 = 2 ** -r;
let p = 1; // product
let n = 0; // count
let s = 1; // sign
let k = 0; // track exponent to prevent under/overflows
if (valueof === undefined) {
for (let value of values) {
if (value != null && (value = +value) >= value) {
n++;
s = Math.sign(value);
if (s === 0) return 0;
p *= Math.abs(value);
while (p > K) (p *= K1), ++k;
while (p < K1) (p *= K), --k;
}
}
} else {
let index = -1;
for (let value of values) {
if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
n++;
s = Math.sign(value);
if (s === 0) return 0;
p *= Math.abs(value);
while (p > K) (p *= K1), ++k;
while (p < K1) (p *= K), --k;
}
}
}
if (n) return s * 2 ** ((Math.log2(p) + k * r) / n)
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {default as descending} from "./descending.js";
export {default as deviation} from "./deviation.js";
export {default as extent} from "./extent.js";
export {Adder, fsum, fcumsum} from "./fsum.js";
export {default as gmean} from "./gmean.js";
export {default as group, flatGroup, flatRollup, groups, index, indexes, rollup, rollups} from "./group.js";
export {default as groupSort} from "./groupSort.js";
export {default as bin, default as histogram} from "./bin.js"; // Deprecated; use bin.
Expand Down
85 changes: 85 additions & 0 deletions test/gmean-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import assert from "assert";
import {gmean} from "../src/index.js";
import {OneTimeNumber} from "./OneTimeNumber.js";

it("gmean(array) returns the geometric mean value for numbers", () => {
assert.strictEqual(gmean([1]), 1);
assert.strictEqual(gmean([2, 4, 8, 16, 32]), 8);
});

it("gmean(array) ignores null, undefined and NaN", () => {
assert.strictEqual(gmean([NaN, 2, 4, 8, 16, 32]), 8);
assert.strictEqual(gmean([10, null, undefined, 1, NaN]), 3.162277660168379);
});

it("gmean(array) returns undefined if the array contains no observed values", () => {
assert.strictEqual(gmean([]), undefined);
assert.strictEqual(gmean([null]), undefined);
assert.strictEqual(gmean([undefined]), undefined);
assert.strictEqual(gmean([NaN]), undefined);
assert.strictEqual(gmean([NaN, NaN]), undefined);
});

it("gmean(array) coerces values to numbers", () => {
assert.strictEqual(gmean(["1"]), 1);
assert.strictEqual(gmean(["5", "1", "2", "3", "4"]), 2.605171084697352);
assert.strictEqual(gmean(["20", "3"]), 7.745966692414834);
});

it("gmean(array) coerces values exactly once", () => {
const numbers = [1, new OneTimeNumber(3)];
assert.strictEqual(gmean(numbers), 1.7320508075688772);
assert.strictEqual(gmean(numbers), 1);
});

it("gmean(array, f) returns the geometric mean value for numbers", () => {
assert.strictEqual(gmean([1].map(box), unbox), 1);
assert.strictEqual(gmean([5, 1, 2, 3, 4].map(box), unbox), 2.605171084697352);
assert.strictEqual(gmean([20, 3].map(box), unbox), 7.745966692414834);
});

it("gmean(array, f) ignores null, undefined and NaN", () => {
assert.strictEqual(gmean([NaN, 1, 2, 3, 4, 5].map(box), unbox), 2.605171084697352);
assert.strictEqual(gmean([10, null, 3, undefined, 5, NaN].map(box), unbox), 5.313292845913056);
});

it("gmean(array, f) returns undefined if the array contains no observed values", () => {
assert.strictEqual(gmean([].map(box), unbox), undefined);
assert.strictEqual(gmean([null].map(box), unbox), undefined);
assert.strictEqual(gmean([undefined].map(box), unbox), undefined);
assert.strictEqual(gmean([NaN].map(box), unbox), undefined);
assert.strictEqual(gmean([NaN, NaN].map(box), unbox), undefined);
});

it("gmean(array, f) coerces values to numbers", () => {
assert.strictEqual(gmean(["1"].map(box), unbox), 1);
assert.strictEqual(gmean(["1", "2", "3", "4", "5"].map(box), unbox), 2.605171084697352);
assert.strictEqual(gmean(["20", "3"].map(box), unbox), 7.745966692414834);
});

it("gmean(array, f) coerces values exactly once", () => {
const numbers = [1, new OneTimeNumber(3)].map(box);
assert.strictEqual(gmean(numbers, unbox), 1.7320508075688772);
assert.strictEqual(gmean(numbers, unbox), 1);
});

it("gmean(array, f) passes the accessor d, i, and array", () => {
const results = [];
const strings = ["a", "b", "c"];
gmean(strings, (d, i, array) => results.push([d, i, array]));
assert.deepStrictEqual(results, [["a", 0, strings], ["b", 1, strings], ["c", 2, strings]]);
});

it("gmean(array, f) uses the undefined context", () => {
const results = [];
gmean([1, 2], function() { results.push(this); });
assert.deepStrictEqual(results, [undefined, undefined]);
});

function box(value) {
return {value: value};
}

function unbox(box) {
return box.value;
}