Class: MDS::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/mds/metric.rb

Overview

In metric MDS dissimilarities are interpreted as distances und the goal is to find an embedding in an Euclidean space that best preserves the given distances.

This implementation gives an analytical solution and avoids iterative optimization. The performance of the algorithm highly depends on the implementation of the eigen-decomposition provided via MatrixInterface.

Examples

  • Examples.minimal_metric

  • Examples.extended_metric

Class Method Summary collapse

Class Method Details

.projectd(d, dims) ⇒ MDS::Matrix

Find a Cartesian embedding for the given distances.

Parameters:

  • d (MDS::Matrix)

    squared Eulcidean distance matrix of observations

  • dims (Integer)

    the number of dimensions of the embedding.

Returns:

  • (MDS::Matrix)

    the matrix containing the cartesian embedding.



51
52
53
54
55
# File 'lib/mds/metric.rb', line 51

def Metric.projectd(d, dims)
  b = Metric.shift(d)
  eval, evec = b.ed
  Metric.project(eval, evec, dims)
end

.projectk(d, k) ⇒ MDS::Matrix

Find a Cartesian embedding for the given distances.

Instead of a fixed dimensionality for the resulting embedding, this method determines the dimensionality based on the variances of distances in its input matrix and the parameter passed. The parameter specifies the percent of variance of distance to preserve in the Cartesian embedding.

to preserve in embedding in the range [0..1].

Parameters:

  • d (MDS::Matrix)

    squared Eulcidean distance matrix of observations

  • k (Float)

    the percent of variance of distances

Returns:

  • (MDS::Matrix)

    the matrix containing the cartesian embedding.



37
38
39
40
41
42
# File 'lib/mds/metric.rb', line 37

def Metric.projectk(d, k)
  b = Metric.shift(d)
  eval, evec = b.ed
  dims = Metric.find_dimensionality(eval, k)
  Metric.project(eval, evec, dims)      
end

.squared_distances(x) ⇒ MDS::Matrix

Calculates the squared Euclidean distances for all pairwise observations in the given matrix. Each observation corresponds to a matrix row and is provided in Cartesian coordinates.

The result is a real symmetric matrix of size NxN, where N is the number of observations in the input. Each element (i,j) in this matrix corresponds to the squared distance between the i-th and j-th observation in the input matrix.

Parameters:

Returns:

  • (MDS::Matrix)

    the squared Euclidean distance matrix



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mds/metric.rb', line 70

def Metric.squared_distances(x)
  # Product of x with transpose of x

  xxt = x * x.t
  # 1xN matrix of ones, where N size of xxt

  ones = Matrix.create(1, xxt.nrows, 1.0)
  # Nx1 matrix containing diagonal elements of x

  diagonals = xxt.diagonals
  c = Matrix.create_block(xxt.nrows, 1) do |i, j|
    diagonals[i]
  end
  c * ones + (c * ones).t - xxt * 2
end