Skip to content

Commit 21e7ff7

Browse files
committed
update
1 parent 56c0d41 commit 21e7ff7

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

mllib/src/main/scala/org/apache/spark/ml/optim/WeightedLeastSquares.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ private[ml] object WeightedLeastSquares {
472472
while (i < triK) {
473473
val l = j - 2
474474
val aw = aSum(l) / wSum
475-
std(l) = math.sqrt(aaValues(i) / wSum - aw * aw)
475+
// We prevent variance from negative value caused by numerical error.
476+
std(l) = math.sqrt(math.max(aaValues(i) / wSum - aw * aw, 0.0))
476477
i += j
477478
j += 1
478479
}
@@ -490,7 +491,8 @@ private[ml] object WeightedLeastSquares {
490491
while (i < triK) {
491492
val l = j - 2
492493
val aw = aSum(l) / wSum
493-
variance(l) = aaValues(i) / wSum - aw * aw
494+
// We prevent variance from negative value caused by numerical error.
495+
variance(l) = math.max(aaValues(i) / wSum - aw * aw, 0.0)
494496
i += j
495497
j += 1
496498
}

0 commit comments

Comments
 (0)