When the algorithm flips a coin

The independence an estimator spends

Every sketch's analysis begins by assuming a truly random hash, and nobody comes back to that line. Independence has a degree, the degree is enumerable over a small field, and an estimator's mean and its variance spend different amounts of it.

Every analysis in this field opens the same way. Let hh be a hash function chosen uniformly at random from all functions U[w]U \to [w]. Then the proof proceeds, the bound comes out, and nobody returns to the first line.

The first line is false. A function chosen uniformly from all functions on a universe of 2322^{32} keys takes 232log2w2^{32} \cdot \log_2 w bits to write down — for w=64w = 64, twenty-five gigabytes — and a summary that needed one would be a summary that had already lost. What is used instead is a family small enough to store, and the entire question is how much of the proof survives the substitution.

The answer is a number, it is a property of the family, and this essay measures it.

The degree of a polynomial family over GF(13), enumeratedEvery member of each family is walked — 28,561 of them for the largest — and for j keys at a time the 13^j tuples of values they produce are tabulated. j-wise independence is the statement that every tuple occurs exactly the same number of times, so a cell marked exact is a fact with no tolerance in it. Beyond the degree the failure is not a bias: whole tuples are produced by no member at all, because a polynomial through k points is unique and the value at the next point is decided. The percentage in each cell is how many of the tuples are unreachable.1 coefficient13 members2 coefficients169 members3 coefficients2,197 members4 coefficients28,561 members1 key2 keys3 keys4 keys5 keysexact92% gone99% gone100% gone100% goneexactexact92% gone99% gone100% goneexactexactexact92% gone99% goneexactexactexactexact92% goneevery member walked · GF(13) · no tolerance and no seeddegree 1, 2, 3, 4
Fig. 1 The property, enumerated. Each row is a family of polynomials over a thirteen-element field, each column a number of keys, and a cell says whether the tuples of values those keys receive are uniform over every member of the family. Beyond the degree the failure is total rather than partial: 92.3% of the possible tuples are produced by no member of the family at all.

What the degree is

A family H\mathcal{H} is jj-wise independent if, for any jj distinct keys x1,,xjx_1, \dots, x_j and any jj values v1,,vjv_1, \dots, v_j, a uniformly drawn hHh \in \mathcal{H} satisfies h(xi)=vih(x_i) = v_i for all ii with probability exactly wjw^{-j}.

Two readings, and the second is the useful one.

The first: jj keys’ hash values look like jj independent uniform draws. The second: knowing j1j-1 of them says nothing about the jj-th. For a family of degree exactly jj, knowing jj of them decides the j+1j+1-th entirely, which is why the failure is so abrupt.

The standard construction is a polynomial. Take a prime pp, draw a0,,aj1a_0, \dots, a_{j-1} uniformly from Zp\mathbb{Z}_p, and set h(x)=(iaiximodp)modwh(x) = (\sum_i a_i x^i \bmod p) \bmod w. It is jj-wise independent because a polynomial of degree j1j-1 is determined by its values at jj points and there is exactly one such polynomial for each choice of those values — so the map from coefficients to value tuples is a bijection. And it stops there for the same reason: given jj points the polynomial is pinned, so the j+1j+1-th value is a function of the others rather than a free choice.

Enumerated, not estimated

Over Z13\mathbb{Z}_{13} a family of kk coefficients has 13k13^k members. For k=4k = 4 that is 28,561 — few enough to walk every one of them and tabulate what it does.

That is what the figure above is. For jj keys the 13j13^j tuples of values are counted across all members, and jj-wise independence is the statement that every count is exactly 13kj13^{k-j}. There is no tolerance in it, no seed, and no statistic: a cell is right or it is not.

