Problem

One scenario is to approximate A∈Rn×dA \in \mathbb{R}^{n \times d} in an online fashion, i.e., the rows of AA comes on by one (no missing values). Denote the ii-th row of AA as aia_i. We need to maintain a smaller matrix B∈Rℓ×dB \in \mathbb{R}^{\ell \times d} (ℓ≪n\ell \ll n), termed the matrix sketch of AA, trying to minimize

max⁡x∈Rd,∥x∥2=1∣∥Ax∥22−∥Bx∥22∣=max⁡x∈Rd,∥x∥2=1∣xT(ATA−BTB)x∣=∥ATA−BTB∥2. \begin{aligned} \max_{x \in \mathbb{R}^d, \Vert x \Vert_2 = 1} \Big| \Vert A x \Vert_2^2 - \Vert B x \Vert_2^2 \Big| &= \max_{x \in \mathbb{R}^d, \Vert x \Vert_2 = 1} \Big| x^T (A^T A - B^T B) x \Big| = \Vert A^T A - B^T B \Vert_2. \end{aligned}

Algorithm

We will present an algorithm, which achieves an error of

∥ATA−BTB∥2≤min⁡k∈[ℓ]∥A−A[k]∥F2ℓ−k, \Vert A^T A - B^T B \Vert_2 \le \min_{k \in [\ell]} \frac{\Vert A - A_{[k]} \Vert _F^2}{ \ell - k },

where ∥A∥F≐∑i∈[n],j∈[d]Ai,j2\Vert A \Vert _F \doteq \sqrt{ \sum_{ i \in [n], j \in [d] } A_{i,j}^2 } is the Frobenius norm of AA.

Algorithm [1]

  1. B←B \leftarrow empty matrix.
  2. For i∈[n]i \in [n]:
  3. \qquad B←[B;ai]B \leftarrow [B ; a_i] matrix obtained by inserting aia_i after the last row BB
  4. \qquad BB has ℓ\ell rows:
  5. \qquad \qquad Ui,Σi,Vi←SVD(B)U_i, \Sigma_i, V_i \leftarrow \text{SVD} (B), such that B=UiΣiViTB = U_i \Sigma_i V_i^T.
  6. \qquad \qquad Σi‾←max⁡{Σi2−σi,ℓ2I,0}\overline{\Sigma_i} \leftarrow \sqrt{ \max \{ \Sigma_i^2 - \sigma_{i, \ell}^2 I, 0 \} }
  7. \qquad \qquad B←Σi‾ViTB \leftarrow \overline{\Sigma_i} V_i^T

Here σi,ℓ\sigma_{i, \ell} is the ℓ\ell-th largest singular value of Σi\Sigma_i. The line 6 throws away all singular values that are less than σi,ℓ\sigma_{i, \ell}, and then subtract the squares of all singular values that are greater by σi,ℓ2\sigma_{i, \ell}^2.

For the convenience of discussion, at each iteration, if BB has less than ℓ\ell rows after insertion, we still define

Ui,Σi,Vi←SVD(B),  and  Σi‾=Σi. U_i, \Sigma_i, V_i \leftarrow \text{SVD} (B) , \, \text{ and } \, \overline{\Sigma_i} = \Sigma_i.

To analyze the error, first observe that, when the algorithm stops,

ATA−BTB=∑i∈[n](AiTAi+Bi−1TBi−1−BiTBi). \begin{aligned} A^T A - B^T B &= \sum_{i \in [n]} \left( A_i^T A_i + B_{i - 1}^T B_{i - 1} - B_i^T B_i \right). \end{aligned}

Therefore, for an arbitrary unit vector x∈Rdx \in \R^d,

xTATAx−xTBTBx=∑i∈[n](xTAiTAix+xTBi−1TBi−1x−xTBiTBix)=∑i=1j(⟨Ai,x⟩2+∥Bi−1x∥22−∥Bix∥22)=∑i=1n(∥ΣiViTv∥22−∥Σi‾ViTv∥22)≤∑i=1nσi,ℓ2. \begin{aligned} x^T A^T A x - x ^T B^T B x &= \sum_{i \in [n]} \left( x^T A_i^T A_i x + x^T B_{i - 1}^T B_{i - 1} x - x^T B_i^T B_i x \right) \\ &= \sum_{i = 1}^j \left( \left\langle A_i, x \right\rangle ^2 + \Vert B_{i - 1} x \Vert_2^2 - \Vert B_{i} x \Vert_2^2 \right) \\ &= \sum_{i = 1}^n \left( \Vert \Sigma_i V_i^T v \Vert_2^2 - \Vert \overline{\Sigma_i} V_i^T v \Vert_2^2 \right) \\ &\le \sum_{i = 1}^n \sigma_{i, \ell }^2. \end{aligned}

The last inequality holds since ViTxV_i^T x is a unit vector, and ΣiTΣi−Σi‾TΣi‾\Sigma_i^T \Sigma_i - \overline{\Sigma_i}^T \overline{\Sigma_i} is a diagonal matrix whose elements are bounded by σi,ℓ2\sigma_{i, \ell}^2.

