# Better Differentially Private Approximate Histograms and Heavy Hitters using the Misra-Gries Sketch

Christian Janos Lebeda, Jakub Tetek

► To cite this version:

Christian Janos Lebeda, Jakub Tetek. Better Differentially Private Approximate Histograms and Heavy Hitters using the Misra-Gries Sketch. ACM Transactions on Database Systems, 2025, 50 (3), pp.1-26. 10.1145/3716375. hal-05240815

HAL Id: hal-05240815

https://hal.science/hal-05240815v1

Submitted on 4 Sep 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
# Better Differentially Private Approximate Histograms and Heavy Hitters using the Misra-Gries Sketch

CHRISTIAN JANOS LEBEDA*, Inria, University of Montpellier, France
JAKUB TĚTEK*, INSAIT, Sofia University "St. Kliment Ohridski", Bulgaria

We consider the problem of computing differentially private approximate histograms and heavy hitters in a stream of elements. In the non-private setting, this is often done using the sketch of Misra and Gries [Science of Computer Programming, 1982]. Chan, Li, Shi, and Xu [PETS 2012] describe a differentially private version of the Misra-Gries sketch, but the amount of noise it adds can be large and scales linearly with the size of the sketch; the more accurate the sketch is, the more noise this approach has to add. We present a better mechanism for releasing a Misra-Gries sketch under (ε, δ)-differential privacy. It adds noise with magnitude independent of the size of the sketch; in fact, the maximum error coming from the noise is the same as the best known in the private non-streaming setting, up to a constant factor. Our mechanism is simple and likely to be practical. We also give a simple post-processing step of the Misra-Gries sketch that does not increase the worst-case error guarantee. It is sufficient to add noise to this new sketch with less than twice the magnitude of the non-streaming setting. This improves on the previous result for ε-differential privacy where the noise scales linearly to the size of the sketch. Finally, we consider a general setting where users can contribute multiple distinct elements. We present a new sketch with maximum error matching the Misra-Gries sketch. For many parameters in this setting our sketch can be released with less noise under (ε, δ)-differential privacy.

## PUBLICATION

The conference version of this paper appears in Proceedings of the 42nd ACM SIGMOD-SIGACT-SIGAI Symposium on Principles of Database Systems, PODS 2023 https://doi.org/10.1145/3584372.3588673.

A condensed version of the paper is available in the 2024 SIGMOD Research Highlight Awards.

The full version is accepted for publication in the ACM Transactions on Database Systems https://doi.org/10.1145/3716375.

## 1 INTRODUCTION

Computing the histogram of a dataset is one of the most fundamental tasks in data analysis. At the same time, releasing a histogram may present significant privacy issues. This makes the efficient computation of histograms under privacy constraints a fundamental algorithmic question. Notably, differential privacy has become in recent years the golden standard for privacy, giving formal mathematical privacy guarantees. It would thus be desirable to have an efficient way of (approximately) computing histograms under differential privacy.

Histograms have been investigated thoroughly in the differentially private setting [3, 4, 12, 14, 17, 19, 22]. These algorithms start by computing the histogram exactly and they then add noise to ensure privacy. However, in practice, the amount of data is often so large that computing the histogram exactly would be impractical. This is, for example, the case when computing the histogram of high-volume streams such as when monitoring computer networks, online users, financial markets, and similar. In that case, we need an efficient streaming algorithm. Since the streaming algorithm would only compute the histogram approximately, the above-mentioned approach that first computes the exact histogram is infeasible. In practice, non-private approximate histograms are often computed

<footnote>*This work was carried out when Lebeda was at IT University of Copenhagen and Tětek was at University of Copenhagen. Both authors were at Basic Algorithms Research Copenhagen (BARC), supported by the VILLUM Foundation grant 16582.</footnote>
using the Misra-Gries (MG) sketch [24]. The MG sketch of size $k$ returns at most $k$ items and their approximate frequencies $\hat{f}$ such that $\hat{f}(x) \in [f(x) - n/(k+1), f(x)]$ for all elements $x$ where $f(x)$ is the true frequency and $n$ is the length of the stream. This error is known to be optimal [8]. In this work, we develop a way of releasing a MG sketch in a differentially private way while adding only a small amount of noise. This allows us to efficiently and accurately compute approximate histograms in the streaming setting while not violating users' privacy. This can then be used to solve the heavy hitters problem in a differentially private way. Our result improves upon the work of Chan, Li, Shi, and Xu [11] who also show a way of privately releasing the MG sketch, but who need a greater amount of noise; we discuss this below.

In general, the issue with making approximation algorithms differentially private is that although we may be approximating a function with low global sensitivity, the algorithm itself (or rather the function it implements) may have a much larger global sensitivity. This increases the amount of noise required to achieve privacy using standard techniques. We get around this issue by exploiting the structure of the difference between the MG sketches for neighboring inputs. This allows us to prove that the following simple mechanism ensures $(\varepsilon, \delta)$-differential privacy: (1) We compute the Misra-Gries sketch, (2) we add to each counter independently noise distributed as Laplace$(1/\varepsilon)$, (3) we add to all counters the same value, also distributed as Laplace$(1/\varepsilon)$, (4) we remove all counters smaller than $1 + 2\ln(3/\delta)/\varepsilon$. Specifically, we show that this algorithm satisfies the following guarantees:

**THEOREM 1 (THEOREM 14 SIMPLIFIED).** The above algorithm is $(\varepsilon, \delta)$-differentially private, uses $2k$ words of space, and returns a frequency oracle $\hat{f}$ with maximum error of $n/(k+1) + O(\log(1/\delta)/\varepsilon)$ with high probability for $\delta$ being sufficiently small.

A construction for a differentially private Misra-Gries sketch has been given before by Chan et al. [11]. However, the more accurate they want their sketch to be (and the bigger it is), their approach has to add more noise. The reason is that they directly rely on the global $\ell_1$-sensitivity of the sketch. Specifically, if the sketch has size $k$ (and thus error $n/(k+1)$ on a stream of $n$ elements), its global sensitivity is $k$, and they thus have to add noise of magnitude $k/\varepsilon$. Their mechanism ends up with an error of $O(k \log(d)/\varepsilon)$ for $\varepsilon$-differential privacy with $d$ being the universe size. This can be easily improved to $O(k \log(1/\delta)/\varepsilon)$ for $(\varepsilon, \delta)$-differential privacy with a thresholding technique similar to what we do in step (4) of our algorithm above. This also means that they cannot get more accurate than error $\Theta(\sqrt{n \log(1/\delta)/\varepsilon})$, no matter what value of $k$ one chooses. We achieve that the biggest error, as compared to the values from the MG sketch, among all elements is $O(\log(1/\delta)/\varepsilon)$ assuming $\delta$ is sufficiently small (we show more detailed bounds including the mean squared errors in Theorem 14). This is the same as the best private solution that starts with an exact histogram [22]. In fact, for any mechanism that outputs at most $k$ heavy hitters there exists input with error at least $n/(k+1)$ in the streaming setting [8] and input with error at least $O(\log(\min(d, 1/\delta))/\varepsilon)$ under differential privacy [4]. In Section 6 we discuss how to achieve $\varepsilon$-differential privacy with error $n/(k+1) + O(\log(d)/\varepsilon)$. Therefore the error of our mechanisms is asymptotically optimal for approximate and pure differential privacy, respectively. The techniques used in Section 6 could also be used to get approximate differential privacy, but the resulting sketch would not have strong competitiveness guarantees with respect to the non-private Misra-Gries sketch, unlike the sketch that we give in Section 5.

Chan et al. [11] use their differentially private Misra-Gries sketch as a subroutine for continual observation and combine sketches with an untrusted aggregator. Those settings are not a focus of our work but our algorithm can replace theirs as the subroutine, leading to better results also for those settings. However, the error from noise still increases linearly in the number of merges when
the aggregator is untrusted. As a side note, we show that in the case of a trusted aggregator, the approach of [11] can handle merge operations without increasing error. While that approach adds significantly more noise than ours if we do not merge, it can with this improvement perform better when the number of merges is very large (at least proportional to the sketch size).

Furthermore, in Section 8 we consider the setting where users contribute multiple elements to the stream. The algorithms discussed above can be adjusted for this setting by scaling the noise linearly proportional to the number of elements per user. This is the optimal scaling for $\varepsilon$-differential privacy or when users can contribute many copies of the same element. However, under $(\varepsilon, \delta)$-differential privacy in the non-streaming setting the magnitude of noise scales only with the square root of the number of elements if they are distinct [30]. We give a generalized version of the Misra-Gries sketch with lower sensitivity for this setting. We achieve less noise for many parameters when users contribute multiple distinct elements. The properties of our new sketch is listed below. It remains an open problem to achieve optimal error in this setting under $(\varepsilon, \delta)$-differential privacy.

**THEOREM 2 (LEMMAS 25, 26 AND 27 SUMMARIZED).** The Privacy-Aware Misra Gries (PAMG) sketch and the MG sketch has the same worst-case error guarantees. If each user contributes a set of up to $m$ elements to the stream, then the sensitivity of the MG sketch scales linearly in $m$. As such, any differential private mechanism that outputs a MG sketch must add noise with magnitude $\Omega(m)$. In contrast, the $\ell_2$-sensitivity of the PAMG sketch is $\sqrt{k}$ independent of the value of $m$.

Another approach that can be used is to use a randomized frequency oracle to recover heavy hitters. However, it seems hard to do this with the optimal error size. In its most basic form [18, Appendix D], this approach needs noise of magnitude $\Theta(\log(d)/\varepsilon)$, even if we have a sketch with sensitivity 1 (the approach increases the sensitivity to $\log(d)$, necessitating the higher noise magnitude), leading to maximum error at least $\Omega(\log(k) \log(d)/\varepsilon)$. Bassily, Nissim, Stemmer, and Guha Thakurta [5] show a more involved approach which reduces the maximum error coming from the noise to $\Theta((\log(k) + \log(d))/\varepsilon)$, but at the cost of increasing the error coming from the sketch by a factor of $\log(d)$. This means that even if we had a sketch with error $\Theta(n/k)$ and sensitivity 1, neither of these two approaches would lead to optimal guarantees, unlike the algorithm we give in this paper.

*Relation to Böhler and Kerschbaum [7].* Essentially the same result as Theorem 14 has been claimed in [7]. However, their approach ignores the discrepancy between the global sensitivity of a function we are approximating and that of the function the algorithm actually computes. Their mechanism adds noise scaled to the sensitivity of the exact histogram which is 1 when a user contributes a single element to the stream. But as shown by Chan et al. [11] the sensitivity of the Misra-Gries sketch scales linearly with the number of counters in the sketch. The algorithm from [7] thus does not achieve the claimed privacy parameters. Moreover, it seems unlikely this could be easily fixed – not without doing something along the lines of what we do in this paper.

See https://github.com/JakubTetek/Differentially-Private-Misra-Gries for sample implementations of some of the algorithms we present in this paper.

## 2 TECHNICAL OVERVIEW

*Misra-Gries sketch.* Since our approach depends on the properties of the MG sketch, we describe it here. Readers familiar with the MG sketch may wish to skip this paragraph. We describe the standard version; in Section 5 we use a slight modification, but we do not need that here.

Suppose we receive a sequence of elements from some universe. At any time, we will be storing at most $k$ of these elements. Each stored item has an associated counter, other elements have
implicitly their counter equal to 0. When we process an element, we do one of the following three updates: (1) if the element is being stored, increment its counter by 1, (2) if it is not being stored and the number of stored items is < *k*, store the element and set its counter to 1, (3) otherwise decrement all *k* counters by 1 and remove those that reach 0. The exact guarantees on the output will not be important now, and we will discuss them in Section 5.

*Main contribution.* We now sketch how to release an MG sketch in a differentially private way.

Consider two neighboring data streams $S = (S_1, \cdots, S_n)$ and $S' = (S_1, \cdots, S_{i-1}, S_{i+1}, \cdots, S_n)$ for some $i \in [n]$. At step $i-1$, the state of the MG sketch on both inputs is exactly the same. $MG_S$ then receives the item $S_i$ while $MG_{S'}$ does not. This either increments one of the counters of $MG_S$ (possibly by adding an element and raising its counter from 0 to 1) or decrements all its counters. In $\ell_1$ distance, the vector of the counters thus changes by at most $k$. One can show by induction that this will stay this way: at any point in time, $\|MG_S - MG_{S'}\|_1 \le k$. By a standard global sensitivity argument, one can achieve pure DP by adding noise of magnitude $k/\epsilon$ to each count. This is the approach used in [11]. Similarly, we could achieve $(\epsilon, \delta)$-DP by using the Gaussian mechanism [15] with noise magnitude proportional to the $\ell_2$-sensitivity, which is $\sup_{S,S'} \|MG_S - MG_{S'}\|_2 \le \sqrt{k}$. We want to instead achieve noise with magnitude $O(1/\epsilon)$ at each count. To this end, we need to exploit the structure of $MG_S - MG_{S'}$.

What we just described requires that we add the noise to the counts of all items in the universe, also to those that are not stored in the sketch. This results in the maximum error of all frequencies depending on the universe's size, which we do not want. However, it is known that this can be easily solved under $(\epsilon, \delta)$-differential privacy by only adding noise to the stored items and then removing values smaller than an appropriately chosen threshold [22]. This may introduce additional error – for this reason, we end up with error $O(\log(1/\delta)/\epsilon)$. As this is a somewhat standard technique, we ignore this in this section, we assume that the sketches $MG_S$ and $MG_{S'}$ store the same set of elements; the thresholding allows us to remove this assumption, while allowing us to add noise only to the stored items, at the cost of only getting approximate differential privacy.

