-
Notifications
You must be signed in to change notification settings - Fork 96
/
mahalanobis.jl
164 lines (138 loc) · 5.21 KB
/
mahalanobis.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
# Mahalanobis distances
"""
Mahalanobis(Q; skipchecks=false) <: Metric
Create a Mahalanobis distance (i.e., a bilinear form) with covariance matrix `Q`.
Upon construction, both symmetry/self-adjointness and positive semidefiniteness are checked,
where the latter check can be skipped by passing the keyword argument `skipchecks = true`.
# Example:
```julia
julia> A = collect(reshape(1:9, 3, 3)); Q = A'A;
julia> dist = Mahalanobis(Q)
Mahalanobis{Matrix{Int64}}([14 32 50; 32 77 122; 50 122 194])
julia> dist = Mahalanobis(A, skipchecks=true)
┌ Warning: matrix is not symmetric/Hermitian
└ @ Distances ...
Mahalanobis{Matrix{Int64}}([1 4 7; 2 5 8; 3 6 9])
"""
struct Mahalanobis{M<:AbstractMatrix} <: Metric
qmat::M
function Mahalanobis(Q::AbstractMatrix; skipchecks::Bool=false)
# TODO: turn the warnings into errors in next breaking release
ishermitian(Q) || @warn "matrix is not symmetric/Hermitian"
if !skipchecks
eigmin(Q) ≥ 0 || @warn "matrix is not positive semidefinite"
end
return new{typeof(Q)}(Q)
end
end
"""
SqMahalanobis(Q; skipchecks=false) <: Metric
Create a squared Mahalanobis distance (i.e., a bilinear form) with covariance matrix `Q`.
Upon construction, both symmetry/self-adjointness and positive semidefiniteness are checked,
where the latter check can be skipped by passing the keyword argument `skipchecks = true`.
# Example:
```julia
julia> A = collect(reshape(1:9, 3, 3)); Q = A'A;
julia> dist = SqMahalanobis(Q)
SqMahalanobis{Matrix{Int64}}([14 32 50; 32 77 122; 50 122 194])
julia> dist = SqMahalanobis(A, skipchecks=true)
┌ Warning: matrix is not symmetric/Hermitian
└ @ Distances ...
SqMahalanobis{Matrix{Int64}}([1 4 7; 2 5 8; 3 6 9])
"""
struct SqMahalanobis{M<:AbstractMatrix} <: SemiMetric
qmat::M
function SqMahalanobis(Q::AbstractMatrix; skipchecks::Bool=false)
# TODO: turn the warnings into errors in next breaking release
ishermitian(Q) || @warn "matrix is not symmetric/Hermitian"
if !skipchecks
eigmin(Q) ≥ 0 || @warn "matrix is not positive semidefinite"
end
return new{typeof(Q)}(Q)
end
end
function result_type(d::Mahalanobis, ::Type{T1}, ::Type{T2}) where {T1,T2}
z = zero(T1) - zero(T2)
return typeof(sqrt(z * zero(eltype(d.qmat)) * z))
end
function result_type(d::SqMahalanobis, ::Type{T1}, ::Type{T2}) where {T1,T2}
z = zero(T1) - zero(T2)
return typeof(z * zero(eltype(d.qmat)) * z)
end
# TODO: merge the following two once we lift the lower bound for julia (above v1.4?)
function (dist::SqMahalanobis)(a::AbstractVector, b::AbstractVector)
if length(a) != length(b)
throw(DimensionMismatch("first array has length $(length(a)) which does not match the length of the second, $(length(b))."))
end
Q = dist.qmat
z = a - b
return dot(z, Q * z)
end
function (dist::Mahalanobis)(a::AbstractVector, b::AbstractVector)
if length(a) != length(b)
throw(DimensionMismatch("first array has length $(length(a)) which does not match the length of the second, $(length(b))."))
end
Q = dist.qmat
z = a - b
return sqrt(dot(z, Q * z))
end
sqmahalanobis(a::AbstractVector, b::AbstractVector, Q::AbstractMatrix) = SqMahalanobis(Q)(a, b)
mahalanobis(a::AbstractVector, b::AbstractVector, Q::AbstractMatrix) = Mahalanobis(Q)(a, b)
function _colwise!(dist, r, a, b)
Q = dist.qmat
get_colwise_dims(size(Q, 1), r, a, b)
z = a .- b
dot_percol!(r, Q * z, z)
end
function colwise!(dist::SqMahalanobis, r::AbstractArray, a::AbstractMatrix, b::AbstractMatrix)
_colwise!(dist, r, a, b)
end
function colwise!(dist::SqMahalanobis, r::AbstractArray, a::AbstractVector, b::AbstractMatrix)
_colwise!(dist, r, a, b)
end
function colwise!(dist::SqMahalanobis, r::AbstractArray, a::AbstractMatrix, b::AbstractVector)
_colwise!(dist, r, a, b)
end
function colwise!(dist::Mahalanobis, r::AbstractArray, a::AbstractMatrix, b::AbstractMatrix)
sqrt!(_colwise!(dist, r, a, b))
end
function colwise!(dist::Mahalanobis, r::AbstractArray, a::AbstractVector, b::AbstractMatrix)
sqrt!(_colwise!(dist, r, a, b))
end
function colwise!(dist::Mahalanobis, r::AbstractArray, a::AbstractMatrix, b::AbstractVector)
sqrt!(_colwise!(dist, r, a, b))
end
function _pairwise!(dist::Union{SqMahalanobis,Mahalanobis}, r::AbstractMatrix, a::AbstractMatrix, b::AbstractMatrix)
Q = dist.qmat
m, na, nb = get_pairwise_dims(size(Q, 1), r, a, b)
Qa = Q * a
Qb = Q * b
sa2 = dot_percol(a, Qa)
sb2 = dot_percol(b, Qb)
mul!(r, a', Qb)
for j = 1:nb
@simd for i = 1:na
@inbounds r[i, j] = eval_end(dist, max(sa2[i] + sb2[j] - 2 * r[i, j], 0))
end
end
r
end
function _pairwise!(dist::Union{SqMahalanobis,Mahalanobis}, r::AbstractMatrix, a::AbstractMatrix)
Q = dist.qmat
m, n = get_pairwise_dims(size(Q, 1), r, a)
Qa = Q * a
sa2 = dot_percol(a, Qa)
mul!(r, a', Qa)
for j = 1:n
for i = 1:(j - 1)
@inbounds r[i, j] = r[j, i]
end
r[j, j] = 0
for i = (j + 1):n
@inbounds r[i, j] = eval_end(dist, max(sa2[i] + sa2[j] - 2 * r[i, j], 0))
end
end
r
end
eval_end(::SqMahalanobis, x) = x
eval_end(::Mahalanobis, x) = sqrt(x)