Let's recall the ridge regression model. We have some training data of the form:
(x1,y1),…,(xn,yn)∈(X,Y),
where X=Rd and Y=R. We represent the training data more succinctly by letting y=[y1,…,yn]⊤ and X be an n×d matrix whose ith row is xi⊤. Then in ridge regression the likelihood model is
y∼N(Xw,σ2In),
where w is a d-dimensional column vector representing the weights to learned, and σ2>0. We also assume a Gaussian prior on w
w∼N(0,λ1Id).
Then the maximum a posteriori (MAP) estimate is given by
If we call this objective L, then wMAP is given by the solution of the equation formed by equating the gradient of L with respect to w to 0. Thus,
0=∇wL=σ21X⊤y−σ21X⊤Xw−λw
and we get
wMAP=(λσ2I+X⊤X)−1X⊤y,
which is called the ridge regression solution, and we denote it by wRR. We are inverting a d×d matrix taking O(d3) time, and matrix multiplication of a d×d matrix with a d×n matrix taking O(d2n) time.
The previous model suffers from limited expressiveness. As we have seen before, the idea of kernel methods is to define a map which takes our inputs from X to an RKHS H,
ϕ:X→H
and then apply the linear model of ridge regression above to ϕ(xi) instead of xi.H could be an infinite-dimensional vector space, but for now suppose it is D-dimensional. Let Φ represent the n×D matrix whose ith row is ϕ(xi)⊤. Then the ridge regression solution is given by
wRR=(λσ2ID+Φ⊤Φ)−1Φ⊤y.
We immediately realize the problem here: we are inverting a D×D matrix and D can be huge! Fortunately, we have a matrix trick that we can exploit, the push-though identity. It looks like this
We are now inverting an n×n matrix taking O(n3) time, and matrix multiplication of a D×n matrix with a n×n matrix taking O(Dn2) time.
Notice that ΦΦ⊤ is the gram matrix, and let's denote it by K. Its (i,j)th entry is Ki,j=⟨ϕ(xi),ϕ(xj)⟩=K(xi,xj), where K:Rd×Rd→C is the kernel function corresponding to the RKHS H.
where kxi denotes the reproducing kernel for the point xi.
If this looks suspiciously similar to the Representer theorem (Theorem-8 here), then that's because it is an instance of it! So prediction takes O(n) time.
The point of this discussion was to show that Kernel Ridge Regression is computationally expensive. Simply storing the gram matrix, K, takes O(n2) space. We need to find methods which can circumvent using K if we are to apply kernel methods to anything other than the smallest of the data sets.
The idea behind kernel approximation methods is to replace the gram matrix, K, with a low rank approximation, i.e., find an n×S matrix, Z, such that
K≈ZZ⊤.
We want S≪n, and thus we significantly reduce our time and space complexity. Z takes only O(nS) space. Inversion of (λσ2In+ZZ⊤) takes only O(nS2) time.
One way to think about this is we want to find a mapping of xi's to some latent space such that the inner product in this latent space approximates the kernel
Ki,j=K(xi,xj)≈zi⊤zj,
where zi denotes the ith row of Z. One popular way to do that is by Nyström approximation, which I won't be discussing at all. The other popular method is to use random Fourier features. This approach was introduced by Rahimi and Recht in their seminal 2007 paper Random Features for Large-Scale Kernel Machines. It relies on a result from functional analysis called Bochner's theorem, so let's digress and discuss that first.
Definition 28: A function K:Rn→C, is said to be positive-definite if
i,j=1∑nαiαjK(xi−xj)≥0
for every choice of x1,…,xn∈Rn and for every choice of complex numbers α1,…,αn.
Notice how similar this definition is to the definition of a kernel function. Positive-definite functions have some nice properties:
Lemma 3: If K:Rn→C is a positive-definite function, then K(−x)=K(x) for every x∈Rn.
It is easy to see that K(0)≥0. In the definition 28 above, let n=2, x1=x, x2=0, α1 be an arbitrary complex number, and α2=1. Then applying the definition of a positive-definite function, we get
(1+∣α1∣2)K(0)+α1K(x)+α1K(−x)≥0.
Let α1=1, then
2K(0)+K(x)+K(−x)≥0.
In particular, K(x)+K(−x) is real, which implies
K(x)+K(−x)=K(x)+K(−x).
Similarly, letting α1=i, we get i(K(x)−K(−x)) is real, which implies
K(x)−K(−x)=−K(x)+K(−x).
Adding these two equations, we get K(−x)=K(x).
Lemma 4: If K:Rn→C is a positive-definite function, then K is bounded. In particular, ∣K(x)∣≤K(0) for every x∈Rn.
From lemma-3, K(0) must be real. In the definition above, let n=2, x1=0, x2=x, α1=∣K(x)∣, and α2=−K(x). Then applying the definition of a positive-definite function, we get
2K(0)∣K(x)∣2−∣K(x)∣K(x)K(−x)−K(x)∣K(x)∣K(x)≥0.
Now use lemma-3 to substitute K(x) for K(−x) in the middle term to get
2K(0)∣K(x)∣2−2∣K(x)∣3≥0.
If ∣K(x)∣=0, then we obviously have our result, since we can easily show K(0)≥0, otherwise we can divide by 2∣K(x)∣2 to get
K(0)−∣K(x)∣≥0,
which is our desired result.
The following theorem is the converse of Bochner's theorem. We state it first since it is easier to prove.
Theorem 9 [Converse of Bochner's theorem]: The Fourier transform of every finite Borel measure on Rn is positive-definite.
Let μ be a finite Borel measure on Rn. Let K:Rn→C be the Fourier transform of μ, i.e.,
K(x)=∫Rnexp(−iω⊤x)dμ(ω).
Let x1,…,xn∈Rn and α1,…,αn∈C be arbitrary. Then
Theorem 10 [Bochner's theorem]: If K is continuous and positive-definite, then K is the Fourier transform of a finite positive Borel measure.
I'll skip the proof since it is beyond my understanding. The proof can be easily found on the internet, see here or here for example.
This finite positive Borel measure is often called as spectral measure. It is easy to see that K(0)=μ(Rn), and thus if we assume K(0)=1 then the spectral measure is a probability measure.
Picking up where we left: We want to find a low-dimensional mapping of xi's into a latent space such that we can approximate the kernel computation with an inner product in this latent space. We will use Bochner's theorem for this. To apply this theorem we need to limit ourselves to a special class of kernels.
Definition 29: A kernel function K:Rd×Rd→C is called shift-invariant if K(x,y)=K(x−y,0).
Gaussian and Laplacian kernels are examples of shift-invariant kernels.
Note that the function K′:Rd→C defined by K′(x)=K(x,0) is positive-definite if K is a shift-invariant kernel. Bochner's theorem then implies the existence of a spectral measure, μ, such that
K(x,y)=K(x−y,0)=K′(x−y)=∫Rdexp{−iω⊤(x−y)}dμ(ω).
Let p denote the Radon-Nikodym derivative of μ with respect to the Lebesgue measure on Rd (or more simply, the density of μ), then we can write the equation above as
K(x,y)=∫Rdexp{−iω⊤(x−y)}p(ω)dω.
For example, if we take the Gaussian kernel
K(x,y)=exp(−2σ2∥x−y∥22),
then since K(0,0)=1, p is a probability density, and can be easily shown to be Gaussian
p∼N(0,σ21Id).
We now see an obvious way to approximate the kernel from equation (1): use Monte Carlo approximation. If we take S samples, ω1,…,ωS∼p(ω),
Let μ be a Borel probability measure on a compact space Ω, and φ:Ω×X→R be a function such that the functions φ(⋅,x):Ω→R are measurable for all x∈X. Now define the set H to consist of all functions f that can written as
f(x)=∫Ωh(ω)φ(ω,x)dμ(ω)for all x∈X,
for some h:Ω→R such that ∫Ωh2dμ<∞. Let us define the squared norm, ∥f∥H2, as the infimum of ∫Ωh2dμ over all functions h for which f can be decomposed as above. Then it can be shown that H is an RKHS with the kernel
K(x,y)=∫Ωφ(ω,x)φ(ω,y)dμ(ω).
Therefore, we can again approximate this kernel with a Monte Carlo approximation:
Random Fourier Features is an easy to implement approach that allows us to apply kernel methods on large data sets. I haven't discussed the number of samples, S, needed to approximate the kernel function within an error bound. You can find such results in the papers linked above.
With this article I conclude the series on kernels. I had initially set out to write a short piece on Random Fourier Features but the subject of kernel theory is vast and beautiful, and that short piece metastasised to three articles.