One scenario is to approximate A∈Rn×d in an online fashion, i.e., the rows of A comes on by one (no missing values).
Denote the i-th row of A as ai.
We need to maintain a smaller matrix B∈Rℓ×d (ℓ≪n), termed the matrix sketch of A, trying to minimize
We will present an algorithm, which achieves an error of
∥ATA−BTB∥2≤k∈[ℓ]minℓ−k∥A−A[k]∥F2,
where ∥A∥F≐∑i∈[n],j∈[d]Ai,j2 is the Frobenius norm of A.
Algorithm [1]
B← empty matrix.
For i∈[n]:
B←[B;ai] matrix obtained by inserting ai after the last row B
B has ℓ rows:
Ui,Σi,Vi←SVD(B), such that B=UiΣiViT.
Σi←max{Σi2−σi,ℓ2I,0}
B←ΣiViT
Here σi,ℓ is the ℓ-th largest singular value of Σi.
The line 6 throws away all singular values that are less than σi,ℓ, and then subtract the squares of all singular values that are greater by σi,ℓ2.
For the convenience of discussion, at each iteration, if B has less than ℓ rows after insertion, we still define
Ui,Σi,Vi←SVD(B), and Σi=Σi.
To analyze the error, first observe that, when the algorithm stops,
Remark: In the proof we apply the fact that the squared Frobenius norm of a matrix M equals the sum of the squares of its singular values.
Let M=∑j=1dσjujvj⊤, then
∥M∥F2=∥UΣVT∥F2=∥Σ∥F2.
The last inequality holds since U and VT are rotations which does not change the Frobenius norm.
We apply similar idea for the heavy hitter problem in streaming model.
Problem
There is a streaming S={a1,...,am} of m elements each from a finite domain [d]. Given a parameter k, we want to find the elements in [d] whose frequencies, i.e., the number of times they appear in S, are at least k.
Algorithm
The following algorithm will provide under-estimates of elements' frequencies, with space usage 2k and error m/k.
Processing Stream
B0← an empty (element, frequency) list of capacity 2k.
For t∈[m]:
Ct← list obtained by inserting {at,1} into Bt−1.
If Ct has size 2k:
Bt← list obtained from Ct by merging frequencies.
// each element in [d] now appears in Bt at most once.
σt← the k-th largest frequency in Bt.
Subtract the frequency of each element in Bt by σt.
Remove elements with non-positive frequencies.
Else
σt←0;
Bt←Ct.
QUERY Input: Bt for some t∈[m], v∈[d]. Output: frequency estimation of v in {a1,....,at}
f^Bt[v]←0;
for (vi,fi)∈Bt do
if vi=v then
f^Bt[v]←f^Bt[v]+fi;
Returnf^Bt[v].
Observe that for v∈[d], if v does not appears in Bt, then the algorithm returns 0.
Time
The amortized time for each update is O(1).
Inserting {ai,1} into Bi−1 takes O(1) time.
Merging Ci, subtracting elements' frequencies by σi and removing non-positive elements take O(k) time. But this happens only every k/2 updates. Hence the amortized time is O(1).
Error
Theorem 1.∀t∈[m],v∈[d], f^Bt[v] is an underestimate of v's frequency in {a1,...at}.
Proof. Obviously true by how the algorithm works.
Theorem 2.∀v∈[d],t∈[m], let ft[v] be the number of times v appears in {a1,...,at}. We have that
ft[v]−f^Bt[v]<kt.
Proof. For each i∈{0,1,...,m}, define ∥Bi∥1=(∑(vj,fj)∈Bifj) and for each i∈[m], let ∥Ci∥1=(∑(vj,fj)∈Cifj). Observe that
[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.