We now focus on the structure of $MG_S - MG_{S'}$. After we add to $MG_S$ the element $S_i$, it either holds (1) that $MG_S - MG_{S'}$ is a vector of all 0's and one 1 or (2) that $MG_S - MG_{S'} = -1^k$ (We use $1^k$ to denote the dimension $k$ vector of all ones). We show by induction that this will remain the case as more updates are done to the sketches (note that the remainders of the streams are the same). We do not sketch the proof here, as it is quite technical.

How do we use the structure of $MG_S - MG_{S'}$ to our advantage? We add noise twice. First, we independently add to each counter noise distributed as Laplace$(1/\epsilon)$. Second, we add to all counters the same value, also distributed as Laplace$(1/\epsilon)$. That is, we release $MG_S + \text{Laplace}(1/\epsilon)^{\otimes k} + \text{Laplace}(1/\epsilon)1^k$ (For $D$ being a distribution, we use $D^{\otimes k}$ to denote the $k$-dimensional distribution consisting of $k$ independent copies of $D$). Intuitively speaking, the first noise hides the difference between $S$ and $S'$ in case (1) and the second noise hides the difference in case (2). We now sketch why this is so for worse constants: $2/\epsilon$ in place of $1/\epsilon$. When proving this formally, we use a more technical proof which leads to the better constant.

We now sketch why this is differentially private. Let $m_S$ be the mean of the counters in $MG_S$ for $S$ being an input stream. We may represent $MG_S$ as $(MG_S - m_S 1, m_S)$ (note that there is a bijection between this representation and the original sketch). We now argue that the $\ell_1$-sensitivity of this representation is < 2 (treating the representation as a $(k+1)$-dimensional vector for the sake of computing the $\ell_1$ distances). Consider the first case. In that case, the averages $m_S, m_{S'}$ differ by $1/k$. As such, $MG_S - m_S 1^k$ and $MG_{S'} - m_{S'} 1^k$ differ by $1/k$ at $k-1$ coordinates and by $1-1/k$ at one
coordinate. The overall $\ell_1$ change of the representation is thus

$$ (k-1) \cdot \frac{1}{k} + (1 - 1/k) + 1/k = 2 - 1/k < 2. $$

Consider now the second case when $MG_S - MG_{S'} = -1^k$. Thus, $MG_S - m_S = MG'_S - m_{S'}$. At the same time $|m_S - m_{S'}| = 1$. This means that the $\ell_1$ distance between the representations is 1. Overall, the $\ell_1$-sensitivity of this representation is $< 2$.

This means that adding noise from Laplace$(2/\varepsilon)^{\otimes k+1}$ to this representation of $MG_S$ satisfies $\varepsilon$-DP. The resulting value after adding the noise is $(MG_S - m_S)^{\otimes k} + \text{Laplace}(2/\varepsilon)^{\otimes k}, m_S + \text{Laplace}(2/\varepsilon)$. In the original vector representation of $MG_S$, this corresponds to $MG_S + \text{Laplace}(2/\varepsilon)^{\otimes k} + \text{Laplace}(2/\varepsilon)^{\otimes k}$ and, by post-processing, releasing this value is also differentially private. But this is exactly the value we wanted to show is differentially private!

*Other settings.* Throughout this paper, we consider the problem of privately releasing a MG sketch in several settings. As discussed above, we introduce a novel differentially private mechanism in Section 5 designed specifically for the sensitivity structure of the Misra-Gries sketch. For the settings in Sections 6, 7, and 8, we instead use various other techniques that bounds the sensitivity. We then apply general mechanisms from the differential privacy literature to release a sketch. We briefly discuss our technical approach to each setting below.

In Section 6, we consider the more restrictive definition of pure differential privacy ($\varepsilon$-DP). The mechanism we discussed above does not satisfy $\varepsilon$-DP because it depends on the thresholding technique that hides the difference in stored keys between neighboring sketches with probability at least $1 - \delta$. Instead, we introduce a simple post-processing procedure for a MG sketch. We show that this post-processing adds additional error of at most $n/(k+1)$ and reduces the $\ell_1$-sensitivity from $k$ to 2. This allows us to privately release the post-processed sketch by adding noise of magnitude independent of the sketch size.

In Section 7, we consider the task of merging MG sketches from several streams, which is a common operation in practice. There, we bound the sensitivity of the merging algorithm introduced by [1] by showing that neighboring merged sketches differ by at most 1 in each counter.

Finally, in Section 8, we consider a generalized setting where users contribute a set of up to $m$ elements to the stream. The sensitivity of an MG sketch scales linearly with $m$ in this setting, which is undesirable under $(\varepsilon, \delta)$-DP, since it is sufficient to scale noise only with $\sqrt{m}$ in the non-streaming setting. To address this challenge, we introduce a new sketch inspired by the MG sketch and designed with privacy in mind. The key difference between our sketch and the standard MG sketch is that we decrement counters at most once per user rather than once per element. We show that our sketch has the same worst-case error guarantees as the MG sketch and that neighboring sketches differ by at most 1 in each counter, similar to the result in Section 7. In fact, our sketch can be seen as repeatedly running the merging algorithm of [1]. Our sketch can be released with less noise than the MG sketch for many parameters under $(\varepsilon, \delta)$-DP. However, the error is still not optimal. It remains an open problem whether it is possible to design a sketch with noise of the same magnitude as the non-streaming setting and error guarantees matching the MG sketch.

## 3 PRELIMINARIES

*Setup of this paper.* We use $\mathcal{U}$ to denote a universe of elements. We assume that $\mathcal{U}$ is a totally ordered set of size $d$. That is, $\mathcal{U} = [d]$ where $[d] = \{1, \dots, d\}$. We write $\mathcal{P}(\mathcal{U})$ to denote the power set of $\mathcal{U}$, and $\mathcal{P}(\mathcal{U})^*$ to denote the set of all streams with finite length where each element of the stream is from $\mathcal{P}(\mathcal{U})$. Given a stream $S \in \mathcal{P}(\mathcal{U})^*$ we want to estimate the frequency in $S$ of each element of $\mathcal{U}$. We use $f(x) = \sum_{i=1}^{|S|} 1[x \in S_i]$ for any $x \in \mathcal{U}$ to denote the true frequency of $x$ in the
stream S, where $1[x \in S_i]$ equals 1 if $x \in S_i$ and 0 otherwise. In general, we have that each element in the stream is a set $S_i \subseteq \mathcal{U}$, but all our results expect for Section 8 are for the common setting where $|S_i| = 1$. For simplicity of presentation, in this setting we denote the input as a stream of elements, that is $\mathcal{U}^*$, rather than a stream of sets each containing a single element. Our algorithms output a set $T \subseteq \mathcal{U}$ of keys and a frequency estimate $c_i$ for all $i \in T$. The value $c_j$ is implicitly 0 for any $j \notin T$. Our goal is to minimize the largest error between $c_x$ and $f(x)$ among all $x \in \mathcal{U}$.

**Differential privacy.** Differential privacy is a rigorous definition for describing the privacy loss of a randomized mechanism introduced by Dwork, McSherry, Nissim, and Smith [14]. Intuitively, differential privacy protects privacy by restricting how much the output distribution can change when replacing the input from one individual. This is captured by the definition of neighboring datasets. We use the add-remove neighborhood definition for differential privacy.

**DEFINITION 3 (NEIGHBORING STREAMS).** Let $S$ be a stream of length $n$. Streams $S$ and $S'$ are neighboring denoted $S \sim S'$ if there exists an $i \in [n+1]$ such that $S = (S'_1, \dots, S'_{i-1}, S'_{i+1}, \dots, S'_{n+1})$ or $S' = (S_1, \dots, S_{i-1}, S_{i+1}, \dots, S_n)$.

**DEFINITION 4 (DIFFERENTIAL PRIVACY [15]).** A randomized mechanism $\mathcal{M}: \mathcal{P}(\mathcal{U})^* \to \mathcal{R}$ satisfies $(\varepsilon, \delta)$-differential privacy if and only if for all pairs of neighboring streams $S \sim S'$ and all measurable sets of outputs $Z \subseteq \mathcal{R}$ it holds that

$$ \Pr[\mathcal{M}(S) \in Z] \le e^{\varepsilon} \Pr[\mathcal{M}(S') \in Z] + \delta. $$

Samples from a Laplace distribution are used in many differentially private algorithms, most notably the Laplace mechanism [14]. We write Laplace($b$) to denote a random variable with a Laplace distribution with scale $b$ centered around 0. We sometimes abuse notation and write Laplace($b$) to denote the value of a random variable drawn from the distribution. Our mechanism also works with other noise distributions. We briefly discuss this in Section 5.2.

**DEFINITION 5 (LAPLACE DISTRIBUTION).** The probability density and cumulative distribution functions of the Laplace distribution centered around 0 with scale parameter $b$ are $f_b(x) = \frac{1}{2b}e^{-|x|^b}$, and $\Pr[\text{Laplace}(b) \le x] = \frac{1}{2}e^{x/b}$ if $x < 0$ and $1 - \frac{1}{2}e^{-x/b}$ for $x \ge 0$.

The sensitivity of a deterministic function taking a stream as input restricts the distance between the outputs for neighboring streams. We use the term sensitivity to describe any predicate that holds for the outputs for all pairs of neighboring streams. Two commonly used sensitivities in differential privacy are the $l_1/l_2$-sensitivities. They are special cases of $l_p$-sensitivity which bounds the distance between outputs for neighboring streams in the $l_p$-norm.

**DEFINITION 6 ($l_p$-SENSITIVITY).** Let $g: \mathcal{P}(\mathcal{U})^* \to \mathbb{R}^d$ be a deterministic function mapping a stream to a vector of real values. The $l_p$-sensitivity of $g$ for any $p \ge 1$ is

$$ \Delta_p = \max_{S \sim S'} \|g(S) - g(S')\|_p, $$

where $\|g(S) - g(S')\|_p$ is the $l_p$-distance between $g(S)$ and $g(S')$ defined as

$$ \|g(S) - g(S')\|_p = \left( \sum_{i=1}^{d} |g(S) - g(S')|^p \right)^{1/p}. $$

# 4 RELATED WORK

Chan et al. [11] show that the global $l_1$-sensitivity of a Misra-Gries sketch is $\Delta_1 = k$. (They actually show that the sensitivity is $k+1$ but they use a different definition of neighboring datasets that assumes $n$ is known. Applying their techniques under our definition yields sensitivity $k$.) They
achieve privacy by adding noise with scale $k/\epsilon$ to all elements in the universe and keep the top-$k$ noisy counts. This gives an expected maximum error of $O(k \log(d)/\epsilon)$ with $\epsilon$-DP for $d$ being the universe size. They use the algorithm as a subroutine for continual observation and merge sketches with an untrusted aggregator. Those settings are not a focus of our work but our algorithm can replace theirs as the subroutine.

Böhler and Kerschbaum [7] worked on differentially private heavy hitters with no trusted server by using secure multi-party computation. One of their algorithms adds noise to the counters of a Misra-Gries sketch. They avoid adding noise to all elements in the universe by removing noisy counts below a threshold which adds an error of $O(\log(1/\delta)/\epsilon)$. This is a useful technique for hiding differences in keys between neighboring sketches that removes the dependency on $d$ in the error. Unfortunately, as stated in the introduction their mechanism uses the wrong sensitivity. The $\ell_1$-sensitivity of the sketch is $k$. If the magnitude of noise and the threshold are increased accordingly the error of their approach is $O(k \log(k/\delta)/\epsilon)$.

If we ignore the memory restriction in the streaming setting, the problem is the same as the top-$k$ problem [10, 13, 23, 26]. The problem we solve can also be seen as a generalization of the sparse histogram problem. This has been investigated in [3, 4, 12, 22]. Notably, Balcer and Vadan [4] provides a lower bound showing that for any $(\epsilon, \delta)$-differentially private mechanism that outputs at most $k$ counters, there exists input such that the expected error for some elements is $\Omega(\min(\log(d/k)/\epsilon, \log(1/\delta)/\epsilon, n))$ (assuming $\epsilon^2 > \delta$). The noise that we add in our main contribution in fact matches the second branch of the min over all elements.

A closely related problem is that of implementing frequency oracles in the streaming setting under differential privacy. This has been studied in e.g. [18, 25, 33]. These approaches do not directly return the heavy hitters. The simplest approach for finding the heavy hitters is to iterate over the universe which might be infeasible. However, there are constructions for finding heavy hitters with frequency oracles more efficiently (see Bassily et al. [5]). However, as we discussed in the introduction, the approach of [5] leads to worse maximum error than what we get unless the sketch size is very large and the universe size is small.

The heavy hitters problem has also received a lot of attention in local differential privacy, starting with the paper introducing the RAPPOR mechanism [16] and continuing with [5, 9, 27, 29, 31, 32]. This problem is practically relevant, it is used for example by Apple to find commonly used emojis [2]. The problem has also been recently investigated when using cryptographic primitives [34].

[6, 28] have recently given general frameworks for designing differentially private approximation algorithms; however, if used naively, they are not very efficient for releasing multiple values (not more efficient than using composition) and they are thus not suitable for the heavy hitters problem.

In Section 8 we consider a more general setting where each item in the stream is a set of size at most $m$ instead of a single element. Both [11] and [7] also consider the setting where multiple elements differ between neighboring sketches and they achieve privacy by scaling the noise linearly to $m$. This scaling is typically used in mechanisms based on Laplace noise and is required for $\epsilon$-DP even if we ignore the memory restriction of the streaming setting. However, when the items are distinct we can instead add noise from the normal distribution. The magnitude of noise scales only with the square root of the number of differing counts. We use such a mechanism in Section 8 based on work by Karrer, Kifer, Wilkins, and Zhang [30]. We discuss their results further in Theorem 23.

# 5 DIFFERENTIALLY PRIVATE MISRA-GRIES SKETCH

In this section, we present our algorithm for privately releasing Misra-Gries sketches. We first present our variant of the non-private Misra-Gries sketch in Algorithm 1 and later show how we add noise to achieve $(\epsilon, \delta)$-differential privacy. The algorithm we use differs slightly from most
implementations of MG in that we do not remove elements that have weight 0 until we need to re-use the counter. This will allow us to achieve privacy with slightly lower error.

At all times, $k$ counters are stored as key-value pairs. We initialize the sketch with dummy keys that are not part of $\mathcal{U}$. This guarantees that we never output any elements that are not part of the stream, assuming we remove the dummy counters as post-processing.

The algorithm processes the elements of the stream one at a time. At each step one of three updates is performed: (1) If the next element of the stream is already stored the counter is incremented by 1. (2) If there is no counter for the element and all $k$ counters have a value of at least 1 they are all decremented by 1. (3) Otherwise, one of the elements with a count of zero is replaced by the new element.

In case (3) we always remove the smallest element with a count of zero. This allows us to limit the number of keys that differ between sketches for neighboring streams as shown in Lemma 8. The choice of removing the minimum element is arbitrary but the order of removal must be independent of the stream so that it is consistent between neighboring datasets. The limit on differing keys allows us to obtain a slightly lower error for our private mechanism. However, it is still possible to apply our mechanism with standard implementations of MG. We discuss this in Section 5.1.

<table><caption>**Algorithm 1: Misra-Gries (MG)**</caption><thead><tr><th colspan="2">Input: Positive integer k and stream S ∈ U*</th></tr></thead><tbody><tr><td>1</td><td>T ← {d + 1, ..., d + k}</td><td>► Start with k dummy counters</td></tr><tr><td>2</td><td>c<sub>i</sub> ← 0 for all i ∈ T</td><td></td></tr><tr><td>3</td><td>foreach x ∈ S do</td><td></td></tr><tr><td>4</td><td>&nbsp;&nbsp;&nbsp;&nbsp;if x ∈ T then</td><td>► Branch 1 - increment count</td></tr><tr><td>5</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c<sub>x</sub> ← c<sub>x</sub> + 1</td><td></td></tr><tr><td>6</td><td>&nbsp;&nbsp;&nbsp;&nbsp;else if c<sub>i</sub> ≥ 1 for all i ∈ T then</td><td>► Branch 2 - decrement all counters</td></tr><tr><td>7</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c<sub>i</sub> ← c<sub>i</sub> - 1 for all i ∈ T</td><td></td></tr><tr><td>8</td><td>&nbsp;&nbsp;&nbsp;&nbsp;else</td><td>► Branch 3 - replace zero counter</td></tr><tr><td>9</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Let y ∈ T be the smallest key satisfying c<sub>y</sub> = 0</td><td></td></tr><tr><td>10</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T ← (T ∪ {x}) \ {y}</td><td></td></tr><tr><td>11</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c<sub>x</sub> ← 1</td><td></td></tr><tr><td>12</td><td>return T, c</td><td></td></tr></tbody></table>

The same guarantees about correctness hold for our version of the MG sketch, as for the original version. This can be easily shown, as the original version only differs in that it immediately removes any key whose counter is zero. Since the counters for items not in the sketch are implicitly zero, one can see by induction that the estimated frequencies by our version are exactly the same as those in the original version. We still need this modified version, as the set of keys it stores is different from the original version, which we use below. The fact that the returned estimates are the same however allows us to use the following fact

**FACT 7 (BOSE, KRANAKIS, MORIN, AND TANG [8]).** Let $\hat{f}(x)$ be the frequency estimates given by an MG sketch of size $k$ for $n$ being the input size. Then it holds that $\hat{f}(x) \in [f(x) - n/(k+1), f(x)]$ for all $x \in \mathcal{U}$, where $f(x)$ is the true frequency of $x$ in $S$.

Note that this is optimal for any mechanism that returns a set of at most $k$ elements. This is easy to see for an input stream that contains $k + 1$ distinct elements each with frequency $n/(k+1)$ since at least one element must be removed.
We now analyze the value of $MG_S - MG_{S'}$ for $S, S'$ being neighboring inputs (recall Definition 3). We will then use this in order to prove privacy. As mentioned in Section 4, Chan et al. [11] showed that the $l_1$-sensitivity for Misra-Gries sketches is $k$. They show that this holds after processing the element that differs for neighboring streams and use induction to show that it holds for the remaining stream. Our analysis follows a similar structure. We expand on their result by showing that the sets of stored elements for neighboring inputs differ by at most two elements when using our variant of Misra-Gries. We then show how all this can be used to get differential privacy with only a small amount of noise.

**LEMMA 8.** Let $T, c \leftarrow MG(k, S)$ and $T', c' \leftarrow MG(k, S')$ be the outputs of Algorithm 1 on a pair of neighboring streams $S \sim S'$ such that $S'$ is obtained by removing an element from $S$. Then $|T \cap T'| \ge k-2$ and all counters not in the intersection have a value of at most 1. Moreover, it holds that either (1) $c_i = c'_i - 1$ for all $i \in T'$ and $c_j = 0$ for all $j \notin T'$ or (2) there exists an $i \in T$ such that $c_i = c'_i + 1$ and $c_j = c'_j$ for all $j \ne i$.

**PROOF.** Let $S \sim S'$ be a pair of neighboring streams where $S'$ is obtained by removing one element from $S$. We show inductively that the Lemma holds for any such $S$ and $S'$. Let $w = T - T'$ and $w' = T' - T$ denote the set of keys that are only in one sketch. Let $c_0$ and $c'_0$ denote the smallest element with a zero count in the respective sketch when such an element exists. Then at any point during execution after processing the element removed from $S$ the sketches are in one of the following states:

(S1) $T = T'$ and $c_i = c'_i - 1$ for all $i \in T$.

(S2) There exist $x_1, x_2 \in \mathcal{U}$ such that $w = \{x_1\}$ and $w' = \{x_2\}$, $c_{x_1} = 0$, $c'_{x_2} = 1$ and $c_i = c'_i - 1$ for all $i \in T \cap T'$.

(S3) $T = T'$ and there exists $x_1 \in \mathcal{U}$ such that $c_{x_1} = c'_{x_1} + 1$ and $c_i = c'_i$ for all $i \in T \setminus \{x_1\}$.

(S4) There exist $x_1, x_2 \in \mathcal{U}$ such that $w = \{x_1\}$ and $w' = \{x_2\}$, $c_{x_1} = 1$, $c'_{x_2} = 0$ and $c_i = c'_i$ for all $i \in T \cap T'$.

(S5) There exist $x_1, x_2, x_3 \in \mathcal{U}$ such that $c_{x_1} = c'_{x_1} + 1$, $w = \{x_2\}$, $w' = \{x_3\}$, $c_{x_2} = 0$, $c'_{x_3} = 0$ and $c_i = c'_i$ for all $i \in T \cap T' \setminus \{x_1\}$.

(S6) There exist $x_1, x_2, x_3, x_4 \in \mathcal{U}$ such that $w = \{x_1, x_2\}$ and $w' = \{x_3, x_4\}$, $c_{x_1} = 1$, $c_{x_2} = c'_{x_3} = c'_{x_4} = 0$, $c_i = c'_i$ for all $i \in T \cap T'$ and $x_4 = c'_0$.

Let $x = S_i$ be the element in stream $S$ which is not in stream $S'$. Since the streams are identical in the first $i-1$ steps the sketches are clearly the same before step $i$. If there is a counter for $x$ in the sketch we execute Branch 1 and the result corresponds to state S3. If there is no counter for $x$ and no zero counters we execute Branch 2 and the result corresponds to state S1. Otherwise, the 3rd branch of the algorithm is executed and $c_0$ is replaced by $x$ which corresponds to state S4. Therefore we must be in one of the states S1, S3, or S4 for $T, c \leftarrow MG(k, (S_1, \dots, S_i))$ and $T', c' \leftarrow MG(k, (S'_1, \dots, S'_{i-1}))$.

We can then prove inductively that the Lemma holds since the streams are identical for the elements $(S_{i+1}, \dots, S_n)$. We have to consider the possibility of each of the branches being executed for both sketches. The simplest case is when the element has a counter in both sketches and Branch 1 is executed on both inputs. This might happen in all states and we stay in the same state after processing the element. But many other cases lead to new states.

Below we systematically consider all outcomes of processing an element $x \in \mathcal{U}$ when the sketches start in each of the above states. When processing each element, one of the three branches is executed for each sketch. This gives us up to 9 combinations to check, although some are impossible for certain states. Furthermore, when Branch 3 is executed we often have to consider which element is replaced as it leads to different states. We refer to $T, c$ and $T', c'$ as sketch 1 and sketch 2, respectively.
**State S1:** If $x \in T$ then $x \in T'$ and Branch 1 is executed for both sketches. Similarly, if Branch 2 is executed for sketch 1 it must also be executed for sketch 2 as all counters are strictly larger. Therefore we stay in state S1 in both cases. It is impossible to execute Branch 3 for sketch 2 since all counters are non-zero by definition. As such the final case to consider is when $x \notin T$ and there is a counter with value 0 in sketch 1. In this case, we execute Branch 3 for sketch 1 and Branch 2 for sketch 2. This results in state S4.

**State S2:** If $x \in T$ we execute Branch 1 on sketch 1 and there are two possible outcomes. If $x \neq x_1$ we also execute Branch 1 on sketch 2 and remain in state S2. If $x = x_1$ we execute Branch 2 on sketch 2 in which case there are no changes to $T$ or $T'$ but now $c_x = 1$ and $c_i = c_i'$ for all $i \in T \cap T'$. As such, we transitioned to state S4.

Since $c_{x_1} = 0$ by definition Branch 2 is never executed on sketch 1 and Branch 3 is never executed on sketch 2 as all counters are non-zero. If $x = x_2$ Branch 3 is executed on sketch 1 and Branch 1 is executed for sketch 2. If $c_0 = x_1$ the sketches have the same keys after processing $x$ and transition to state S1, otherwise if $c_0 \neq x_1$ the sketches still differ for one key and remain in state S2.

Finally, if Branch 3 is executed on sketch 1 and Branch 2 is executed on sketch 2 we again have two possibilities. In both cases, the sketches store the same count on all elements from $T \cap T'$ after processing $x$. If $c_0 = x_1$ it is removed from $T$ and replaced by $x$ with $c_x = 1$ which corresponds to state S4. If $c_0 \neq x_1$ we must have that $c_0 \in T \cap T'$. The two sketches now differ on exactly two keys after processing $x$. One of the two keys stored in sketch 2 that are not in sketch 1 must be the minimum zero key since the elements $c_0$ and $x_2$ now have counts of zero in sketch 2 and $c_0$ was the minimum zero key in $T \cap T'$. Therefore we transition to state S6.

**State S3:** The simplest case is $x \in T$ since then $x \in T'$ and Branch 1 is executed for both sketches. If Branch 2 is executed for sketch 1 and $c'_{x_1} \neq 0$ Branch 2 is also executed for sketch 2. For both cases, we remain in state S3. Instead, if $c'_{x_1} = 0$ Branch 3 is executed for sketch 2. Since all counters are decremented for sketch 1 and $x_1$ is replaced in sketch 2 we transition to state S2. Lastly, if Branch 3 is executed for sketch 1 it is also executed for sketch 2 and there are two cases. If the same element is removed we remain in state S3. Otherwise, if $x_1$ is replaced in sketch 2 we transition to state S4.

**State S4:** Sketch 2 contains a counter with a value of zero in states S4, S5, and S6. Therefore Branch 2 is never executed on sketch 2 in the rest of the proof. If Branch 1 is executed for both sketches we stay in the same state as always but if $x = x_1$ Branch 1 is executed for sketch 1 and Branch 3 is executed for sketch 2. If $c'_0 = x_2$ then $T = T'$ after processing $x$ and we transition to state S3. If $c'_0 \neq x_2$ another element is removed from sketch 2 which must also have a count of zero in sketch 1 and we go to state S5.

If Branch 2 is executed on sketch 1 we know that $c'_{x_2}$ must be the only zero counter in sketch 2. Therefore it does not matter if Branch 1 or 3 is executed on sketch 2. For both cases, we set $c_x = 1$ and the sketches differ in one key which corresponds to state S2.

Finally, if Branch 3 is executed on sketch 1 we again have two cases that lead to the same state. If $x = x_2$ or $c'_0 = x_2$ the counter $c'_{x_2}$ is updated or replaced but the counter that was removed from sketch 1 still remains in sketch 2. Otherwise, we have $c_0 = c'_0$ and we replace the same counter in sketches 1 and 2. Therefore we remain in state S4 in both cases.

**State S5:** Since by definition both sketches contain counters with a value of zero, Branch 2 is never executed while in this state. If $x \in T \cap T'$ we remain in the same state as always. We have to consider the cases where $x = x_2$, $x = x_3$, and $x \notin T \cup T'$. The resulting state depends on the elements that are replaced in the sketch. For $x = x_2$ we transition to state S3 if $c'_0 = x_3$ and remain in state S5 otherwise. The same argument shows that we transition to state S3 or S5 based on $c_0$ if $x = x_3$. When $x \notin T \cup T'$ we execute Branch 3 on both sketches. We transition to state S3 only if
$c_0 = x_2$ and $c'_0 = x_3$ since otherwise both sketches still have a zero counter that is not stored in the other sketch and we stay in state S5.

**State S6:** Similar to state S5, Branch 2 is never executed from this state. Here we have to consider the five cases where $x \in T \cap T'$, $x = x_1$, $x = x_2$, $x \in w'$, and $x \notin T \cup T'$. We know that $x_4$ is replaced whenever $x \notin T'$. If $x \in T \cap T'$ we execute Branch 1 on both sketches and remain in state S6. If $x = x_1$ we transition to state S5 and for $x = x_2$ we transition to state S4. When $x \in w'$ there are two possibilities. We always have $c_x = c'_x$ after updating. If $c_0 = x_2$ the sketches will share $k-1$ keys and transition to state S4. If $c_0 \neq x_2$ then another element that has a count of zero in both sketches is replaced in sketch 1. We know that either this element or the remaining zero-valued element of $w'$ must be the smallest zero-valued element in sketch 2. Therefore we remain in state S6.

The final case to consider is when $x \notin T \cup T'$. In this case Branch 3 is executed for sketch 2 and $x_4$ is replaced with $x$ in $T'$. If $c_0 = x_2$ we transition to state S4. Otherwise, either $x_3$ or the element that was replaced from sketch 1 must be the minimum element with a count of zero in sketch 2. As such, we remain in state S6. □

Next, we consider how to add noise to release the Misra-Gries sketch under differential privacy. Recall that Chan et al. [11] achieves privacy by adding noise to each counter which scales with $k$. We avoid this by utilizing the structure of sketches for neighboring streams shown in Lemma 8. We sample noise from Laplace$(1/\epsilon)$ independently for each counter, but we also sample one more random variable from the same distribution which is added to all counters. Small values are then discarded using a threshold to hide differences in the sets of stored keys between neighboring inputs. This is similar to the technique used by e.g. [22]. The algorithm takes the output from MG as input. We sometimes write PMG$(k, S)$ as a shorthand for PMG(MG$(k, S)$).

<caption>**Algorithm 2:** Private Misra-Gries (PMG)</caption>

**Parameters:** $\epsilon, \delta > 0$
**Input** : Output from Algorithm 1: $T, c \leftarrow MG(k, S)$
1 &nbsp;&nbsp;&nbsp;&nbsp; $\tilde{T} \leftarrow \emptyset$
2 &nbsp;&nbsp;&nbsp;&nbsp; Sample $\eta \sim \text{Laplace}(1/\epsilon)$
3 &nbsp;&nbsp;&nbsp;&nbsp; **foreach** $x \in T$ **do**
4 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $c_x \leftarrow c_x + \eta + \text{Laplace}(1/\epsilon)$
5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; **if** $c_x \ge 1 + 2 \ln(3/\delta)/\epsilon$ **then**
6 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $\tilde{T} \leftarrow \tilde{T} \cup \{x\}$
7 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $\tilde{c}_x \leftarrow c_x$
8 &nbsp;&nbsp;&nbsp;&nbsp; **return** $\tilde{T}, \tilde{c}$

* ▶ Add the two noise samples to count
* ▶ Small noisy values are removed

We prove the privacy guarantees in three steps. First, we show that changing either a single counter or all counters by 1 does not change the output distribution significantly (Corollary 10). This assumes that, for neighboring inputs, the set of stored elements is exactly the same. By Lemma 8, we have that the difference between the sets of stored keys is small and the corresponding counters are $\le 1$. Relying on the thresholding, we bound the probability of outputting one of these keys (Lemma 11). Finally, we combine these two lemmas to show that the privacy guarantees hold for all cases (we do this in Lemma 12).

**LEMMA 9.** Let us have $x, x' \in \mathbb{R}^k$ such that one of the following three cases holds

(1) $\exists i \in [k]$ such that $|x_i - x'_i| = 1$ and $x_j = x'_j$ for all $j \neq i$.

(2) $x_i = x'_i - 1$ for all $i \in [k]$.
$$ (3) \quad x_i = x'_i + 1 \text{ for all } i \in [k]. $$

Then we have for any measurable set Z that

$$ \mathrm{Pr}[x + \mathrm{Laplace}(1/\varepsilon)^{\otimes k} + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z] \le e^{\varepsilon} \mathrm{Pr}[x' + \mathrm{Laplace}(1/\varepsilon)^{\otimes k} + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z]. $$

PROOF. Throughout the proof, we construct sets by applying a translation to all elements of another set. That is, for any $\phi \in \mathbb{R}^k$ and measurable set Z we define $Z - \phi = \{a \in \mathbb{R}^k | a + \phi \in Z\}$. We first focus on the simpler case (1). It holds by the law of total expectation that

$$ \begin{align*} \mathrm{Pr}[x + \mathrm{Laplace}(1/\varepsilon)^{\otimes k} + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z] &= \\ \mathbb{E}_{N \sim \mathrm{Laplace}(1/\varepsilon)} [\mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon)^{\otimes k} \in Z - x - N\mathbf{1}^k | N]] &\le \\ e^{\varepsilon} \mathbb{E}_{N \sim \mathrm{Laplace}(1/\varepsilon)} [\mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon)^{\otimes k} \in Z - x' - N\mathbf{1}^k | N]] &= \\ e^{\varepsilon} \mathrm{Pr}[x' + \mathrm{Laplace}(1/\varepsilon)^{\otimes k} + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z], \end{align*} $$

where to prove the inequality, we used that for any measurable set A, it holds

$$ \mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon)^{\otimes k} \in A] \le e^{\varepsilon} \mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon)^{\otimes k} \in A - \phi] $$

