Back to Streaming Sketching

AMS F2 Estimation

September 28, 2026 Wasil Zafar 18 min read

Build the canonical second-moment linear sketch.

Table of Contents

  1. Estimating Collision Energy with Random Signs
  2. The AMS Projection and Unbiased Square
  3. Fourth Moments, Variance and Repetition
  4. Detecting Skew in Network and Event Streams
  5. Simulate and Verify the AMS Estimator

Estimating Collision Energy with Random Signs

Suppose a network sees flows with frequencies \(f=(3,2,1)\). The first frequency moment \(F_1=\sum_i|f_i|=6\) measures total mass, while the second moment \[ F_2=\sum_i f_i^2=3^2+2^2+1^2=14 \] emphasizes concentration. If the same six events belonged to one key, \(F_2\) would be \(36\). This sensitivity makes \(F_2\), sometimes called collision energy or the squared \(\ell_2\) norm, useful for detecting skew.

The AMS idea compresses the whole vector to one signed number. Give every key a stable random sign \(s(i)\in\{-1,+1\}\), and maintain \[ Z=\sum_i s(i)f_i. \] For an update \((i,\Delta)\), perform \(Z\leftarrow Z+s(i)\Delta\). At query time return \(X=Z^2\). Positive and negative cross-key interactions cancel in expectation, leaving the sum of squares.

As an analogy, imagine placing each key's frequency on one of two pans according to a random sign. The signed imbalance \(Z\) depends strongly on the assignment, but its square contains information about total squared weight. One weighing is noisy; many independent weighings, averaged in groups and then combined by a median, become reliable.

This deep dive builds on frequency moments and norms by supplying a streaming construction for \(F_2\). A Rademacher sign is equally likely to be \(+1\) or \(-1\). The sketch is an inner product \(Z=\langle s,f\rangle\). Its square is unbiased when \(\mathbb E[Z^2]=F_2\). A fourth moment such as \(\mathbb E[Z^4]\) controls \(\operatorname{Var}(Z^2)\). Repetition means independent rows, not repeated queries to an unchanged row.

The canonical sketch is linear, so insertion-only and turnstile updates both work: negative deltas simply subtract signed mass. The boundary is randomness quality. Pairwise-independent signs are enough for unbiasedness, but the standard variance proof needs four-wise independence. A deterministic all-\(+1\) sign map estimates \((\sum_i f_i)^2\), not \(F_2\).

From a random balance scale to the AMS estimator Key frequencies receive positive or negative signs, accumulate into a signed projection, and are squared and repeated to estimate the second moment. Frequency vectorA:3 gets +B:2 gets −C:1 gets +stable sign per key Signed projectionZ = 3 − 2 + 1 = 2one counter updateX = Z² = 4one trial can be far from 14 Amplified estimateaverage independent Xmedian of groupstarget: F₂ = 14relative error with confidence input massrandom projectionreliable aggregate
ConceptIntuitive meaningExact exampleNon-example or boundaryMain assumptionQuestion answered
\(F_2\)Squared concentration of frequencies\((3,2,1)\) gives \(14\)\(F_1=6\)Frequencies share one key universeHow concentrated is the stream?
Rademacher signFair random \(+1\) or \(-1\) per key\(s(A)=+1,s(B)=-1\)All signs \(+1\)Stable sign within a rowHow are cross terms randomized?
Inner productSigned projection of the vector\(Z=\langle s,f\rangle=2\)Summing unsigned frequenciesSame sign map for all updates of a keyWhat does one AMS row store?
Unbiased squareCorrect average over sign randomness\(\mathbb E[Z^2]=F_2\)One observed square equals truthPairwise-independent mean-zero signsIs the estimator centered correctly?
Fourth momentExpected fourth power of projection\(\mathbb E[Z^4]\le3F_2^2\)Squaring the expected projectionFour-wise-independent signsHow large can variance be?
RepetitionIndependent projections and aggregationAverage \(r\), median across \(g\) groupsQuerying one row repeatedlyIndependent seeds between rows/groupsHow are error and failure reduced?
Turnstile updateSigned change to one coordinate\((A,-2)\) changes \(Z\) by \(-2s(A)\)Rehashing the same key after deletionStable encoding and sign familyCan deletions be supported linearly?
Warm-up

Estimating Collision Energy with Random Signs

