This post is not about Spark, Storm or Samza. It's about a data structure which is one of the relatively new entrants in the domain of stream processing, which is simple to implement, but has already proved to be of immense use in serving a certain class of queries over huge streams of data. I have been doing some readings about the application of such structures and thought of sharing them with the readers of my blog.

# Using Sublinear Space

Besides data processing, these tools also support data mining over streams that include serving specialized queries over data using limited space and time. Ok, so once we store all data as they come we can always serve queries with O(n) space. But since we are talking about huge data streams, it may not even be possible to run algorithms on the full set of data - it simply will be too expensive. Even if we have the entire set of data in a data warehouse, the processing of the entire data set may take time and consume resources that we cannot afford to have, considering the fee charged under the evolving models of using the platform-as-a-service within the cloud based infrastructure. Also the fact that these algorithms will be working on data streams, there's a high likelihood that they will get to see these data only in a single pass. The bottom line is that we need to have algorithms that work on sub-linear space.

Working on sublinear space implies that we don't get to store or see all data - hence an obvious conclusion from this will be the fact that we also don't get to deliver an accurate answer to some queries. We rely on some approximation techniques and deliver an accuracy with a reasonably high probability bound. We don't store all data, instead we store a lossy compressed representation of the data and deliver user queries from this subset instead of the entire set.

One widely used technique for storing a subset of data is through Random Sampling, where the data stored is selected through some stochastic mechanism. There are various ways to determine which data we select for storing and how we build the estimator for querying the data. There are pros and cons with this approach, but it's one of the simplest ways to do approximation based queries on streaming data.

There are a few other options like Histograms and Wavelet based synopses. But one of the most interesting data structures that have been developed in recent times is the

*Sketch*, which uses summary based techniques for delivering approximation queries, gets around the typical problems that sampling techniques have and are highly parallelizable in practice.

An important class of sketch is one where the sketch vector (which is the summary information) is a linear transform of the input vector. So if we model the input as a vector we can multiply it by a sketch matrix to obtain the sketch vector that contains the synopses data that we can use for serving approximation queries. Here's a diagrammatic representation of the sketch as a linear transform of the input data.

# Count-Min Sketch

One of the most popular forms of the sketch data structure is the Count-Min Sketch introduced by Muthukrishnan and Cormode in 2003. The idea is quite simple and the data structure is based on probabilistic algorithms to serve various types of queries on streaming data. The data structure is parameterized by two factors - ε and δ, where the error in answering the query is within a factor of ε with probability δ. So you can tune these parameters based on the space that you can afford and accordingly amortize the accuracy of results that the data structure can serve you.

Consider this situation where you have a stream of data (typically modeled as a vector) like updates to stock quotes in a financial processing system arriving continuously that you need to process and report statistical queries on a real time basis.

`a[1 .. n]`and the updates received at time

`t`are of the form (i

_{t}, c

_{t}) which mean that the stock quote for

`a[i`has been incremented by c

_{t}]_{t}. There are various models in which this update can appear as discussed in Data Streams: Algorithms and Applications by Muthukrishnan which includes negative updates as well and the data structure can be tuned to handle each of these variants.

`count[w, d]`that stores the synopses of the original vector and which is used to report results of queries using approximation techniques. Hence the total space requirement of the data structure is

`(w * d)`. We can bound each of

`w`and

`d`in terms of our parameters ε and δ and control the level of accuracy that we want our data structure to serve.

`{h`that hash each of our inputs to the range

_{1}.. h_{d}}`(1 .. w)`. Just for the more curious mind, pairwise independence is a method to construct a universal hash family, a technique that ensures lower number of collisions in the hash implementation.

_{t}, c

_{t}) comes for the stream, we hash

`a[i`through each of the hash functions

_{t}]`h`and increment each of the

_{1}.. h_{d}`w`entries in the array that they hash to.

for i = 1 to d

v = h(i)(a[i

count[i, v] += c

end

for i = 1 to d

v = h(i)(a[i

_{t}]) // v is between 1 and wcount[i, v] += c

_{t}// increment the cell count by c_{t}end

At any point in time if we want to know the approximate value of an element

`a[i]`of the vector

`a`, we can get it from computing the minimum of all values in each of the

`d`cells of

`count`where

`i`hashes to. This can be proved formally. But the general intuition is that since we are using hash functions there's always a possibility of multiple

`i`'s colliding on to the same cell and contributing additively to the value of the cell. Hence the minimum among all hash values is the closest candidate to give us the correct result for the query.

The figure above shows the processing of the updates in a Count-Min sketch. This is typically called the Point Query that returns an approximation of

`a[i]`. Similarly we can use a Count-Min sketch to get approximation queries for ranges which is typically a summation over multiple point queries. Another interesting application is to serve inner product queries where the data structure is used to query inner products of 2 vectors, a typical application of this being the estimation of join sizes in relational query processing. The paper Statistical Analysis of Sketch Estimators gives all details of how to use sketching as a technique for this.

Count-Min sketches have some great properties which make them a very useful data structure when processing distributed streams. They have associativity properties and can be modelled as monoids and hence terribly performant in a distributed environment where you can parallelize sketch operations. In a future post I will discuss some implementation techniques and how we can use count-min sketches to serve some useful applications over data streams. Meanwhile Twitter's algebird and ClearSpring's stream-lib offer implementations of Count-Min sketch and various other data structures applicable for stream mining applications.

## 6 comments:

Nice introduction! Very clear and intuitive.

Just a short note, in the bullet list you wrote that we hash c_t, but I guess you meant a[i_t], right?

Also, it think about CMS as a kind of generalisation of the LSH for time series, maybe I wrong but still ^^

Hi Andy -

Thanks for the comment.

You are correct .. we need to hash a[i_t] and the hash family maps them to {1, 2, ... w}

Regarding your other observation, yes, LSH also employs hashing and is used for applications like approximate nearest neighbor for finding similarity. But I am not sure if it requires pairwise independent hash functions. It's similar to CMS but not sure if it can be termed a generalization .. In fact I just discovered that there are implementations of lsh where count-min sketch is used to store counts of words mainly exploiting the ability of cms to process distributional similarity ..

Indeed, "generalization" is not the ad-hoc term. Actually, having pair-wise independent should be important for LSH as well otherwise it would imply "unnecessary" work.

The limit would be to have a complete linear system, but we would fallback to something having the same cons as a classical count (thinking out of loud).

This usage of LSH for CMS sounds interesting indeed! On my side, I just felt on this: https://hips.seas.harvard.edu/blog/2013/01/02/hashing-streaming-and-sketching/

Yes .. that's the paper I started from and then went on to read the linked one by Goyal et al .. http://biglearn.org/2012/files/papers/biglearning2012_submission_21.pdf .. which actually has the implementation of lsh in terms of cms ..

Oh! Now I can see the connections ;-)

Hi Debasish Ghosh! I am doing a school project that consists in saving a data stream with count min and then later showing the data with dashboards. I read your article and I have some questions. The examples i've seen after some research are always point queries. Adding an item or making a query only receives a single input(ex: add(bob) or query(bob)). How does it work if i want to save data from a database? How can the attributes relate one another in that case? And can i do a range query without a loop of point queries?

Could you help me?

Thanks in advance!

Post a Comment