for any $\phi \in \mathbb{R}^k$ with $\|\phi\|_1 \le 1$ (see [14]). Specifically, we have set $A = Z - x - N\mathbf{1}^k$ and $\phi = x - x'$ such that $\|\phi\|_1 = 1$.

We now focus on the cases (2) and (3). We will prove below that for $x, x'$ satisfying one of the conditions (2), (3) and for any measurable A, Z and $N_1 \sim \mathrm{Laplace}(1/\varepsilon)^{\otimes k}$, it holds

$$ \mathrm{Pr}[x + N_1 + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z | N_1 \in A] \le e^{\varepsilon} \mathrm{Pr}[x' + N_1 + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z | N_1 \in A]. $$

This allows us to argue like above:

$$ \begin{align*} \mathrm{Pr}[x + \mathrm{Laplace}(1/\varepsilon)^{\otimes k} + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z] &= \\ \mathbb{E}_{N_1 \sim \mathrm{Laplace}(1/\varepsilon)}^{\otimes k} [\mathrm{Pr}[x + N_1 + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z | N_1]] &\le \\ e^{\varepsilon} \mathbb{E}_{N_1 \sim \mathrm{Laplace}(1/\varepsilon)}^{\otimes k} [\mathrm{Pr}[x' + N_1 + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z | N_1]] &= \\ e^{\varepsilon} \mathrm{Pr}[x' + \mathrm{Laplace}(1/\varepsilon)^{\otimes k} + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z], \end{align*} $$

which would conclude the proof. Let $g: \mathbb{R} \to \mathbb{R}^k$ be the function $g(a) = a\mathbf{1}^k$ and define $g^{-1}(B) = \{a \in \mathbb{R}|g(a) \in B\}$ and note that $g$ is measurable. We focus on the case (2); the same argument works for (3) as we discuss below. It holds

$$ \begin{align*} \mathrm{Pr}[x + N_1 + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z | N_1 \in A] &= \\ \mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z - x - N_1 | N_1 \in A] &= \\ \mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon) \in g^{-1}(Z - x - N_1) | N_1 \in A] &= \\ \mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon) \in g^{-1}(Z - x' - N_1) - 1 | N_1 \in A] &\le \\ e^{\varepsilon} \mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon) \in g^{-1}(Z - x' - N_1) | N_1 \in A] &= \\ e^{\varepsilon} \mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z - x' - N_1 | N_1 \in A] &= \\ e^{\varepsilon} \mathrm{Pr}[x' + N_1 + \mathrm{Laplace}(1/\varepsilon)\mathbf{1}^k \in Z | N_1 \in A]. \end{align*} $$