Let \(f=(2,1)\), so exact \(F_2=5\). Enumerate the four equally likely sign pairs.

  1. Signs \((+,+)\) give \(Z=3\), \(Z^2=9\).
  2. Signs \((+,-)\) give \(Z=1\), \(Z^2=1\).
  3. Signs \((-,+)\) give \(Z=-1\), \(Z^2=1\).
  4. Signs \((-,-)\) give \(Z=-3\), \(Z^2=9\).
  5. Their average is \((9+1+1+9)/4=5=F_2\).

Checkpoint: individual values are noisy but never “corrected” after observation. Reflection: why would choosing the best-looking trial after seeing the exact answer destroy the estimator's interpretation?

The AMS Projection and Unbiased Square

Let \(f\in\mathbb R^u\) be the current frequency vector. Choose a sign function \(s:U\to\{-1,+1\}\) with \(\mathbb E[s(i)]=0\) and pairwise-independent values. Maintain \[ Z=\sum_i s(i)f_i. \] Because the sketch is linear, update \((i,\Delta)\) costs one sign evaluation and one addition. Squaring gives \[ Z^2=\sum_i s(i)^2f_i^2+2\sum_{i\lt j}s(i)s(j)f_if_j. \] Since \(s(i)^2=1\), the diagonal sum is exactly \(F_2\). Pairwise independence and zero means give \(\mathbb E[s(i)s(j)]=0\) for \(i\ne j\). Therefore \[ \mathbb E[Z^2]=\sum_i f_i^2=F_2. \]

This proof works for negative frequencies because \(f_i f_j\) can be signed while the sign expectation still kills every cross term. It also exposes two implementation obligations: every occurrence of the same canonical key in a row must receive the same sign, and distinct rows must use independent seeds. Assigning a fresh sign per event would estimate event-level noise rather than the frequency vector.

Unbiasedness alone is insufficient. A random variable can have the correct mean and enormous variance. To control \(\operatorname{Var}(Z^2)\), expand \(Z^4\). With four-wise-independent Rademacher signs, only index patterns in which every sign appears an even number of times survive expectation: \[ \mathbb E[Z^4] =\sum_i f_i^4+6\sum_{i\lt j}f_i^2f_j^2 =3F_2^2-2F_4 \le3F_2^2. \] Thus \[ \operatorname{Var}(Z^2)=\mathbb E[Z^4]-F_2^2\le2F_2^2. \]

A counterexample clarifies the independence boundary. Let one random bit \(r\) define \(s(i)=r\) for every key. Each individual sign is fair, but signs are perfectly correlated. Then \(Z^2=(\sum_i f_i)^2\), and cross terms do not vanish. Pairwise independence fixes unbiasedness; four-wise independence supports the stated fourth-moment bound. Full independence is sufficient but not necessary.

AMS F2 Estimation: The AMS Projection and Unbiased Square
flowchart TD
    A[Frequency vector f and updates] --> B[Stable Rademacher sign per key]
    B --> C[Linear projection Z equals sum of sign times frequency]
    C --> D[Expand Z squared]
    D --> E[Diagonal terms equal F2]
    D --> F[Pairwise independence cancels cross terms]
    E --> G[Expected square equals F2]
    F --> G
    G --> H[Expand fourth moment]
    H --> I[Four-wise independence removes odd patterns]
    I --> J[Variance at most 2 times F2 squared]
    J --> K[Average rows then median groups]
    B --> L{Signs correlated or change by event?}
    L -->|yes| M[Bias or variance proof fails]
    classDef navy fill:#17365D,color:#fff,stroke:#17365D;
    classDef teal fill:#147D84,color:#fff,stroke:#0D565B;
    classDef purple fill:#6C3BAA,color:#fff,stroke:#472675;
    classDef crimson fill:#C6284D,color:#fff,stroke:#861B35;
    class A,B,C,D navy;
    class E,F,G teal;
    class H,I,J,K purple;
    class L,M crimson;
Diagonal terms survive and cross terms cancel Expanding the square of an AMS projection separates always-positive diagonal squares from signed cross terms whose expectations vanish. ProjectionZ = s₁f₁ + s₂f₂ + …one linear counterupdates add sᵢΔ Expand Z²diagonal: Σ fᵢ²cross: 2Σ sᵢsⱼfᵢfⱼseparate proof obligations Expectationsᵢ² = 1E[sᵢsⱼ] = 0E[Z²] = F₂requires valid signs survivescancels in meanassumption boundary