It is left to bound the size of ∑i=1nσi,ℓ2\sum_{i = 1}^n \sigma_{i, \ell }^2:

ℓ⋅∑i=1nσi,ℓ2≤∑i=1n(∥Σi∥F2−∥Σi‾∥F2)=∑i=1n(∥Ci∥F2−∥Bi∥F2)=∑i=1n(∥ai∥22+∥Bi−1∥F2−∥Bi∥F2)=∥A∥F2−∥B∥F2. \begin{aligned} \ell \cdot \sum_{i = 1}^n \sigma_{i, \ell }^2 &\le \sum_{i = 1}^n \left( \Vert \Sigma_i \Vert^2_F - \Vert \overline{\Sigma_i} \Vert^2 _F \right) \\ &= \sum_{i = 1}^n \left( \Vert C_i \Vert_F^2 - \Vert B_{i} \Vert^2_F \right) \\ &= \sum_{i = 1}^n \left( \Vert a_i \Vert_2^2 + \Vert B_{i - 1} \Vert^2_F - \Vert B_{i} \Vert^2_F \right) \\ &= \Vert A \Vert^2_F - \Vert B \Vert^2_F. \end{aligned}

Finally, let u1,…,ud∈Rdu_1, \ldots, u_d \in \mathbb{R}^d be the right singular vectors of AA. Then for each k∈[ℓ]k \in [\ell], it holds that

∥A∥F2−∥B∥F2=∑i=1k∥Aui∥22+∑i=k+1d∥Aui∥22−∥B∥F2=∑i=1k∥Aui∥22+∥A−A[k]∥22−∥B∥F2≤∑i=1k(∥Aui∥22−∥Bui∥22)+∥A−A[k]∥22∥B∥F2=∥BV∥F2≥∑i=1k∥Bui∥22≤∑i=1k(∥Aui∥22−∥Bui∥22)+∥A−A[k]∥22≤k⋅∥ATA−BTB∥2+∥A−A[k]∥22. \begin{aligned} \Vert A \Vert^2_F - \Vert B \Vert^2_F &= \sum_{i = 1}^k \Vert A u_i \Vert_2^2 + \sum_{i = k + 1}^d \Vert A u_i \Vert_2^2 - \Vert B \Vert^2_F \\ &= \sum_{i = 1}^k \Vert A u_i \Vert_2^2 + \Vert A - A_{[k]} \Vert_2^2 - \Vert B \Vert^2_F \\ &\le \sum_{i = 1}^k \big( \Vert A u_i \Vert_2^2 - \Vert B u_i \Vert_2^2 \big) + \Vert A - A_{[k]} \Vert_2^2 & \qquad \Vert B \Vert^2_F = \Vert B V\Vert^2_F \ge \sum_{i = 1}^k \Vert B u_i \Vert_2^2 \\ &\le \sum_{i = 1}^k \big( \Vert A u_i \Vert_2^2 - \Vert B u_i \Vert_2^2 \big) + \Vert A - A_{[k]} \Vert_2^2 \\ &\le k \cdot \Vert A^T A - B^T B \Vert_2 + \Vert A - A_{[k]} \Vert_2^2. \end{aligned}

In conclusion, we have

ℓ⋅∥ATA−BTB∥2≤k⋅∥ATA−BTB∥2+∥A−A[k]∥22, \ell \cdot \Vert A^T A - B^T B \Vert_2 \le k \cdot \Vert A^T A - B^T B \Vert_2 + \Vert A - A_{[k]} \Vert_2^2,

which proves the theorem.

Remark: In the proof we apply the fact that the squared Frobenius norm of a matrix MM equals the sum of the squares of its singular values. Let M=∑j=1dσjujvj⊤M = \sum_{j = 1}^d \sigma_j u_j v_j^\top, then

∥M∥F2=∥UΣVT∥F2=∥Σ∥F2. \begin{align} \Vert M \Vert_{\mathrm{F}}^2 = \Vert U \Sigma V^T \Vert_{\mathrm{F}}^2 = \Vert \Sigma \Vert_{\mathrm{F}}^2. \end{align}

The last inequality holds since UU and VTV^T are rotations which does not change the Frobenius norm.

In conclusion,

∥ATA−BTB∥2∥A∥F2≤∥ATA−BTB∥2∥A∥F2−∥B∥F2≤2k. \begin{aligned} \frac{ \Vert A^T A - B^T B \Vert_2 }{ \Vert A \Vert^2_F} \le \frac{ \Vert A^T A - B^T B \Vert_2 }{ \Vert A \Vert^2_F - \Vert B \Vert^2_F} \le \frac{2}{k}. \end{aligned}

□\square

A Counterpart For Heavy Hitters

We apply similar idea for the heavy hitter problem in streaming model.

Problem

