Skip to content

Commit 6166473

Browse files
MechCodermengxr
authored andcommitted
[SPARK-7844] [MLLIB] Fix broken tests in KernelDensity
The densities in KernelDensity are scaled down by (number of parallel processes X number of points). It should be just no.of samples. This results in broken tests in KernelDensitySuite which haven't been tested properly. Author: MechCoder <[email protected]> Closes #6383 from MechCoder/spark-7844 and squashes the following commits: ab81302 [MechCoder] Math->math 9b8ed50 [MechCoder] Make one pass to update count a92fe50 [MechCoder] [SPARK-7844] Fix broken tests in KernelDensity
1 parent b7d8085 commit 6166473

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

mllib/src/main/scala/org/apache/spark/mllib/stat/KernelDensity.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class KernelDensity extends Serializable {
9393
x._1(i) += normPdf(y, bandwidth, logStandardDeviationPlusHalfLog2Pi, points(i))
9494
i += 1
9595
}
96-
(x._1, n)
96+
(x._1, x._2 + 1)
9797
},
9898
(x, y) => {
9999
blas.daxpy(n, 1.0, y._1, 1, x._1, 1)

mllib/src/test/scala/org/apache/spark/mllib/stat/KernelDensitySuite.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class KernelDensitySuite extends FunSuite with MLlibTestSparkContext {
2929
val densities = new KernelDensity().setSample(rdd).setBandwidth(3.0).estimate(evaluationPoints)
3030
val normal = new NormalDistribution(5.0, 3.0)
3131
val acceptableErr = 1e-6
32-
assert(densities(0) - normal.density(5.0) < acceptableErr)
33-
assert(densities(0) - normal.density(6.0) < acceptableErr)
32+
assert(math.abs(densities(0) - normal.density(5.0)) < acceptableErr)
33+
assert(math.abs(densities(1) - normal.density(6.0)) < acceptableErr)
3434
}
3535

3636
test("kernel density multiple samples") {
@@ -40,7 +40,9 @@ class KernelDensitySuite extends FunSuite with MLlibTestSparkContext {
4040
val normal1 = new NormalDistribution(5.0, 3.0)
4141
val normal2 = new NormalDistribution(10.0, 3.0)
4242
val acceptableErr = 1e-6
43-
assert(densities(0) - (normal1.density(5.0) + normal2.density(5.0)) / 2 < acceptableErr)
44-
assert(densities(0) - (normal1.density(6.0) + normal2.density(6.0)) / 2 < acceptableErr)
43+
assert(math.abs(
44+
densities(0) - (normal1.density(5.0) + normal2.density(5.0)) / 2) < acceptableErr)
45+
assert(math.abs(
46+
densities(1) - (normal1.density(6.0) + normal2.density(6.0)) / 2) < acceptableErr)
4547
}
4648
}

0 commit comments

Comments
 (0)