To prove the inequality, we again used the standard result that for any measurable A, it holds that $\mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon) \in A] \le e^{\varepsilon} \mathrm{Pr}[\mathrm{Laplace}(1/\varepsilon) \in A - 1]$. The same holds for $A + 1$; this allows us to use the exact same argument in case (3), in which the proof is the same except that $-1$ on lines 4,5 of the manipulations is replaced by $+1$. □
COROLLARY 10. Let $T, c$ and $T', c'$ be two sketches such that $T = T'$ and one of following holds:

(1) $\exists i \in T$ such that $|c_i - c'_i| = 1$ and $c_j = c'_j$ for all $j \neq i$.

(2) $c_i = c'_i - 1$ for all $i \in T$.

(3) $c_i = c'_i + 1$ for all $i \in T$.

Then for any measurable set of outputs $Z$, we have:

$$ \mathrm{Pr}[\mathrm{PMG}(T, c) \in Z] \le e^{\epsilon} \mathrm{Pr}[\mathrm{PMG}(T', c') \in Z]. $$

PROOF. Consider first a modified algorithm PMG' that does not perform the thresholding: that is, if we remove the condition on line 5. It can be easily seen that PMG' only takes the vector $c$ and releases $c + \mathrm{Laplace}(1/\epsilon)^{\otimes k} + \mathrm{Laplace}(1/\epsilon)1^k$. We have just shown in Lemma 9 that this means that for any measurable $Z'$,

$$ \mathrm{Pr}[\mathrm{PMG}'(T, c) \in Z'] \le e^{\epsilon} \mathrm{Pr}[\mathrm{PMG}'(T', c') \in Z'] . $$

