← All algorithms

Dimensionality Reduction

Multidimensional Scaling, MDS

2023-12-23

Multidimensional scaling is a method of finding the position of samples in reverse when the correlation of distances between each sample is given. Recalling that PCA found the projection plane that maximizes the variance of the given samples through eigenvalue analysis, MDS is a method of finding the position of samples through the eigenvalue analysis of the matrix of the multiplications between samples which derived from the distance matrix.

MDS is used to display data in low dimensions when the values of the samples are already known, or to estimate those values in low dimensions when the values of the samples are unknown.

If there is sample data X which has n number of samples and d number of features, its array size is [nxd] matrix. The distance matrix D between any two samples xi and xj which belong to X are defined as below and its size is [nxn] matrix.

\(D_{ij}=(x_i-x_j)(x_i-x_j)^T\)

which can expand to

\(D_{ij}=x_ix_i^T+x_jx_j^T-2x_ix_j^T\)

If we define the multiplication matrix of two samples xixjT≡Bij, and the X has been standardized at the mean (i.e. Σxi = 0) during preprocessing, Bij can be expressed as a function of Dij. This is the core idea of MDS.

Since B = XXT, B is symmetric and positive definite matrix of size [nxn] (because [nxd][dxn]=[nxn]). B can be decomposed by B = VΛVT by the eigenvalue decomposition, where V = [u1 u2 u3.. ud 0 0 0], the eigenvector matrix and size of each eigenvector u is [nx1], Λ is [nxn] diagonal matrix with d eigenvalues)

​The low-dimensional representation X is defined by VΛ1/2.

​

The relationship between the distance matrix D and multiplication matrix B can be expressed as following. The proof is given at the <Note> at the end of the blog.

\(B_{ij}=-\dfrac{1}{2}\left(D_{ij}-\dfrac{1}{n}\sum _j^{\ }D_{ij}-\dfrac{1}{n}\sum _i^{\ }D_{ij}+\dfrac{1}{n^2}\sum _i^{\ }\sum _j^{\ }D_{ij}\right)\).

And its matrix form becomes,

\(B=-\dfrac{1}{2}HDH\)

Here, \(H=\textbf{I}-\dfrac{1}{n} \textbf {11}^T\),

where I is the identy matrix of size [nxn], 1 is the vector of one, and 11T is the matrix of one. The matrix H is the size of [nxn] and all of its diagonal terms is 1-1/n, and off-diagonal terms is 1/n.

The matrix B can be expressed in the following component format.

\(B_{ij}=-\dfrac{1}{2}(D_{ij}-\mu_i-\mu_j+\mu_{ij})\)

where μi is the row-wise average of the Dij, μj is the column-wise average of the Dij, and μij is the grand average.


For the implementation of MDS in Excel, we use the same Swiss Roll dataset been used for LLE. The step by steps are as follows: 

Step 1. Data generation. Refer the LLE blog.

Step 2. Calculate the distance matrix, Dij, by using the SUMXMY2 function.

Dij =SUMXMY2(INDEX($A$1:$C$501,$E2,0),INDEX($A$1:$C$501,F$1,0))

INDEX(Array, r, 0) function returns the sample data at the row location = r.

SUMXMY2(xi,xj) function returns the Euclidean distance ||xi-xj||2 between xi and xj.

​

Step 3. Calculate Bij matrix by using Dij, μi, μj, and μij

\(B_{ij}=-\dfrac{1}{2}(D_{ij}-\mu_i-\mu_j+\mu_{ij})\)

​

Step 4. Eigenvalue decomposition toward B by using the Power method (e.g. NIPALS) to find the first eigenvectors V1 and eigenvalues Λ1 lower dimensional representation.

\(X_1=V_1\Lambda_1^{1/2}\)

The figure below shows the radial distance from the origin vs. X1. As shown in the plot, MDS attempts to preserve the distances between samples at the lower dimensional mapping.

​

<Note> Proof of the relationship between B and D

From the definitions of B and D

