kumulant

stat.cardinality

Approximate distinct-count estimation. HyperLogLogStat takes a stream of opaque Long keys (com.eignex.kumulant.core.DiscreteStat) and answers "how many distinct keys have I seen?" in bounded memory. The right pre-step is to hash domain-specific keys through com.eignex.kumulant.math.hash64 so the input has uniform 64-bit entropy; value.hashCode().toLong() only provides 32 bits and skews the estimator on low-cardinality domains.

Accuracy

Memory is O(2^precision) bytes; precision 14 is about 16 KB at ~1% relative error. At small cardinalities the estimator switches to linear counting over its registers, so low counts stay tight.

Merge

HyperLogLogStat merges exactly by taking the cell-wise max of two register vectors of the same precision. Two replicas observing overlapping streams stitch together correctly without double-counting, which makes it a clean fit for distributed cardinality estimation: workers compute slices in parallel, ship snapshots, and the coordinator merges them into a single estimator.

Concurrency

Each update is a CAS-max on one register. Lock-free and exact under every com.eignex.kumulant.core.Concurrency level.

Types

Link copied to clipboard
@Serializable
@SerialName(value = "HyperLogLogResult")
data class HyperLogLogResult(val estimate: Double, val precision: Int, val registers: IntArray, val totalSeen: Long, val hasher: HasherRef = HasherRef.SplitMix64) : HasObservationCount

HyperLogLog snapshot. estimate is the corrected cardinality (linear counting at the small-range, raw HLL elsewhere). registers are the dense rho values per bucket and are required to merge two snapshots without loss; precision selects bucket count m = 2^precision. totalSeen is the unweighted update count.

Link copied to clipboard
class HyperLogLogStat(val precision: Int = StatDefaults.HYPER_LOG_LOG_PRECISION, val hasher: LongHasher = SplitMix64, val concurrency: Concurrency = Concurrency.None) : DiscreteStat<HyperLogLogResult>

HyperLogLog cardinality estimator with a small-range linear-counting fallback.