The failures beyond the degree have the same number in every row, and the number is worth stating: exactly (p1)/p=92.3%(p-1)/p = 92.3\% of the tuples are unreachable. For j=k+1j = k+1 the first kk values determine the last one, so of the pp possible values for the final key, exactly one occurs and p1p-1 do not. At j=k+2j = k+2 it is (p21)/p2=99.4%(p^2-1)/p^2 = 99.4\%, and so on.

That is a fact about a family over a thirteen-element field, established by exhaustion. Everything below is about families over 23112^{31} - 1, where exhaustion is not available and the honest description of what can be measured is a good deal weaker.

The degree of a polynomial family over GF(13), enumeratedEvery member of each family is walked — 2,197 of them for the largest — and for j keys at a time the 13^j tuples of values they produce are tabulated. j-wise independence is the statement that every tuple occurs exactly the same number of times, so a cell marked exact is a fact with no tolerance in it. Beyond the degree the failure is not a bias: whole tuples are produced by no member at all, because a polynomial through k points is unique and the value at the next point is decided. The percentage in each cell is how many of the tuples are unreachable.2 coefficients169 members3 coefficients2,197 members1 key2 keys3 keys4 keysexactexact92% gone99% goneexactexactexact92% goneevery member walked · GF(13) · no tolerance and no seeddegree 2, 3
Fig. 2 The same enumeration restricted to the two families the estimators here actually use in miniature — degree two, which is Carter and Wegman’s, and degree three. The step from one to the other is one more coefficient, one more multiplication per evaluation, and the entire difference between an estimator with a variance bound and one without.
The degree of a polynomial family over GF(11), enumeratedEvery member of each family is walked — 14,641 of them for the largest — and for j keys at a time the 11^j tuples of values they produce are tabulated. j-wise independence is the statement that every tuple occurs exactly the same number of times, so a cell marked exact is a fact with no tolerance in it. Beyond the degree the failure is not a bias: whole tuples are produced by no member at all, because a polynomial through k points is unique and the value at the next point is decided. The percentage in each cell is how many of the tuples are unreachable.3 coefficients1,331 members4 coefficients14,641 members1 key2 keys3 keys4 keys5 keysexactexactexact91% gone99% goneexactexactexactexact91% goneevery member walked · GF(11) · no tolerance and no seeddegree 3, 4
Fig. 3 The same enumeration over an eleven-element field rather than a thirteen-element one. The unreachable fraction beyond the degree is (p1)/p(p-1)/p again — 90.9% instead of 92.3% — which is the clearest statement that the failure is about the polynomial being pinned rather than about any particular prime.

Where the estimator spends it

The tug-of-war estimator is one register holding Z=xs(x)fxZ = \sum_x s(x) f_x, reported as Z2Z^2. Expanding the square gives

Z2=xfx2+xys(x)s(y)fxfy.Z^2 = \sum_x f_x^2 + \sum_{x \neq y} s(x)s(y)\, f_x f_y.

The expectation of every term in the second sum is E[s(x)s(y)]\mathbb{E}[s(x)s(y)] times a constant, and that is zero as soon as two distinct keys’ signs are independent. So the mean is exactly F2F_2 at degree two, and no more is needed for it.

The variance is E[Z4]F22\mathbb{E}[Z^4] - F_2^2, and Z4Z^4 expands into products of four signs. Terms like s(x)s(y)s(z)s(t)s(x)s(y)s(z)s(t) for four distinct keys must vanish, and terms like s(x)2s(y)2s(x)^2 s(y)^2 must be one; neither is controlled by pairwise independence. The bound Var[Z2]2F22\mathrm{Var}[Z^2] \le 2F_2^2 is a theorem about a four-wise family, and against a merely pairwise one there is no bound at all.

So the two halves of the same analysis buy different amounts of the same thing, and the phrase “assume a random hash” conceals both purchases.

What it is worth, measured

Two hundred draws each, sixteen registers, the same 20,000-item stream, everything identical but where the signs come from:

signs mean error rms error worst run
a polynomial of degree 4 −1.2% 21.9% 60%
Carter–Wegman, degree 2 −2.1% 39.0% 183%
multiply-shift +10.9% 38.3% 168%
a fixed function of the key −68.4% 68.4% 68%

The first two rows are the measurement this essay exists for. Both estimators are centred — the pairwise one’s mean is inside its own standard error of zero — and the pairwise one’s spread is 1.78 times larger, with a worst run three times as bad. The theory says the mean survives and the variance does not; the measurement says the mean survived and the variance grew by 78%.

The third row is a family with no pairwise independence at all, and it is biased: 10.9% high against a standard error of 2.6%, which is four standard errors. The fourth row is a fixed function rather than a family, and it is not an estimator in any sense — every run returns the same wrong number, and the spread of zero would look like extraordinary precision to anybody measuring only the spread.

Relative error of the second moment against bits of stateEach point is 16 independent runs of the tug-of-war estimator at one state size, against an exact F2 of 153,983,342 computed by a hash map the estimator never sees. The only difference between the series is where the ±1 signs come from: a polynomial of degree 4 (stated degree 4), Carter–Wegman, degree 2 (stated degree 2), multiply-shift (stated degree 1). The mean of the estimator is right as soon as two distinct keys' signs are independent; the variance is a statement about four at a time, which is what the separation between these lines is. Both axes are logarithmic. Model: cash register, one pass, no deletions.10,0000.1bits of state heldrelative error, rms over runsdegree 4degree 2multiply-shiftF2 = 153,983,342 exactly · 16 runs per point · cash-register model4.9% / 8.4% / 17.8% at 20,480 bits
Fig. 4 The same comparison across state sizes. The four-wise family’s error falls at the −½ slope that averaging independent registers produces. The pairwise one falls too, more slowly. The family with no independence stops falling: from 22.6% at 1,280 bits to 17.0% at 20,480, which is sixteen times the state for a fifth of the improvement.

The plateau is the finding

That last line deserves its own heading, because it is the practical form of everything above.

Averaging ss estimators divides the variance by ss when the estimators are independent of each other and each has a bounded variance. A family too weak to bound the variance of one register does not stop the estimator from working; it stops the extra registers from buying anything. The sketch continues to return numbers, they continue to be roughly right, and the money spent on state stops converting into accuracy.

At 640 registers the weak family is at 17.0% — worse than the four-wise family at forty registers, which is 17.5% for a sixteenth of the state. A practitioner who measured accuracy at one size, found it acceptable, and then scaled the structure up would find the second half of that budget had bought nothing, with nothing in the output to say why.

What a degree costs to run

A degree is not free and its price is easy to state exactly, which is unusual for anything in this essay.

Storage. kk coefficients of log2p\lceil \log_2 p \rceil bits. At p=2311p = 2^{31}-1 that is 31 bits each, so a four-wise family is 124 bits against a pairwise family’s 62. Beside a sketch holding twenty thousand bits it is nothing, and beside a five-bit register bank it is not.

Arithmetic. Evaluating a polynomial of degree k1k-1 by Horner’s rule is k1k-1 multiplications and k1k-1 additions, each modulo pp. Doubling the degree doubles the work per key per hash, and the previous section’s accounting — 640 hash evaluations per arrival at the largest setting — is multiplied by it.

There is one piece of arithmetic here that is worth naming because getting it wrong is invisible. Multiplying two numbers below 2312^{31} produces up to sixty-two bits, and a double carries fifty-three. The obvious expression (a * x) % p therefore returns the modulus of a rounded product, which is uniform-looking garbage — and a family built on it distributes keys perfectly well, passes the generic meter above, and has no independence of any degree. The multiplication is split so that it stays inside a double, and it is shared with the hashing library rather than written twice, because a second body for it would be a second chance to get it wrong.

Why not simply use a cryptographic hash

The obvious escape is to stop reasoning about degrees and use a function that behaves randomly for every purpose anybody has tried — SipHash, or a truncated SHA-256.

