Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/1025#discussion_r15534415
--- Diff:
core/src/main/scala/org/apache/spark/util/random/SamplingUtils.scala ---
@@ -88,14 +91,73 @@ private[spark] object SamplingUtils {
*/
def computeFractionForSampleSize(sampleSizeLowerBound: Int, total: Long,
withReplacement: Boolean): Double = {
- val fraction = sampleSizeLowerBound.toDouble / total
if (withReplacement) {
- val numStDev = if (sampleSizeLowerBound < 12) 9 else 5
- fraction + numStDev * math.sqrt(fraction / total)
+ PoissonBounds.getUpperBound(sampleSizeLowerBound) / total
} else {
- val delta = 1e-4
- val gamma = - math.log(delta) / total
- math.min(1, fraction + gamma + math.sqrt(gamma * gamma + 2 * gamma *
fraction))
+ val fraction = sampleSizeLowerBound.toDouble / total
+ BinomialBounds.getUpperBound(1e-4, total, fraction)
}
}
}
+
+/**
+ * Utility functions that help us determine bounds on adjusted sampling
rate to guarantee exact
+ * sample sizes with high confidence when sampling with replacement.
+ */
+private[spark] object PoissonBounds {
+
+ /**
+ * Returns a lambda such that Pr[X > s] is very small, where X ~
Pois(lambda).
+ */
+ def getLowerBound(s: Double): Double = {
+ math.max(s - numStd(s) * math.sqrt(s), 1e-15)
+ }
+
+ /**
+ * Returns a lambda such that Pr[X < s] is very small, where X ~
Pois(lambda).
+ *
+ * @param s sample size
+ */
+ def getUpperBound(s: Double): Double = {
+ math.max(s + numStd(s) * math.sqrt(s), 1e-10)
+ }
+
+ private def numStd(s: Double): Double = {
+ // TODO: Make it tighter.
+ if (s < 6.0) {
+ 12.0
+ } else if (s < 16.0) {
+ 9.0
+ } else {
+ 6.0
+ }
+ }
+}
+
+/**
+ * Utility functions that help us determine bounds on adjusted sampling
rate to guarantee exact
+ * sample size with high confidence when sampling without replacement.
+ */
+private[spark] object BinomialBounds {
+
+ val minSamplingRate = 1e-10
+
+ /**
+ * Returns a threshold `p` such that if we conduct n Bernoulli trials
with success rate = `p`,
+ * it is very unlikely to have more than `fraction * n` successes.
+ */
+ def getLowerBound(delta: Double, n: Long, fraction: Double): Double = {
+ val gamma = - math.log(delta) / n * (2.0 / 3.0)
+ math.max(minSamplingRate,
--- End diff --
It should be okay to return `0` here. The `minSamplingRate` should be
applied to `getUpperBound`.
---
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.
---