For \(Z=\sum_i s(i)f_i\), pairwise-independent mean-zero signs give \(\mathbb E[Z^2]=F_2\); four-wise independence gives \(\mathbb E[Z^4]\le3F_2^2\) and \(\operatorname{Var}(Z^2)\le2F_2^2\). The estimator is linear and turnstile-compatible, but its guarantee is over the chosen sign family and requires stable keys, sufficient independence, and safe arithmetic.

Fourth Moments, Variance and Repetition

One squared row has relative variance at most two, so a single result is not a high-accuracy estimate. Average \(r\) independent squares: \[ Y=\frac1r\sum_{t=1}^r Z_t^2,\qquad \mathbb E[Y]=F_2,\qquad \operatorname{Var}(Y)\le\frac{2F_2^2}{r}. \] Chebyshev's inequality yields \[ \Pr[|Y-F_2|\ge\varepsilon F_2]\le\frac{2}{r\varepsilon^2}. \] Choosing \(r=\lceil8/\varepsilon^2\rceil\) makes a group fail with probability at most \(1/4\). Build an odd number \(g\) of independent groups and return their median. A Chernoff analysis gives exponentially small failure; the conservative choice \(g\ge12\ln(1/\delta)\), rounded upward to an odd integer, is sufficient under this \(1/4\) group-failure bound.

The memory is \(O(\varepsilon^{-2}\log(1/\delta))\) counters plus compact sign seeds. Update time matches the number of counters unless vectorized or accelerated. Constants are intentionally visible: another analysis or estimator family may improve them, but silently quoting only big-O hides operational cost.

For \(f=(2,1)\), one row takes values \(1\) or \(9\) with equal probability. Its mean is \(5\), but variance is \(16\). Averaging four independent rows reduces variance to \(4\). The exact general bound \(2F_2^2=50\) is safe but loose here. If the vector is perturbed to \((2,-1)\), exact \(F_2\) remains \(5\); the sign outcomes swap, demonstrating turnstile compatibility.

Numeric limits remain real. Squaring a 64-bit projection may require wider accumulation or checked arithmetic. Floating-point group means can lose precision when values span huge ranges. Reusing a seed across rows prevents variance reduction, while exposing fixed signs to an adaptive adversary changes the practical threat model even when the nonadaptive theorem is correct.

Worked Example

Fourth Moments, Variance and Repetition

Use \(f=(2,1)\) and four independent sign pairs chosen as \((+,+),(+,-),(-,+),(-,-)\).

  1. Projections are \(3,1,-1,-3\); squares are \(9,1,1,9\).
  2. Their group mean is \(5\), exactly matching \(F_2=5\).
  3. Compute the empirical second moment of the four squares: squared deviations from five are all \(16\), so the population variance is \(16\).
  4. Perturb the vector to \((2,-1)\). Projections become \(1,3,-3,-1\), squares \(1,9,9,1\), and the mean remains five.
  5. Invalid case: use one shared sign for both keys. Every row square becomes \(9\) or \(1\) depending on vector signs, but a fixed correlated construction no longer averages according to the independent AMS proof.

Checkpoint: distinguish randomness across keys from independence across repeated rows.

from hashlib import blake2b
from statistics import median

def sign(seed, row, key):
    raw = f"{seed}|{row}|{key}".encode()
    value = blake2b(raw, digest_size=8, person=b"ams-f2-v1").digest()
    return 1 if value[0] & 1 else -1

class AMSF2:
    def __init__(self, rows_per_group, groups, seed=19):
        if rows_per_group < 1 or groups < 1 or groups % 2 == 0:
            raise ValueError("positive rows and positive odd groups required")
        self.r, self.g, self.seed = rows_per_group, groups, seed
        self.z = [0] * (rows_per_group * groups)

    def update(self, key, delta):
        if not isinstance(delta, int):
            raise TypeError("integer delta required")
        for row in range(len(self.z)):
            self.z[row] += sign(self.seed, row, key) * delta

    def estimate(self):
        means = []
        for group in range(self.g):
            start = group * self.r
            squares = [x * x for x in self.z[start:start + self.r]]
            means.append(sum(squares) / self.r)
        return median(means)

def exact_f2(updates):
    freq = {}
    for key, delta in updates:
        freq[key] = freq.get(key, 0) + delta
    return sum(value * value for value in freq.values()), freq

normal = [("hot", 300)] + [(f"k{i}", i % 5 + 1) for i in range(200)]
truth, _ = exact_f2(normal)
sketch = AMSF2(256,  nine_groups := 9)
for item in normal:
    sketch.update(*item)