It is a real answer and it is what most deployed sketches do. Two things are worth saying about it, and neither is that it is wrong.

It replaces a proof with an assumption that is much harder to state. “This family is four-wise independent” is a theorem with a two-line proof. “No adversary bounded by such-and-such resources can distinguish this function from random” is a cryptographic assumption, and the accuracy of a sketch built on it is a corollary of that assumption rather than of anything provable about the structure. For a summary whose whole appeal is a stated guarantee, that is a real trade rather than a free upgrade.

It costs more per evaluation than the property being bought. A SipHash round is several times the arithmetic of a degree-four polynomial, and the sketch above evaluates its hash hundreds of times per arrival. Where the required degree is known — and for this estimator it is exactly four — buying more than that is buying something the analysis cannot use.

The recommendation this site will make is the one it makes everywhere: state which property the argument consumes, and then buy that property. The estimator needs four-wise independence for its variance and two for its mean, both of those are theorems, and a family of four coefficients supplies both for 124 bits.

Measuring a family that cannot be enumerated

The families that actually run are over p=2311p = 2^{31} - 1 with up to 21242^{124} members. Enumeration is not available, and what is left is sampling: draw the family many times, tabulate the sign patterns of jj fixed keys, and compare the counts with uniformity by a chi-square statistic.

It works, in the sense of producing a number. It does not work, in the sense of establishing anything.

The sampled meter on generic probe keys: χ² per degree of freedomEach family is drawn 6,000 times and the sign patterns of j fixed keys are tabulated; the number in each cell is the chi-square statistic divided by its degrees of freedom, which is one when the patterns are uniform. A shaded cell is a failure. The probe keys are 3, 1,009, 65,537, 7,919… — six unrelated numbers, chosen without reference to any family. This meter can only ever report finding nothing, which is not the same as independence: the enumerated plate is what a fact looks like by comparison.the low bit of the keystated 0multiply-shiftstated 1Carter–Wegman, degree 2stated 2a polynomial of degree 3stated 3a polynomial of degree 4stated 41 key2 keys3 keys4 keys5 keys6e+36e+36e+36e+36e+32.70.50.20.61.45.40.51.51.31.60.30.41.71.01.55.52.41.61.40.96,000 draws · generic probe keys · χ² per degree of freedom0, 5, 5, 5, 5
Fig. 5 The sampled meter on six unrelated probe keys. Every family passes at every number of keys — including the one with no independence of any kind and including one whose degree is provably two. The statistic sits between 0.2 and 5.5 where uniformity predicts 1, which is exactly what six thousand draws of a uniform table look like.

The trouble is that independence is a property of a family over all tuples of keys, and a meter only ever sees the tuple it was handed. Six keys chosen without reference to the family are overwhelmingly likely to be six keys on which the family behaves.

Choose them with reference to the family instead:

