kumulant

stat.quantile

Bounded-memory quantile estimators and histograms. Every entry trades a different precision-versus-cost knob: relative error guarantees, fixed-precision over a known range, reservoir sampling for raw values back, or constant memory at the cost of accuracy.

Picking a quantile estimator

StatMemoryPrecisionReach for it when
DDSketchStatO(log(maxIndexableValue / minIndexableValue) / relativeError)Relative value error within the indexable range; out-of-range values fold into edge binsValues spanning orders of magnitude with a configured range and error target.
TDigestStatO(compression)Adaptive resolution, finer near the tails; no relative-error guaranteeYou care about extreme percentiles without choosing a relative-error parameter.
BucketHistogramStatO(edges)Caller-supplied edgesYou know the meaningful value buckets ahead of time (SLO latency buckets, a probability range split evenly with BucketHistogramStat.linear) and want per-bucket counts.
ReservoirHistogramStatO(capacity)Raw values backDownstream needs the actual observations (to feed another stat or compute quantities the sketches don't expose).
FrugalQuantileStatO(1); one estimateCoarse, single-quantileYou can fit only a few bytes per stat and only care about one percentile.

Result shapes

ResultShape
SketchResultDDSketch snapshot: log-spaced bin map + precomputed quantiles at the configured probabilities
QuantileResultFrugalQuantileStat single-quantile scalar
TDigestResultt-digest centroids + precomputed quantiles
SparseHistogramResultParallel lower/upper bound arrays with weights; produced by BucketHistogramStat and SketchResult.toSparseHistogram
ReservoirResultBounded reservoir sample of raw values + the sampling weight

SketchResult and TDigestResult include quantiles at configured probabilities. ReservoirResult.quantile computes a quantile from retained values on demand. Histogram conversion helpers project these results to SparseHistogramResult when downstream consumers need a shared histogram shape.

PIT-style equiprobable histogram

The pitHistogram(numBins) factory in com.eignex.kumulant.stat.score is built from this family: a stream of PIT values (which are uniform under correct distributional forecasts) fed into an equiprobable BucketHistogramStat over [0, 1] exposes the deviation from uniformity that the corresponding PIT test consumes.

Merge

  • DDSketch adds matching logarithmic bins. Incoming indices outside the receiver's indexable range fold into its edge bins.

  • BucketHistogram adds each incoming interval to the bucket holding its midpoint; matching edges merge exactly and other layouts are approximated.

  • t-digest combines centroids and recompresses; the result remains approximate and depends on merge order.

  • ReservoirHistogram retains the highest admission keys from both samples. Equivalence to a single reservoir requires independent admission streams and source samples large enough for the receiver's capacity.

  • FrugalQuantile averages two point estimates. This is a coarse, non-associative approximation with no accuracy guarantee.

Concurrency

Histogram-shaped stats (DDSketchStat, BucketHistogramStat) use independent additive cells. Concurrent modes preserve per-cell updates, with JVM striped adders under com.eignex.kumulant.core.Concurrency.HighWrite; multi-bin reads are not atomic. ReservoirHistogramStat locks updates, merges, and reads under Strict and HighWrite. Relaxed admission uses CAS with a separate RNG lock and can expose mismatched keys and values during a read. FrugalQuantileStat locks steps under Strict and HighWrite and uses atomic updates under Relaxed; estimates depend on observation order. TDigestStat locks its body under every concurrent mode. com.eignex.kumulant.core.Concurrency.None provides no synchronisation.

Types

Link copied to clipboard
class BucketHistogramStat(edges: DoubleArray, val concurrency: Concurrency = Concurrency.None) : SeriesStat<SparseHistogramResult>

Weighted histogram over fixed, caller-chosen bucket edges.

Link copied to clipboard
class DDSketchStat(val relativeError: Double = StatDefaults.DD_SKETCH_RELATIVE_ERROR, val probabilities: DoubleArray = StatDefaults.quantileProbabilities(), val minIndexableValue: Double = StatDefaults.DD_SKETCH_MIN_INDEXABLE_VALUE, val maxIndexableValue: Double = StatDefaults.DD_SKETCH_MAX_INDEXABLE_VALUE, val concurrency: Concurrency = Concurrency.None) : SeriesStat<SketchResult>

DDSketchStat: relative-error quantile sketch with logarithmic bins.

Link copied to clipboard
class FrugalQuantileStat(val q: Double, val stepSize: Double = StatDefaults.FRUGAL_QUANTILE_STEP_SIZE, val initialEstimate: Double = StatDefaults.FRUGAL_QUANTILE_INITIAL_ESTIMATE, val concurrency: Concurrency = Concurrency.None) : SeriesStat<QuantileResult>

Frugal-streaming single-quantile estimator.

Link copied to clipboard
@Serializable
@SerialName(value = "QuantileResult")
data class QuantileResult(val probability: Double, val quantile: Double, val hasObservation: Boolean = true) : Result

Single estimated quantile with the probability it targets.

Link copied to clipboard
class ReservoirHistogramStat(val capacity: Int = StatDefaults.RESERVOIR_HISTOGRAM_CAPACITY, val seed: Long = Random.Default.nextLong(), val concurrency: Concurrency = Concurrency.None) : SeriesStat<ReservoirResult>

Weighted reservoir sample of size capacity via Algorithm A-Res (Efraimidis & Spirakis): each item gets a key u^(1/w) and the top-k keys are retained, giving an unbiased weight-proportional sample.

Link copied to clipboard
@Serializable
@SerialName(value = "ReservoirResult")
data class ReservoirResult(val values: DoubleArray, val keys: DoubleArray, val capacity: Int, val totalSeen: Long, val totalWeight: Double) : HasObservationCount

Reservoir sampling snapshot.

Link copied to clipboard
@Serializable
@SerialName(value = "SketchResult")
data class SketchResult(val probabilities: DoubleArray, val quantiles: DoubleArray, val gamma: Double, val totalWeights: Double, val zeroCount: Double, val positiveBins: Map<Int, Double>, val negativeBins: Map<Int, Double>) : HasObservationCount

DDSketch snapshot: logarithmic bins plus precomputed quantiles for probabilities.

Link copied to clipboard
@Serializable
@SerialName(value = "SparseHistogramResult")
data class SparseHistogramResult(val lowerBounds: DoubleArray, val upperBounds: DoubleArray, val weights: DoubleArray) : Result

Histogram as parallel [lowerBounds, upperBounds) bucket arrays with weights.

Link copied to clipboard
@Serializable
@SerialName(value = "TDigestResult")
data class TDigestResult(val probabilities: DoubleArray, val quantiles: DoubleArray, val means: DoubleArray, val weights: DoubleArray, val totalWeight: Double, val compression: Double) : HasObservationCount

T-digest snapshot: means/weights are the centroid arrays sorted by mean, with quantiles precomputed for probabilities via CDF inversion.

Link copied to clipboard
class TDigestStat(val compression: Double = StatDefaults.TDIGEST_COMPRESSION, val probabilities: DoubleArray = StatDefaults.quantileProbabilities(), val concurrency: Concurrency = Concurrency.None) : SeriesStat<TDigestResult>

Buffered merging T-Digest (Dunning) with k1 scaling function for high-fidelity extreme-quantile estimates and bounded centroid count. compression (delta) caps centroids to roughly ~6*delta.

Functions

Link copied to clipboard
fun linearEdges(lowerBound: Double, upperBound: Double, binCount: Int): DoubleArray

binCount + 1 evenly spaced edges from lowerBound to upperBound, both inclusive.

Link copied to clipboard

Linear-interpolated quantile at probability from the reservoir sample. Each retained value counts once: A-Res admission already favours heavier observations, so the sample stands for the weighted stream and reweighting it would count the weights twice.

Link copied to clipboard

Project a SketchResult into a SparseHistogramResult by expanding its bin indices to bucket boundaries.

Convert centroids to a sparse histogram with bins centered on each centroid.

Bucket the retained sample into binCount equal-width bins between min and max.