Let $\tau(x) = x$ for $x \ge 1 + 2 \ln(3/\delta)/\epsilon$ and 0 otherwise. Since $\mathrm{PMG}(T, c) = \tau(\mathrm{PMG}'(T, c))$, it then holds

$$ \mathrm{Pr}[\mathrm{PMG}(T, c) \in Z] = \mathrm{Pr}[\mathrm{PMG}'(T, c) \in \tau^{-1}(Z)] \le $$

$$ e^{\epsilon} \mathrm{Pr}[\mathrm{PMG}'(T', c') \in \tau^{-1}(Z)] = e^{\epsilon} \mathrm{Pr}[\mathrm{PMG}(T', c') \in Z], $$

as we wanted to show.

□

Next, we bound the effect on the output distribution from keys that differ between sketches by $\delta$.

LEMMA 11. Let $T, c$ and $T', c'$ be two sketches of size $k$ and let $\hat{T} = T \cap T'$. If we have that $|\hat{T}| \ge k-2$, $c_i = c'_i$ for all $i \in \hat{T}$, and for all $x \notin \hat{T}$, it holds $c_x, c'_x \le 1$. Then for any measurable set $Z$, it holds

$$ \mathrm{Pr}[\mathrm{PMG}(T, c) \in Z] \le \mathrm{Pr}[\mathrm{PMG}(T', c') \in Z] + \delta. $$

PROOF. Let $\mathrm{PMG}'(T, c)$ denote a mechanism that executes $\mathrm{PMG}(T, c)$ and post-processes the output by discarding any elements not in $\hat{T}$. It is easy to see that (a) $\mathrm{Pr}[\mathrm{PMG}'(T, c) \in Z] = \mathrm{Pr}[\mathrm{PMG}'(T', c') \in Z]$ since the input sketches are identical for all elements in $\hat{T}$. Moreover, for any output $\tilde{T}, \tilde{c} \leftarrow \mathrm{PMG}(T, c)$ for which $\tilde{T} \subseteq \hat{T}$, the post-processing does not affect the output. This gives us the following inequalities: (b) $\mathrm{Pr}[\mathrm{PMG}(T, c) \in Z] \le \mathrm{Pr}[\mathrm{PMG}'(T, c) \in Z] + \mathrm{Pr}[\tilde{T} \not\subseteq \hat{T}]$ and (c) $\mathrm{Pr}[\mathrm{PMG}'(T', c') \in Z] \le \mathrm{Pr}[\mathrm{PMG}(T, c) \in Z] + \mathrm{Pr}[\tilde{T}' \not\subseteq \hat{T}]$. Combining equations (a) – (c), we get the inequality $\mathrm{Pr}[\mathrm{PMG}(T, c) \in Z] \le \mathrm{Pr}[\mathrm{PMG}(T', c') \in Z] + \mathrm{Pr}[\tilde{T} \not\subseteq \hat{T}] + \mathrm{Pr}[\tilde{T}' \not\subseteq \hat{T}]$.

As such, the Lemma holds if $\mathrm{Pr}[\tilde{T} \not\subseteq \hat{T}] + \mathrm{Pr}[\tilde{T}' \not\subseteq \hat{T}] \le \delta$. That is, it suffices to prove that with probability at most $\delta$ any noisy count for elements not in $\hat{T}$ is at least $1 + 2 \ln(3/\delta)/\epsilon$. The noisy count for such a key can only exceed the threshold if one of the two noise samples added to the key is at least $\ln(3/\delta)/\epsilon$. From Definition 5 we have $\mathrm{Pr}[\mathrm{Laplace}(1/\epsilon) \ge \ln(3/\delta)/\epsilon] = \delta/6$. There are at most 4 keys not in $\hat{T}$ which are in $T \cup T'$ and therefore at most 6 noise samples affect the probability of outputting such a key (the 4 individual Laplace noise samples and the 2 global Laplace noise samples, one for each sketch). By a union bound the probability that any of these samples exceeds $\ln(3/\delta)/\epsilon$ is at most $\delta$. □

We are now ready to prove the privacy guarantee of Algorithm 2.

LEMMA 12. Algorithm 2 is $(\epsilon, \delta)$-differentially private for any $k$.

PROOF. The Lemma holds if and only if for any pair of neighboring streams $S \sim S'$ and any measurable set $Z$ we have:

$$ \mathrm{Pr}[\mathrm{PMG}(T, c) \in Z] \le e^{\epsilon} \mathrm{Pr}[\mathrm{PMG}(T', c') \in Z] + \delta, $$
where $T, c \leftarrow \text{MG}(k, S)$ and $T', c' \leftarrow \text{MG}(k, S')$ denotes the non-private sketches for each stream.

We prove the guarantee above using an intermediate sketch that “lies between” $T, c$ and $T', c'$. The sketch has support $T'$ and we denote the counters as $\hat{c}$. By Lemma 8, we know that $|T \cap T'| \ge k - 2$ and all counters in $c$ and not in $T \cap T'$ are at most 1. We will now present some conditions on $\hat{c}$ such that if these conditions hold, the lemma follows. We will then prove the existence of such $\hat{c}$ below. Assume that $\hat{c}_i = c_i$ for all $i \in T \cap T'$ and $\hat{c}_j \le 1$ for all $j \in T' \setminus T$. Lemma 11 then tells us that

$$ \Pr[\text{PMG}(T, c) \in Z] \le \Pr[\text{PMG}(T', \hat{c}) \in Z] + \delta. $$

Assume also that one of the cases required for Corollary 10 holds between $\hat{c}$ and $c'$. We have

$$ \Pr[\text{PMG}(T', \hat{c}) \in Z] \le e^{\epsilon} \Pr[\text{PMG}(T', c') \in Z]. $$

Therefore, if such a sketch $T', \hat{c}$ exists for all $S$ and $S'$ the lemma holds since

$$ \Pr[\text{PMG}(T, c) \in Z] \le \Pr[\text{PMG}(T', \hat{c}) \in Z] + \delta \le e^{\epsilon} \Pr[\text{PMG}(T', c') \in Z] + \delta. $$

It remains to prove the existence of $\hat{c}$ such that $\hat{c}_i = c_i$ for all $i \in T \cap T'$ and $\hat{c}_j \le 1$ for all $j \in T' \setminus T$ and such that one of the conditions (1) – (3) of Corollary 10 holds between $\hat{c}$ and $c'$. We first consider neighboring streams where $S'$ is obtained by removing an element from $S$. From Lemma 8 we have two cases to consider. If $c_i = c'_i - 1$ for all $i \in T'$ we simply set $\hat{c} = c$. Recall that we implicitly have $c_i = 0$ for $i \notin T$. Therefore the sketch satisfies the two conditions above since $\hat{c}_i = c_i$ for all $i \in \mathcal{U}$ and condition (2) of Corollary 10 holds. In the other case where $c_i = c'_i + 1$ for exactly one $i \in T$ there are two possibilities. If $i \in T'$ we again set $\hat{c} = c$. When $i \notin T'$ there must exist at least one element $j \in T'$ such that $c'_j = 0$ and $j \notin T$. We set $\hat{c}_j = 1$ and $\hat{c}_i = c'_i$ for all $i \ne j$. In both cases $\hat{c}_i = c_i$ for all $i \in T \cap T'$ and $\hat{c}_j$ is at most one for $j \notin T$. There is exactly one element with a higher count in $\hat{c}$ than $c'$ which means that condition (1) of Corollary 10 holds.

If $S$ is obtained by removing an element from $S'$ the cases from Lemma 8 are flipped. If $c_i - 1 = c'_i$ for all $i \in T$ and $c'_j = 0$ for $j \notin T$ we set $\hat{c}_i = c_i$ if $i \in T$ and $\hat{c}_i = 1$ otherwise. It clearly holds that $\hat{c}_i = c_i$ for all $i \in T \cap T'$ and $\hat{c}_j \le 1$ for all $j \notin T$. Since $\hat{c}_i = c'_i + 1$ for all $i \in T'$ condition (3) of Corollary 10 holds. Finally, if $c_i + 1 = c'_i$ for exactly one $i \in T'$ we simply set $\hat{c} = c$. It clearly holds that $\hat{c}_i = c_i$ for all $i \in T \cap T'$, we have $\hat{c}_j = 0$ for all $j \notin T$, and condition (1) of Corollary 10 holds between $\hat{c}$ and $c'$. $\square$

Next, we analyze the error compared to the non-private MG sketch. We state the error in terms of the largest error among all elements of the sketch. Recall that we implicitly say that the count is zero for any element not in the sketch.

**LEMMA 13.** Let $\tilde{T}, \tilde{c} \leftarrow \text{PMG}(T, c)$ denote the output of Algorithm 2 for any sketch $T, c$ with $|T| = k$. Then with probability at least $1 - \beta$ we have

$$ \tilde{c}_x \in \left[ c_x - \frac{2 \ln \left( \frac{k+1}{\beta} \right)}{\varepsilon} - 1 - \frac{2 \ln (3/\delta)}{\varepsilon}, c_x + \frac{2 \ln \left( \frac{k+1}{\beta} \right)}{\varepsilon} \right] $$

for all $x \in T$ and $\tilde{c}_x = 0$ for all $x \notin T$.

**PROOF.** The two sources of error are the noise samples and the thresholding step. We begin with a simple bound on the absolute value of the Laplace distribution.

$$ \Pr \left[ \left| \text{Laplace}(1/\varepsilon) \right| \ge \frac{\ln((k+1)/\beta)}{\varepsilon} \right] = 2 \cdot \Pr \left[ \text{Laplace}(1/\varepsilon) \le -\frac{\ln((k+1)/\beta)}{\varepsilon} \right] = \beta/(k+1). $$

Since $k+1$ samples are drawn we have by a union bound that the absolute value of all samples is bounded by $\ln((k+1)/\beta)/\varepsilon$ with probability at least $1 - \beta$. As such the absolute error from the
Laplace samples is at most $2 \ln((k+1)/\beta)/\epsilon$ for all $x \in T$ since two samples are added to each count. Removing noisy counts below the threshold potentially adds an additional error of at most $1+2\ln(3/\delta)/\epsilon$. It is easy to see that $\tilde{c}_x = 0$ for all $x \notin T$ since the algorithm never outputs any such elements. $\square$

**THEOREM 14.** PMG$(k, S)$ satisfies $(\epsilon, \delta)$-differential privacy. Let $f(x)$ denote the frequency of any element $x \in \mathcal{U}$ in $S$ and let $\hat{f}(x)$ denote the estimated frequency of $x$ from the output of PMG$(k, S)$. For any $x$ with $f(x) = 0$ we have $\hat{f}(x) = 0$ and with probability at least $1 - \beta$ we have for all $x \in \mathcal{U}$

$$ \hat{f}(x) \in \left[ f(x) - \frac{2 \ln \left( \frac{k+1}{\beta} \right)}{\epsilon} - 1 - \frac{2 \ln(3/\delta)}{\epsilon} - \frac{|S|}{k+1}, f(x) + \frac{2 \ln \left( \frac{k+1}{\beta} \right)}{\epsilon} \right]. $$

Moreover, the algorithm outputs all $x$, such that $\hat{f}(x) > 0$ and there are at most $k$ such elements. PMG$(k, S)$ uses $2k$ words of memory. The mean squared error for any fixed element $x \in \mathcal{U}$ is bounded by $\mathbb{E}[(\hat{f}(x) - f(x))^2] \le 3 \left(1 + \frac{2+2\ln(3/\delta)}{\epsilon} + \frac{|S|}{k+1}\right)^2$.

**PROOF.** The space complexity is clearly as claimed, as we are storing at any time at most $k$ items and counters. We focus on proving privacy and correctness.

If $f(x) = 0$ we know that $x \notin T$ where $T$ is the keyset after running Algorithm 1. Since Algorithm 2 outputs a subset of $T$ we have $\hat{f}(x) = 0$. The first part of the Theorem follows directly from Fact 7 and Lemmas 12 and 13.

We now bound the mean squared error. There are three sources of error. Let $r_1$ be the error coming from the Laplace noise, $r_2$ from the thresholding, and $r_3$ the error made by the MG sketch. Then

$$ \mathbb{E}[(\hat{f}(x) - f(x))^2] = \mathbb{E}[(r_1 + r_2 + r_3)^2] \le 3(\mathbb{E}[r_1^2] + \mathbb{E}[r_2^2] + \mathbb{E}[r_3^2]) $$

by equivalence of norms (for any dimension $n$ vector $v$, $\|v\|_1 \le \sqrt{n}\|v\|_2$). The errors $r_2, r_3$ are deterministically bounded $r_2 \le 1 + 2\ln(3/\delta)/\epsilon$ and $r_3 \le |S|/(k+1)$. $\mathbb{E}[r_1^2]$ is the variance of the Laplace noise; we added two independent noises each with scale $1/\epsilon$ and thus variance $2/\epsilon^2$ for a total variance of $4/\epsilon^2$. This finishes the proof. $\square$

## 5.1 Privatizing standard versions of the Misra-Gries sketch

The privacy of our mechanism as presented in Algorithm 2 relies on our variant of the Misra-Gries algorithm. Our sketch can contain elements with a count of zero. However, elements with a count of zero are removed in the standard version of the sketch. As such, sketches for neighboring datasets can differ for up to $k$ keys if one sketch stores $k$ elements with a count of 1 and the other sketch is empty. It is easy to change Algorithm 2 to handle these implementations. We simply increase the threshold to $1 + 2 \ln(\frac{k+1}{2\delta})/\epsilon$ since the probability of outputting any of the $k$ elements with a count of 1 is bounded by $\delta$.

## 5.2 Tips for practitioners

Here we discuss some technical details to keep in mind when implementing our mechanism.

The output of the Misra-Gries algorithm is an associative array. In Algorithm 2 we add appropriate noise such that the associative array can be released under differential privacy. However, for some implementations of associative arrays such as hash tables the order in which keys are added affects the data structure. Using such an implementation naively violates differential privacy, but it is easily solved either by outputting a random permutation of the key-value pairs or using a fixed order e.g. sorted by key.
PROOF. Let $\alpha$ denote the number of times Branch 2 of Algorithm 1 was executed. Then the frequency estimate of any $x \in \mathcal{U}$ before post-processing must be in $[f(x) - \alpha, f(x)]$, since only the decrement step of the MG sketch introduces any error. Note that we have $\alpha \le |S|/(k+1)$ by Fact 7. Notice that $\sum_{x \in T} c_x = |S| - \alpha \cdot (k+1)$ since $k$ counters are decremented and 1 element is ignored each time Branch 2 of Algorithm 1 is executed. As such, we have $\gamma = |S|/(k+1) - \alpha$ and $c_x - \gamma \in [f(x) - |S|/(k+1), f(x) - |S|/(k+1) + \alpha]$. The claim therefore holds for any $x \in \hat{T}$ since $\hat{c}_x = c_x - \gamma$. For any key that is removed by post-processing, that is $x \in T \setminus \hat{T}$, it holds that $c_x - \gamma \le 0$. This implies that the true frequency $f(x)$ is at most $|S|/(k+1)$ so the error guarantee still holds. Since any $x \notin T$ is unaffected by post-processing, those frequency estimates still lie in the interval $[f(x) - \alpha, f(x)]$. $\square$

LEMMA 16. Let $\hat{T}$, $\hat{c}$ and $\hat{T}'$, $\hat{c}'$ denote the output of Algorithm 3 for a pair of neighboring streams. Consider $\hat{c} \in \mathbb{R}^d$ where we define $\hat{c}_x = 0$ for all $x \notin \hat{T}$ and define $\hat{c}'$ similarly. Then we have $\|\hat{c} - \hat{c}'\|_1 < 2$.

PROOF. Let $S \sim S'$ denote any pair of neighboring streams where $S'$ is obtained by removing one element from $S$. The proof is symmetric if $S'$ is obtained by adding one element to $S$. We show that the sensitivity is bounded by considering the effect of our proposed post-processing for both cases of Lemma 8. Notice that we can ignore any counter that has a value of zero in any sketch, because they are always removed by the condition on Line 5.

We first consider the case where $c_i = c'_i - 1$ for all $i \in T'$. Here we have $\sum_{x \in T} c_x = \sum_{x \in T'} c'_x - k$ which implies that $\gamma = \gamma' - k/(k+1) = \gamma' - 1 + 1/(k+1)$. As such, we have for any $i \in T'$ that $c_i - \gamma = c'_i - \gamma' - 1/(k+1)$. From this it follows that for any $i \in \hat{T}'$ we have $\hat{c}_i - \hat{c}'_i = -1/(k+1)$, and for any $j \notin \hat{T}'$ we have $\hat{c}_j - \hat{c}'_j = 0$. We can thus bound the distance between the vectors by $\|\hat{c} - \hat{c}'\|_1 = \sum_{i \in [d]} |\hat{c}_i - \hat{c}'_i| = \sum_{x \in \hat{T}'} |\hat{c}_x - \hat{c}'_x| = |\hat{T}'| \cdot 1/(k+1) \le k/(k+1) < 1$.

Next, we consider the case where sketches differ in only one counter before post-processing. That is, there exists an element $x \in \mathcal{U}$ such that $c_x = c'_x + 1$ and all other counters are equal. In this case we have $\gamma = \gamma' - 1/(k+1)$ and $c_i - \gamma = c'_i - \gamma' + 1/(k+1)$ for any $i \neq x$. As such, we either have $\hat{c}_i - \hat{c}'_i = 1/(k+1)$ or $\hat{c}_i = \hat{c}'_i = 0$ for each $i \in \mathcal{U} \setminus \{x\}$. Furthermore, we have that $0 \le \hat{c}_x - \hat{c}'_x \le 1 + 1/(k+1)$. We conclude the proof by bounding the distance between the vectors by $\|\hat{c} - \hat{c}'\|_1 = \sum_{i \in [d]} |\hat{c}_i - \hat{c}'_i| = \sum_{i \in \hat{T}} |\hat{c}_i - \hat{c}'_i| = |\hat{T} \setminus \{x\}| \cdot 1/(k+1) + |\hat{c}_x - \hat{c}'_x| \le 1 + k/(k+1) < 2$. $\square$

We achieve $\varepsilon$-differential privacy by adding noise to our new sketch. We essentially use the same technique as Chan et al. [11] but the noise no longer scale linearly in $k$ as the sensitivity of Algorithm 3 is bounded by $2$. Specifically, we add noise sampled from Laplace$(2/\varepsilon)$ independently to the count of each element from $\mathcal{U}$ and release the top-$k$ noisy counts. A simple union bound shows that with probability at least $1 - \beta$ the absolute value of all samples is bounded by $2 \ln(d/\beta)/\varepsilon$. Note that it might be infeasible to actually sample noise for each element when $\mathcal{U}$ is large; we refer to previous work on how to implement this more efficiently [4, 11, 12].

It is worth noting that the low sensitivity of the post-processed sketch can also be utilized under $(\varepsilon, \delta)$-differential privacy. We can use an approach similar to [22]. They add noise to all non-zero counters and remove noisy counts below a threshold to hide small counters. Applying the standard approach for histograms would require a threshold with a small dependence on $k$ as neighboring sketches might disagree on all keys. However, [3, Algorithm 9] extended the technique to real-valued vectors by probabilistically rounding elements with a value less than the $\ell_1$-sensitivity. If we apply their technique directly we get a threshold of $4 + 2 \ln(1/\delta)/\varepsilon$. As such, we can achieve error guarantees that match those from Theorem 14 up to constant factors. However, this approach has worse guarantees than Algorithm 2 when comparing to the non-private Misra-Gries sketch. By Lemma 13 the error of Algorithm 2 when compared to the non-private MG sketch is $O(\log(1/\delta)/\varepsilon)$
with high probability (for sufficiently small $\delta$). Here the error is $n/(k+1) + O(\log(1/\delta)/\epsilon)$ since we subtract up to $n/(k+1)$ from the counters before adding noise.

# 7 PRIVATIZING MERGED SKETCHES

In practice, it is often important that we may merge sketches. This is for example commonly used when we have a dataset distributed over many servers. Each dataset consists of multiple streams in this setting, and we want to compute an aggregated sketch over all streams. We say that datasets are neighboring if we can obtain one from the other by removing a single element from one of the streams. If the aggregator is untrusted we must add noise to each sketch before performing any merges. This is the setting in [11] and we can run their merging algorithm. However, since we add noise to each sketch the error scales with the number of sketches. In particular, the error from the thresholding step of Algorithm 2 scales linearly in the number of sketches for worst-case input. In the rest of this section, we consider the setting where aggregators are trusted. We can achieve optimal error in this setting by applying the post-processing step from the previous section to each sketch before aggregating the counters of each element. The $l_1$-sensitivity of the aggregated sketch is still bounded by 2. However, the aggregated sketch might have much more than $k$ counters. This approach increases the memory requirement of the aggregator linearly with the total number of sketches in the worst case. Next we consider a merging algorithm where aggregators never store more than $2k$ counters.

Agarwal, Cormode, Huang, Phillips, Wei, and Yi [1] introduced the following simple merging algorithm in the non-private setting. Given two Misra-Gries sketches $T_1, c_1 \leftarrow \text{MG}(k, S^{(1)})$ and $T_2, c_2 \leftarrow \text{MG}(k, S^{(2)})$ they first compute the sum of all counters $c_1 + c_2$. There are up to $2k$ counters at this point. They subtract the value of the $k+1$'th largest counter from all elements. Finally, any non-positive counters are removed leaving at most $k$ counters. They show that merged sketches have the same worst-case guarantee as non-merged Misra-Gries sketches. That is, if we compute a Misra-Gries sketch for each stream $(S^{(1)}, \dots, S^{(l)})$ and merge them into a single sketch, the frequency estimate of all elements is at most $N/(k+1)$ less than the true frequency. Here $N$ is the total length of all streams. This holds for any order of merging and the streams do not need to have the same length.

Unfortunately, the structure between neighboring sketches where either a single counter or exactly $k$ counters differ by 1 breaks down when merging. Therefore we cannot run Algorithm 2 on the merged sketch. However, as we show below, the global sensitivity of merged sketches is independent of the number of merges. The sensitivity only depends on the number of counters. We first show a property for a single merge operation; this will allow us to bound the sensitivity for any number of merges. Note that unlike in Section 5, we do not limit the number of keys that differ between sketches and we do not store keys with a count of zero.

**LEMMA 17.** Let $T_1, c_1, T'_1, c'_1$ and $T_2, c_2$ denote Misra-Gries sketches of size $k$ and denote the sketches merged with the algorithm above as $\hat{T}, \hat{c} \leftarrow \text{Merge}(T_1, c_1, T_2, c_2)$ and $\hat{T}', \hat{c}' \leftarrow \text{Merge}(T'_1, c'_1, T_2, c_2)$. If $T'_1 \subseteq T_1$ and $c_{1i} - c'_{1i} \in \{0, 1\}$ for all $i \in \mathcal{U}$ then at least one of the following holds $(1) \hat{T}' \subseteq \hat{T}$ and $\hat{c}_i - \hat{c}'_i \in \{0, 1\}$ for all $i \in \mathcal{U}$ or $(2) \hat{T} \subseteq \hat{T}'$ and $\hat{c}'_i - \hat{c}_i \in \{0, 1\}$ for all $i \in \mathcal{U}$.

**PROOF.** Let $\bar{c}$ and $\bar{c}'$ denote the merged counters before subtracting and removing values. Then clearly $\bar{c}_i - \bar{c}'_i \in \{0, 1\}$ for all $i \in \mathcal{U}$. Therefore we have that $\bar{c}_{k+1} - \bar{c}'_{k+1} \in \{0, 1\}$ where $\bar{c}_{k+1}$ denotes the value of the $k+1$'th largest counter in $\bar{c}$. Note that it does not matter if the $k+1$'th largest counter corresponds to a different element. If $\bar{c}_{k+1} = \bar{c}'_{k+1}$ we subtract the same value from each sketch and we have $\hat{c}_i - \hat{c}'_i \in \{0, 1\}$ for all $i \in \mathcal{U}$ and also $\hat{T}' \subseteq \hat{T}$. If $\bar{c}_{k+1} - \bar{c}'_{k+1} = 1$ we subtract one more from each count in $\hat{c}$ and we have $\hat{c}'_i - \hat{c}_i \in \{0, 1\}$ for all $i \in \mathcal{U}$ and $\hat{T} \subseteq \hat{T}'$. $\square$
**COROLLARY 18.** Let $(S^{(1)}, \dots, S^{(l)})$ and $(S'^{(1)}, \dots, S'^{(l)})$ denote two sets of streams such that $S^{(i)} \sim S'^{(i)}$ for one $i \in [l]$ and $S^{(j)} = S'^{(j)}$ for any $j \neq i$. Let $T$, $c$ and $T'$, $c'$ be the result of merging Misra-Gries sketches computed on both sets of streams in any fixed merging order. Then $c$ and $c'$ differ by 1 for at most $k$ elements and agree on all other counts.

**PROOF.** The condition of Lemma 17 clearly holds for sketches of a pair of neighboring datasets by Lemma 8 if we remove all elements with a count of zero. The claim holds by induction after each merging operation by Lemma 17.

$\square$

Since the $l_1$-sensitivity is $k$ we can use the algorithm in [11] that adds noise with magnitude $k/\epsilon$ to all elements in $\mathcal{U}$ and keeps the top-$k$ noisy counts. If we only add noise to non-zero counts we can hide that up to $k$ keys can change between neighboring inputs with a threshold. The two approaches have expected maximum error compared to the non-private merged sketch of $O(k \log(d)/\epsilon)$ and $O(k \log(k/\delta)/\epsilon)$, respectively. For $(\epsilon, \delta)$-differential privacy we can also utilize that the $l_2$-sensitivity is $\sqrt{k}$ because counters only differ by 1. This allows us to add noise that scales slower with $k$ using the Gaussian Sparse Histogram Mechanism [30] which we discuss further in the next section.

# 8 USER-LEVEL DIFFERENTIAL PRIVACY

So far we considered the setting where a user has exactly one element. In this section, we consider the more general setting where a user contributes up to $m$ distinct elements. Specifically, this means that the input is a stream $S = (S_1, S_2, \dots, S_{n-1}, S_n)$, where for all $i \in [n]$ we have $S_i \subseteq \mathcal{U}$ and $|S_i| \le m$. Our goal is to estimate the frequency of each element, that is $f(x) = \sum_{i \in [n]} \mathbb{1}[x \in S_i]$. We denote the total length of the stream as $N = \sum_{i \in [n]} |S_i|$. If we want to compute a MG sketch in this setting we have to flatten the input. We denote by $\hat{S}$ a stream created by processing the sets in $S$ one at a time by iterating over each element in the set in some fixed order (e.g. ascending order).

We first show that Algorithm 2 satisfies differential privacy in this setting. The magnitude of noise and the threshold increase as a function of $m$. We then introduce a new sketch with similar error guarantees to the MG sketch that can be released with less noise than Algorithm 2 for many parameters in this setting. Note that throughout this section we assume that $k > m$. This is a fair assumption because the error guarantees of the MG sketch are meaningless when $m > k$ because $f(x) \le n$ by definition and $N/(k+1) = (n \cdot m)/(k+1) \ge n$ if $|S_i| = m > k$ for all $i \in [n]$.

We start by restating the group privacy property of differential privacy in our context.

**LEMMA 19 (GROUP PRIVACY (FOLLOWING [15]))**. Let $\mathcal{M}$ be a mechanism satisfying $(\epsilon, \delta)$-differential privacy where one stream of a neighboring pair of streams is obtained by removing 1 element from the other. Then $\mathcal{M}$ satisfies $(m\epsilon, m\epsilon^\delta)$-differential privacy for neighboring streams where one stream is obtained by removing at most $m$ elements from (or adding at most $m$ elements to) the other stream.

This allows us to upper bound the privacy parameters when streams differ by multiple elements.

**LEMMA 20.** Let $\hat{S}$ be the flattened version of the stream $S$. Then releasing PMG$(k, \hat{S})$ with parameters $\epsilon = \epsilon'/m$ and $\delta = \delta'/(m\epsilon^\delta)$ satisfies $(\epsilon', \delta')$-differential privacy.

**PROOF.** PMG$(k, \hat{S})$ satisfies $(\epsilon, \delta)$-differential privacy for streams differing in a single element by Lemma 12. For any neighboring streams $S \sim S'$ we have that $\hat{S}$ can be created by removing at most $m$ elements from $\hat{S}'$, or vice versa. It follows from Lemma 19 that PMG$(k, \hat{S})$ satisfies $(\epsilon', \delta')$-differential privacy as $m\epsilon = \epsilon'$ and $m\epsilon^\delta = m\epsilon^\delta = \delta'$ by definition. $\square$

Since the proof above does not depend on the fact that the elements of a user are distinct or appear in consecutive order the algorithm can be used in an even more general setting.
**COROLLARY 21.** *Algorithm 2 with the parameters of Lemma 20 satisfies $(\varepsilon', \delta')$-differential privacy under any definition of neighboring streams where one stream is obtained from the other by adding or removing up to m (possibly duplicate) elements.*

We can use a similar argument to achieve pure differential privacy when streams differ by multiple elements using our technique from Section 6.

**LEMMA 22.** Let $S \in \mathcal{U}^*$ be a stream of elements where a neighboring stream $S'$ is obtained from $S$ by adding or removing up to m (possibly duplicate) elements. If we compute a MG sketch of $S$ followed by the post-processing step from Section 6, we can release the sketch under $\varepsilon$-DP using the technique of [11] when noise is sampled from Laplace$(2m/\varepsilon)$.

**PROOF.** It follows from Lemma 19 since the mechanism satisfies $(\varepsilon/m)$-differential privacy for streams differing by 1 element because the $\ell_1$-sensitivity is bounded by 2 as discussed in Section 6. $\square$

We cannot hope to avoid scaling the noise linearly with $m$ in the generalized setting of Corollary 21 and Lemma 22 where users can contribute duplicate elements to the stream. The reason is that any frequency can differ by $m$ between neighboring streams if a user contributes only identical elements. We instead focus on the setting where elements are distinct. In this setting, additional improvements are possible under $(\varepsilon, \delta)$-DP because the $\ell_2$ sensitivity is smaller, as we discuss below.

Algorithm 2 and the mechanism by [11] relies on noise from the Laplace distribution. This choice works well for the settings of Sections 5 and 6. However, when users have multiple distinct items we can instead use Gaussian noise under $(\varepsilon, \delta)$-differential privacy. The magnitude of Gaussian noise required for differential privacy scales with the $\ell_2$-sensitivity rather than the $\ell_1$-sensitivity which is typically used for Laplace noise. If we consider the task of releasing $f(x)$ for all $x \in \mathcal{U}$ without memory constraints it is clear that a user changes at most $m$ distinct counts by 1 each. The $\ell_1$-sensitivity of $f$ is therefore $m$, but the $\ell_2$-sensitivity is only $\sqrt{m}$.

We next discuss a general mechanism for releasing sketches using Gaussian noise. The mechanism is similar to the technique with Laplace noise by [22]. The idea is to add noise from a Gaussian distribution to all non-zeroes counters and then remove small noisy counts. This is known as the Gaussian Sparse Histogram Mechanism. Karrer, Kifer, Wilkins, and Zhang [30] gave an exact analysis of the parameters required for the mechanism to satisfy $(\varepsilon, \delta)$-DP. They consider a more general version of the mechanism, and we restate their result for our setting in Theorem 23.

**THEOREM 23 (GAUSSIAN SPARSE HISTOGRAM MECHANISM (GSHM) - FOLLOWING [30, THEOREM 5.4]).**
Let $T, c$ and $T'$, $c'$ be frequency sketches for streams $S$ and $S'$, such that for any neighboring pair $S \sim S'$, the counters $c$ and $c'$ differ for at most $l$ counts and agree on the count for all other elements. The differing counts are either (a) all 1 higher in $c$ than in $c'$ or (b) all 1 lower in $c$ than in $c'$. Then the Gaussian Sparse Histogram Mechanism, denoted GSHM($T, c, l, \sigma, \tau$), adds noise from $\mathcal{N}(0, \sigma^2)$ independently to each non-zero count in $c$ and removes all noisy counts below the threshold $1 + \tau$. The mechanism satisfies $(\varepsilon, \delta)$-differential privacy if and only if the following inequality holds

$$ \delta \ge \max \left[ 1 - \Phi\left(\frac{\tau}{\sigma}\right)^l, \max_{j \in [l]} \left(1 - \Phi\left(\frac{\tau}{\sigma}\right)^{l-j}\right) + \Phi\left(\frac{\tau}{\sigma}\right)^{l-j} \left[ \Phi\left(\frac{\sqrt{j}}{2\sigma} - \frac{(\varepsilon - \gamma)\sigma}{\sqrt{j}}\right) - e^{\varepsilon - \gamma}\Phi\left(-\frac{\sqrt{j}}{2\sigma} - \frac{(\varepsilon - \gamma)\sigma}{\sqrt{j}}\right) \right], \max_{j \in [l]} \left(\frac{\sqrt{j}}{2\sigma} - \frac{(\varepsilon + \gamma)\sigma}{\sqrt{j}}\right) - e^{\varepsilon + \gamma}\Phi\left(-\frac{\sqrt{j}}{2\sigma} - \frac{(\varepsilon + \gamma)\sigma}{\sqrt{j}}\right) \right] $$

where $\gamma = (l-j) \log \Phi(\frac{\tau}{\sigma})$.
The inequality of Theorem 23 gives the tightest results, but it can be difficult to parse. We present simpler parameters for the Gaussian Sparse Histogram Mechanism below. Note that our analysis is far from tight and any deployment of the GSHM should preferably set parameters using the exact analysis presented in Theorem 23. We present the version with worse parameters for presentation purposes only as it is easier to read off the asymptotic behavior of the mechanism.

**LEMMA 24.** Let $T, c, T', c',$ and $GSHM(T, c, l, \sigma, \tau)$ all be defined as in Theorem 23. $GSHM(T, c, l, \sigma, \tau)$ satisfies $(\varepsilon, \delta)$-differential privacy when $\varepsilon < 1$ for $\sigma = \sqrt{l2 \ln(2.5/\delta)} / \varepsilon$ and $\tau = \sqrt{2 \ln(2l/\delta)} \sigma$.

**PROOF.** We use a proof similar to that currently used for the Google Differential Privacy library [20] where the budget for $\delta$ is split between the noise and the threshold. Let $E_0$ denote the event where only counters that are non-zero in both sketches are above the threshold. We use $\mathcal{M}(x)$ and $\mathcal{M}(x')$ to denote $GSHM(T, c, l, \sigma, \tau)$ and $GSHM(T', c', l, \sigma, \tau)$, respectively. We have that

$$ 
\begin{aligned}
\Pr[\mathcal{M}(x) \in Z] &= \Pr[\mathcal{M}(x) \in Z|E_0] \Pr[E_0] + \Pr[\mathcal{M}(x) \in Z|\neg E_0] \Pr[\neg E_0] \\
&\le \Pr[\mathcal{M}(x) \in Z|E_0] \Pr[E_0] + \delta/2 \\
&\le (e^\varepsilon \Pr[\mathcal{M}(x') \in Z|E_0] + \delta/2) \Pr[E_0] + \delta/2 \\
&\le e^\varepsilon \Pr[\mathcal{M}(x') \in Z \text{ and } E_0] + \delta/2 + \delta/2 \\
&\le e^\varepsilon \Pr[\mathcal{M}(x') \in Z] + \delta.
\end{aligned}
$$

The first inequality follows from a union bound and the fact that $\Pr[\mathcal{N}(0, \sigma^2) > t] \le e^{-(t/\sigma)^2/2}$ for any positive $t$. The second inequality follows from [15, Theorem A.1] since $\Delta_2 = \sqrt{l}$. $\square$

As seen above the noise and threshold for the Gaussian Sparse Histogram Mechanism scales with the $l_2$-sensitivity rather than the $l_1$-sensitivity. Adding or removing a user to or from the stream only changes the true frequency of any $x \in \mathcal{U}$ by 1. As such the $l_2$ distance between the frequencies for any pair of neighboring streams $S \sim S'$ is $\sqrt{\sum_{x \in \mathcal{U}} (f(x) - f(x'))^2} \le \sqrt{m}$. We might hope that the $l_2$-sensitivity of the MG sketch is low, or perhaps that we can use a technique similar to Algorithm 2 with Gaussian noise. Alternatively, we could remove small values using the post-processing from Section 6 if that guaranteed low $l_2$-sensitivity. Unfortunately, these techniques cannot be used to reduce the dependence on $m$ because there exist neighboring streams where a single counter differs by $m$. That statement holds even if we perform the post-processing from Section 6.

**LEMMA 25.** Let $T, c$ and $T', c'$ denote MG sketches of size $k$ for a pair of neighboring streams $S \sim S'$. There exist neighboring streams such that $c_x - c'_{x'} = m$ for some element $x \in \mathcal{U}$.

**PROOF.** There are many such pairs of streams, here we give an example of a pair where all counters for elements other than $x$ are zero in both sketches. Let $S_{k+1}$ be the user that is removed from $S$ to obtain $S'$. We construct $(S_1, S_2, \dots, S_{k-1}, S_k)$ such that it contains exactly $m$ copies of $k$ distinct elements that does not include $x$. This is always possible for any $m \le k$ by cycling through the same order of $k$ elements and taking $m$ elements at a time. E.g, for $m = 2$ and items $\{1, 2, 3\}$ the stream could be $(\{1, 2\}, \{3, 1\}, \{2, 3\})$. If $S_{k+1}$ contains $m$ elements that are not in the sketch all counters are decremented and the sketch for $S$ is empty after processing $S_{k+1}$. Now, the rest of the stream contains only copies of the singleton $\{x\}$. After processing $S_{k+1+m}$ the sketch for $S$ has a single counter $c_x = m$ while the sketch for $S'$ is empty. Furthermore, if we extend the streams further with more copies of $\{x\}$ we have $c_x = |S| - k - 1$ and $c'_{x'} = |S| - k - 1 - m$. $\square$

Since there exist neighboring streams for which the MG sketch differs by $m$ for a single counter, as shown above, we must add noise with magnitude scaled to $m$ to satisfy differential privacy. We also cannot hope to avoid these problematic inputs using some post-processing for small counters
similar to Section 6 because the counter that differs by $m$ can be arbitrarily large for sufficiently long streams. We instead use a different sketching algorithm to remove the linear dependency on $m$. In Algorithm 4 we present a novel frequency sketch. Our sketch is similar in spirit to the MG sketch as it is a generalization of the original MG sketch for sets. The key difference is that we always increment all counters for the elements of a user and decrement all counters at most once per user instead of once per element.

<table><caption>**Algorithm 4: Privacy-Aware Misra-Gries (PAMG)**</caption><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td>1</td><td>T &larr; &empty;</td><td></td></tr><tr><td>2</td><td>foreach S<sub>i</sub> &isin; S do</td><td></td></tr><tr><td>3</td><td>foreach x &isin; S<sub>i</sub> do</td><td rowspan="4">▷ Increment count of each element in S<sub>i</sub></td></tr><tr><td>4</td><td>if x &isin; T then</td></tr><tr><td>5</td><td>c<sub>x</sub> &larr; c<sub>x</sub> + 1</td></tr><tr><td>6</td><td>else</td></tr><tr><td>7</td><td>T &larr; T &cup; {x}</td><td></td></tr><tr><td>8</td><td>c<sub>x</sub> &larr; 1</td><td></td></tr><tr><td>9</td><td>if |T| &gt; k then</td><td rowspan="2">▷ The size can temporarily be up to k + m</td></tr><tr><td>10</td><td>foreach x &isin; T do</td></tr><tr><td>11</td><td>c<sub>x</sub> &larr; c<sub>x</sub> - 1</td><td rowspan="3">▷ Decrement all counters</td></tr><tr><td>12</td><td>if c<sub>x</sub> = 0 then</td></tr><tr><td>13</td><td>T &larr; T \ {x}</td></tr><tr><td>14</td><td>return T, c</td><td></td></tr></tbody></table>

We start our analysis by showing that the error guarantees of Algorithm 4 match the MG sketch.

**LEMMA 26.** Let $\hat{f}(x)$ be the frequency estimates given by Algorithm 4 with size $k$. Then for all $x \in \mathcal{U}$, it holds that $\hat{f}(x) \in [f(x) - \lfloor N/(k+1) \rfloor, f(x)]$, where $f(x)$ is the true frequency of $x$ in $S$ and $N = \sum_{i \in [n]} |S_i|$ is the total size of the stream.

**PROOF.** The proof is similar to the analysis of the MG sketch by [8]. Notice that similar to the MG sketch, we only introduce errors when decrementing counters. The error of any estimate $\hat{f}(x)$ is exactly the number of times the counter for $x$ was decremented on line 11. As such, we can bound the maximum error of all counters by the number of times the condition on line 9 evaluates to true. Notice that whenever the condition is true the sum of all counters is decreased by at least $k+1$. Since the sum is incremented exactly $N$ times and counters are never negative this happens at most $\lfloor N/(k+1) \rfloor$ times. □

Next, we bound the sensitivity of Algorithm 4.

**LEMMA 27.** Let $T, c \leftarrow PAMG(k, S)$ and $T', c' \leftarrow PAMG(k, S')$ be the outputs of Algorithm 4 on a pair of neighboring streams $S \sim S'$. Then it holds that either, (1) $T' \subseteq T$ and $c_i - c'_i = \{0, 1\}$ for all $i \in T$ or (2) $T \subseteq T'$ and $c'_i - c_i = \{0, 1\}$ for all $i \in T'$.

**PROOF.** We first show that the condition holds after processing the user that is only present in one of the streams. Without loss of generality consider the case where $S'$ is obtained by removing $S_i$ from $S$. Since the two cases of the lemma are symmetric the proof is the same if $S$ is obtained by removing some user $S'_i$ from $S'$. Since the algorithm is deterministic the sketches for the two streams are identical after processing $S_{i-1}$. The sketch for $S$ then processes $S_i$. After running the
loop on Lines 3-8 the count for each element in $S_i$ is 1 higher compared to the sketch for $S'$. If the condition on Line 9 evaluates to false we finished processing $S_i$ and the state corresponds to case (1) of the lemma. If the condition is true we decrement all counters by 1 and remove elements with a count of 0. The state then corresponds to case (2) of the lemma.

We now show that if one of the conditions holds before processing some user $S_j$ one of the conditions holds also after processing $S_j$. This proves the lemma by induction. Assume that case (1) of the lemma holds before processing $S_j$. The proof is identical for case (2) due to symmetry. In each iteration of the loop on lines 3-8 we increment the same counter in both sketches which does not affect the condition of case (1). As such, we only have to consider the effect of decrementing counters. If we either do not decrement the counters in any sketch or we decrement the counters in both sketches the state after processing $S_j$ still corresponds to case (1). However, if $|T| > k \ge |T'|$ we only decrement the counters in c. The state then corresponds to case (2). Note that we never decrement the counters in $c'$ without decrementing the counters in c since we have that $|T| \ge |T'|$. □

As mentioned in Section 7 it is often important in practice that we can merge sketches. A nice property of Algorithm 4 is that the sensitivity has the structure we used to bound the sensitivity of merged sketches. The PAMG sketch can be seen as a special case of the merging algorithm discussed in Section 7. The algorithm is equivalent to computing a MG sketch for each user $T_i, c_i \leftarrow \text{MG}(k, S_i)$ and merging each $T_i, c_i$ with $T, c$ to obtain a sketch for the full stream. As such, merging sketches from Algorithm 4 does not increase sensitivity.

**COROLLARY 28.** Let $(S^{(1)}, \dots, S^{(l)})$ and $(S'^{(1)}, \dots, S'^{(l)})$ denote sets of streams such that $S^{(i)} \sim S'^{(i)}$ for one $i \in [l]$ and $S^{(j)} = S'^{(j)}$ for any $j \neq i$. Let $T, c$ and $T', c'$ be the result of merging PAMG sketches computed on both sets of streams in any fixed order using the merging algorithm by [1] as discussed in Section 7. Then it holds that either, (1) $T' \subseteq T$ and $c_i - c_i' = \{0, 1\}$ for all $i \in T$ or (2) $T \subseteq T'$ and $c_i' - c_i = \{0, 1\}$ for all $i \in T'$.

**PROOF.** It holds by induction and Lemma 17. The condition required by Lemma 17 holds for sketches of any pair of neighboring streams by Lemma 27. □

**LEMMA 29.** Let $T, c$ be the result of merging PAMG sketches of size $k$ computed on a set of streams using the merging algorithm from Section 7. Let $f(x)$ be the true frequency of $x$ across all streams and let $M$ be the total number of elements across all streams. Then for all $x \in \mathcal{U}$ it holds that

$$c_x \in \left[ f(x) - \frac{M}{k+1}, f(x) \right].$$

**PROOF.** Agarwal et al. [1] show that this holds for the MG sketch in Lemma 1 and Theorem 1 of their paper. They used the properties of the MG sketch that error is only introduced when decrementing counters, and counters for $k+1$ distinct elements are decremented each time. Since at least $k+1$ counters for distinct elements are decremented in Algorithm 4 the lemma follows from their proof. □

Finally, we are ready to summarize our results for this section in the following theorem.

**THEOREM 30.** Let $(S^{(1)}, \dots, S^{(l)})$ and $(S'^{(1)}, \dots, S'^{(l)})$ denote sets of streams such that $S^{(i)} \sim S'^{(i)}$ for one $i \in [l]$ and $S^{(j)} = S'^{(j)}$ for any $j \neq i$. Let $T, c$ and $T', c'$ be the result of merging PAMG sketches of size $k$ computed on both sets of streams in any fixed order using the merging algorithm discussed in Section 7. Then releasing the output of GSHM($T, c, k, \sigma, \tau$) where $\sigma$ and $\tau$ are chosen according to Theorem 23 satisfies $(\epsilon, \delta)$-differential privacy. For any $\epsilon < 1$ we have that $\tau = O(\sqrt{k \ln(k/\delta)/\epsilon})$. Let
$\hat{f}(x)$ be the estimate of the true frequency $f(x)$ across all streams and let $M$ denote the total number of elements across all streams. Then we have for all $x \in \mathcal{U}$ with probability at least $1 - 2\delta$ that

$$ \hat{f}(x) \in \left[ f(x) - \frac{M}{k+1} - 2\tau - 1, f(x) + \tau \right]. $$

PROOF. The privacy guarantees follow directly from Corollary 28 and Theorem 23. The three sources of error are the estimation error of the non-private sketch, the noise added to each counter, and the error from removing values below the threshold. The error of the merged sketch is at most $\frac{M}{k+1}$ by Lemma 29. From the first part of the condition of Theorem 23 we have that $1 - \Phi(\frac{\tau}{\delta})^k \le \delta$. That is, the $k$ samples of Gaussian noise are all bounded by $\tau$ with probability at least $1 - \delta$. Since the noise is symmetric we can use a union bound to bound the absolute value of the noise by $\tau$ with probability at least $1 - 2\delta$. Removing noisy counters below the thresholding adds error at most $1 + \tau$. The asymptotic behavior of $\tau$ for $\varepsilon < 1$ follows from the looser bound in Lemma 24. $\square$

# 9 OPEN PROBLEM

In Section 8 we introduced a frequency sketch for a stream of users each with up to $m$ distinct elements. Our sketch has error guarantees similar to the MG sketch and the $\ell_2$-sensitivity of the sketch with size $k$ is $\sqrt{k}$. The main open problem that we leave is if there exists a sketch with similar error guarantees and $\ell_2$-sensitivity of $\sqrt{m}$ or $O(\sqrt{m})$. This would allow us to add noise to the sketch with magnitude matching the non-streaming setting. More specifically, we ideally want a sketch with all the properties listed below for the setting of Section 8.

OPEN PROBLEM. Is it possible to design a sketch with the following properties (1) the sketch uses $O(k)$ space (2) the sketch (non-private) returns a frequency oracle with error at most $N/(k+1)$ for any stream (3) there exists an $(\varepsilon, \delta)$-DP mechanism that releases the sketch with error $O(\sqrt{m}\log(1/\delta)/\varepsilon)$?

The $\ell_2$-sensitivity of our sketch exceeds $O(\sqrt{m})$ because the number of elements that are decremented when the sketch is full can differ between neighboring sketches. Any counter-based sketch with low sensitivity likely must ensure that the number of decremented counters remains stable. During this project we experimented with variants that always decremented a fixed number of elements when full. Unfortunately, those attempts yielded higher sensitivity that the PAMG as the sketches did not have the property that all counts differ by at most 1 between neighboring streams.

# ACKNOWLEDGMENT

This work was supported by the VILLUM Foundation grant 16582. We thank Rasmus Pagh for suggesting this problem and for helpful discussions. We thank Martin Aumüller for his valuable feedback. Finally, we thank anonymous reviewers for suggestions that improved presentation.

# REFERENCES

[1] P. K. Agarwal, G. Cormode, Z. Huang, J. M. Phillips, Z. Wei, and K. Yi. Mergeable summaries. *ACM Trans. Database Syst.*, 38(4), dec 2013.

[2] Apple. Differential privacy technical overview. Retrieved March 6, 2025 from https://www.apple.com/privacy/docs/Differential_Privacy_Overview.pdf.

[3] M. Aumüller, C. J. Lebeda, and R. Pagh. Representing sparse vectors with differential privacy, low error, optimal space, and fast access. *Journal of Privacy and Confidentiality*, 12(2), Nov. 2022.

[4] V. Balcer and S. Vadhan. Differential privacy on finite computers. *Journal of Privacy and Confidentiality*, 9(2), Sep. 2019.

[5] R. Bassily, K. Nissim, U. Stemmer, and A. Guha Thakurta. Practical locally private heavy hitters. *Advances in Neural Information Processing Systems*, 30, 2017.

[6] J. Blocki, E. Grigorescu, T. Mukherjee, and S. Zhou. How to make your approximation algorithm private: A black-box differentially-private transformation for tunable approximation algorithms of functions with low sensitivity. *arXiv preprint arXiv:2210.03831*, 2022.
[7] J. Böhler and F. Kerschbaum. Secure multi-party computation of differentially private heavy hitters. In *Y. Kim, J. Kim, G. Vigna, and E. Shi, editors, CCS '21: 2021 ACM SIGSAC Conference on Computer and Communications Security, Virtual Event, Republic of Korea, November 15 - 19, 2021*, pages 2361-2377. ACM, 2021.

[8] P. Bose, E. Kranakis, P. Morin, and Y. Tang. Bounds for frequency estimation of packet streams. In *J. F. Sibeyn, editor, SIROCCO 10: Proceedings of the 10th International Colloquium on Structural Information Complexity, June 18-20, 2003, Umeå Sweden*, volume 17 of *Proceedings in Informatics*, pages 33-42. Carleton Scientific, 2003.

[9] M. Bun, J. Nelson, and U. Stemmer. Heavy hitters and the structure of local privacy. *ACM Transactions on Algorithms (TALG)*, 15(4):1-40, 2019.

[10] R. S. Carvalho, K. Wang, L. Gondara, and C. Miao. Differentially private top-k selection via stability on unknown domain. In *Conference on Uncertainty in Artificial Intelligence*, pages 1109-1118. PMLR, 2020.

[11] T.-H. H. Chan, M. Li, E. Shi, and W. Xu. Differentially private continual monitoring of heavy hitters from distributed streams. In *International Symposium on Privacy Enhancing Technologies Symposium*, pages 140-159. Springer, 2012.

[12] G. Cormode, C. M. Procopioc, D. Srivastava, and T. T. L. Tran. Differentially private summaries for sparse data. In *ICDT*, pages 299-311. ACM, 2012.

[13] D. Durfee and R. M. Rogers. Practical differentially private top-k selection with pay-what-you-get composition. *Advances in Neural Information Processing Systems*, 32, 2019.

[14] C. Dwork, F. McSherry, K. Nissim, and A. Smith. Calibrating noise to sensitivity in private data analysis. In *Theory of Cryptography: Third Theory of Cryptography Conference, TCC 2006, New York, NY, USA, March 4-7, 2006. Proceedings 3*, pages 265-284. Springer, 2006.

[15] C. Dwork and A. Roth. The algorithmic foundations of differential privacy. *Foundations and Trends in Theoretical Computer Science*, 9(3-4):211-407, 2014.

[16] Ü. Erlingsson, V. Pihur, and A. Korolova. Rappor: Randomized aggregatable privacy-preserving ordinal response. In *Proceedings of the 2014 ACM SIGSAC conference on computer and communications security*, pages 1054-1067, 2014.

[17] Q. Geng, P. Kairouz, S. Oh, and P. Viswanath. The staircase mechanism in differential privacy. *IEEE Journal of Selected Topics in Signal Processing*, 9(7):1176-1184, 2015.

[18] B. Ghazi, N. Golowich, R. Kumar, R. Pagh, and A. Velingker. On the power of multiple anonymous messages. *IACR Cryptol. ePrint Arch.*, page 1382, 2019.

[19] A. Ghosh, T. Roughgarden, and M. Sundararajan. Universally utility-maximizing privacy mechanisms. *SIAM Journal on Computing*, 41(6):1673-1693, 2012.

[20] Google Anonymization Team. Delta for thresholding, 2020. Retrieved March 6, 2025 from https://github.com/google/differential-privacy/blob/main/common_docs/Delta_From_Thresholding.pdf.

[21] S. Haney, D. Desfontaines, L. Hartman, R. Shrestha, and M. Hay. Precision-based attacks and interval refining: how to break, then fix, differential privacy on finite computers. *CoRR*, abs/2207.13793, 2022.

[22] A. Korolova, K. Kenthapadi, N. Mishra, and A. Ntoulas. Releasing search queries and clicks privately. In *WWW*, pages 171-180. ACM, 2009.

[23] D. J. Mir, S. Muthukrishnan, A. Nikolov, and R. N. Wright. Pan-private algorithms via statistics on sketches. In *M. Lenzerini and T. Schwentick, editors, Proceedings of the 30th ACM SIGMOD-SIGACT-SIGART Symposium on Principles of Database Systems, PODS 2011, June 12-16, 2011, Athens, Greece*, pages 37-48. ACM, 2011.

[24] J. Misra and D. Gries. Finding repeated elements. *Science of Computer Programming*, 2(2):143-152, 1982.

[25] R. Pagh and M. Thorup. Improved utility analysis of private countsketch. In *Advances in Neural Information Processing Systems*, volume 35, pages 25631-25643, 2022.

[26] G. Qiao, W. Su, and L. Zhang. Oneshot differentially private top-k selection. In *International Conference on Machine Learning*, pages 8672-8681. PMLR, 2021.

[27] Z. Qin, Y. Yang, T. Yu, I. Khalil, X. Xiao, and K. Ren. Heavy hitter estimation over set-valued data with local differential privacy. In *Proceedings of the 2016 ACM SIGSAC Conference on Computer and Communications Security*, pages 192-203, 2016.

[28] J. Tetek. Additive noise mechanisms for making randomized approximation algorithms differentially private. In *APPROX/RANDOM*, volume 317 of *LIPICS*, pages 73:1-73:20. Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 2024.

[29] T. Wang, N. Li, and S. Jha. Locally differentially private heavy hitter identification. *IEEE Transactions on Dependable and Secure Computing*, 18(2):982-993, 2019.

[30] A. Wilkins, D. Kifer, D. Zhang, and B. Karrer. Exact privacy analysis of the gaussian sparse histogram mechanism. *Journal of Privacy and Confidentiality*, 14(1), Feb. 2024.

[31] H. Wu and A. Wirth. Asymptotically optimal locally private heavy hitters via parameterized sketches. In *International Conference on Artificial Intelligence and Statistics*, pages 7766-7798. PMLR, 2022.

[32] D. Zhao, S. Zhao, H. Chen, R. Liu, C. Li, and W. Liang. Efficient protocols for heavy hitter identification with local differential privacy. *Frontiers of Computer Science*, 16(5):1-11, 2022.
[33] F. Zhao, D. Qiao, R. Redberg, D. Agrawal, A. El Abbadi, and Y.-X. Wang. Differentially private linear sketches: Efficient implementations and applications. In *Advances in Neural Information Processing Systems*, volume 35, pages 12691–12704, 2022.

[34] W. Zhu, P. Kairouz, B. McMahan, H. Sun, and W. Li. Federated heavy hitters discovery with differential privacy. In *International Conference on Artificial Intelligence and Statistics*, pages 3837–3847. PMLR, 2020.