The sampled meter on structured probe keys: χ² per degree of freedomEach family is drawn 6,000 times and the sign patterns of j fixed keys are tabulated; the number in each cell is the chi-square statistic divided by its degrees of freedom, which is one when the patterns are uniform. A shaded cell is a failure. The probe keys are 12,345, 2,147,495,993, 99,991, 2,147,583,639… — each key and the same key with its top bit flipped, which is the arithmetic these families are built from. This meter can only ever report finding nothing, which is not the same as independence: the enumerated plate is what a fact looks like by comparison.the low bit of the keystated 0multiply-shiftstated 1Carter–Wegman, degree 2stated 2a polynomial of degree 3stated 3a polynomial of degree 4stated 41 key2 keys3 keys4 keys5 keys6e+36e+36e+36e+36e+30.12e+39e+21e+36e+20.70.61.548.222.20.51.00.90.91.10.01.60.40.80.76,000 draws · structured probe keys · χ² per degree of freedom0, 1, 3, 5, 5
Fig. 6 The same meter, the same families, the same number of draws — and probe keys built from the arithmetic the families use: each key paired with the same key with its top bit flipped. Multiply-shift’s statistic goes from 0.5 to two thousand at two keys, which is what a deterministic relationship looks like when it is being tested for independence. For a 32-bit multiply-shift the top bit of a(x+231)a \cdot (x + 2^{31}) is the complement of the top bit of axa \cdot x whenever aa is odd, and aa is always odd.
The sampled meter on neighbours probe keys: χ² per degree of freedomEach family is drawn 6,000 times and the sign patterns of j fixed keys are tabulated; the number in each cell is the chi-square statistic divided by its degrees of freedom, which is one when the patterns are uniform. A shaded cell is a failure. The probe keys are 1, 2, 3, 4… — six consecutive small integers. This meter can only ever report finding nothing, which is not the same as independence: the enumerated plate is what a fact looks like by comparison.the low bit of the keystated 0multiply-shiftstated 1Carter–Wegman, degree 2stated 2a polynomial of degree 3stated 3a polynomial of degree 4stated 41 key2 keys3 keys4 keys5 keys6e+36e+36e+36e+36e+30.42.186.32e+22e+20.81.21.043.564.10.10.31.10.92.24.80.30.70.51.36,000 draws · neighbours probe keys · χ² per degree of freedom0, 2, 3, 4, 5
Fig. 7 And a third set: six consecutive small integers. Multiply-shift fails here too, at three keys rather than two, and the Carter–Wegman family fails at four. The three probe sets disagree about every family, which is the whole difficulty stated as a picture.

What the meter can and cannot say

Collecting the three sets:

family stated generic structured neighbours
a fixed function 0 0 0 0
multiply-shift 1 5 1 2
Carter–Wegman 2 5 3 3
degree 3 3 5 5 4
degree 4 4 5 5 5

Three things this table says, and one it does not.

A sampled meter reports finding nothing, which is not the same as independence. Every entry in the generic column is a five, and three of those families are not five-wise independent. The correct reading of a passing chi-square is “these six thousand draws on these six keys gave no evidence against”, and that sentence is nearly worthless on its own.

A meter with well-chosen probes catches the family that fails hardest. Multiply-shift is caught at two keys by a factor of two thousand, not by a marginal statistic. When a family has a deterministic relationship in it, the right probe finds it decisively.

And it misses the family that fails by the least. Carter–Wegman is exactly two-wise independent and no meter here reports 2 for it; the best any probe set manages is 3. Its three-key failure is a bias in the parity of a linear function over a large prime, which is real, is provable, and is far too small to see in six thousand draws.

What the table does not say is that any family here is fit for any purpose. That is decided by which moments of which estimator the family has to support, and by nothing about the family alone.

Which degree each structure in this collection needs

The tug-of-war estimator spends two on its mean and four on its variance, and that pattern is not shared by its neighbours. Collecting the requirements makes the point that a degree is bought per argument rather than per field.

Count-Min needs two. Its guarantee is an expectation followed by Markov’s inequality: the expected collision mass in a cell is εf1\varepsilon\lVert f\rVert_1 provided any two distinct keys land independently, and the failure probability comes from repeating that over dd rows rather than from any higher moment. Pairwise is the whole purchase, which is why a Count-Min sketch built on Carter and Wegman’s family is on exactly as firm ground as one built on anything stronger.

Count-Sketch needs four, for the same reason the tug-of-war estimator does and in the same place. Its cell is a signed sum, its analysis bounds a variance, and a variance of a signed sum is a statement about products of four hash values.

A quantile summary needs none. Greenwald–Khanna has no randomness in it at all, so the column is empty rather than small — which is the trade the model essay in this collection draws out, and it is visible here as a structure that cannot spend a resource because it never had a use for one.

