<<

Moment-Based Quantile Sketches for Efficient High Cardinality Aggregation Queries∗

Edward Gan, Jialin Ding, Kai Sheng Tai, Vatsal Sharan, Peter Bailis Stanford InfoLab ABSTRACT Country Interactive analytics increasingly involves querying for quantiles CAN, v7, iOS 6.1 CAN, v7, iOS 6.2 CAN, v7, iOS 6.3 OS over sub-populations of high cardinality datasets. Data process- Mergeable Summaries USA, v7, iOS 6.1 USA, v7, iOS 6.2 USA, v7, iOS 6.3 ing engines such as Druid and Spark use mergeable summaries Aggregation to estimate quantiles, but summary merge times can be a bottle- Query CAN, v8, iOS 6.3 neck during aggregation. We show how a compact and efficiently App Version Merged Aggregate mergeable quantile sketch can support aggregation workloads. This USA, v8, iOS 6.1 USA, v8, iOS 6.2 USA, v8, iOS 6.3 USA, iOS 6.3 data structure, which we refer to as the moments sketch, operates with a small memory footprint (200 bytes) and computationally … efficient (50ns) merges by tracking only a set of summary statis- tics, notably the moments. We demonstrate how we can Figure 1: Given a data cube with pre-aggregated summaries, efficiently estimate quantiles using the method of moments andthe we can compute roll-ups by merging the relevant sum- maximum entropy principle, and show how the use of a cascade maries. Efficiently mergeable summaries enable scalable ag- further improves query time for threshold predicates. Empirical gregations. evaluation shows that the moments sketch can achieve less than 1 percent quantile error with 15× less overhead than comparable similar engines can reduce query times and memory usage by oper- summaries, improving end query time in the MacroBase engine by ating over the relevant summaries directly, effectively constructing up to 7× and the Druid engine by up to 60×. a data cube [33, 65]. Given a time interval and a metric with d associated dimensions, 1 INTRODUCTION Druid maintains one pre-aggregated summary for each d-tuple Performing interactive multi-dimensional analytics over data from of dimension values. These summaries are kept in RAM across a sensors, devices, and servers increasingly requires computing ag- number of nodes, with each node scanning relevant summaries gregate for specific subpopulations and time windows [4, to process subsets of the data specified by a user query. Figure1 29, 65]. In applications such as A/B testing [38, 42], exploratory illustrates how these mergeable [3] summaries can be aggregated data analysis [8, 74], and operations monitoring [2, 14], analysts to compute quantile roll-ups along different dimensions without perform aggregation queries to understand how specific user co- scanning over the raw data. horts, device types, and feature flags are behaving. In particular, More concretely, a Druid-like data cube in our Microsoft deploy- computing quantiles over these subpopulations is an essential part ment with 6 dimension columns, each with 10 distinct values, is of debugging and real-time monitoring workflows [26]. stored as a set of up to 106 summaries per time interval. On this As an example of this quantile-driven analysis, our collabora- cube, computing the 99-th latency for a specific app tors on a Microsoft application monitoring team collect billions of version can require 100,000 merges, or even more for aggregation telemetry events daily from millions of heterogeneous mobile de- across complex time ranges. When there are a limited number of vices. Each device tracks multiple metrics including request latency dimensions but enormous data volumes, it is cheaper to maintain and memory usage, and is associated with dimensional metadata these summaries than scan over billions of raw datapoints. such as application version and hardware model. Engineers issue Many quantile summaries support the merge operation [3, 28, arXiv:1803.01969v2 [cs.DB] 13 Jul 2018 quantile queries on a Druid-like [82] in-memory data store, aggre- 34], but their runtime overheads can lead to severe performance gating across different dimensions to monitor their application (e.g., penalties on high-cardinality datasets. Based on our experiments examine memory trends across device types) and debug regressions (Section 6.2.1), one million 1KB GK-sketches [34] require more (e.g., examine tail latencies across versions). Querying for a single than 3 seconds to merge sequentially, limiting the types of queries percentile in this deployment can require aggregating hundreds of users can ask interactively. The merging can be parallelized, but thousands of dimension value combinations. additional worker threads still incur coordination and resource When users wish to examine the quantiles of specific slices of usage overheads. Materialized views [37, 44, 51, 61], sliding window a dataset, OLAP engines such as Druid and Spark support com- sketches [25], and dyadic intervals can also reduce this overhead. puting approximate quantiles using compressed representations However, dyadic intervals only apply to ordered dimensions and (summaries) of the data values [39, 67, 82, 83]. By pre-aggregating maintaining materialized views for multiple dimension roll-ups can a summary for each combination of dimension values, Druid and be prohibitively expensive in a real-time stream, so merge time remains a relevant bottleneck. ∗A version of this paper has been accepted to VLDB 2018. This document is its associ- In this paper, we enable interactive quantile queries over high- ated technical report. cardinality aggregates by introducing a compact and efficiently 1 mergeable quantile sketch and associated quantile estimation rou- integrated with MacroBase and 60× faster end-to-end queries when tines. We draw a connection between the classic method of moments integrated with Druid. for parameter estimation in statistics [79] and the need for efficient In summary, we make the following contributions: summary data structures. We show that storing the sample mo- • We illustrate how statistical moments are useful as efficient ments µ = 1 Í xi and log-moments ν = 1 Í logi (x) can enable i n i n mergeable quantile sketches in aggregation and threshold- accurate quantile estimation over a of real-world datasets based queries over high-cardinality data. while utilizing fewer than 200 bytes of memory and incurring merge times of less than 50 nanoseconds. In the context of quantile esti- • We demonstrate how statistical and numerical techniques mation, we refer to our proposed summary data structure as the allow us to solve for accurate quantile estimates in less than moments sketch. 1 ms, and show how the use of a cascade further improves While constructing the moments sketch is straightforward, the estimation time on threshold queries by up to 25×. inverse problem of estimating quantiles from the summary is more • We evaluate the use of moments as quantile summaries on a complex. The statistics in a moments sketch provide only loose variety of real-world datasets and show that the moments constraints on the distribution of values in the original dataset: sketch enables 15 to 50× faster query times in isolation, up many distributions might match the moments of a moments sketch to 7× faster queries when integrated with MacroBase and but fail to capture the dataset structure. Therefore, we make use of up to 60× faster queries when integrated with Druid over the principle of maximum entropy [41] to compute a “least-biased” comparably-accurate quantile summaries. quantile estimate for a moments sketch. On continuous real-valued datasets, we find that this approach yields more accurate estimates The remainder of this paper proceeds as follows. In Section2, we than alternative methods, achieving ϵ ≤ 1% error with 200 bytes discuss related work. In Section3, we review relevant background of memory. To achieve this, we also describe a series of practical material. In Section4, we describe the proposed moments sketch. optimizations to standard entropy maximization that allow us to In Section5, we describe a cascade-based approach for efficiently compute quantile estimates in under 1 millisecond on a range of answering threshold-based queries. In Section6, we evaluate the real-world datasets. moments sketch in a series of microbenchmarks. In Section7, we These query times make the moments sketch a suitable sum- evaluate the moments sketch as part of the Druid and MacroBase mary when many merges (hundreds of thousands) are required, systems, and benchmark its performance in a sliding window work- memory per-summary may be limited to less than 1 kilobyte, and flow. We conclude in Section8. We include supplemental appen- ϵ = .01 error is acceptable. The moments sketch and our maxi- dices in an extended technical report [30]. mum entropy estimate is most useful in datasets without strong discretization and when very small < 10−4 error is not required. 2 RELATED WORK The maximum entropy principle is less accurate when there are clusters of discrete values in a dataset (Section 6.2.3), and floating High-performance aggregation. The aggregation scenarios in point stability (Section 4.3.2) limits the minimum achievable error Section1 are found in many existing streaming data systems [ 8, using this approach. 16, 24, 65, 82], as well as data cube [33, 69], data exploration [2], Moving beyond simple quantile queries, many complex queries and visualization [17] systems. In particular, these systems are can depend on the quantile estimates of multiple subpopulations. For perform interactive aggregations over time windows and along example, data exploration systems such as MacroBase [8] are in- many cube dimensions, motivating the design of our sketch. Many terested in finding all subpopulations that match a given threshold of these systems use approximate query processing, sampling, and condition (e.g., subpopulations where the 95th percentile latency summaries to improve query performance [4, 35, 59], but do not de- is greater than the global 99th percentile latency). Given a large velop data structures specific to quantiles. We believe the moments number of subpopulations, the cost of millisecond-level quantile sketch serves as a useful primitive in these engines. estimates on thousands of subgroups will accumulate. Therefore, Sensor networking is a rich source of algorithms for heavily to support threshold queries over multiple populations, we extend resource-constrained settings. Sensor network aggregation systems our quantile estimator with a cascade [75], or sequence of increas- [53] support large scale roll-ups, but work in this area is largely ingly precise and increasingly expensive estimates based on bounds focused on the complementary problem of communication plans such as the Markov inequalities. For queries with threshold condi- over a network [21, 45, 54]. Mean, min, max, and tions, the cascades dramatically reduce the overhead of quantile in particular are used in [53] as functions amenable to computation- estimation in a moments sketch, by up to 25×. constrained environments, but the authors do not consider higher We implement the moments sketch both as a reusable library moments or their application to quantile estimation. and as part of the Druid and MacroBase analytics engines. We Several database systems make use of in empirically compare its accuracy and efficiency with alternative general-purpose analytics. Muthukrishan et al [60] observe that mergeable quantile summaries on a variety of real-world datasets. the moments are a convenient in streaming settings and We find that the moments sketch offers 16 to 50× faster merge use it to fill in missing integers. Data Canopy78 [ ] uses first and times than alternative summaries with comparable accuracy. This second moments as an efficiently mergeable statistic for comput- enables 15 to 50× faster query times on real datasets. Moreover, ing standard deviations and linear regressions. Similarly, systems the moments sketch enables up to 7× faster analytics queries when on probabilistic data cubes such as [81] use the first and second moments to prune queries over cube cells that store distributions

