--- title: "Metric MDS" author: "James Melville" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Metric MDS} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE, echo = FALSE, message = FALSE} knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, comment = "#>") library(mize) ``` Minimizing the Rosenbrock function is all very well, but there's often a more complex relationship between the parameters we're looking to optimize and what would be convenient to pass to a cost function. In many cases there are other quantities that we need to define and store somewhere for the cost function to provide a value, but which aren't under our direct control and hence are not suitable for optimization. They just need to be available for evaluation inside `fn` and `gr`. This vignette is less to do with the running of `mize` itself, and more to do with using it to solve an actual problem. It contains plenty of R code, but not a lot of it is `mize`-specific. Hopefully it will demonstrate how it can be used for non-trivial problems. ### Metric Multi-Dimensional Scaling Metric multi-dimensional scaling (Metric MDS) is a way to provide a low-dimensional (usually two-dimensional) view of a high dimensional data set, although all it needs is a distance matrix to work off. Let's take the `eurodist` dataset as an example, which can be found in the `datasets` package. The description says: > The `eurodist` gives the road distances (in km) between 21 cities in Europe. We should be able to reconstruct the relative locations of the 21 cities on a 2D plot with this information, subject to some error because roads aren't straight between the cities and the cities themselves lie on the non-flat surface of the Earth. ### The Metric MDS Cost Function The parameters we're going to optimize are the 2D locations of each city. The cost function, however, is more naturally expressed as being related to the distance matrices. What we want is for the distance matrix of the output, which we'll call $D$, to resemble the input distance matrix, $R$, as much as possible. One obvious way to express this as a cost function is to consider the square loss: $$C = \sum_{i