There is a streaming S={a1,...,am}S = \{ a_1, ..., a_m \} of mm elements each from a finite domain [d][d]. Given a parameter kk, we want to find the elements in [d][d] whose frequencies, i.e., the number of times they appear in SS, are at least kk.

Algorithm

The following algorithm will provide under-estimates of elements' frequencies, with space usage 2k2k and error m/km / k.

Processing Stream

  1. B0←B_0 \leftarrow an empty (element, frequency) list of capacity 2k2k.
  2. For t∈[m]t \in [m]:
  3. \qquad Ct←C_t \leftarrow list obtained by inserting {at,1}\{ a_t, 1 \} into Bt−1B_{t - 1}.
  4. \qquad If CtC_t has size 2k2k:
  5. \qquad \qquad Bt←B_t \leftarrow list obtained from CtC_t by merging frequencies.
  6. \qquad \qquad // each element in [d][d] now appears in BtB_t at most once.
  7. \qquad \qquad σt←\sigma_t \leftarrow the kk-th largest frequency in BtB_t.
  8. \qquad \qquad Subtract the frequency of each element in BtB_t by σt\sigma_t.
  9. \qquad \qquad Remove elements with non-positive frequencies.
  10. \qquad Else
  11. \qquad \qquad σt←0;\sigma_t \leftarrow 0;
  12. \qquad \qquad Bt←CtB_t \leftarrow C_t.

QUERY
Input: BtB_t for some t∈[m]t \in [m], v∈[d].v \in [d].
Output: frequency estimation of vv in {a1,....,at}\{a_1, ...., a_t \}

  1. f^Bt[v]←0;\hat f_{B_t}[v] \leftarrow 0;
  2. for (vi,fi)∈Bt(v_i, f_i) \in B_t do
  3. \qquad if vi=vv_i = v then
  4. \qquad \qquad f^Bt[v]←f^Bt[v]+fi;\hat f_{B_t} [ v ] \leftarrow \hat f_{B_t} [v] + f_i;
  5. Return f^Bt[v]\hat f_{B_t} [v].

Observe that for v∈[d]v \in [d], if vv does not appears in BtB_t, then the algorithm returns 0.

Time

The amortized time for each update is O(1)O(1).

  1. Inserting {ai,1}\{a_i, 1\} into Bi−1B_{i - 1} takes O(1)O(1) time.
  2. Merging CiC_i, subtracting elements' frequencies by σi\sigma_i and removing non-positive elements take O(k)O(k) time. But this happens only every k/2k / 2 updates. Hence the amortized time is O(1)O(1).

Error

Theorem 1. ∀t∈[m],v∈[d]\forall t \in [m], v \in [d], f^Bt[v]\hat f_{B_t} [v] is an underestimate of vv's frequency in {a1,...at}\{a_1, ... a_t \}.

Proof. Obviously true by how the algorithm works.

Theorem 2. ∀v∈[d],t∈[m]\forall v \in [d], t \in [m], let ft[v]f_t [v] be the number of times vv appears in {a1,...,at}\{a_1, ..., a_t \}. We have that

ft[v]−f^Bt[v]<tk. f_t[v] - \hat f_{B_t} [v] < \frac{t}{k}.

Proof. For each i∈{0,1,...,m}i \in \{0, 1, ..., m \}, define ∥Bi∥1=(∑(vj,fj)∈Bifj)\Vert B_i \Vert_1 = \left( \sum_{ (v_j, f_j) \in B_i } f_j \right) and for each i∈[m]i \in [m], let ∥Ci∥1=(∑(vj,fj)∈Cifj)\Vert C_i \Vert_1 = \left( \sum_{ (v_j, f_j) \in C_i } f_j \right). Observe that

ft[v]−f^Bt[v]≤∑i∈[t]σi. \begin{aligned} f_t[v]- \hat f_{B_t} [v] \le \sum_{i \in [t] } \sigma_i. \end{aligned}

On the other hand,

t−∥Bm∥1=∑i∈[t](1+∥Bi−1∥1−∥Bi∥1)=∑i∈[t](∥Ci∥1−∥Bi∥1)≥k⋅∑i∈[t]σi. \begin{aligned} t - \Vert B_m \Vert_1 &= \sum_{i \in [t] } \left( 1 + \Vert B_{i - 1} \Vert_1 - \Vert B_i \Vert_1 \right) \\ &= \sum_{i \in [t] } \left( \Vert C_i \Vert_1 - \Vert B_i \Vert_1 \right) \\ &\ge k \cdot \sum_{i \in [t] } \sigma_i. \end{aligned}

Combined, we get ft[v]−f^Bt[v]≤t−∥Bt∥1kf_t[v]- \hat f_{B_t} [v] \le \frac{t - \Vert B_t \Vert_1}{k}.

Reference

[1]. Edo, Liberty. "Simple and deterministic matrix sketching." In Proceedings of the 19th ACM SIGKDD international conference on Knowledge discovery and data mining, pp. 581-588. ACM, 2013.