assert abs(sketch.estimate() - truth) / truth < 0.20

turnstile = [("a", 20), ("b", 7), ("a", -6), ("debt", -9), ("b", -2)]
turn_truth, vector = exact_f2(turnstile)
turn = AMSF2(256, 9)
for item in turnstile:
    turn.update(*item)
assert abs(turn.estimate() - turn_truth) / turn_truth < 0.25

empty = AMSF2(8, 3)
assert empty.estimate() == 0

all_positive_projection = sum(vector.values())
bad_square = all_positive_projection * all_positive_projection
assert bad_square != turn_truth

try:
    AMSF2(8, 4)
    raise AssertionError("even group count accepted")
except ValueError:
    pass

print("normal:", round(sketch.estimate(), 2), "exact:", truth)
print("turnstile:", round(turn.estimate(), 2), "exact:", turn_truth)
print("correlated-sign failure:", bad_square)
ConstructionIndependence/assumptionsSample structureGuarantee or roleCostStrengthFailure modePrefer when
One AMS rowPairwise signs for mean; four-wise for varianceOne \(Z^2\)Unbiased, relative variance at most twoOne counterMinimal linear sketchVery noisy resultTeaching and proof building block
Mean of rowsIndependent four-wise sign maps\(r\) squared rowsFailure at most \(2/(r\varepsilon^2)\) by Chebyshev\(O(r)\) update/memoryReduces variance directlyCorrelated seedsConstant-confidence estimate
Median of meansIndependent groups and rows\(r=\lceil8/\varepsilon^2\rceil\), \(g=O(\log1/\delta)\)Relative error \(\varepsilon\) with failure \(\delta\)\(O(rg)\)High confidence under weak moment boundsWrong grouping or reused randomnessCanonical robust deployment
Exact frequency mapExact key identity and sufficient memoryOne counter per distinct keyExact \(F_2\)\(O(u)\) memoryAudit oracleCardinality-scale memorySmall windows and sampled validation
All-positive projectionNo useful sign independence\((\sum_i f_i)^2\)Not an \(F_2\) estimatorOne counterNone for general \(F_2\)Cross terms remainOnly as a counterexample

Detecting Skew in Network and Event Streams

In a teaching case, compare two ten-event streams. Ten distinct keys give \(F_2=10\); one key repeated ten times gives \(F_2=100\). AMS demonstrates how a tiny linear state distinguishes diffuse from concentrated frequency vectors without naming the heavy key.

In professional network telemetry, maintain AMS sketches for fixed windows of source–destination flow updates. A rising normalized concentration such as \(F_2/F_1^2\) can signal a shift toward dominant flows. Because sketches are linear, compatible regional tables merge by counterwise addition. The contract must include window, key encoding, row/group layout, sign-family version, seeds, arithmetic width, and update semantics.

An event platform can similarly monitor tenant–event skew, including corrections expressed as negative deltas. AMS measures concentration but does not identify the responsible keys; pair it with a heavy-hitter summary or exact candidate audit. A flat vector and a different vector may share the same \(F_2\), so business interpretation needs additional signals.

Failure cases include adaptive producers that learn signs, accidental seed reuse, inconsistent canonicalization across partitions, overflow before squaring, and merging different sketch versions. Evidence invalidating the choice includes systematic exact-audit bias, group estimates that are strongly correlated, empirical failure exceeding \(\delta\), negative impossible results caused by arithmetic faults, or latency from too many rows exceeding the processing budget.

AMS F2 in distributed stream monitoringCanonical signed events feed compatible regional AMS projections, merge into a concentration estimate, and are checked against trust and failure boundaries. Stream edgecanonical flow keysigned deltauntrusted producer Regional AMSsame signs and layoutlinear projections Zreject version mismatch Skew monitorsum Z then squaremedian group estimateaudit bias and overflow contractsketch statefailure boundary
AMS F2 Estimation: Detecting Skew in Network and Event Streams
flowchart TD
    A[Define concentration metric and window] --> B{Need only F2, not key identities?}
    B -->|no| C[Pair with heavy hitters or exact map]
    B -->|yes| D{Stable canonical turnstile updates?}
    D -->|no| E[Reject until input contract is fixed]
    D -->|yes| F[Choose epsilon delta and row groups]
    F --> G[Select four-wise sign family and safe counters]
    G --> H[Validate against exact sampled windows]
    H --> I{Bias failure and latency acceptable?}
    I -->|no| J[Reject resize or repair randomness]
    I -->|yes| K[Version sketch and deploy]
    K --> L[Monitor correlation overflow and drift]
    L --> M{Assumptions still hold?}
    M -->|yes| L
    M -->|no| J
    classDef navy fill:#17365D,color:#fff,stroke:#17365D;
    classDef teal fill:#147D84,color:#fff,stroke:#0D565B;
    classDef purple fill:#6C3BAA,color:#fff,stroke:#472675;
    classDef crimson fill:#C6284D,color:#fff,stroke:#861B35;
    class A,B,D navy;
    class F,G purple;
    class H,K,L teal;
    class C,E,I,J,M crimson;
