-
Notifications
You must be signed in to change notification settings - Fork 85
/
cmds.jl
308 lines (247 loc) · 9.07 KB
/
cmds.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Classical Multidimensional Scaling
## convert Gram matrix to Distance matrix
"""
gram2dmat!(D, G)
Convert a Gram matrix `G` to a distance matrix, and write the results to `D`.
"""
function gram2dmat!(D::AbstractMatrix{DT}, G::AbstractMatrix) where {DT<:Real}
# argument checking
m = size(G, 1)
n = size(G, 2)
m == n || error("D should be a square matrix.")
size(D) == (m, n) ||
throw(DimensionMismatch("Sizes of D and G do not match."))
# implementation
for j = 1:n
for i = 1:j-1
@inbounds D[i,j] = D[j,i]
end
D[j,j] = zero(DT)
for i = j+1:n
@inbounds D[i,j] = sqrt(G[i,i] + G[j,j] - 2 * G[i,j])
end
end
return D
end
"""
gram2dmat(G)
Convert a Gram matrix `G` to a distance matrix.
"""
gram2dmat(G::AbstractMatrix{T}) where {T<:Real} = gram2dmat!(similar(G, T), G)
## convert Distance matrix to Gram matrix
"""
dmat2gram!(G, D)
Convert a distance matrix `D` to a Gram matrix, and write the results to `G`.
"""
function dmat2gram!(G::AbstractMatrix{GT}, D::AbstractMatrix) where GT
# argument checking
n = LinearAlgebra.checksquare(D)
size(G) == (n, n) ||
throw(DimensionMismatch("Sizes of G and D do not match."))
# implementation
u = zeros(GT, n)
s = 0.0
for j = 1:n
s += (u[j] = sum(abs2, view(D,:,j)) / n)
end
s /= n
for j = 1:n
for i = 1:j-1
@inbounds G[i,j] = G[j,i]
end
for i = j:n
@inbounds G[i,j] = (u[i] + u[j] - abs2(D[i,j]) - s) / 2
end
end
return G
end
momenttype(T) = typeof((zero(T) * zero(T) + zero(T) * zero(T))/ 2)
"""
dmat2gram(D)
Convert a distance matrix `D` to a Gram matrix.
"""
dmat2gram(D::AbstractMatrix{T}) where {T<:Real} = dmat2gram!(similar(D, momenttype(T)), D)
## Classical MDS
"""
*Classical Multidimensional Scaling* (MDS), also known as Principal Coordinates Analysis (PCoA),
is a specific technique in this family that accomplishes the embedding in two steps:
1. Convert the distance matrix to a Gram matrix. This conversion is based on
the following relations between a distance matrix ``D`` and a Gram matrix ``G``:
```math
\\mathrm{sqr}(\\mathbf{D}) = \\mathbf{g} \\mathbf{1}^T + \\mathbf{1} \\mathbf{g}^T - 2 \\mathbf{G}
```
Here, ``\\mathrm{sqr}(\\mathbf{D})`` indicates the element-wise square of ``\\mathbf{D}``,
and ``\\mathbf{g}`` is the diagonal elements of ``\\mathbf{G}``. This relation is
itself based on the following decomposition of squared Euclidean distance:
```math
\\| \\mathbf{x} - \\mathbf{y} \\|^2 = \\| \\mathbf{x} \\|^2 + \\| \\mathbf{y} \\|^2 - 2 \\mathbf{x}^T \\mathbf{y}
```
2. Perform eigenvalue decomposition of the Gram matrix to derive the coordinates.
*Note:* The Gramian derived from ``D`` may have non-positive or degenerate
eigenvalues. The subspace of non-positive eigenvalues is projected out
of the MDS solution so that the strain function is minimized in a
least-squares sense. If the smallest remaining eigenvalue that is used
for the MDS is degenerate, then the solution is not unique, as any
linear combination of degenerate eigenvectors will also yield a MDS
solution with the same strain value.
"""
struct MDS{T<:Real} <: NonlinearDimensionalityReduction
d::Real # original dimension
X::AbstractMatrix{T} # fitted data, X (d x n)
λ::AbstractVector{T} # eigenvalues in feature space, (k x 1)
U::AbstractMatrix{T} # eigenvectors in feature space, U (n x k)
end
## properties
"""
size(M)
Returns tuple where the first value is the MDS model `M` input dimension,
*i.e* the dimension of the observation space, and the second value is the output
dimension, *i.e* the dimension of the embedding.
"""
size(M::MDS) = (M.d, size(M.U,2))
"""
projection(M)
Get the MDS model `M` eigenvectors matrix (of size ``(n, p)``) of the embedding space.
The eigenvectors are arranged in descending order of the corresponding eigenvalues.
"""
projection(M::MDS) = M.U
"""
eigvecs(M::MDS)
Get the MDS model `M` eigenvectors matrix.
"""
eigvecs(M::MDS) = projection(M)
"""
eigvals(M::MDS)
Get the eigenvalues of the MDS model `M`.
"""
eigvals(M::MDS) = M.λ
"""
loadings(M::MDS)
Get the loading of the MDS model `M`.
"""
loadings(M::MDS) = sqrt.(M.λ)' .* M.U
## use
"""
predict(M, x::AbstractVector)
Calculate the out-of-sample transformation of the observation `x` for the MDS model `M`.
Here, `x` is a vector of length `d`.
"""
function predict(M::MDS, x::AbstractVector{T}; distances=false) where {T<:Real}
d = if isnan(M.d) # model has only distance matrix
@assert distances "Cannot transform points if model was fitted with a distance matrix. Use point distances."
size(x, 1) != size(M.X, 1) && throw(
DimensionMismatch("Point distances should be calculated to all original points"))
x
else
if distances
size(x, 1) != size(M.X, 2) && throw(
DimensionMismatch("Point distances should be calculated to all original points."))
x
else
size(x, 1) != size(M.X, 1) && throw(
DimensionMismatch("Points and original data must have same dimensionality."))
pairwise((x,y)->norm(x-y), M.X, x)
end
end
# get distance matrix
D = isnan(M.d) ? M.X : pairwise((x,y)->norm(x-y), eachcol(M.X), symmetric=true)
d = d.^2
# b = 0.5*(ones(n,n)*d./n - d + D*ones(n,1)./n - ones(n,n)*D*ones(n,1)./n^2)
mD = mean(D.^2, dims=2)
b = (d .- mean(d, dims=1) .- mD .+ mean(mD)) ./ -2
# sqrt(λ)⁻¹U'b
λ = vcat(M.λ, zeros(T, size(M)[2] - length(M.λ)))
return M.U' * b ./ sqrt.(λ)
end
"""
predict(M)
Returns a coordinate matrix of size ``(p, n)`` for the MDS model `M`, where each column
is the coordinates for an observation in the embedding space.
"""
function predict(M::MDS{T}) where {T<:Real}
d, p = size(M)
# if there are non-positive missing eigval then pad with zeros
λ = vcat(M.λ, zeros(T, p - length(M.λ)))
return diagm(0=>sqrt.(λ)) * M.U'
end
## show
function show(io::IO, M::MDS)
d, p = size(M)
print(io, "Classical MDS(indim = $d, outdim = $p)")
end
## interface functions
"""
fit(MDS, X; kwargs...)
Compute an embedding of `X` points by classical multidimensional scaling (MDS).
There are two calling options, specified via the required keyword argument `distances`:
mds = fit(MDS, X; distances=false, maxoutdim=size(X,1)-1)
where `X` is the data matrix. Distances between pairs of columns of `X` are computed using the Euclidean norm.
This is equivalent to performing PCA on `X`.
mds = fit(MDS, D; distances=true, maxoutdim=size(D,1)-1)
where `D` is a symmetric matrix `D` of distances between points.
"""
function fit(::Type{MDS}, X::AbstractMatrix{T};
maxoutdim::Int = size(X,1)-1,
distances::Bool) where T<:Real
# get distance matrix and space dimension
D, d = if !distances
pairwise((x,y)->norm(x-y), eachcol(X), symmetric=true), size(X,1)
else
X, NaN
end
G = dmat2gram(D)
n = size(D, 1)
m = min(maxoutdim, n) #Actual number of eigenpairs wanted
E = eigen!(Hermitian(G))
#Sometimes dmat2gram produces a negative definite matrix, and the sign just
#needs to be flipped. The heuristic to check for this robustly is to check
#if there is a negative eigenvalue of magnitude larger than the largest
#positive eigenvalue, and flip the sign of eigenvalues if necessary.
mineig, maxeig = extrema(E.values)
if mineig < 0 && abs(mineig) > abs(maxeig)
#do flip
ord = sortperm(E.values)
λ = -E.values[ord[1:m]]
else
ord = sortperm(E.values; rev=true)
λ = E.values[ord[1:m]]
end
for i in 1:m
if λ[i] <= 0
#Keeping all remaining eigenpairs would not change solution (if 0)
#or make the answer _worse_ (if <0).
#The least squares solution would want to throw all these away.
@warn("Gramian has only $(i-1) positive eigenvalue(s)")
m = i-1
ord = ord[1:m]
λ = λ[1:m]
break
end
end
#Check if the last considered eigenvalue is degenerate
if m>0
nevalsmore = sum(abs.(E.values[ord[m+1:end]] .- λ[m]) .< n*eps())
nevals = sum(abs.(E.values .- λ[m]) .< n*eps())
if nevalsmore > 1
@warn("The last eigenpair is degenerate with $(nevals-1) others; $nevalsmore were ignored. Answer is not unique")
end
end
U = E.vectors[:, ord[1:m]]
#Add trailing zero coordinates if dimension of embedding space (p) exceeds
#number of eigenpairs used (m)
if m < maxoutdim
U = [U zeros(T, n, maxoutdim-m)]
end
return MDS(d, X, λ, U)
end
"""
stress(M)
Get the stress of the MDS mode `M`.
"""
function stress(M::MDS)
# calculate distances if original data was stored
DX = isnan(M.d) ? M.X : pairwise((x,y)->norm(x-y), eachcol(M.X), symmetric=true)
DY = pairwise((x,y)->norm(x-y), eachcol(predict(M)), symmetric=true)
n = size(DX,1)
return sqrt(2*sum((DX - DY).^2)/sum(DX.^2));
end