Github user josepablocam commented on a diff in the pull request:
https://github.com/apache/spark/pull/7075#discussion_r35943124
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/stat/test/KolmogorovSmirnovTest.scala
---
@@ -190,5 +191,93 @@ private[stat] object KolmogorovSmirnovTest extends
Logging {
val pval = 1 - new KolmogorovSmirnovTest().cdf(ksStat, n.toInt)
new KolmogorovSmirnovTestResult(pval, ksStat,
NullHypothesis.OneSampleTwoSided.toString)
}
+
+ /**
+ * Implements a two-sample, two-sided Kolmogorov-Smirnov test, which
tests if the 2 samples
+ * come from the same distribution
+ * @param data1 `RDD[Double]` first sample of data
+ * @param data2 `RDD[Double]` second sample of data
+ * @return
[[org.apache.spark.mllib.stat.test.KolmogorovSmirnovTestResult]] with the test
+ * statistic, p-value, and appropriate null hypothesis
+ */
+ def testTwoSamples(data1: RDD[Double], data2: RDD[Double]):
KolmogorovSmirnovTestResult = {
+ val n1 = data1.count().toDouble
+ val n2 = data2.count().toDouble
+ val isSample1 = true // identifier for sample 1, needed after co-sort
+ // combine identified samples
+ val joinedData = data1.map(x => (x, isSample1)) ++ data2.map(x => (x,
!isSample1))
+ // co-sort and operate on each partition
+ val localData = joinedData.sortBy { case (v, id) => v }.mapPartitions
{ part =>
+ searchTwoSampleCandidates(part, n1, n2) // local extrema
+ }.collect()
+ val ksStat = searchTwoSampleStatistic(localData, n1 * n2) // result:
global extreme
+ evalTwoSampleP(ksStat, n1.toInt, n2.toInt)
+ }
+
+ /**
+ * Calculates maximum distance candidates and counts from each sample
within one partition for
+ * the two-sample, two-sided Kolmogorov-Smirnov test implementation
+ * @param partData `Iterator[(Double, Boolean)]` the data in 1 partition
of the co-sorted RDDs,
+ * each element is additionally tagged with a boolean
flag for sample 1 membership
+ * @param n1 `Double` sample 1 size
+ * @param n2 `Double` sample 2 size
+ * @return `Iterator[(Double, Double, Double)]` where the first element
is an unadjusted minimum
+ * distance , the second is an unadjusted maximum distance (both
of which will later
+ * be adjusted by a constant to account for elements in prior
partitions), and a
+ * count corresponding to the numerator of the adjustment
constant coming from this
--- End diff --
done, added. Let me know if this is clearer now. I will push the change in
a bit, after fixing rest.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]