\(D_{ij}=x_ix_i^T+x_jx_j^T-2x_ix_j^T=x_ix_i^T+x_jx_j^T-2B_{ij}\). 

B becomes

\(B_{ij}=-\dfrac{1}{2}\left(D_{ij}-x_ix_i^T-x_jx_j^T\right)\).  (1)

Column-wise average Dij

\(\dfrac{1}{n}\sum _j^{\ }D_{ij}=\dfrac{1}{n}\sum _j^{\ }x_ix_i^T+\dfrac{1}{n}\sum _j^{\ }x_jx_j^T-\dfrac{1}{n}\sum _j^{\ }2x_ix_j^T\)

\(=x_ix_i^T+\dfrac{1}{n}\sum _j^{\ }x_jx_j^T-\dfrac{2}{n}x_i \sum _j^{\ }x_j^T=x_ix_i^T+\dfrac{1}{n}\sum _j^{\ }x_jx_j^T\) ​          

where \(\sum _j^{\ }x_j^T\) is zero since X are standardized at the mean.

Rearrange the result to define the \(-x_ix_i^T\)

\(-x_ix_i^T=-\dfrac{1}{n}\sum _j^{\ }D_{ij}+\dfrac{1}{n}\sum _j^{\ }x_jx_j^T\)  (2)

Row-wise average Dij

\(\dfrac{1}{n}\sum _i^{\ }D_{ij}=\dfrac{1}{n}\sum _i^{\ }x_ix_i^T+\dfrac{1}{n}\sum _i^{\ }x_jx_j^T-\dfrac{1}{n}\sum _i^{\ }2x_ix_j^T\)

\(=\dfrac{1}{n}\sum _i^{\ }x_ix_i^T+x_jx_j^T-\dfrac{2}{n}\left(\sum _i^{\ }x_i\right)x_i^T=\dfrac{1}{n}\sum _i^{\ }x_ix_i^T+x_jx_j^T\)

Rearrange it to define the \(-x_jx_j^T\)

\(-x_jx_j^T=-\dfrac{1}{n}\sum _i^{\ }D_{ij}+\dfrac{1}{n}\sum _i^{\ }x_ix_i^T\)  (3)

Find Grand Average of the distance matrix.

\(\dfrac{1}{n^2}\sum _i^{\ }\sum _j^{\ }D_{ij}=\dfrac{1}{n}\sum _i^{\ }x_ix_i^T+\dfrac{1}{n^2}\sum _i^{\ }\sum _j^{\ }x_jx_j^T\)

\(=\dfrac{1}{n}\sum _i^{\ }x_ix_i^T+\dfrac{1}{n}\sum _j^{\ }x_jx_j^T\) (4)

Now plug (2),(3),(4) into (1) to find the formula for B as a function of D

\(B_{ij}=-\dfrac{1}{2}\left(D_{ij}-x_ix_i^T-x_jx_j^T\right)\)

\(=-\dfrac{1}{2}\left(D_{ij}-\dfrac{1}{n}\sum_{j}^{\ }D_{ij}+\dfrac{1}{n}\sum_ {j}^{\ }x_jx_j^T-\dfrac{1}{n}\sum_{i}^{\ }D_{ij}+\dfrac{1}{n}\sum_{i}^{\ }x_ix_i^T\right)\)

\(=-\dfrac{1}{2}\left(D_{ij}-\dfrac{1}{n}\sum_{j}^{\ }D_{ij}-\dfrac{1}{n}\sum _{i}^{\ }D_{ij}+\dfrac{1}{n}\sum_{j}^{\ }x_jx_j^T+\dfrac{_1}{n}\sum_{i}^{\ }x_ix_i^T\right)\)

\(=-\dfrac{1}{2}\left(D_{ij}-\dfrac{1}{n}\sum_{j}^{\ }D_{ij}-\dfrac{1}{n}\sum _{i}^{\ }D_{ij}+\dfrac{1}{n^2}\sum_{i}^{\ }\sum_{j}^{\ }D_{ij}\right)\)