Multidimensional scaling (MDS) is a data visualization technique in which the dimension of the data is reduced in a non-linear way. The data is represented as a \(N\times N\) distance matrix \((d_{ij})_{ij}\), and \(N\) points \(x_i\) in
a \(D\) dimensional space (typically \(D=2\)) are chosen such that the Euclidean distances \(\|x_i - x_j\|\)
resemble the input distances \(d_{ij}\) "as good as possible".
In metric MDS, an objective function \(E(x) = \sum_{1\leq i < j \leq N} (d_{ij} - \|x_i - x_j\|)^2\) is defined
that needs to be minimized. For different flavors of MDS, this objective function is defined differently.
In order to minimize the objective function, e.g. the conjugate gradient descent method is used.
This method requires that one calculates the gradient \(\nabla E\) of the objective function. Of course, this is not
so difficult is the case of metric MDS, but more difficult objective functions might require more effort.
Enter
Stan.
Stan uses automatic differentiation for Hamiltonian Monte Carlo, but Stan can also be used for maximum likelihood. Hence, if we can formulate the MDS problem in terms of a likelihood function, we can let Stan do all the work. The parameters of the model are the \(x_i\), the data is given by the distances \(d_{ij}\). If we assume that given the parameters, the data is distributed as
\[
d_{ij} \sim \mathcal{N}(\|x_i - x_j\|, \sigma^2)\,,
\]
then maximizing the (log) likelihood is equivalent to minimizing the function \(E\).
The parameter \(\sigma^2\) is merely a nuisance parameter that needs to be estimated as well.
An implementation of MDS in the Stan programming language
Implementing MDS in Stan is fairly straightforward,
but there are a few snags that we should be aware of.
First, if \(x\) solves the MDS problem, then also any Euclidean transformation of \(x\) is a solution.
Hence, the model as stated above has too many parameters. We solve this by fixing the first
point at the origin, restricting the next point to a \(1\)-dimensional half space, the third point
to a \(2\)-dimensional half space et cetera. The last \(N-D-1\) points are unrestricted.
In Stan, we can accomplish this by using a
cholesky_factor_cov
matrix:
A positive-definite lower-trangular matrix. We then use the
transformed parameters
block to concatenate the points together into a single matrix.
Secondly, the data that we use in the example below is highly censored.
Many of the distances are missing, and some are right censored. In such a case
MDS can be used to infer the missing distances, and not merely visualize the data.
The data that is passed to Stan, therefore, is a list of edges, a list of distances,
and a list of codes that determine the type of censoring.
Thirdly, as the title of this post suggests, we will use Stan to do some sort of
Bayesian MDS. In this case, we will sample a collection of "maps" \(x\) from a posterior distribution,
that gives information about the location of each point, but also the uncertainty of this location.
In this case, the fact that we restrict the first \(D+1\) points, comes back to bite us, as the
uncertainty of these points will be different than the unrestricted points.
Furthermore, it might be hard to compare the individual maps to one another,
and for instance compute sensible mean locations of the points, as some maps may be
"twisted" more than others.
Therefore, we use the
generated quantities
block to center and rotate
(cf. PCA) the sampled maps.
Example: Antigenic cartography of the influenza virus
An interesting application of MDS is antigenic cartography of the influenza virus.
Influenza virus circumvents human antibody responses by continually evolving its surface proteins,
in particular, hemagglutinin (HA). This is known as antigenic drift.
In order to decide whether flu vaccines need to be updated,
the hemagglutination inhibition (HI) assay is used to determine if the induced antibody response is still
effective against future strains. The titers measured in the HI assay can be used to define "distances"
between antisera and antigens. Using MDS, the antisera and antigens can be drawn into a "map",
that shows the antigenic drift of the virus. This was done by
Smith et al. in 2004.
Conveniently, the data used for their map is
available online.
This table gives HI titers \(H_{ij}\) of antigen \(i\) and antiserum \(j\).
A small titer corresponds to a large distance, which are defined as \(d_{ij} = \log_2(H_{\max,j}) - \log_2(H_{ij})\),
where \(H_{\max,j} = \max_{k} H_{kj}\).
As an example, I recreated their antigenic map using the Stan model above, and a Python script below.
The white squares denote the relative positions of the antisera in the "antigenic space", while the colored circles
represent the antigens. The colors map to the years in which the infuenza strains were isolated.
Bayesian multidimensional scaling
For antigenic cartography of IAV, Bayesian MDS has been introduced by
Bedford et al.,
who used multiple HI assay results per antigen/antiserum pair to incorporate the uncertainty of these measurements in their antigenic map. Moreover, they were able to use genetic and temporal information about the antigens (i.e. the RNA sequences of HA and their isolation dates) to inform the position of the antigens and antisera on the map. We will not go this far in this post, but since we have already formulated the MDS algorithm in Stan, we might as well make a "Bayesian" antigenic map. This can give some insight into the uncertainty of the positions of the antigens and antisera. This is not unlike the confidence areas as drawn by Smith et al. (the ellipsoid shapes). The result is given by the following figure.
Again, squares indicate antisera and colored circles the antigens. All the individual MCMC samples are represented by the grey dots.
The MCMC samples for each antigen or antiserum are used to draw a two-dimensional error bar (i.e. ellipse) around the mean location.
A Python script for parsing the HI titer data, compiling and running the Stan model and drawing the maps is added below.
For it to work, you will need to download the
mds_model.stan
file and make a csv file called
baselinemap.csv
with the
HI table