2 of values. In addition, many methods use compressed data repre- An ϵ-approximate ϕ-quantile is an element with rank between sentations to perform statistical analyses such as , (ϕ − ϵ)n and (ϕ + ϵ)n [3]. Given an estimated ϕ-quantile qˆϕ , we can logistic regression, and PCA [19, 63, 70, 80]. We are not aware of also define its quantile error ε [52] as the following: prior work utilizing higher moments to efficiently estimate quan- 1   ε = rank qˆ − ⌊ϕn⌋ , (1) tiles for high-dimensional aggregation queries. n ϕ Quantile summaries. There are a variety of summary data struc- such that an ϵ-approximate quantile has error at most ε. For ex- tures for the ϵ-approximate quantile estimation problem [18, 23, 34, ample, given a dataset D = {1,..., 1000}, an estimate qˆ0.5 = 504 71]. Some of these summaries assume values from a fixed universe for the ϕ = 0.5 quantile would have error ε = 0.003. In this paper, [23, 71], while others operate using only comparisons [3, 34]. Our we consider datasets D represented by collections of real numbers proposed moments sketch and others [12, 28] operate on real val- D ⊂ . ues. Agarwal et al. [3] provide the initial motivation for mergeable Quantile summaries are data structures that provide approximate summaries, as well as a proposed mergeable quantile sketch. The quantile estimates for a dataset given space sub-linear in n. These authors in [52, 77] benchmark a variety of quantile summaries but summaries usually have a parameter kϵ that trades off between do not directly evaluate merge time. Zhuang [84] evaluates merge the size of the summary and the accuracy of its estimates. An ϵ- performance of a variety of quantile summaries in a distributed approximate quantile summary provides ϵ approximate ϕ-quantiles, setting, finding the Random summary to be the fastest. To our knowl- where ϵ can be a function of space usage and the dataset [18, 23, edge we are the first to introduce and evaluate the moments sketch 34, 71]. for fast merge times and low space overhead. Method of moments. The method of moments is a well-established 3.2 Mergeable Summaries statistical technique for estimating the parameters of Agarwal et al. [3] introduce the concept of mergeability to accurately distributions [79]. The main idea behind this approach is that the combine summaries in distributed settings. Formally, for a summary parameters of a distribution of interest P can be related to the ex- with parameter kϵ , we use S(D,kϵ ) to denote a valid summary for pectations of functions of the X ∼ P. As a general a dataset D. For any pair of datasets D1 and D2, the summarization method for consistent statistical parameter estimation, the method routine S is mergeable if there exists an algorithm (i.e., the “merge” of moments is used across a wide range of fields, including econo- procedure) that produces a combined summary metrics [36], physics [32, 57], and machine learning [7, 11, 43]. In S(D ⊎ D ,k ) = merge(S(D ,k ), S(D ,k )) this work, we demonstrate how the method of moments, applied in 1 2 ϵ 1 ϵ 2 ϵ conjunction with practical performance optimizations, can scale to from any two input summaries, where ⊎ denotes multiset addition. support real-world latency-sensitive query processing workloads. Intuitively, a summary is mergeable if there is no accuracy cost to Maximum entropy principle. The maximum entropy principle combining pre-computed summaries compared with constructing a prescribes that one should select the least informative distribution summary on the raw data. Thus, mergeable summaries are algebraic that is consistent with the observed data. In the database commu- aggregate functions in the data cube literature [33]. As an example, nity, this principle has been applied to estimating cardinality [73] an equi-depth histogram [22] on its own is not mergeable because and predicate selectivity [55]. Mead and Papanicolaou [57] apply there is no way to accurately combine two overlapping histogram the maximum entropy principle to the problem of estimating distri- buckets without access to additional data. butions subject to moment constraints; follow-up work proposes Mergeable summaries can be incorporated naturally into a va- the use of Chebyshev polynomials for stability [10, 72] and faster riety of distributed systems. In the MapReduce paradigm, a “map” approximation algorithms [9], though we have not seen any practi- function can construct summaries over shards while a “reduce” cal implementations suitable for use in a database. The maximum function merges them to summarize a complete dataset [3]. In the entropy principle is also used in machine learning, notably in the GLADE system [68], mergeable summaries are an example of a context of maximum entropy models [13]. For example, in natu- Generalized Linear Aggregate (GLA), a user-defined computation ral language processing, maximum entropy models are a popular that can be incrementally aggregated across nodes. choice for tasks such as text classification [62] and sequence tag- ging [48]. 3.3 Query Model As described in Section1, we focus on improving the performance of quantile queries over aggregations on high cardinality datasets. 3 BACKGROUND Given a dataset with d categorical dimensions, we consider data In this section, we review the approximate quantile estimation cubes that maintain summaries for every d-way dimension value problem, mergeable quantile summaries, and our target query cost tuple as one natural setting for high performance aggregations, and model. many other settings are also applicable [78]. In these settings, query time is heavily dependent on the number of merges and the time 3.1 Quantile Queries per merge. We consider two broad classes of queries in this paper. First, sin- Given a dataset D with n elements, for any ϕ ∈ (0, 1), the ϕ-quantile gle quantile queries ask for quantile estimates for a single specified of D is the item x ∈ D with rank r(x) = ⌊ϕn⌋, where the rank of an population. For example, we can query the 99th percentile of latency element x is the number of elements in D smaller than x. over the last two weeks for a given version of the application: 3 2 Algorithm 1: Moments sketch operations min max count x x … ln(x) … input: number of moments k P P P function Init(x) ← ∞ −∞ k Moments k Log Moments xmin, xmax , µì,νì,n ← 0ì, 0ì,n Figure 2: The moments sketch is an array of floating point function Accumulate(x) values. xmin, xmax ← min{x, xmin}, max{x, xmax} n ← n + 1 SELECT percentile(latency, 99) FROM requests for i ∈ {1,...,k} do n−1 1 i WHERE time > date_sub(curdate(), 2 WEEK) µi ← n µi + n x ▷ Standard moments AND app_version = "v8.2" if x > 0 then n−1 1 i νi ← νi + log (x) ▷ Log-moments To process this query in time tquery, we would need to merge nmerge n n function Merge(o) ▷ o another sketch summaries, each with runtime overhead tmerge, and then estimate x ← min{o.x , x } the quantile from the merged summary with runtime cost test. This min min min results in total query time: xmax ← max{o.xmax, xmax} µì,νì,n ←µ ì + o.µì,νì + o.νì,n + o.n tquery = tmerge · nmerge + test. (2) We evaluate the different regimes where queries are bottlenecked on merges and estimation in Figure6 in Section 6.2.2: merge time highest power used in the moments. We refer to k as the order of a n ≥ 4 begins to dominate at around merge 10 . moments sketch. Each sample moment provides additional infor- threshold We also consider queries which are conditioned on sub- mation about the distribution, so higher-order moments sketches groups or windows with above a specified threshold. are more precise but have higher space and computation time over- For example, we may be interested in combinations of application heads. version and hardware platform for which the 99th percentile latency The moments sketch supports a number of basic operations: init exceeds 100ms: creates an empty sketch, accumulate updates the sketch via point- SELECT app_version, hw_model, wise additions, and merge updates the sketch by merging it with PERCENTILE(latency, 99) as p99 another moments sketch. One can construct a moments sketch over FROM requests a dataset using either accumulate or merge. When accumulating GROUPBY app_version, hw_model elements point-wise, we update the minimum and maximum, then HAVING p99 > 100 add to the counts and moments. As an implementation detail, we Í i Í i ( ) Such queries are very useful for debugging and data exploration [8], can accumulate the unscaled sums x and log x instead of the , but have additional runtime cost that depends on the number of µi νi . We merge two moments sketches by combining the minimum, maximum, count, and the moments via comparison and potentially groups ngroups since test can be significant when one is searching for high quantiles over thousands of sub-groups. This results in vectorized addition. This merge operation preserves the property accumulate total query time: that a moments sketch constructed using only is identi- cal (up to floating point precision) to a moments sketch constructed tquery = tmerge · nmerge + test · ngroups. (3) from merging existing sketches of partitions of the data, so there is no accuracy loss in pre-aggregating. We provide pseudocode for 4 THE MOMENTS SKETCH these in Algorithm1. The moments sketch additionally supports In this section, we describe how we perform quantile estimation quantile estimation routines described in Section 4.2 in order to using the moments sketch. First, we review the summary statistics answer end-user queries. The moments sketch thus supports all stored in the moments sketch and describe how they comprise an of the basic user-defined aggregate operations20 [ , 68] and can be efficiently mergeable sketch. Second, we describe how wecanuse incorporated into data systems using this API. the method of moments and the maximum entropy principle to Log-moments. The moments sketch records logarithmic moments estimate quantiles from the moments sketch, with details on how we (log-moments) in order to recover better quantile estimates for resolve practical difficulties with numerical stability and estimation long-tailed datasets. In particular, taking the logarithm of data time. We conclude with a discussion of theoretical guarantees on points is useful when values in the dataset can vary over several the approximation error of quantiles estimated from the sketch. orders of magnitude. In general, when updating a moments sketch in a streaming manner or when maintaining multiple moments 4.1 Moments Sketch Statistics sketches in a distributed setting, we cannot know a priori whether The moments sketch of a dataset D is a set of floating point values: standard moments or log-moments are more appropriate for the the minimum value xmin, the maximum value xmax, the count n, given dataset. Therefore, our default approach is to store both sets of 1 Í i the sample moments µi = n x ∈D x and the sample logarithmic moments up to the same order k. Given additional prior knowledge 1 Í i moments νi = n x ∈D log (x) for i ∈ {1,...,k} (Figure2). The of the data distribution, we may choose to maintain a moments moments sketch has an integer parameter k, which describes the sketch with only a single set of moments. 4 Data points with negative values pose a potential problem for and k2 log-moments stored in the sketch; incorporating more mo- the log-moments since the logarithm is undefined for these points. ments leads to more precise estimates but more computationally There are several strategies for addressing this, including storing expensive estimation. As previously noted, for datasets with non- separate sums for positive and negative values and shifting all positive values (i.e., xmin ≤ 0), we set k2 = 0. Therefore, our goal is values so that they are positive. In this paper, we adopt the simple to find the solution f to the following optimization problem: approach of ignoring the log sums when there are any negative values, and computing estimates using the remaining statistics. maximize H[f ] (4) f ∈F[xmin,xmax] Remark on pathological distributions. The moments of certain ∫ xmax i “pathological” distributions may be undefined; for example, the subject to x f (x)dx = µi , i ∈ {1,...,k1} x −1 2−1 min Cauchy distribution f (x) = π 1 + x does not have finite ∫ xmax i moments of any order. However, the moments sketch tracks the log (x)f (x)dx = νi , i ∈ {1,...,k2} moments of an empirical dataset, which are always well-defined. xmin This suits our goal of estimating quantiles of a finite dataset, rather where F[xmin, xmax] denotes the set of distributions supported on than an underlying distribution. [xmin, xmax]. It is well known that the solution to Problem (4) is a member of 4.2 Estimating Quantiles the class of exponential family distributions [41]:

Method of moments. To estimate quantiles from a moments k k Õ1 Õ2 sketch, we apply the method of moments [7, 11, 43, 79] to con- f (x;θ) = exp ©θ + θ xi + θ logi (x)ª , ­ 0 i k1+i ® struct a distribution f (x) whose moments match those recorded i=1 i=1 in the sketch. Specifically, given a moments sketch with minimum « ¬ xmin and maximum xmax, we solve for a pdf f (x) supported on where θ0 is a normalization constant such that f (x;θ) integrates to [xmin, xmax] with moments equal to the values in the moments 1 over the domain [xmin, xmax]. The maximum entropy distribution sketch. is determined by the parameter θ such that f (x;θ) satisfies the

