-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Originally we were using Welford’s algorithm, but this is primarily useful when computing the variance in a numerically stable manner, since Welford’s approach requires an incremental mean. I’ve removed a test for the mean of more than one instance of Number.MAX_VALUE as this is unlikely to occur in practice; most likely this was the reason I used Welford’s algorithm in the first place. There’s a paper [1] comparing various algorithms for computing the mean, and Welford’s is actually slightly less accurate than the naïve approach. There are some more accurate approaches but I think it’s overkill for d3.mean. [1] Youngs, Edward A., and Elliot M. Cramer. "Some results relevant to choice of sum and sum-of-product algorithms." Technometrics 13.3 (1971): 657-665. Related: d3#1842.
- Loading branch information
1 parent
624f21c
commit c0e84e2
Showing
4 changed files
with
11 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import "../math/number"; | ||
|
||
d3.mean = function(array, f) { | ||
var n = array.length, | ||
var s = 0, | ||
n = array.length, | ||
a, | ||
m = 0, | ||
i = -1, | ||
j = 0; | ||
j = n; | ||
if (arguments.length === 1) { | ||
while (++i < n) if (d3_number(a = array[i])) m += (a - m) / ++j; | ||
while (++i < n) if (d3_number(a = array[i])) s += a; else --j; | ||
} else { | ||
while (++i < n) if (d3_number(a = f.call(array, array[i], i))) m += (a - m) / ++j; | ||
while (++i < n) if (d3_number(a = f.call(array, array[i], i))) s += a; else --j; | ||
} | ||
return j ? m : undefined; | ||
return j ? s / j : undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters