Skip to content
Snippets Groups Projects
Commit 9d359043 authored by Christopher Nguyen's avatar Christopher Nguyen
Browse files

In the current code, when both partitions happen to have zero-length, the return mean will be NaN.

Consequently, the result of mean after reducing over all partitions will also be NaN,
which is not correct if there are partitions with non-zero length. This patch fixes this issue.
parent fff37285
No related branches found
No related tags found
No related merge requests found
...@@ -37,17 +37,23 @@ class StatCounter(values: TraversableOnce[Double]) extends Serializable { ...@@ -37,17 +37,23 @@ class StatCounter(values: TraversableOnce[Double]) extends Serializable {
if (other == this) { if (other == this) {
merge(other.copy()) // Avoid overwriting fields in a weird order merge(other.copy()) // Avoid overwriting fields in a weird order
} else { } else {
val delta = other.mu - mu if (n == 0) {
if (other.n * 10 < n) { mu = other.mu
mu = mu + (delta * other.n) / (n + other.n) m2 = other.m2
} else if (n * 10 < other.n) { n = other.n
mu = other.mu - (delta * n) / (n + other.n) } else if (other.n != 0) {
} else { val delta = other.mu - mu
mu = (mu * n + other.mu * other.n) / (n + other.n) if (other.n * 10 < n) {
mu = mu + (delta * other.n) / (n + other.n)
} else if (n * 10 < other.n) {
mu = other.mu - (delta * n) / (n + other.n)
} else {
mu = (mu * n + other.mu * other.n) / (n + other.n)
}
m2 += other.m2 + (delta * delta * n * other.n) / (n + other.n)
n += other.n
} }
m2 += other.m2 + (delta * delta * n * other.n) / (n + other.n) this
n += other.n
this
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment