SHRIPHANI PALAKODETY: Posts tagged 'spectral-clustering'SHRIPHANI PALAKODETY: Posts tagged 'spectral-clustering'
http://blog.shriphani.com/tags/spectral-clustering.html
Tue, 07 Apr 2015 02:54:43 UTTue, 07 Apr 2015 02:54:43 UT1800The Smallest Eigenvalues of a Graph Laplacian
http://blog.shriphani.com/2015/04/06/the-smallest-eigenvalues-of-a-graph-laplacian/?utm_source=spectral-clustering&utm_medium=RSS
urn:http-blog-shriphani-com:-2015-04-06-the-smallest-eigenvalues-of-a-graph-laplacianTue, 07 Apr 2015 02:54:43 UT<html>
<p>Given a graph $ G = (V, E) $, its adjacency matrix $ A $ contains an entry at $ A_{ij} $ if vertices $ i $ and $ j $ have an edge between them. The degree matrix $ D $ contains the degree of each vertex along its diagonal.</p>
<p>The graph laplacian of $ G $ is given by $ D - A $.</p>
<p>Several popular techniques leverage the information contained in this matrix. This blog post focuses on the two smallest eigenvalues.</p>
<p>First, we look at the eigenvalue 0 and its eigenvectors. A very elegant result about its multiplicity forms the foundation of spectral clustering.</p>
<p>Then we look at the second smallest eigenvalue and the corresponding eigenvector. A slightly more involved result (YMMV) allows us to partition the graph in question. A recent publication by John Urschel (who apparently moonlights as a sportsperson) focused on this quantity.</p>
<p>The insights provided here sacrifice some rigor for the sake of brevity. I find such descriptions help me study without getting bogged down too much with details. A bibliography provided at the end contains links to actual proofs.</p>
<!-- more-->
<h1 id="eigenvalue-0">Eigenvalue 0</h1>
<h2 id="the-insight">The Insight</h2>
<p>The zero-th eigenvalue tells us whether the graph is connected or not.</p>
<p>In particular, if a graph has $ k $ connected components, then eigenvalue 0 has multiplicity $ k $ (i.e. $ k $ distinct non-trivial eigenvectors).</p>
<p>A blueprint for the proof looks like this (detailed proof provided later):</p>
<ul>
<li>The eigenvector corresponding to eigenvalue 0 (known hereafter as $ \lambda_0 $) must contain some non-zero entries - (this is established by showing that $ L $ is positive semi-definite (psd)).</li>
<li>In fact, if vertices $ i $ and $ j $ are connected, then components $ i $ and $ j $ in $ \lambda_0 $ must be equal.</li>
<li>If the graph is connected, apply transitive property and you get an eigenvector where all the components are equal (or all components set to 1).</li></ul>
<p> This looks like so: <img src="/img/ev_all_ones.svg" /></p>
<ul>
<li>If the graph is not connected, then consider each connected component separately and run this procedure on it again. For $ k $ connected portions of the graph, we should have $ k $ distinct eigenvectors, each of which contains a distinct, disjoint set of components set to 1.</li></ul>
<p> So, if the graph has 2 connected components, then the eigenvalue 0 has 2 non-trivial eigenvectors:
<br /> <img src="/img/several_evs.svg" /></p>
<p> In the diagram above, the vertices 1, 2, and 3 form one connected component and vertices 0, 4, and 5 form the other component.</p>
<p> A toy example illustrates this nicely.</p>
<p> In this example, we have a graph with 6 vertices. Say the graph is connected.</p>
<p> Let us see what the value of the desired eigenvector is $ < 1, 1, 1, 1, 1, 1 > $.</p>
<p> For 2 connected components, this value possibly is $ < 1, 1, 1, 0, 0, 0 > and < 0, 0, 0, 1, 1, 1 > $</p>
<p> And so on.</p>
<h2 id="from-eigenvectors-to-clustering">From Eigenvectors to Clustering</h2>
<p>The gist of the previous section is:</p>
<p>(i) The multiplicity of eigenvalue 0 equals the number of connected components. (ii) If vertices $ i $ and $ j $ are connected, then in a certain eigenvector corresponding to the eigenvalue zero, the $ i^{th} $ and $ j^{th} $ components are set to 1.</p>
<p>The next task is to use these insights for clustering a set of points.</p>
<p>A simple strategy seems to be (i) constructing a graph from this point set, (ii) ensuring that different clusters become different connected components in this graph, and (iii) look at the eigenvectors of the first eigenvalue for some assistance in discovering these clusters.</p>
<p>The first two steps are trivial. We can build a $ k $-NN graph - a graph where each point in the dataset is a vertex and an edge exists between this point and the $ k $ points closest to it.</p>
<p>So this diagram illustrates this setting. Let us say we have 6 points in the dataset and 2 clusters.</p>
<p>Let us construct a $ k $-NN graph - I’m going to set $ k \leftarrow 2 $ (this is for convenience). The $ k $-NN graph looks like:</p>
<p><img src="/img/toy_knn.svg" /></p>
<p>Thus there are 2 connected components.</p>
<p>The insight from the previous section tells us that the eigenvalue 0 has multiplicity 2 and the 2 distinct eigenvectors look like:</p>
<p><img src="/img/toy_evs.svg" /></p>
<p>Consider a matrix with these eigenvectors as its columns:</p>
<div>$$ \begin{bmatrix} 1 & 0\\ 1 & 0\\ 1 & 0\\ 0 & 1\\ 0 & 1\\ 0 & 1\\ \end{bmatrix} $$</div>
<p>This matrix has as many rows as the original dataset.</p>
<p>It also has the effect of causing the clusters in the graph to pop out.</p>
<p>In this example, I can tell that the first three points belong in the same cluster. The next three points form the second cluster.</p>
<p>If you supply this matrix to any classic clustering algorithm (say $ k $-means), it should have no issues clustering this and assigning points to the correct clusters.</p>
<p>This is exactly what spectral clustering does.</p>
<p>Thus the steps involved are:</p>
<ul>
<li>Construct a $ k $-NN graph (or indeed any other graph - say one that uses a threshold test on euclidean distances).</li>
<li>Obtain the laplacian of this graph.</li>
<li>Obtain the eigendecomposition of the laplacian, retain the first $ k $ columns of the eigenvector matrix.</li>
<li>Supply this matrix to $ k $-means (or your favorite clustering algorithm).</li></ul>
<p>Spectral clustering deals well with non-convex cluster shapes because of the underlying graph constructed. The manifold considered as a result captures the shape of the clusters reasonably well - something we cannot accomplish if only euclidean distances are used:</p>
<p><img src="/img/non_convex.svg" /></p>
<p>This is a neat trick exploited by the <a href="http://blog.shriphani.com/2014/11/12/the-isomap-algorithm/">isomap algorithm</a> which was covered in a previous post.</p>
<p>Constructing the graph tends to be a bit involved - often there isn’t a clear way to build one. The performance of spectral clustering depends on how the connected components in the graph reflect clusters in the dataset.</p>
<p>A connected graph (which you can produce quite easily by picking a large $ k $) will yield poor results.</p>
<p>Despite these issues, spectral clustering is a very powerful and well-studied technique and belongs in any practitioner’s toolbox (IMO).</p>
<h1 id="second-smallest-eigenvalue-of-the-laplacian">Second Smallest Eigenvalue of the Laplacian</h1>
<p>For the sake of brevity, I will call this quantity $ \lambda_1 $. I will call the associated eigenvector $ v_1 $.</p>
<p>M. Fiedler in his landmark monograph called this quantity the algebraic connectivity of a graph. $ \lambda_1 $ and its eigenvectors provide amazing insights.</p>
<p>One of the insights is:</p>
<p>If $ \lambda_1 = 0 $ clearly eigenvalue 0 has multiplicity greater than 1. Thus the graph is not connected.</p>
<p>This is fairly trivial to establish - the insight from the previous section covers it.</p>
<p>The next insight, my favorite, involves partitioning a graph.</p>
<p>When we partition a graph (into say 2 partitions), we desire 2 reasonably large groups of vertices with very few edges between them.</p>
<p>Observe that this exercise is a waste of time if the graph isn’t connected (there are already two distinct components with no edges between them). Thus it makes sense to only consider connected graphs.</p>
<p>Let us try to give a formal shape to the partitioning problem.</p>
<p>Partitioning can be defined as assigning a value of $ +1 $ or $ –1 $ to each vertex. Vertices with different assignments are in different partitions.</p>
<p>Say vertex $ v_i $ gets assigned value $ x_i $.</p>
<p>Let us assume a perfect paritioning. Exactly $ |V|/2 $ points are assigned and $ x_i = +1 $ and the other half assigned $ x_i = –1 $.</p>
<p>Now, a pair of vertices $ v_i $ and $ v_j $ that belong to <em>different partitions</em> are assigned values $ x_i $ and $ x_j $ where $ x_i \neq x_j $. Thus, the only possible value for $ (x_i - x_j)^{2} $ is $ 4 $.</p>
<p>For each edge from one partition to the other, we have a value of 4. Thus, the number of edges from one partition to the other is given by:</p>
<div> $$ \sum_{i=1, j=i}^{i=n, j=n} \frac{(x_i - x_j)^2}{4} $$</div>
<p>Also, assuming a perfect paritioning, an equal number of vertices are assigned values $ +1 $ and $ –1 $. Thus we have:</p>
<div> $$ \sum_{i} x_i = 0 $$</div>
<p>Our objective is to minimize the number of edges from one partition to the other while achieving a reasonable size for each partition.</p>
<p>This can be expressed as the following optimization function:</p>
<p>Minimize</p>
<div>$$ \sum_{i=1, j=i}^{i=n, j=n} \frac{(x_i - x_j)^2}{4} $$</div>
<p>with the constraint</p>
<div>$$ \sum_{i} x_i = 0 $$</div>
<p>.</p>
<p>This unfortunately has a trivial solution. Set all $ x_i $ to 0.</p>
<p>An additional constraint eliminates this problem. The new constraint is:</p>
<div> $$ \sum_{i} {x_i}^{2} = |V| $$</div>
<p>We work with matrix variants of these equations. Clearly, the component responsible for the number of edges looks like $ \frac{x^T \mathcal{L} x}{4} $.</p>
<p>The constraint that enforces reasonable partition sizes is $ x^T\mathcal{1} = 0$. Here $ \mathcal{1} $ is a vector of all ones.</p>
<p>Finally, the term responsible for ensuring a non-trivial solution is $ x^Tx = |V| $.</p>
<p>And the lagrangian looks like:</p>
<div> $$ \nabla \frac{x^T \mathcal{L} x}{4} - \nabla \eta_1 (x^Tx - |V|) - \nabla \eta_2 (x^Te) = 0 $$</div>
<p>which becomes:</p>
<div> $$ \mathcal{L} x - \eta_1 x - \eta_2 \mathcal{1} = 0 $$</div>
<p>Multiply by $ \mathcal{1}^T $ on both sides:</p>
<div> $$ \mathcal{1}^T \mathcal{L} x - \eta_1 \mathcal{1}^T x - \eta_2 \mathcal{1}^T \mathcal{1} = 0 $$</div>
<p>This essentially becomes:</p>
<div> $$ \mathcal{L}x - \eta_1x = 0 $$</div>
<p>Thus $ x $ is clearly an eigenvector of the graph laplacian and $ \eta_1 $ is an eigenvalue.</p>
<p>Obviously, the eigenvector of the eigenvalue 0 doesn’t work (it assigns the value 1 to all points).</p>
<p>Clearly $ v_1 $ (the eigenvector of the second smallest eigenvalue) is a solution (the intuition is that the smaller the eigenvalue, the fewer the edges between the two partitions).</p>
<p>Thus, the eigenvector $ v_1 $ (a.k.a the Fiedler vector) provides an assignment to each vertex in the graph. This assignment can be used to partition the graph.</p>
<p>There is just one issue here. The eigenvector contains real values, not necessarily $ 1 $ and $ –1 $.</p>
<p>A whole host of tricks can be applied to convert the entries in the eigenvector to $ +1 $ and $ –1 $:</p>
<div>
<ul>
<li>$ sgn(v_{1_{i}}) $ i.e. vertex $ i $ gets assigned a value $ x_i = $ the sign of the $ i^{th} $ component of the eigenvector $ v_1 $.</li>
<li>$ x_i = +1 $ if $ v_{1_i} > m $, $ -1 $ otherwise. Here $ m $ is the median of all the eigenvalue components (or the mean or zero or whatever).</li></ul></div>
<p>And this is how the Fiedler vector helps with graph partitioning.</p>
<p>The bibliography attached to this post contains some amazing literature that I had a lot of fun reading. I have mirrored these documents in github and provided the github link.</p>
<h1 id="bibliography">Bibliography</h1>
<ol>
<li><a href="/docs/Luxburg07_tutorial.pdf">A Tutorial on Spectral Clustering</a> - Ulrike von Luxburg - A self-contained and elaborate tutorial on spectral clustering.</li>
<li><a href="/docs/Algebraic_Connectivity.pdf">Algebraic Connectivity of Graphs</a> - Miroslav Fiedler - A landmark paper on the properties of the second smallest eigenvalue and its associated eigenvector</li>
<li><a href="/docs/Pothen89Partition.pdf">Partitioning Sparse Matrices with Eigenvectors of Graphs</a> - Alex Pothen, Horst Simon, Kang-Pu Paul Liu - An algorithm to leverage the Fiedler vector for graph Partitioning</li></ol></html>