Skip to content

Commit

Permalink
fix: ensure kplus reach ratio not exceeding 1.0 (#1868)
Browse files Browse the repository at this point in the history
…ution.
  • Loading branch information
ple13 authored Oct 23, 2024
1 parent 19a32bd commit 34eb548
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.wfanet.measurement.measurementconsumer.stats

import com.google.common.math.DoubleMath.fuzzyCompare
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow
import kotlin.random.Random
import kotlin.random.asJavaRandom
Expand Down Expand Up @@ -81,6 +83,8 @@ interface Variances {

/** Default implementation of [Variances]. */
object VariancesImpl : Variances {
private const val TOLERANCE = 1E-6

/**
* Computes the variance of a reach measurement that is computed using the deterministic count
* distinct methodology.
Expand Down Expand Up @@ -441,11 +445,14 @@ object VariancesImpl : Variances {
val maximumFrequency = params.measurementParams.maximumFrequency

var suffixSum = 0.0
// There is no estimate of zero-frequency reach
// There is no estimate of zero-frequency reach.
val kPlusRelativeFrequencyDistribution: Map<Int, Double> =
(maximumFrequency downTo 1).associateWith { frequency ->
suffixSum += params.relativeFrequencyDistribution.getOrDefault(frequency, 0.0)
suffixSum
require(fuzzyCompare(suffixSum, 1.0, TOLERANCE) <= 0) {
"kPlus relative frequency must not exceed 1, but got $suffixSum."
}
min(1.0, suffixSum)
}

val relativeVariances: Map<Int, Double> =
Expand Down Expand Up @@ -554,7 +561,10 @@ object VariancesImpl : Variances {
val kPlusRelativeFrequencyDistribution: Map<Int, Double> =
(maximumFrequency downTo 1).associateWith { frequency ->
suffixSum += frequencyParams.relativeFrequencyDistribution.getOrDefault(frequency, 0.0)
suffixSum
require(fuzzyCompare(suffixSum, 1.0, TOLERANCE) <= 0) {
"kPlus relative frequency must not exceed 1, but got $suffixSum."
}
min(1.0, suffixSum)
}

val countVariances: Map<Int, Double> =
Expand Down Expand Up @@ -656,7 +666,10 @@ object VariancesImpl : Variances {
val kPlusRelativeFrequencyDistribution: Map<Int, Double> =
(maximumFrequency downTo 1).associateWith { frequency ->
suffixSum += params.relativeFrequencyDistribution.getOrDefault(frequency, 0.0)
suffixSum
require(fuzzyCompare(suffixSum, 1.0, TOLERANCE) <= 0) {
"kPlus relative frequency must not exceed 1, but got $suffixSum."
}
min(1.0, suffixSum)
}

val countVariances: Map<Int, Double> =
Expand Down

0 comments on commit 34eb548

Please sign in to comment.