public void TestSingleItem() { var quantiles = GenerateTestQuantiles(0.001).ToArray(); var sketch = new DDSketch(); CompareAgainstExactQuantiles(sketch, dataset: new double[] { 1 }, quantiles); }
public void TestUniformlyDistributed( int seed, int size, double range = 5.0, double increment = 0.001) { var quantiles = GenerateTestQuantiles(increment).ToArray(); var rng = new Random(seed); var dataset = Enumerable.Range(0, size).Select(_ => (rng.NextDouble() - 0.5) * range).ToList(); var sketch = new DDSketch(); CompareAgainstExactQuantiles( sketch, dataset, quantiles); }
public void CompareAgainstExactQuantiles( DDSketch sketch, IReadOnlyList <double> dataset, double[] quantiles) { Contract.Requires(sketch.Count == 0); foreach (var element in dataset) { sketch.Insert(element); } // Computation of all of these is done in the same order, so results should be the same. sketch.Count.Should().Be((ulong)dataset.Count); sketch.Sum.Should().Be(dataset.Sum()); sketch.Average.Should().Be(dataset.Average()); sketch.Max.Should().Be(dataset.Max()); sketch.Min.Should().Be(dataset.Min()); var sortedSequence = dataset.ToList(); sortedSequence.Sort(); foreach (var quantile in quantiles) { // NOTE(jubayard): this is floating point code, with relatively sensitive behavior with respect to // operations. Be careful :) var(minExact, maxExact) = ExactQuantile(sortedSequence, quantile); var estimate = sketch.Quantile(quantile); // 1e-12 is the acceptable error due to floating point approximation var minExpected = minExact * (1 - sketch.Alpha) - 1e12; var maxExpected = maxExact * (1 + sketch.Alpha) + 1e12; Assert.True(estimate >= minExpected && estimate <= maxExpected, $"Min=[{minExpected}] Max=[{maxExpected}] Estimate=[{estimate}]"); } }