Github user josepablocam commented on a diff in the pull request: https://github.com/apache/spark/pull/7075#discussion_r35942456 --- 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 + * partition + */ + private def searchTwoSampleCandidates( + partData: Iterator[(Double, Boolean)], + n1: Double, + n2: Double) + : Iterator[(Double, Double, Double)] = { + // fold accumulator: local minimum, local maximum, index for sample 1, index for sample2 + case class KS2Acc(min: Double, max: Double, ix1: Int, ix2: Int) --- End diff -- How about ExtremaAndRunningIndices? The indices aren't the positions of the extrema themselves, but rather than running indices that we use for the position of element i in sample 1 and sample 2, to then calculate the empirical CDF values associated with that element.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org