And a cardinality estimator’s requirement is not known to be a degree. The standard analysis of HyperLogLog assumes a fully random function, and the register a key lands in and the run of zeros it contributes are both derived from the same hash value — so the argument is about the whole distribution of the hash rather than about any fixed number of keys at a time. Families with proven guarantees for it exist and are not the polynomials above.

Two things follow from reading that list rather than any one row of it.

The requirement is a property of the proof, not of the structure. “This estimator needs degree four” is shorthand for “the argument used here needs degree four”, and a sharper argument can lower the number without changing a line of the code. That is not hypothetical — several results in this literature are exactly that, a known structure re-analysed under a weaker family. So the honest form of a requirement names the analysis it comes from.

And the requirement cannot be discovered by measurement. Nothing in the plates above would distinguish a structure that needs four from one that needs two; both look fine on a good family and both look plausible on a bad one until the state is scaled. The degree comes out of reading the proof and asking which moment it bounds, which is a paper exercise, and it is the only route to the number.

And one property that is not a degree at all

The title of this essay says independence has a degree, and that is true of the property the polynomials supply. It is not true of every property an estimator can need, and the counterexample is in this collection already.

A bottom-kk sketch keeps the kk smallest hash values in the stream and estimates the cardinality from them. What its analysis requires is that each key be equally likely to be the minimum of any given set — that for any set SS and any xSx \in S, the probability that h(x)h(x) is the smallest value in h(S)h(S) is 1/S1/|S|. That is min-wise independence, and it is a statement about the ordering the hash induces rather than about the joint distribution of any fixed number of values.

The two properties do not contain one another in the way a reader would expect. A family can be jj-wise independent for a respectable jj and still be a poor min-wise family on sets larger than jj, because a minimum over a large set is a question about all of its elements at once. Exact min-wise families are known to be expensive; the practical answer is an approximate version, where the probability is 1/S1/|S| to within a factor, and the sketch’s error inherits that factor.

So “assume a random hash” hides at least two different purchases, and knowing that one of them is a small integer says nothing about the other. The general form of the essay’s rule survives and gets longer: state which property the argument consumes, buy that property, and check that it is the property and not merely the nearest one with a number attached.

The rule this leaves

The degree of a family is a design parameter, chosen the way a word size or a block size is chosen, and it costs something exact: one more coefficient of storage and one more multiplication per evaluation per unit of degree. Four-wise signs cost twice the arithmetic of pairwise ones and, on the evidence above, are worth it for a variance and unnecessary for a mean.

The failure mode to carry away is the one the plateau shows. A hash that is too weak does not produce obviously wrong answers, and it does not produce error bars that widen honestly. It produces answers that are approximately right and a structure whose accuracy stops responding to its size — which is exactly the symptom a practitioner is least likely to attribute to the hash, and most likely to attribute to the data.

2,048 keys in 256 buckets — multiply–shift, a chosen at randomEach bar is one bucket's load, with keys computed from this hash function. The average load is 8.0 and the worst bucket here holds 2,048. The multiplier was drawn at random from the family, so an adversary who does not know it cannot compute a colliding set. The collision rate over all pairs is 1.00e+0, against 3.91e-3 for a perfectly uniform map.average 8.001,0242,048bucket, 0 to 255keys in the bucketmultiply–shift · keys built for this hashworst bucket 2,048 against 8.0
Fig. 8 The same family failing in the way this site has already recorded, one field back: keys chosen against the hash pile into a few buckets. That essay was about an adversary with the seed and this one is about no adversary at all — the keys in the structured probe set were not chosen to attack anything, only to be related to each other, and that was enough.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

Every essay whose body links to this one.

The objects this essay names

Each one links to every other essay that touches it.

Adversarial inputEstimatorFalsificationHash familyHonest limitIndependence assumptionk-wise independenceRandom bitsSecond frequency momentSeeded randomnessTug-of-warUnbiased estimatorUniversal hashingVariance