NLA-lib Explorer

CountSketch

Use a sparse randomized embedding to compress a vector or matrix.

Definition

Definition (CountSketch). Choose a hash h:\{1,\ldots,n\}\to\{1,\ldots,s\} and signs \sigma_i\in\{-1,1\}. Define \mathbf{S}_{a,i}=\sigma_i when h(i)=a, and zero otherwise. Each input coordinate contributes to one output bucket. The standard law uses uniform independent hash values and independent Rademacher signs; embedding guarantees specify the required output dimension.

Lean statement and details
NLAlib.countSketchMatrix.{u_2, u_3, u_4} {𝕜 : Type u_2} [RCLike 𝕜] {k : Type u_3} {n : Type u_4}
  [DecidableEq k] (h : n → k) (σ : n → ℝ) : Matrix k n 𝕜

Full source on GitHub

Copy link

Mathematical guarantees

Theorem (Exact squared Gram error). Let \mathbf{U}\in\mathbb{R}^{n\times d} have orthonormal columns, and let \mathbf{S} be a standard CountSketch with s\ge1 rows. Write \ell_i=\|\mathbf{U}_{i,:}\|_2^2 for its leverage scores. Then

\mathbb{E}\|(\mathbf{S}\mathbf{U})^{\mathsf{T}}\mathbf{S}\mathbf{U}-\mathbf{I}\|_{\mathrm{F}}^2=\frac{d^2+d-2\sum_i\ell_i^2}{s}.

This is the real specialization of the checked real-or-complex identity. It gives the exact mean squared distortion of the subspace Gram matrix.

Lean statement and details

General form: sizes are index types

NLAlib.HasOrthonormalCols.integral_frobNorm_conjTranspose_mul_self_sub_one_sq_of_hasLaw_countSketch.{u_1,
    u_2, u_3, u_4, u_6}
  {Ω : Type u_1} {mΩ : MeasurableSpace Ω} {μ : Measure Ω} {𝕜 : Type u_2} [RCLike 𝕜] {k : Type u_3}
  {n : Type u_4} {r : Type u_6} [Fintype k] [Fintype n] [DecidableEq k] {S : Ω → Matrix k n 𝕜}
  [Fintype r] [DecidableEq r] {U : Matrix n r 𝕜} (hU : HasOrthonormalCols U)
  (hS : HasLaw S (countSketch k n) μ) :
  ∫ (ω : Ω), frobNorm ((S ω * U)ᴴ * (S ω * U) - 1) ^ 2 ∂μ =
    (↑(Fintype.card r) ^ 2 + frobNorm (Uᵀ * U) ^ 2 - 2 * ∑ i, leverageScore U i ^ 2) /
      ↑(Fintype.card k)

Full source on GitHub

Copy link

Further results

Theorem (AMM with a CountSketch).

, the bound \mathbb{E}\|\mathbf{A}^{\mathsf{H}}\mathbf{S}^{\mathsf{H}}\mathbf{S}\mathbf{B} - \mathbf{A}^{\mathsf{H}}\mathbf{B}\|_{\mathsf{F}}^2 \le \frac{2\|\mathbf{A}\|_{\mathsf{F}}^2\|\mathbf{B}\|_{\mathsf{F}}^2}{\lvert k \rvert}\,, over any field. [Woodruff2014, Theorems 13 and 14] (there 3\varepsilon through the JL moment, over \mathbb{R}).

General form: sizes are index types

Lean statement and details
NLAlib.integral_frobNorm_conjTranspose_mul_sub_sq_le_of_hasLaw_countSketch.{u_1, u_2, u_3, u_4, u_5,
    u_6}
  {Ω : Type u_1} {mΩ : MeasurableSpace Ω} {μ : Measure Ω} {𝕜 : Type u_2} [RCLike 𝕜] {k : Type u_3}
  {n : Type u_4} {q : Type u_5} {r : Type u_6} [Fintype k] [Fintype n] [DecidableEq k]
  {S : Ω → Matrix k n 𝕜} [Fintype q] [Fintype r] (hS : HasLaw S (countSketch k n) μ)
  (A : Matrix n q 𝕜) (B : Matrix n r 𝕜) :
  ∫ (ω : Ω), frobNorm (Aᴴ * (S ω)ᴴ * S ω * B - Aᴴ * B) ^ 2 ∂μ ≤
    2 * frobNorm A ^ 2 * frobNorm B ^ 2 / ↑(Fintype.card k)

Full source on GitHub

Copy link

Theorem (CountSketch JL moment).

at p = 2 with \delta = 2/(\lvert k \rvert \varepsilon^2) \,, any field: \mathbb{E}(\|\mathbf{S}\mathbf{x}\|^2 - 1)^2 \le 2/\lvert k \rvert for unit \mathbf{x}, the AMM bound at a one-column matrix. [Woodruff2014, Theorem 14] (after Thorup and Zhang, not in hand; over \mathbb{R}, there in size form).

General form: sizes are index types

Lean statement and details
NLAlib.hasJLMoment_of_hasLaw_countSketch.{u_1, u_2, u_3, u_4} {Ω : Type u_1}
  {mΩ : MeasurableSpace Ω} {μ : Measure Ω} {𝕜 : Type u_2} [RCLike 𝕜] {k : Type u_3} {n : Type u_4}
  [Fintype k] [Fintype n] [DecidableEq k] {S : Ω → Matrix k n 𝕜} {ε : ℝ}
  (hS : HasLaw S (countSketch k n) μ) (hε : 0 < ε) :
  HasJLMoment S ε (2 / (↑(Fintype.card k) * ε ^ 2)) 2 μ

Full source on GitHub

Copy link

Theorem (CountSketch OSI).

for \varepsilon > 0 and nonempty k, a CountSketch is an oblivious subspace injection for dimension d with constant 1 - \varepsilon and failure probability (d^2 + d)/(\lvert k \rvert \varepsilon^2) \,: the lower half of the OSE, with isotropy. [CamanoEpperlyMeyerTropp2025, Definition 1.2] for the notion; their Table 3 lists CountSketch only as an OSE, with k = O(r^2).

General form: sizes are index types

Lean statement and details
NLAlib.isObliviousSubspaceInjection_of_hasLaw_countSketch.{u_1, u_2, u_3, u_4} {Ω : Type u_1}
  {mΩ : MeasurableSpace Ω} {μ : Measure Ω} {𝕜 : Type u_2} [RCLike 𝕜] {k : Type u_3} {n : Type u_4}
  [Fintype k] [Fintype n] [DecidableEq k] {S : Ω → Matrix k n 𝕜} {ε : ℝ} [Nonempty k]
  [DecidableEq n] (hS : HasLaw S (countSketch k n) μ) (d : ℕ) (hε : 0 < ε) :
  IsObliviousSubspaceInjection S d (1 - ε) ((↑d ^ 2 + ↑d) / (↑(Fintype.card k) * ε ^ 2)) μ

Full source on GitHub

Copy link

Mathematical background

Appears under these tasks