Case Study

Detecting Skew in Network and Event Streams: AMS F2 Estimation

Scenario: a provider processes \(10^8\) flow updates per minute across 20 regions, including late corrections. It wants 10% relative error with failure probability below 1% for per-minute \(F_2\).

  1. Set \(\varepsilon=0.1,\delta=0.01\). The conservative derivation suggests \(r=\lceil8/\varepsilon^2\rceil=800\) rows per group and an odd \(g\) at least \(12\ln(100)\); benchmark this large constant rather than hiding it.
  2. Use a documented four-wise-independent sign family, signed wide counters, stable flow encoding, and independent seeds. Treat these as merge metadata.
  3. Build regional projections over the same window and add corresponding \(Z\) counters before squaring. Never average already-squared regional estimates to estimate the union.
  4. Replay at least 30 windows with exact \(F_2\). Record relative error, signed bias, group correlation, overflow headroom, and update latency.
  5. Stress one dominant flow, a flat distribution, retractions, empty windows, and crafted keys.
  6. Decision: deploy only if exact audits meet the target, row groups behave independently, and latency is sustainable. Otherwise tune constants using justified evidence, strengthen the sign implementation, or use an alternate sketch.

Simulate and Verify the AMS Estimator

Mastery begins by deriving, not memorizing, the update rule. Expand \(Z^2\), label diagonal and cross terms, and state precisely why each expectation vanishes. Then expand \(Z^4\), enumerate even-multiplicity index patterns, and connect the result to variance, Chebyshev, group size, and median amplification.

Implementation work should maintain an exact oracle on small streams. Test insertion-only, general turnstile, empty, one-key, flat, skewed, overflow-adjacent, and adversarially correlated-sign cases. Distinguish independence across keys inside a row from independence across rows. Verify merge by comparing counterwise-added sketches with a sketch of the concatenated update stream.

For extension, derive the AMS inner-product estimator: use compatible signed projections of \(f\) and \(g\), multiply them, and show the expected product is \(\langle f,g\rangle\). Then connect \(F_2\) estimation to self-join size, heavy-hitter residual energy, and linear-sketch composition.

Practice

Practice: Simulate and Verify the AMS Estimator

Use \(f=(3,-2,1)\), exact \(F_2=14\), and enumerate all eight fully independent sign triples.

  1. Recall (2 points): define Rademacher signs, projection, unbiasedness, fourth moment, and repetition.
  2. Calculate (4): list every \(Z\) and \(Z^2\); confirm their mean is 14.
  3. Proof (6): derive \(\mathbb E[Z^2]\) and \(\mathbb E[Z^4]=3F_2^2-2F_4\). Mark where pairwise and four-wise independence enter.
  4. Amplify (4): choose \(r\) for \(\varepsilon=0.2\), derive the Chebyshev failure, and choose an odd group count for a stated \(\delta\).
  5. Debug (3): reuse one sign bit for all keys and show the surviving cross terms.
  6. Verify (4): run at least 100 deterministic seeds; report mean estimate, relative bias, median relative error, and empirical failure rate.
  7. Stretch (4): prove linear merge correctness and derive an inner-product estimator using two compatible projections.

Completion criteria: exact enumeration matches \(F_2\), every independence assumption is named, code has an exact oracle, merge headers are checked, and invalid randomness fails visibly. Rubric: 24–27 professional mastery; 18–23 sound with minor gaps; 12–17 revisit moments and amplification; below 12 redo the sign enumeration.

Use AMS when a linear, turnstile-compatible estimate of concentration is needed and key identities are not. Keep sign stability, four-wise independence, independent repetitions, counter width, and merge metadata inside the correctness contract; then validate bias, correlation, and tail failure against exact windows before trusting production thresholds.