∫ xmax ∫ xmax moment constraints in Problem (4). i i x f (x)dx = µi log (x)f (x)dx = νi In order to solve for θ, we define the potential function L(θ) xmin xmin from [57]: We can then report the quantiles of f (x) as estimates for the quan- k k tiles of the dataset. ∫ xmin Õ1 Õ2 L(θ) = exp © θ xi + θ logi xªdx (5) In general, a finite set of moments does not uniquely determine ­ i k1+i ® xmin i=0 i=1 a distribution [5]. That is, there are often many possible distribu- « ¬ k k tions with varying quantiles that each match a given set of sample Õ1 Õ2 − − − moments. Therefore, we must disambiguate between them. θ0 θi µi θk1+iνi i=0 i=1 Maximum entropy. In this work, we use the principle of max- imum entropy [41] to select a unique distribution that satisfies L(θ) is a convex function over θ and is constructed so that the min- ( ) the given moment constraints. Intuitively, the differential Shan- imizing solution θopt = arg minθ ∈Rk1+k2−1 L θ is exactly the set non entropy H of a distribution with pdf f (x), defined as H[f ] = of coefficients which satisfy the constraints in Problem (4). Equa- ∫ − X f (x) log f (x)dx, is a measure of the degree of uninformative- tion (5) thus transforms the constrained optimization problem in (4) ness of the distribution. For example, a uniform distribution has into an unconstrained convex optimization problem which we solve higher entropy than a point mass distribution. Thus, the maximum using Newton’s method [15]. We show the explicit formulas for the entropy distribution can be seen as the distribution that encodes the gradient and Hessian that of Equation (5) in AppendixA in [ 30]. least additional information about the data beyond that captured First-order optimization routines such as SGD and BFGS [50] are by the given moment constraints. also viable: they do not use the Hessian but require more steps Applying the maximum entropy principle to the moments sketch, to achieve convergence. As we will describe in Section 4.3, each we estimate quantiles by solving for the pdf f that maximizes the additional entry in our Hessian can be computed efficiently using entropy while matching the moments in the sketch. Following, Chebyshev approximations, making second order methods more ef- we estimate quantiles using numeric integration and the Brent’s ficient overall. We provide a lesion study comparison in Section 6.3. method for root finding [64]. In practice, we find that the use of maximum entropy distribu- 4.3 Practical Implementation tions yields quantile estimates with comparable accuracy to alterna- In this section, we outline implementation concerns that are im- tive methods on a range of real-world datasets, unless the datasets portant for querying the moments sketch in practice. We include a are more discrete than continuous. We discuss our empirical results number of optimizations to improve the stability and performance further in Section 6.2.3. of Newton’s method, and also discuss the stability of the moments Optimization. We now describe how to solve for the maximum sketch under floating point precision. Due to space constraints, entropy distribution f . We trade off between accuracy and estima- some equations are omitted and provided in AppendixA andB tion time by solving for f subject to the first k1 standard moments in [30]. 5 4.3.1 Optimizing Newton’s Method. The primary source of dif- x, then each integral can be evaluated in closed form. The fac- ficulties in implementing Newton’s method is the Hessian ∇2L of tors in the integrand that do not appear as polynomials in x are our objective L. In our case: m˜ i (x),i ∈ {k1 + 1,...,k1 + k2}, which are polynomials in log(x), and the pdf f (x;θ). Therefore, we compute Chebyshev polynomial ∫ xmax 2 ∇ L(θ)ij = mi (x)mj (x)f (x;θ)dx, (6) approximations of these factors and replace each instance in the xmin integrands with its corresponding approximation.1 n where the functions mi (x) range over the set of functions Approximating each of the factors with a degree c polynomial takes O(nc ·lognc ) using a fast cosine transform [64], so computing i i {x : i ∈ {1,...,k1}} ∪ {log (x) : i ∈ {1,...,k2}}. the Hessian can be done in O(k2nc lognc + nck1k2). This is not an asymptotic improvement over naive numeric integration, but the There are two main challenges in performing a Newton step using number of complex function evaluations (i.e. cos(x), ex ) is reduced ∇2L this Hessian. First, can be nearly singular and cause numerical substantially. As we show in our empirical evaluation (Section 6.3), instabilities in Newton’s method that prevent or slow down con- polynomial approximations reduce solve times 20× compared to nu- vergence. Second, since the integral in Eq. (6) has no closed form, merically integrating each entry of the Hessian independently. We O(k2) the cost of performing numerical integrations to compute find in our experiments that the major bottleneck during maximum ∇2L in each iteration can be expensive. entropy optimization is the cosine transform used to construct the Conditioning. To quantify the degree of numerical instability, polynomial approximations. we use the condition number of the Hessian ∇2L. The condition number κ(A) of a matrix A describes how close a matrix is to being 4.3.2 Floating point stability. Numeric floating point stability singular: matrices with high condition number are close to being limits the range of usefulk in a moments sketch. Both our estimation singular, and log κ provides an estimate of how many digits of routine and error bounds (Section 4.4) use moments corresponding 10 [− , ] precision are lost when inverting A. In particular, the use of the to data shifted onto the range 1 1 . On scaled data with range [ − , + ] powers m (x) ∈ {xi : i ∈ {1,...,k }} can result in ill-conditioned c 1 c 1 , this leads to numeric error ϵk in the k-th shifted moment, i 1 k k Hessians [31]. For example, when solving for a maximum entropy bounded by ϵk ≤ 2 (|c| + 1) ϵs where ϵs is the relative error in the raw moments sketch power sums. This shift is the primary distribution with k1 = 8,k2 = 0, xmin = 20, and xmax = 100, we encountered κ(∇2L) ≈ 3 · 1031 at θ = 0, making even the very first source of precision loss. We relate the loss to the error bounds in Newton step unstable. Section 4.4 to show that when using double precision floating point moments up to around k ≤ 13.06 provide numerically We mitigate this issue by using a change of basis from the func- 0.78+log10(|c |+1) j j tions mi (x) = x and mi (x) = log (x) to the basis of Chebyshev useful values. Data centered at 0 (c = 0) have stable higher moments polynomials Ti (x). Chebyshev polynomials are bounded polyno- up to k = 16, and in practice we encounter issues when k ≥ 16. We mials supported on [−1, 1] and are often used for polynomial ap- provide derivations and evaluations of this formula in AppendixB proximation [10, 64]. Using them we define the new basis m˜ i as andC in [30] follows: ( 4.4 Quantile Error Bounds Ti (s1(x)), i ∈ {1,...,k1} m˜ i (x) = Recall that we estimate quantiles by constructing a maximum en- T (s (log(x))), i ∈ {k + 1,...,k + k } i−k1 2 1 1 2 tropy distribution subject to the constraints recorded in a moments sketch. Since the true empirical distribution is in general not equal where s1,s2 are linear scaling functions that map to [−1, 1]. The new j j to the estimated maximum entropy distribution, to what extent basis functions m˜ i (x) can be expressed in terms of x and log (x) using standard formulae for Chebyshev polynomials and the bino- can the quantiles estimated from the sketch deviate from the true quantiles? In this section, we discuss worst-case bounds on the mial expansion [56]. Using this new basis for mi Equation (6), we any found that the condition number for the above example is reduced discrepancy between two distributions which share the same to κ ≈ 11.3, making precision loss during each Newton step less of moments, and relate these to bounds on the quantile estimate errors. a concern. In practice, error on non-adversarial datasets is lower than these On certain datasets, if ill-conditioned matrices are still an issue bounds suggest. We consider distributions supported on [−1, 1]: we can scale and at query time we further limit ourselves to using the first k1 ≤ k shift any distribution with bounded support to match. By Proposi- moments and k2 ≤ k log moments by selecting k1,k2 such that tion 1 in Kong and Valiant [47], any two distributions supported the condition number of the Hessian is less than a threshold κmax. on [−1, 1] with densities f and д and standard moments µf , µд, the Our heuristics select k1,k2 by greedily incrementing k1 and k2 and ( , ) favoring moments which are closer to the moments expected from Wasserstein distance (or Earth Mover’s distance) W1 f д between a uniform distribution. f and д is bounded by:   Efficient Integration. Naïvely computing the Hessian in Equa- 1 k W1(f ,д) ≤ O + 3 ∥µf − µд ∥2 . tion (6) requires evaluating O(k2) numerical integrals per itera- k tion, which can lead to prohibitively slow estimation time. We For univariate distributions f and д, the Wasserstein distance reduce this computational overhead by using polynomial approx- between the distributions is equal to the L1 distance between their imations of the functions appearing in the integrands. If the in- tegrands m˜ i (x)m˜ j (x)f (x;θ) were expressible as polynomials in 1Compare with Clenshaw-Curtis integration [64]. 6 respective cumulative distribution functions F and G (see Theorem Algorithm 2: Threshold Query Cascade 6.0.2 in [6]). Thus: macro CheckBound(rlower, rupper, rt ) ∫ +1 if rlower > rt then W1(f ,д) = |F(x) − G(x)| dx. return true −1 else if r < r then If f is the true dataset distribution, we estimate qˆ by calculating upper t ϕ return false the ϕ-quantile of the maximum entropy distribution fˆ. The Threshold(threshold t, quantile ϕ) error ε(qˆϕ ) is then equal to the gap between the CDFs: ε(qϕ ) = if t > xmax then return false |F(qˆϕ ) − Fˆ(qˆϕ )|. Therefore, the average quantile error over the support [−1, 1] is bounded as follows: if t < xmin then return true +1   ∫ 1 r , r ← MarkovBound(t) ▷ ( ) ≤ k ∥ − ∥ . lower upper Markov Bound ε x dx O + 3 µf µfˆ 2 (7) −1 k CheckBound(rlower, rupper, nϕ) r , r ← RTTBound(t) ▷ RTT Bound Since we can run Newton’s method until the moments µf and µfˆ lower upper k CheckBound(r , rupper, nϕ) match to any desired precision, the 3 ∥µf − µ ˆ ∥2 term is negligible. lower f q ← MaxEntQuantile(ϕ) ▷ Maximum Entropy Equation (7) does not directly apply to the ϵ used in Section6, ϕ avg return q > t which is averaged over ϕ for uniformly spaced ϕ-quantiles rather ϕ than over the support of the distribution. Since ϕ = Fˆ(qˆϕ ), we can relate ϵavg to Eq. (7) using our maximum entropy distribution fˆ: ∫ 1 ∫ +1 ˆ ! ˆ fmax ϵavg = ε(qˆϕ )dϕ = ε(x)f (x)dx ≤ O ϵ for quantile estimates t = qˆ . We refer to this procedure as the 0 −1 k ϕ MarkovBound procedure. where fˆmax is the maximum density of our estimate. Thus, we ex- The authors in [66] provide a procedure (Section 3, Figure 1 in pect the average quantile error ϵavg to have a decreasing upper [66]) for computing tighter but more computationally expensive bound as k increases, with higher potential error when fˆ has re- bounds on the CDF F(t) of a distribution given its moments. We gions of high density relative to its support. Though these bounds refer to this procedure as the RTTBound procedure, and as with are too conservative to be useful in practice, they provide useful the MarkovBound procedure, use it to bound the error of a quantile intuition on how worst case error can vary with k and fˆ (Figure 23). estimate qˆϕ . The RTTBound procedure does not make use of the standard moments and log moments simultaneously, so we run 5 THRESHOLD QUERIES RTTBound once on the standard moments and once on log moments We described in Section 3.3 two types of queries: single quantile and take the tighter of the bounds. queries and threshold queries over multiple groups. The optimiza- tions in Section 4.3 can bring quantile estimation overhead down 5.2 Cascades for Threshold queries ≤ to 1ms, which is sufficient for interactive latencies on single Given a moments sketch, Algorithm2 shows how we calculate quantile queries. In this section we show how we can further re- Threshold(t, ϕ): whether the dataset has quantile estimateqˆϕ above duce quantile estimation overheads on threshold queries. Instead a fixed cutoff t. We use this routine whenever we answer queries of computing the quantile on each sub-group directly, we compute on groups with a predicate qˆϕ > t, allowing us to check whether a sequence of progressively more precise bounds in a cascade [75], a subgroup should be included in the results without computing and only use more expensive estimators when necessary. We first qˆϕ directly. The threshold check routine first performs a simple describe a series of bounds relevant to the moments sketch in Sec- filter on whether the threshold t falls in the range [xmin, xmax]. tion 5.1 and then show how they can be used in end-to-end queries Then, we can use the Markov inequalities MarkovBound to calcu- in Section 5.2. late lower and upper bounds on the rank of the threshold rank(t) in the subpopulation. Similarly the RTTBound routine uses more 5.1 Moment-based inequalities sophisticated inequalities in [66] to obtain tighter bounds on the Given the statistics in a moments sketch, we apply a variety of rank. These bounds are used to determine if we can resolve the classical inequalities to derive bounds on the quantiles. These pro- threshold predicate immediately. If not, we solve for the maximum vide worst-case error guarantees for quantile estimates, and enable entropy distribution as described in Section 4.2( MaxEntQuantile) faster query processing for threshold queries over multiple groups. and calculate qˆϕ . One simple inequality we make use of is Markov’s inequality. The Markov and RTTBound bounds are cheaper to compute Given a non-negative dataset D with moments µi Markov’s in- than our maximum entropy estimate, making threshold predicates  µ  equality tells us that for any value t, rank(t) ≥ n 1 − k where cheaper to evaluate than explicit quantile estimates. The bounds t k the rank is the number of elements in D less than t. We can apply apply to any distribution or dataset that matches the moments Markov’s inequality to moments of transformations of D including in a moments sketch, so this routine has no false negatives and is + − consistent with calculating the maximum entropy quantile estimate T (D) = {x − xmin : x ∈ D}, T (D) = {xmax − x : x ∈ D}, and T l (D) = {log(x) : x ∈ D} to bound rank(t) and thus also the error up front. 7 6 EVALUATION milan hepmass occupancy retail power expon In this section we evaluate the efficiency and accuracy of the mo- size 81M 10.5M 20k 530k 2M 100M ments sketch in a series of microbenchmarks, and then show how min 2.3e−6 −1.961 412.8 1 0.076 1.2e−7 the moments sketch provides end-to-end performance improve- max 7936 4.378 2077 80995 11.12 16.30 ments in the Druid and Macrobase data analytics engines [8, 82]. mean 36.77 0.0163 690.6 10.66 1.092 1.000 This evaluation demonstrates that: stddev 103.5 1.004 311.2 156.8 1.057 0.999 skew 8.585 0.2946 1.654 460.1 1.786 1.994 (1) The moments sketch supports 15 to 50× faster query times than comparably accurate summaries on quantile aggrega- Table 1: Dataset Characteristics. The evaluation datasets tions. cover a range of distribution types.

(2) The moments sketch provides ϵavg ≤ 0.01 estimates across a range of real-world datasets using less than 200 bytes of up to order k, but choose at query time which moments to make storage. use of as described in Section 4.3.2. We quantify the accuracy of a quantile estimate using the quan- (3) Maximum entropy estimation is more accurate than alter- tile error ε as defined in Section 3.1. Then, as in [52, 77] we can native moment-based quantile estimates, and our solver im- compare the accuracies of summaries on a given dataset by com- proves estimation time by 200× over naive solutions. puting their average error ϵavg over a set of uniformly spaced ϕ- (4) Integrating the moments sketch as a user-defined sketch quantiles. In the evaluation that follows, we test on 21 equally provides 7× faster quantile queries than the default quantile spaced ϕ between 0.01 and 0.99. summary in Druid workloads. We evaluate each summary via single-threaded experiments on (5) Cascades can provide 25× higher query throughput com- a machine with an Intel Xeon E5-4657L 2.40GHz processor and 1TB pared to direct moments sketch usage in Macrobase thresh- of RAM, omitting the time to load data from disk. old queries. 6.1.1 Datasets. We make use of six real-valued datasets in our Throughout the evaluations, the moments sketch is able to ac- experiments, whose characteristics are summarized in Table1. The celerate a variety of aggregation-heavy workloads with minimal milan dataset consists of Internet usage measurements from Nov. space overhead. 2013 in the Telecom Italia Call Data Records [40]. The hepmass dataset consists of the first feature in the UCI49 [ ] HEPMASS dataset. 6.1 Experimental Setup The occupancy dataset consists of CO2 measurements from the UCI We implement the moments sketch and its quantile estimation Occupancy Detection dataset. The retail dataset consists of integer routines in Java2. This allows for direct comparisons with the open purchase quantities from the UCI Online Retail dataset. The power source quantile summaries [1, 67] and integration with the Java- dataset consists of Global Active Power measurements from the UCI based Druid [82] and MacroBase [8] systems. In our experimental Individual Household Electric Power Consumption dataset. The results, we use the abbreviation M-Sketch to refer to the moments exponential dataset consists of synthetic values from an exponential sketch. distribution with λ = 1. We compare against a number of alternative quantile summaries: a mergeable equi-width histogram (EW-Hist) using power-of-two 6.2 Performance Benchmarks ranges [65], the ‘GKArray’ (GK) variant of the Greenwald Khanna We begin with a series of microbenchmarks evaluating the moments [34, 52] sketch, the AVL-tree T-Digest (T-Digest)[28] sketch, the sketch query times and accuracy. streaming histogram (S-Hist) in [12] as implemented in Druid, the ‘Random’ (RandomW) sketch from [52, 77], reservoir sampling 6.2.1 Query Time. Our primary metric for evaluating the mo- (Sampling)[76], and the low discrepancy mergeable sketch (Merge12) ments sketch is total query time. We evaluate quantile aggregation from [3], both implemented in the Yahoo! datasketches library [1]. query times by pre-aggregating our datasets into cells of 200 val- The GK sketch is not usually considered mergeable since its size ues and maintaining quantile summaries for each cell. Then we can grow upon merging [3], this is especially dramatic in the pro- measure the time it takes to performing a sequence of merges duction benchmarks in Appendix D.4 in [30]. We do not compare and estimate a quantile. In this performance microbenchmark, the against fixed-universe quantile summaries such as the Q-Digest71 [ ] cells are grouped based on their sequence in the dataset, while or Count-Min sketch [23] since they would discretize continuous the evaluations in Section7 group based on column values. We values. divide the datasets into a large number of cells to simulate produc- Each quantile summary has a size parameter controlling its mem- tion data cubes, while in Appendix D.3 and D.4 in [30] we vary ory usage, which we will vary in our experiments. Our implemen- the cell sizes. Since the moments sketch size and merge time are tations and benchmarks use double precision floating point val- data-independent, the results generalize as we vary cell size. ues. During moments sketch quantile estimation we run Newton’s Figure3 shows the total query time to merge the summaries and method until the moments match to within δ = 10−9, and select then compute a quantile estimate when each summary is instanti- 4 k1,k2 using a maximum condition number κmax = 10 . We con- ated at the smallest size sufficient to achieve ϵavg ≤ .01 accuracy. struct the moments sketch to store both standard and log moments We provide the parameters we used and average observed space usage in Table2. On the long-tailed milan dataset, the S-Hist and 2 https://github.com/stanford-futuredata/msketch EW-Hist summaries are unable to achieve ϵavg ≤ .01 accuracy with 8 milan (406k cells) hepmass (52.5k cells) M-Sketch RandomW T-Digest S-Hist 104 Merge12 GK Sampling EW-Hist 2.85k 2.07k 1.84k 3 824 milan hepmass exponential 10 552 337 268 310 10 2 157 175 102 76.1 90.6 86.9 22.6 10 3 1 10 5.7 Query Time (ms) 4 1.52 10 100 M-SketchMerge12RandomW GK T-DigestSamplingS-Hist EW-Hist M-SketchMerge12RandomW GK T-DigestSamplingS-Hist EW-Hist Merge Time (ms) 10 5 102 103 104 102 103 104 102 103 104 Size (Bytes) Size (Bytes) Size (Bytes)

Figure 3: Total query time using different summaries to esti- Figure 4: Per-merge latencies. The moments sketch provides mate quantiles with ϵavg ≤ .01. The moments sketch enables faster merge times than alternative summaries at the same significantly faster queries at this accuracy. size.

M-Sketch RandomW T-Digest S-Hist Merge12 GK Sampling EW-Hist dataset milan hepmass milan hepmass exponential sketch param size (b) param size (b) 102 M-Sketch k = 10 200 k = 3 72 0 Merge12 k = 32 5920 k = 32 5150 10 RandomW 1 1 ϵ = 40 3200 ϵ = 40 3375 2 1 1 10 GK ϵ = 60 720 ϵ = 40 496 Query Time (ms) T-Digest δ = 5.0 769 δ = 1.5 93 10 4 Sampling 1000 samples 8010 1000 8010 102 103 104 102 103 104 102 103 104 S-Hist 100 bins 1220 100 1220 Size (Bytes) Size (Bytes) Size (Bytes) EW-Hist 100 bins 812 15 132 Figure 5: Quantile Estimation time. Estimation time on the Table 2: Summary size parameters used in Figure3. We use moments sketch is slower than other sketches but under these parameters to compare the query times at ϵavg ≤ .01 3ms for k = 10. accuracy. and we include supplemental parallel experiments in AppendixF less than 100 thousand buckets, so we provide timings at 100 buck- in [30]. The other major contributor to query time is estimation ets for comparison. The moments sketch provides 15 to 50× faster time. In Figure5 we measure the time to estimate quantiles given query times than RandomW, the next fastest accurate summary. As an existing summary. The moments sketch provides on average 2 a baseline, sorting the milan dataset takes 7.0 seconds, selecting an ms estimation times, though estimation time can be higher when exact quantile online takes 880ms, and streaming the data pointwise our estimator chooses higher k1,k2 to achieve better accuracy. This into a RandomW sketch with ϵ = 1/40 takes 220ms. These methods is the cause for the spike at k = 4 in the milan dataset and users do not scale as well as using pre-aggregated moments sketches as can can mitigate this by lowering the condition number threshold dataset density grows but the number of cells remains fixed. κmax. Other summaries support microsecond estimation times. The moments sketch thus offers a tradeoff of better merge time for 6.2.2 Merge and Estimation Time. Recall that for a basic aggre- worse estimation time. If users require faster estimation times, the t = t · n + t . gate quantile query query merge merge est Thus we also cascades in Section 5.2 and the alternative estimators in Section 6.3 t t measure merge and est to quantify the regimes where the moments can assist. We show how the merge time and estimation time trade- sketch performs well. In these experiments, we vary the summary off define regimes where each component dominates depending size parameters, though many summaries have a minimum size, on the number of merges. In Figure6 we measure how the query and the moments sketch runs into numeric stability issues past time changes as we vary the number of summaries (cells of size k ≥ 15 on some datasets (see Section 4.3.2). 200) we aggregate. We use the moments sketch with k = 10 and In Figure4 we evaluate the average time required to merge one compare against the mergeable Merge12 and RandomW summaries of the cell summaries. Larger summaries are more expensive to with parameters from Table2. When n ≥ 104 merge time dom- < merge merge, and the moments sketch has faster ( 50ns) merge times inates and the moments sketch provides better performance than throughout its size range. When comparing summaries using the alternative summaries. However, the moments sketch estimation parameters in Table2, the moments sketch has up to 50× faster times dominate when nmerge ≤ 100. merge times than other summaries with the same accuracy. One can also parallelize the merges by sharding the data and hav- 6.2.3 Accuracy. The moments sketch accuracy is dataset depen- ing separate nodes operate over each partition, generating partial dent, so in this section we compare the average quantile error on summaries to be aggregated into a final result. Since each parallel our evaluation datasets. worker can operate independently, in these settings the moments Figure7 illustrates the average quantile error ϵavg for summaries sketch maintains the same relative performance improvements of different sizes constructed using pointwise accumulation onthe over alternative summaries when we can amortize fixed overheads, complete dataset. The moments sketch achieves ϵ ≤ 10−4 accuracy 9 M-Sketch Merge12 RandomW

g M-Sketch:10 v 0.03 a

milan hepmass exponential Merge12:32 r o

r 0.02 GK:50 r E

RandomW:40 102 n a 0.01 e M 0.00 100 101 102 103 Cardinality (No. of distinct uniform values) Query Time (ms)

2 4 6 2 4 6 2 4 6 10 10 10 10 10 10 10 10 10 Figure 8: Accuracy of maximum entropy estimates on dis- Number of Cells Number of Cells Number of Cells tributions with varying cardinality. The moments sketch is Figure 6: Comparing total query time using different merge- less accurate on discretized datasets, and fails to converge able summaries as we vary the number of merges. The mo- for cardinalities n < 5. ments sketch provides performance improvements when 4 milan retail occupancy nmerge ≥ 10 . g

v With Log a 10 1

No Log r o

M-Sketch RandomW T-Digest S-Hist r r

Merge12 GK Sampling EW-Hist E

n

milan hepmass occupancy a 100 e 10 2 M

10 1

g 5 10 5 10 5 10 v a Number of Moments Number of Moments Number of Moments 2 r 10 o r r E 3 n 10 Figure 9: Accuracy with and without log moments. Given the a e

M same total space budget, log moments improve accuracy on 10 4 the long-tailed milan and retail datasets, and do not affect 5 10 accuracy significantly on other datasets such as occupancy power retail exponential 100 a dataset consisting of uniformly spaced points in the range [−1, 1], 10 1 g v

a eventually failing to converge on datasets with fewer than five

2 r 10 o

r distinct values. If users are expecting to run queries on primarily r E 3 n 10 low-cardinality datasets, fixed-universe sketches or heavy-hitters a e M 10 4 sketches may be more appropriate.

10 5 102 103 104 102 103 104 102 103 104 6.3 Quantile Estimation Lesion Study Size (Bytes) Size (Bytes) Size (Bytes) To evaluate each component of our quantile estimator design, we Figure 7: Average error for summaries of different sizes. The compare the accuracy and estimation time of a variety of alternative moments sketch delivers consistent ϵavg ≤ 0.015 with fewer techniques on the milan and hepmass datasets. We evaluate the than 200 bytes. impact of using log moments, the maximum entropy distribution, and our optimizations to estimation. To examine effectiveness of log moments, we compare our max- on the synthetic exponential dataset, and ϵ ≤ 10−3 accuracy on imum entropy quantile estimator accuracy with and without log the high entropy hepmass dataset. On other datasets it is able to moments. For a fair comparison, we compare the estimates pro- achieve ϵavg ≤ 0.01 with fewer than 200 bytes of space. On the duced from k standard moments and no log moments with those k integer retail dataset we round estimates to the nearest integer. The produced from up to 2 of each. Figure9 illustrates how on some EW-Hist summary, while efficient to merge, provides less accurate long-tailed datasets, notably milan and retail, log moments reduce estimates than the moments sketch, especially in the long-tailed the error from ϵ > .15 to ϵ < .015. On other datasets, log moments milan and retail datasets. do not have a significant impact. We provide further experiments in [30] showing how the mo- We compare our estimator (opt) with a number of other estima- ments sketch worst-case error bounds are comparable to other tors that make use of the same moments. The gaussian estimator summaries (AppendixE), that the moments sketch is robust to fits a Gaussian distribution to the mean and standard deviation. changes in skew and the presence of outliers (AppendixD), and The mnat estimator uses the closed form discrete CDF estimator that the moments sketch generalizes to a production workload (Ap- in [58]. The svd estimator discretizes the domain and uses singu- pendix D.4). However, on datasets with low-entropy, in particular lar value decomposition to solve for a distribution with matching datasets consisting of a small number of discrete point masses, the moments. The cvx-min estimator also discretizes the domain and maximum entropy principle provides poor accuracy. In the worst uses a convex solver to construct a distribution with minimal maxi- case, the maximum entropy solver can fail to converge on datasets mum density and matching moments. The cvx-maxent estimator with too few distinct values. Figure8 illustrates how the error of the discretizes the domain and uses a convex solver to maximize the maximum entropy estimate increases as we lower the cardinality of entropy, as described in Chapter 7 in [15]. The newton estimator 10 milan hepmass 102 99 1 10 5.02 5.88 6.52 3.51 2.69

) 1.73 1.38 1.47 12.1 0 101 % 10 (

0.40 0.40 0.40 3.65

g 0.20 v 0.12 1.7 a 1 10 100 0.02 0.02 0.02

Query Time (s) 0.273 10 2 10 1 301 286 sum M-Sketch@10 S-Hist@10 S-Hist@100 S-Hist@1000 2 83.3 10 34.1 44.8 32.6 40 Quantile Summary ) 8.61 s 1.62 m 0 0.76 (

10

t 0.18 0.18 s

e Figure 11: Druid end-to-end query benchmark. The mo- t 2 10 2e-03 7e-04 7e-041e-03 ments sketch allows for faster query times than the compa-

gaussianmnat svd cvx-mincvx-maxentnewton bfgs opt gaussianmnat svd cvx-mincvx-maxentnewton bfgs opt rable S-Hist summary with 100 bins. Runtime for a native sum operation is a lower bound on query time.

we benchmark a moments sketch with k = 10 against S-Hists Figure 10: Lesion study comparing our optimized maximum with 10, 100, and 1000 centroids. entropy solver to other estimators. Our opt estimator pro- In our experiments, we deploy Druid on a single node – the same vides at least 5× less error than estimators that do not use machine described in section 6.1 – with the same base configuration maximum entropy, and up to 200× faster estimation times used in the default Druid quickstart. In particular, this configuration than naive maximum entropy solvers. dedicates 2 threads to process aggregations. Then, we ingest 26 million entries from the milan dataset at a one hour granularity and implements our estimator without the integration techniques in construct a cube over the grid ID and country dimensions, resulting Sec. 4.3, and uses adaptive Romberg integration instead [64]. The in 10 million cells. bfgs estimator implements maximum entropy optimization using Figure 11 compares the total time to query for a quantile on the first-order L-BFGS50 [ ] method as implemented in a Java port the complete dataset using the different summaries. The moments of liblbfgs [46]. sketch provides 7× lower query times than a S-Hist with 100 bins. Figure 10 illustrates the average quantile error and estimation Furthermore, as discussed in Section 6.2.1, any S-Hist with fewer time for these estimators. We run these experiments with k = 10 than 10 thousand buckets provides worse accuracy on milan data moments. For uniform comparisons with other estimators, on the than the moments sketch. As a best-case baseline, we also show milan dataset we only use the log moments, and on the hepmass the time taken to compute a native sum query on the same data. dataset we only use the standard moments. We perform discretiza- The 1 ms cost of solving for quantile estimates from the moments tions using 1000 uniformly spaced points and make use of the ECOS sketch on this dataset is negligible here. convex solver [27]. Solvers that use the maximum entropy principle provides at least 5× less error than estimators that do not. Further- 7.2 Threshold queries more, our optimizations are able to improve the estimation time by a factor of up to 200× over an implementation using generic In this section we evaluate how the cascades described in Section 5.2 solvers, and provide faster solve times than naive Newton’s method improve performance on threshold predicates. First we show in or BFGS optimizers. As described in Section 4.3, given the com- Section 7.2.1 how the MacroBase analytics engine can use the mo- putations needed to calculate the gradient, one can compute the ments sketch to search for anomalous dimension values. Then, we Hessian relatively cheaply, so our optimized Newton’s method is show in Section 7.2.2 how historical analytics queries can use the faster than BFGS. moments sketch to search and alert on sliding windows. 7.2.1 MacroBase Integration. The MacroBase engine searches 7 APPLYING THE MOMENTS SKETCH for dimension values with unusually high outlier rates in a dataset In this section, we evaluate how the moments sketch affects perfor- [8]. For example, given an overall 2% outlier rate, MacroBase may mance when integrated with other data systems. We examine how report when a specific app version has an outlier rate of 20%.We the moments sketch improves query performance in the Druid ana- integrate the moments sketch with a simplified deployment of Mac- lytics engine, as part of a cascade in the Macrobase feature selection roBase where all values greater than the global 99th percentile t99 engine [8], and as part of exploratory sliding window queries. are considered outliers. We then query MacroBase for all dimension values with outlier rate at least r = 30× greater than the overall 7.1 Druid Integration outlier rate. This is equivalent to finding subpopulations whose To illustrate the utility of the moments sketch in a modern analytics 70th percentile is greater than t99. engine, we integrate the moments sketch with Druid [82]. We do Given a cube with pre-aggregated moments sketches for each this by implementing moments sketch as an user-defined aggre- dimension value combination and no materialized roll-ups, Mac- gation extension, and compare the total query time on quantile roBase merges the moments sketches to calculate the global t99, and queries using the moments sketch with the default S-Hist sum- then runs Algorithm2 on every dimension-value subpopulation, mary used in Druid and introduced in [12]. The authors in [12] searching for subgroups with q.7 > t99. We evaluate the perfor- observe on average 5% error for an S-Hist with 100 centroids, so mance of this query on 80 million rows of the milan internet usage 11 1.0 42.4 6.30 5.26 Estimation Estimation 40 0.8 Merge Merge 0.6 19.6 0.48 20 0.4 9.3 Runtime (s) 6.27 2.69 2.47 Runtime (s) 0.2 0 0.08 0.04 Baseline +Simple +Markov +RTT Merge12a Merge12b 0.0 Baseline +Simple +Markov +RTT Merge12

Figure 12: Runtime of MacroBase queries: the final mo- Figure 14: Sliding window query: moments sketch with cas- ments sketch cascade outperforms queries using alternate cades runs 13× faster than Merge12. sketches.

a b c 108 queries. This is useful when, for instance, users are searching for 14.3M 1.000 1.0 time windows of unusually high CPU usage spikes. 106 494k For this benchmark, we aggregated the 80 million rows of the 67.8k 28.3k 36.5k 0.5 4 milan dataset at a 10-minute granularity, which produced 4320 10 2.65k Fraction hit 0.140 501 259 0.019 0.007 panes that spanned the month of November. We augmented the mi- Throughput (QPS) 0.0 Baseline+Simple+Markov+RTT SimpleMarkov RTT MaxEnt SimpleMarkov RTT MaxEnt lan data with two spikes corresponding to hypothetical anomalies. Each spike spanned a two-hour time frame and contributed 10% more data to those time frames. Given a global 99th percentile of Figure 13: Cascades in MacroBase: (a) as we incrementally around 500 and a maximum value of 8000, we added spikes with add cascade stages, threshold query throughput increases. values x = 2000 and x = 1000 (b) The cascade proceeds from faster to slower estimates. We then queried for the 4-hour time windows whose 99th per- (c) Each stage of the cascade processes a smaller fraction of centile was above a threshold t = 1500. When processing this query queries. using a moments sketch, we can update sliding windows using turnstile semantics, subtracting the values from the oldest pane data from November 2013, pre-aggregated by grid ID, country, and and merging in the new one, and use our cascade to filter windows at a four hour granularity. This resulted in 13 million cube cells, with quantiles above the threshold. each with its own moments sketch. Figure 14 shows the runtime of the sliding window query using Running the MacroBase query produces 19 candidate dimension both the moments sketch and Merge12. Faster moments sketch values. We compare the total time to process this query using direct merge times and the use of turnstile semantics then allow for 13× quantile estimates, our cascades, and the alternative Merge12 quan- faster queries than Merge12. tile sketch. In the first approachMerge12a ( ), we merge summaries during MacroBase execution as we do with a moments sketch. In 8 CONCLUSION the second approach (Merge12b), we calculate the number of values In this paper, we show how to improve the performance of quantile greater than the t99 for each dimension value combination and ac- aggregation queries using statistical moments. Low merge overhead cumulate these counts directly, instead of the sketches. We present allows the moments sketch to outperform comparably accurate ex- this as an optimistic baseline, and is not always a feasible substitute isting summaries when queries aggregate more than 10 thousand for merging summaries. summaries. By making use of the method of moments and the max- Figure 12 shows the query times for these different methods: the imum entropy principle, the moments sketch provides ϵavg ≤ 0.01 baseline method calculates quantile estimates directly, we show the accuracy on real-world datasets, while the use of numeric optimiza- effect of incrementally adding each stage of our cascade ending tions and cascades keep query times at interactive latencies. with +RTTBound. Each successive stage of the cascade improves query time substantially. With the complete cascade, estimation ACKNOWLEDGMENTS time is negligible compared to merge time. Furthermore, the mo- This research was made possible with feedback and assistance ments sketch with cascades has 7.9× lower query times than using from our collaborators at Microsoft including Atul Shenoy, Will the Merge12 sketch, and even 3.7× lower query times than the Mortl, Cristian Grajdeanu, Asvin Ananthanarayan, and John Sheu. Merge12b baseline. This research was supported in part by affiliate members and other In Figure 13 we examine the impact the cascade has on estimation supporters of the Stanford DAWN project – Facebook, Google, Intel, time directly. Each additional cascade stage improves threshold Microsoft, NEC, Teradata, VMware, and SAP – as well as Toyota, query throughput and is more expensive than the last. The complete Keysight Technologies, Hitachi, Northrop Grumman, Amazon Web cascade is over 250× faster than this baseline, and 25× faster than Services, Juniper, NetApp, and the NSF under CAREER grant CNS- just using a simple range check. 1651570 and GRFP grant DGE-114747. 7.2.2 Sliding Window Queries. Threshold predicates are broadly applicable in data exploration queries. In this section, we evaluate REFERENCES how the moments sketch performs on sliding window alerting [1] 2017. Yahoo! Data Sketches Library. https://datasketches.github.io/. 12 [2] Lior Abraham, John Allen, Oleksandr Barykin, Vinayak Borkar, Bhuwan Chopra, [31] Walter Gautschi. 1978. Questions of Numerical Condition Related to Polynomials. Ciprian Gerea, Daniel Merl, Josh Metzler, David Reiss, Subbu Subramanian, In Recent Advances in Numerical Analysis, Carl De Boor and Gene H. Golub (Eds.). Janet L. Wiener, and Okay Zed. 2013. Scuba: Diving into Data at Facebook. VLDB Academic Press, 45 – 72. 6, 11 (2013), 1057–1067. [32] Walton C Gibson. 2014. The method of moments in electromagnetics. CRC press. [3] Pankaj K. Agarwal, Graham Cormode, Zengfeng Huang, Jeff Phillips, Zhewei [33] Jim Gray, Surajit Chaudhuri, Adam Bosworth, Andrew Layman, Don Reichart, Wei, and Ke Yi. 2012. Mergeable Summaries. In PODS. Murali Venkatrao, Frank Pellow, and Hamid Pirahesh. 1997. Data cube: A re- [4] Sameer Agarwal, Barzan Mozafari, Aurojit Panda, Henry Milner, Samuel Madden, lational aggregation operator generalizing group-by, cross-tab, and sub-totals. and Ion Stoica. 2013. BlinkDB: Queries with Bounded Errors and Bounded Data mining and knowledge discovery 1, 1 (1997), 29–53. Response Times on Very Large Data. In EuroSys. 29–42. [34] Michael Greenwald and Sanjeev Khanna. 2001. Space-efficient online computa- [5] N.I. Akhiezer. 1965. The Classical Moment Problem and Some Related Questions in tion of quantile summaries. In SIGMOD, Vol. 30. 58–66. Analysis. Oliver & Boyd. [35] Alex Hall, Alexandru Tudorica, Filip Buruiana, Reimar Hofmann, Silviu-Ionut [6] Luigi Ambrosio, Nicola Gigli, and Giuseppe Savaré. 2008. Gradient flows: in metric Ganceanu, and Thomas Hofmann. 2016. Trading off Accuracy for Speed in spaces and in the space of probability measures. Springer Science & Business PowerDrill. In ICDE. 2121–2132. Media. [36] Lars Peter Hansen. 1982. Large sample properties of generalized method of [7] Animashree Anandkumar, Daniel Hsu, and Sham M Kakade. 2012. A method of moments estimators. Econometrica (1982), 1029–1054. moments for mixture models and hidden Markov models. In COLT. 33–1. [37] Venky Harinarayan, Anand Rajaraman, and Jeffrey D. Ullman. 1996. Implement- [8] Peter Bailis, Edward Gan, Samuel Madden, Deepak Narayanan, Kexin Rong, and ing Data Cubes Efficiently. In SIGMOD. 205–216. Sahaana Suri. 2017. MacroBase: Prioritizing attention in fast data. In SIGMOD. [38] Daniel N. Hill, Houssam Nassif, Yi Liu, Anand Iyer, and S.V.N. Vishwanathan. 541–556. 2017. An Efficient Bandit Algorithm for Realtime Multivariate Optimization. In [9] A. Balestrino, A. Caiti, A. Noe’, and F. Parenti. 2003. Maximum entropy based KDD. 1813–1821. numerical algorithms for approximation of probability density functions. In 2003 [39] Tim Hunter, Hossein Falaki, and Joseph Bradley. 2016. Approximate Algorithms in European Control Conference (ECC). 796–801. Apache Spark: HyperLogLog and Quantiles. https://databricks.com/blog/2016/05/ [10] K. Bandyopadhyay, A. K. Bhattacharya, Parthapratim Biswas, and D. A. Drabold. 19/approximate-algorithms-in-apache-spark-hyperloglog-and-quantiles.html. 2005. Maximum entropy and the problem of moments: A stable algorithm. Phys. [40] Telecom Italia. 2015. Telecommunications - SMS, Call, Internet - MI. https: Rev. E 71 (May 2005), 057701. Issue 5. //doi.org/10.7910/DVN/EGZHFV http://dx.doi.org/10.7910/DVN/EGZHFV. [11] Mikhail Belkin and Kaushik Sinha. 2010. Polynomial Learning of Distribution [41] E. T. Jaynes. 1957. Information Theory and Statistical Mechanics. Phys. Rev. 106 Families. In FOCS. 10. (May 1957), 620–630. Issue 4. [12] Yael Ben-Haim and Elad Tom-Tov. 2010. A streaming parallel decision tree [42] Ramesh Johari, Pete Koomen, Leonid Pekelis, and David Walsh. 2017. Peeking at algorithm. Journal of Machine Learning Research 11, Feb (2010), 849–872. A/B Tests: Why It Matters, and What to Do About It. In KDD. 1517–1525. [13] Adam L Berger, Vincent J Della Pietra, and Stephen A Della Pietra. 1996. A [43] Adam Tauman Kalai, Ankur Moitra, and Gregory Valiant. 2010. Efficiently maximum entropy approach to natural language processing. Computational learning mixtures of two Gaussians. In STOC. 553–562. linguistics 22, 1 (1996), 39–71. [44] N. Kamat, P. Jayachandran, K. Tunga, and A. Nandi. 2014. Distributed and [14] B. Beyer, C. Jones, J. Petoff, and N.R. Murphy. 2016. Site Reliability Engineering: interactive cube exploration. In IDCE. 472–483. How Google Runs Production Systems. O’Reilly Media, Incorporated. [45] David Kempe, Alin Dobra, and Johannes Gehrke. 2003. Gossip-based computation [15] Stephen Boyd and Lieven Vandenberghe. 2004. Convex Optimization. Cambridge of aggregate information. In FOCS. 482–491. University Press, New York, NY, USA. [46] Vinh Khuc. 2017. lbfgs4j. https://github.com/vinhkhuc/lbfgs4j https://github. [16] Lucas Braun, Thomas Etter, Georgios Gasparis, Martin Kaufmann, Donald Koss- com/vinhkhuc/lbfgs4j. mann, Daniel Widmer, Aharon Avitzur, Anthony Iliopoulos, Eliezer Levy, and [47] Weihao Kong and Gregory Valiant. 2017. Spectrum estimation from samples. Ning Liang. 2015. Analytics in Motion: High Performance Event-Processing AND Ann. Statist. 45, 5 (10 2017), 2218–2247. Real-Time Analytics in the Same Database. In SIGMOD. 251–264. [48] John D. Lafferty, Andrew McCallum, and Fernando C. N. Pereira. 2001. Condi- [17] Mihai Budiu, Rebecca Isaacs, Derek Murray, Gordon Plotkin, Paul Barham, Samer tional Random Fields: Probabilistic Models for Segmenting and Labeling Sequence Al-Kiswany, Yazan Boshmaf, Qingzhou Luo, and Alexandr Andoni. 2016. Inter- Data. In ICML. 282–289. acting with Large Distributed Datasets Using Sketch. In Eurographics Symposium [49] M. Lichman. 2013. UCI Machine Learning Repository. http://archive.ics.uci.edu/ on Parallel Graphics and Visualization. ml [18] Chiranjeeb Buragohain and Subhash Suri. 2009. Quantiles on streams. In Ency- [50] Dong C. Liu and Jorge Nocedal. 1989. On the limited memory BFGS method for clopedia of Database Systems. Springer, 2235–2240. large scale optimization. Mathematical Programming 45, 1 (Aug 1989), 503–528. [19] Yixin Chen, Guozhu Dong, Jiawei Han, Jian Pei, Benjamin W Wah, and Jianyong [51] Shaosu Liu, Bin Song, Sriharsha Gangam, Lawrence Lo, and Khaled Elmeleegy. Wang. 2006. Regression cubes with lossless compression and aggregation. TKDE 2016. Kodiak: Leveraging Materialized Views for Very Low-latency Analytics 18, 12 (2006), 1585–1599. over High-dimensional Web-scale Data. VLDB 9, 13 (2016), 1269–1280. [20] Sara Cohen. 2006. User-defined aggregate functions: bridging theory and practice. [52] Ge Luo, Lu Wang, Ke Yi, and Graham Cormode. 2016. Quantiles over data streams: In SIGMOD. 49–60. experimental comparisons, new analyses, and further improvements. VLDB 25, 4 [21] Graham Cormode and Minos Garofalakis. 2007. Streaming in a connected world: (2016), 449–472. querying and tracking distributed data streams. In SIGMOD. 1178–1181. [53] Samuel Madden, Michael J. Franklin, Joseph M. Hellerstein, and Wei Hong. 2002. [22] Graham Cormode, Minos Garofalakis, Peter J Haas, and Chris Jermaine. 2012. TAG: A Tiny AGgregation Service for Ad-hoc Sensor Networks. OSDI. Synopses for massive data: Samples, histograms, wavelets, sketches. Foundations [54] Amit Manjhi, Suman Nath, and Phillip B Gibbons. 2005. Tributaries and deltas: and Trends in Databases 4, 1–3 (2012), 1–294. Efficient and robust aggregation in sensor network streams. In SIGMOD. 287–298. [23] Graham Cormode and S. Muthukrishnan. 2005. An Improved Data Stream [55] Volker Markl, Peter J Haas, Marcel Kutsch, Nimrod Megiddo, Utkarsh Srivastava, Summary: The Count-min Sketch and Its Applications. J. Algorithms 55, 1 (April and Tam Minh Tran. 2007. Consistent selectivity estimation via maximum 2005), 58–75. entropy. VLDB 16, 1 (2007), 55–76. [24] Chuck Cranor, Theodore Johnson, Oliver Spataschek, and Vladislav Shkapenyuk. [56] John C Mason and David C Handscomb. 2002. Chebyshev polynomials. CRC 2003. Gigascope: A Stream Database for Network Applications. In SIGMOD. Press. 647–651. [57] Lawrence R. Mead and N. Papanicolaou. 1984. Maximum entropy in the problem [25] Mayur Datar, Aristides Gionis, Piotr Indyk, and Rajeev Motwani. 2002. Main- of moments. J. Math. Phys. 25, 8 (1984), 2404–2417. taining stream statistics over sliding windows. SIAM journal on computing 31, 6 [58] Robert M. Mnatsakanov. 2008. Hausdorff moment problem: Reconstruction of (2002), 1794–1813. distributions. Statistics & Probability Letters 78, 12 (2008), 1612 – 1618. [26] Jeffrey Dean and Luiz André Barroso. 2013. The Tail atScale. Commun. ACM 56, [59] Dominik Moritz, Danyel Fisher, Bolin Ding, and Chi Wang. 2017. Trust, but 2 (2013), 74–80. Verify: Optimistic Visualizations of Approximate Queries for Exploring Big Data. [27] A. Domahidi, E. Chu, and S. Boyd. 2013. ECOS: An SOCP solver for embedded In CHI. 2904–2915. systems. In European Control Conference (ECC). 3071–3076. [60] S. Muthukrishnan. 2005. Data Streams: Algorithms and Applications. Found. [28] Ted Dunning and Otmar Ertl. 2017. Computing extremeley accurate quantiles Trends Theor. Comput. Sci. 1, 2 (Aug. 2005), 117–236. using t-digests. https://github.com/tdunning/t-digest. [61] Arnab Nandi, Cong Yu, Philip Bohannon, and Raghu Ramakrishnan. 2011. Dis- [29] W. Feng, C. Zhang, W. Zhang, J. Han, J. Wang, C. Aggarwal, and J. Huang. tributed cube materialization on holistic measures. In ICDE. IEEE, 183–194. 2015. STREAMCUBE: Hierarchical spatio-temporal hashtag clustering for event [62] Kamal Nigam. 1999. Using maximum entropy for text classification. In IJCAI exploration over the Twitter stream. In ICDE. 1561–1572. Workshop on Machine Learning for Information Filtering. 61–67. [30] Edward Gan, Jialin Ding, Kai Sheng Tai, Vatsal Sharan, and Peter Bailis. 2018. [63] Carlos Ordonez, Yiqun Zhang, and Wellington Cabrera. 2016. The Gamma matrix Moment-Based Quantile Sketches for Efficient High Cardinality Aggregation Queries. to summarize dense and sparse data sets for big data analytics. TKDE 28, 7 (2016), Technical Report. Stanford University. http://arxiv.org/abs/1803.01969 1905–1918.

13 [64] William H Press. 2007. Numerical recipes 3rd edition: The art of scientific computing. Cambridge university press. [65] Ariel Rabkin, Matvey Arye, Siddhartha Sen, Vivek S. Pai, and Michael J. Freedman. 2014. Aggregation and Degradation in JetStream: Streaming Analytics in the Wide Area. In NSDI. 275–288. [66] Sandor Racz, Arpad Tari, and Miklos Telek. 2006. A moments based distribution bounding method. Mathematical and Computer Modelling 43, 11 (2006), 1367 – 1382. [67] Nelson Ray. 2013. The Art of Approximating Distributions: Histograms and Quantiles at Scale. http://druid.io/blog/2013/09/12/the-art-of-approximating- distributions.html. [68] Florin Rusu and Alin Dobra. 2012. GLADE: A Scalable Framework for Efficient Analytics. SIGOPS Oper. Syst. Rev. 46, 1 (Feb. 2012), 12–18. [69] Sunita Sarawagi. 2000. User-adaptive exploration of multidimensional data. In VLDB. 307–316. [70] Jayavel Shanmugasundaram, Usama Fayyad, and Paul S Bradley. 1999. Com- pressed data cubes for olap aggregate query approximation on continuous di- mensions. In KDD. 223–232. [71] Nisheeth Shrivastava, Chiranjeeb Buragohain, Divyakant Agrawal, and Subhash Suri. 2004. and beyond: new aggregation techniques for sensor networks. In International conference on Embedded networked sensor systems. 239–249. [72] RN Silver and H Röder. 1997. Calculation of densities of states and spectral functions by Chebyshev recursion and maximum entropy. Physical Review E 56, 4 (1997), 4822. [73] U. Srivastava, P. J. Haas, V. Markl, M. Kutsch, and T. M. Tran. 2006. ISOMER: Consistent Histogram Construction Using Query Feedback. In ICDE. 39–39. [74] Manasi Vartak, Sajjadur Rahman, Samuel Madden, Aditya Parameswaran, and Neoklis Polyzotis. 2015. SeeDB: Efficient Data-driven Visualization Recommen- dations to Support Visual Analytics. VLDB 8, 13 (2015), 2182–2193. [75] Paul Viola and Michael Jones. 2001. Rapid object detection using a boosted cascade of simple features. In CVPR, Vol. 1. IEEE, I–511–I–518. [76] Jeffrey S. Vitter. 1985. Random Sampling with a Reservoir. ACM Trans. Math. Softw. 11, 1 (1985), 37–57. [77] Lu Wang, Ge Luo, Ke Yi, and Graham Cormode. 2013. Quantiles over Data Streams: An Experimental Study. In SIGMOD (SIGMOD ’13). 737–748. [78] Abdul Wasay, Xinding Wei, Niv Dayan, and Stratos Idreos. 2017. Data Canopy: Accelerating Exploratory Statistical Analysis. In SIGMOD. 557–572. [79] Larry Wasserman. 2010. All of Statistics: A Concise Course in Statistical Inference. Springer. [80] Ruibin Xi, Nan Lin, and Yixin Chen. 2009. Compression and aggregation for logistic regression analysis in data cubes. TKDE 21, 4 (2009), 479–492. [81] X. Xie, X. Hao, T. B. Pedersen, P. Jin, and J. Chen. 2016. OLAP over probabilistic data cubes I: Aggregating, materializing, and querying. In ICDE. 799–810. [82] Fangjin Yang, Eric Tschetter, Xavier Léauté, Nelson Ray, Gian Merlino, and Deep Ganguli. 2014. Druid: A Real-time Analytical Data Store. In SIGMOD. 157–168. [83] Matei Zaharia, Mosharaf Chowdhury, Tathagata Das, Ankur Dave, Justin Ma, Murphy McCauley, Michael J Franklin, Scott Shenker, and Ion Stoica. 2012. Re- silient distributed datasets: A fault-tolerant abstraction for in-memory cluster computing. In NSDI. 2–2. [84] Zixuan Zhuang. 2015. An Experimental Study of Distributed Quantile Estimation. ArXiv abs/1508.05710 (2015). arXiv:1508.05710 http://arxiv.org/abs/1508.05710

14 A MAXIMUM ENTROPY ESTIMATION A.2 Practical Implementation Details A.1 Newton’s Method Details Chebyshev Polynomial Basis Functions. Recall that we wish to solve the following optimization problem: As described in Section 4.3, we can improve the stability of our optimization problem by using Chebyshev polynomials. To do so, maximize H[f ] we must redefine our mi (x) f ∈F[xmin,xmax] ∫ xmax ( i Ti (s1(x)), i ∈ {1,...,k1} subject to x f (x)dx = µi , i ∈ {1,...,k1} m (x) = i ( ( ( ))) ∈ { } xmin Ti−k1 s2 h x , i k1 + 1,...,k1 + k2 ∫ xmax i log (x)f (x)dx = νi , i ∈ {1,...,k2} where Ti (x) are Chebyshev polynomials of the first kind64 [ ] and x min the s1,s2 are linear scaling functions to map onto [−1, 1] defined as: .  xmax + xmin  xmax − xmin  Throughout this section it is easier to reformulate this problem s1(x) = x − / in more general terms, using the functions 2 2  h(x ) + h(x )  h(x ) − h(x )  s (x) = x − max min / max min . ( i 2 x 0 ≤ i ≤ k1 2 2 mi (x) = (8) h(x)i−k1 k + ≤ i ≤ k + k . 2 1 1 1 2 The formulae for µi , Γ, ∇Γ, ∇ Γ still hold, but now the µi are x (Í where h(x) = log(x) or h(x) = e depending on whether we work 1 Ti (s1(x)) 0 ≤ i ≤ k1 ′ µ = x . (13) using the x or log-transformed x = log(x) as our primary metric. i n Í ( ( ( ))) ≤ ≤ x Ti−ka s2 h x k1 + 1 i k1 + k2. Letting kt = k1 + k2, and folding the νi into a larger µì vector, our Í i optimization problem is then: These can be computed from the quantities µi = x x , νi = Í h(x)i originally stored in the moments sketch by using the bi- [ ] x maximize H f (9) nomial expansion and standard formulae for expressing Chebyshev f ∈F[xmin,xmax] ∫ xmax polynomials in terms of standard monomials [56]. ( ) ( ) ∈ { } subject to mi x f x dx = µi , i 0,...,kt Chebyshev Polynomial Integration. xmin In this section we will show how Chebyshev approximation . provides for efficient ways to compute the gradient and Hessian. Functional analysis [41] tells us that a maximal entropy solution Here it is easier to work with the change of variables u = s (x) so to Eq. (9) has the form: 1 u ( ) ( −1( )) [− ] that f u = f s1 u has domain 1, 1 . First, we will examine k the case when k2 = 0. If we can approximate f (u;θ) as a linear Õt ( ) © ( )ª combination of chebyshev polynomials: f x;θ = exp ­ θimi x ® , i=0 k « ¬ Õt u ( ) © ( )ª Then if we define the potential function L(θ) from [57]: f u;θ = exp ­ θimi u ® (14) i=0 « ¬ k k n ∫ xmin Õt Õt Õc ( ) © ( )ª − ≈ ( ) L θ = exp ­ θimi x ® θi µi (10) cjTj u (15) xmin i=0 i=0 j=0 « ¬ 1 We can calculate the gradient and Hessian of L(θ) as follows: Then using polynomial identities such asTi (x)Tj (x) = 2 (Ti+j (x)+ T|i−j |(x)) we can evaluate the Gradient and Hessian in Eqs. (11), k ∂L ∫ xmin Õt (12) using O(k1nc ) algebraic operations. = mi (x) exp © θimi (x)ª − µi (11) u ( ) ∂θ ­ ® We can approximate f u;θ using the Clenshaw Curtis quad- i xmin i=0 « ¬ rature formulas to approximate a function f supported on [−1, 1] (Eq. 5.9.4 in [64]): k 2 ∫ xmin t ∂ Γ Õ nc −1     ! = mi (x)mj (x) exp © θimi (x)ª (12) 2 f (1) f (−1) Õ πi πi ∂θ ∂θ ­ ® aj = − + f cos cos (16) i j xmin i=0 n 2 2 n n « ¬ c i=1 c c Note that when the gradient given in Eq. (11) is zero then the Then constraints in Eq. (9) are satisfied. Since L(θ) is convex and has nc k 1 Õ domain R d , this means that by solving the unconstrained mini- f (x) ≈ T0(x) + aiTi (x) (17) 2 mization problem over L(θ) we can find a solution θ we can find i=1 a solution to the constrained maximum entropy problem. Since where Eq. (16) can be evaluated in nc lognc time using the Fact Co- Newton’s method is a second order method, we can use Equations sine Transform [64]. The case when k2 > 0 is similar except we need u ( ) ( ( ( −1( ))) u ( ) (10), (11), (12) are to execute Newton’s method with backtracking to approximate not just f u;θ but also Ti s2 h s1 u f u;θ line search [15]. for i ≤ k2. 15 B NUMERIC STABILITY OF MOMENTS 40 empirical As mentioned in Section 4.3.2, floating point arithmetic limits the lower bound usefulness of higher moments. This is because the raw moments 30 1 Í i n x are difficult to optimize over and analyze: both maximum max k 20 entropy estimation (Section 4.3) and theoretical error bounds (Sec- [− ] tion 4.4) apply naturally to moments on data in the range 1, 1 . 10 In particular, shifting the data improves the conditoning of the op- 0 2 4 6 8 10 timization problem dramatically. However, when merging sketches c (offset) from different datasets, users may not know the full range ofthe data ahead of time, so the power sums stored in a moments sketch Figure 15: Highest order usable moments for data centered correspond to data in an arbitrary range [a,b]. Thus, we will ana- at different locations. Our data-independent bound is con- lyze how floating point arithmetic affects the process of scaling and servative compared to values on a uniform dataset. shifting the data so that it is supported in the range [−1, 1]. This is similar to the process of calculating a variance by shifting the data 10 2 so that it is centered at zero. hepmass 5 10 We can calculate the moments of scaling data x = k ·x with s occupancy

scale s

o 8 L 10 error only in the last digit, so we can assume we have data in the n o

s i 11 range [c − 1,c + 1]. Let µ be the moments of the x , and let µ s 10 i scale i i c

e 14 be the moments of the shifted data xshif t = xscale − c. Using the r 10 P binomial expansion 10 17 0 5 10 15 20 1 Õ Number of Moments (k) µs = (x − c)k k n x ∈xscale Figure 16: Precision loss from shifting and converting k   higher moments to chebyshev moments. The occupancy Õ k k−i = µi (−c) dataset exhibits more precision loss because it is centered i i=0 further away from zero.

Using numerically stable addition, we can calculate µk to a relative precision δ close to machine precision δ ≈ 2−53. Then, the absolute s Plugging in double precision for δ into Eq. (19), we know that error δk in estimating µ is bounded by: k ≤ 53 log 2 ≤ ( 2 − ) ≤ k log 6 20, so log k k 2.58 k   Õ k k−i δk ≤ |µi ||c| δ 53 log 2 − 2.58 i k ≤ (20) i=0 log 6 + log (|c| + 1) k Õ k 13.35 δ ≤ (|c| + 1)kδ ≤ (21) k i .78 + log (|c| + 1) i=0 k k ≤ 2 (|c| + 1) δ Equation 21 is a conservative bound on the number of numer- ically stable moments we can extract from an moments sketch, We know that the average quantile error (Equation7 in Section 4.4) and suggests that when our data is centered at 0, we have at least is bounded by 17 stable moments. When the raw data have range [xmin, 3xmin],   then c = 2, and we have at least 10 stable moments. In our eval- 1 k ϵavg ≤ O + 3 ∥µ − µ ∥2 , uations, 10 stable moments are enough to achieve quantile error k f fˆ ≈ .01. Figure 15 describes how the bound in Equation 21 varies   with c, and compares it with the highest order stable moment of so if we can calculate all of the µs to within precision 3−k 1 − 1 i k−1 k a uniform distribution supported on [c − 1,c + 1]. This confirms then we have enough precision to bound the quantile estimate by that our formula is a conservative bound on the true precision loss  1  O k−1 . This way, we can show that the error bound from using due to the shift. If the raw data are centered even further from 0, the first k moments will be at least as tight as the bound from using users can consider pre-shifting all of their data to make better use the first k − 1 moments. As k and |c| grow, achieving this precision of numeric precision. becomes more and more difficult, and we can solve for the cutoff As a measure of the downstream impact of this effect on some of point using base-10 log. our evaluation datasets, Figure 16 shows the precision loss during Chebyshev polynomial calculation ∆µ = |µi − µˆi | where µi is the  1 1  2k (|c| + 1)kδ ≤ 3−k − (18) true Chebyshev moment and µˆi is the value calculated from the k − 1 k moments sketch. Precision loss is more severe on the occupancy 1 dataset which is centered away from zero (c ≈ 1.5) compared with k (log 6 + log (|c| + 1)) ≤ log − log (k2 − k)) (19) δ the hepmass dataset (c ≈ 0.4). See Table1.

16 milan hepmass

1 ks = 0.1 k = 6 10 g

1 v ks = 1.0

10 a k = 10 2 k = 10.0 r 10 s

2 k = 18 o 10 r r

k = 22 E 3 10 n Avg Error 3 10 a e 4 M 10

20 40 60 20 40 60 10 5 Precision (bits per value) Precision (bits per value) 2 4 6 8 10 12 14 M-sketch order Figure 17: Average error for low-precision moments sketches after 100 thousand merges. Twenty bits of preci- Figure 18: Accuracy of estimates on Gamma distributions sion is sufficient to maintain accuracy for both datasets. with varying shape parameter ks . The maximum entropy principle is able to construct an accurate estimate across a C LOW-PRECISION STORAGE range of parameters. In AppendixB we discussed how floating point precision limits the usability of higher moments. Conversely, in settings where space is heavily constrained, the data is well-centered, and we only need a limited number of moments, the moments sketch can be EW-Hist:20 0.20

compressed by reducing the precision of the sketch contents using g EW-Hist:100 v a M-Sketch:10 randomized . 0.15 r

o Merge12:32 r As a proof-of-concept of this approach, we created an encoder r E GK:50 0.10 n that compresses the double precision floating point values in a a RandomW:40 e moments sketch using reduced floating point precision, quantizing M 0.05 the significand and removing unused bits in the exponent. This low- 0.00 precision representation has a negligible impact on merge times 101 102 103 since we can convert them to and from native double precision Outlier Magnitude using simple bit manipulation. Figure 19: Mean error on a Gaussian dataset with outliers of We evaluate the encoding by constructing 100 thousand pre- different magnitudes added. The moments sketch remains aggregated moments sketches, reducing their precision, and then accurate for large outliers, but the EW-Hist accuracy de- merging them and querying for quantiles on the aggregation. Fig- grades. ure 17 illustrates how the quality of the final estimate remains stable as the precision is decreased until we reach a minimum threshold, after which accuracy degrades. On the milan dataset, a moments sketch with k = 10 can be stored with 20 bits per value without noticeably affecting our quantile estimates, representing a 3× space In this specific case, our solver uses a heuristic to decide that given reduction compared to standard double precision floating point. a maximum condition number, optimizing using 3 log moments is These results are consistent with the bounds in SectionB and more valuable than 2 log moments and 2 standard moments. This show how higher moments require more bits of precision. However, choice leads to worse accuracy on a Gamma distribution, but in the bounds are conservative since they only consider the midpoint general it is difficult to know which subset of moments will yield c of a dataset and are otherwise both dataset-independent and the most accurate estimate. More effective heuristics for choos- agnostic to the maximum entropy principle. ing subsets of moments that do not exceed a condition number threshold is an open direction for future research. D ADDITIONAL WORKLOADS The moments sketch accuracy and performance generalizes across a range of workloads. In this section we evaluate its performance on D.2 Outlier Values datasets with varying skew, in the presence of outlier values, under The moments sketch, unlike histogram-based sketches, is also some- a coarser pre-aggregation policy, and on a production workload. what robust to the presence of large outlier values in a dataset. In Figure 19 we evaluate the effect of adding a fixed fraction δ = 0.01 D.1 Data Skew of outlier values from a Gaussian with mean µo and standard devi- Our usage of log-moments greatly reduces the impact of data skew ation σ = 0.1 to a dataset of 10 million standard Gaussian points. on the accuracy of moments sketch quantile estimates. In Figure 18 As we increase the magnitude µo of the outliers, the EW-Hist sum- we vary the shape parameter ks of a Gamma distribution with scale maries with 20 and 100 bins lose accuracy though a moments sketch 2 factor θ = 1. The skew of this distribution is √ so ks = 0.1 corre- with k = 10 remains accurate. The Merge12 sketch is agnostic to ks value magnitudes and is unaffected by the outliers. If extremely sponds to very high skew. For ks = 0.1, 1.0, 10.0, our estimator can −3 large outliers are expected, floating point precision suffers and the achieve ϵavд ≤ 10 error. The accuracy regressions on orders 3 and 7 occur when the solver stops making use of all available mo- moments sketch can be used in conjunction with standard outlier ments to reduce the condition number of the Hessian (Section 4.3). removal techniques. 17 M-Sketch Merge12 GK EW-Hist T-Digest Sampling Data Values Cell Sizes 1.0 milan hepmass exponential gauss

10 2 0.5 CDF

10 3

0.0 4 10 1 3 5 2 4 6 Merge Time (ms) 10 10 10 10 10 10 Value Cell Size 10 5 102 103 104 102 103 104 102 103 104 102 103 104 Size (Bytes) Size (Bytes) Size (Bytes) Size (Bytes) Figure 21: Microsoft data values and cell sizes. Figure 20: Merge times on with sketches on cells of 2000 el- ements, and on a Gaussian dataset with cells of 10000 ele- ments. Since the moments sketch has a fixed size, its per- M-Sketch RandomW T-Digest S-Hist merge times remain faster than alternative sketches with Merge12 GK Sampling EW-Hist comparable accuracy. 10 1

g 1 v 10 10 2 a

r 2 o 10 r

3 r

10 E 3 D.3 Varying Aggregation n 10 4 a 10 e

M 4 In our main evaluations, we group our datasets into cells of 200 Merge Time (ms) 10 10 5 elements and construct sketches for each cell to maintain a pre- 102 103 104 102 103 104 aggregated collection of data summaries. We do not target deploy- Size (bytes) Size (Bytes) ments where very few elements can be pre-aggregated per sum- mary: in these cases merging moments sketches is relatively expen- Figure 22: Merge times and accuracy on the Microsoft sive. On the other hand production data systems can have much dataset. The merge performance of the moments sketch gen- larger data volumes and opportunities to pre-aggregate more ele- eralizes to workloads with variable sized cells, and exhibits ments per cell. Since the moments sketch is fixed-size regardless of an error rate of ϵavд < .01. the data, increasing the number of elements per cell does not affect its merge time performance, while other sketches which which have not reached their maximum capacity will be correspondingly M-Sketch RandomW T-Digest EW-Hist larger and slower to merge. Merge12 GK Sampling In Figure 20 we measure the time taken per merge for different milan hepmass exponential 100 summaries constructed on cells of 2000 elements for the milan, hepmass, and exponential dataset, and cells of 10000 elements on 10 1 a synthetic Gaussian dataset with 1 billion points. The relative performance of different sketches matches closely with Figure4, ex- 10 2 cept that larger Sampling and Merge12 summaries are now slower Error upper bound when constructed on more than 200 elements. 102 103 104 102 103 104 102 103 104 Size (Bytes) Size (Bytes) Size (Bytes)

D.4 Production workload Figure 23: Average bound size for summaries of different In this section we evaluate merge time and accuracy on a produc- sizes. No summary is able to provide ϵbound ≤ .01 guaran- tion workload from Microsoft that contains 165 million rows of tees with less than 1000 bytes. application telemetry data for an integer-valued performance met- ric. We group and pre-aggregate based on four columns that encode information about application version, network type, location, and grows considerably when merging the heterogenous summaries in time, resulting in 400 thousand cells. Notably, these cells do not this workload to preserve its accuracy. correspond to equal sized partitions, but have a minimum size of 5 elements, a maximum size of 722044 elements, and an average size of 2380 elements. Figure 21 illustrates the distribution of integer E ERROR UPPER BOUNDS data values and the cell sizes. Thus far we have evaluated observed accuracy. For comparison, Then, we measure the performance and accuracy of merging the Figure 23 shows the average guaranteed upper bound error provided cells to perform a quantile aggregation query. Figure 22 illustrates by different summaries constructed using pointwise accumulation that on this workload with variable sized cells, the moments sketch on the datasets (no merging). These are in general higher than the still provides faster merge times than comparably accurate sum- observed errors. We use the RTTBound routine in Section 5.1 to maries (c.f. Appendix D.3). The moments sketch achieves ϵavд < .01 bound the moments sketch error. We omit the S-Hist since it does error when we round estimates to the nearest integer on this in- not provide upper bounds. When merging is not a concern, the GK tegral dataset. Since the GK sketch is not strictly mergeable [3], it summary provides the best guaranteed error.

18 M-Sketch RandomW T-Digest S-Hist moments sketch and other summaries can be used in more sophis- Merge12 GK Sampling EW-Hist ticated distributed aggregation plans as well, such as in [17, 68], milan hepmass though since the moments sketch is so compact and cheap to merge, multi-level hierarchical aggregation is only profitable when enor- 5 10 mous numbers of cores are available.

103 Merges per ms

100 101 100 101 Number of Threads Number of Threads

Figure 24: Strong scaling of parallel merging. For fixed num- ber of merges, the throughput of the moments sketch scales with the number of threads available up to 8-way paral- lelism, and remains faster than alternatives. The solid line shows ideal moments sketch scaling.

M-Sketch RandomW T-Digest S-Hist Merge12 GK Sampling EW-Hist milan hepmass

105

103 Merges per ms

100 101 100 101 Number of Threads Number of Threads

Figure 25: Weak scaling of parallel merging. For fixed num- ber of merges per thread, the moments sketch and other summaries scale nearly linearly with parallelism.

F PARALLEL MERGES In Section 6.2.1, we evaluated merge time through single-threaded experiments. We evaluate how well throughput generalizes to par- allel aggregation by sharding pre-computed summaries into equal sized batches, and merging the summaries in each batch on an independent worker thread. After all threads have completed, we combine the merged result from each batch using a sequential merge to obtain a final summary for the complete dataset. In Figure 24 we evaluate strong scalability by measuring the total throughput in merging 400 thousand summaries (constructed from blocks of 200 elements) as we increase the number of threads. In our experiments we needed to duplicate the hepmass dataset to yield 400 thousand summaries, and initialized summaries us- ing the parameters in Table2. The moments sketch remains faster than alternate summaries as we increase the amount of parallelism, though thread overheads and variance in stragglers limits paral- lelism on these datasets past 8 threads when there is less work per thread. In Figure 25 we evaluate weak scalability by performing a similar experiment but increase the dataset size alongside thread count, keeping number of merges per thread constant. Under these conditions, the moments sketch and other summaries achieve even better scalability. These experiments confirm our intuition that since merges can be performed independently, single-threaded performance is in- dicative of parallel performance, and the relative speedup provided by the moments sketch remains stable in parallel settings. The 19