This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathwmetrics.jl
165 lines (125 loc) · 4.56 KB
/
wmetrics.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
# Weighted metrics
###########################################################
#
# Metric types
#
###########################################################
type WeightedEuclidean{T<:FloatingPoint} <: Metric
weights::Vector{T}
end
type WeightedSqEuclidean{T<:FloatingPoint} <: SemiMetric
weights::Vector{T}
end
type WeightedCityblock{T<:FloatingPoint} <: Metric
weights::Vector{T}
end
type WeightedMinkowski{T<:FloatingPoint} <: Metric
weights::Vector{T}
p::Real
end
type WeightedHamming{T<:FloatingPoint} <: Metric
weights::Vector{T}
end
result_type{T}(::WeightedEuclidean{T}, T1::Type, T2::Type) = T
result_type{T}(::WeightedSqEuclidean{T}, T1::Type, T2::Type) = T
result_type{T}(::WeightedCityblock{T}, T1::Type, T2::Type) = T
result_type{T}(::WeightedMinkowski{T}, T1::Type, T2::Type) = T
result_type{T}(::WeightedHamming{T}, T1::Type, T2::Type) = T
###########################################################
#
# Specialized distances
#
###########################################################
# Weighted squared Euclidean
function wsumsqdiff(w::AbstractVector, a::AbstractVector, b::AbstractVector)
n = get_common_len(w, a, b)::Int
s = 0.
for i = 1:n
@inbounds s += abs2(a[i] - b[i]) * w[i]
end
s
end
evaluate{T<:FloatingPoint}(dist::WeightedSqEuclidean{T}, a::AbstractVector, b::AbstractVector) = wsumsqdiff(dist.weights, a, b)
wsqeuclidean(a::AbstractVector, b::AbstractVector, w::AbstractVector) = evaluate(WeightedSqEuclidean(w), a, b)
function pairwise!{T<:FloatingPoint}(r::AbstractMatrix, dist::WeightedSqEuclidean{T}, a::AbstractMatrix, b::AbstractMatrix)
w = dist.weights
m::Int, na::Int, nb::Int = get_pairwise_dims(length(w), r, a, b)
sa2 = wsumsq_percol(w, a)
sb2 = wsumsq_percol(w, b)
At_mul_B!(r, a, b .* w)
for j = 1 : nb
for i = 1 : na
@inbounds r[i,j] = sa2[i] + sb2[j] - 2 * r[i,j]
end
end
r
end
function pairwise!{T<:FloatingPoint}(r::AbstractMatrix, dist::WeightedSqEuclidean{T}, a::AbstractMatrix)
w = dist.weights
m::Int, n::Int = get_pairwise_dims(length(w), r, a)
sa2 = wsumsq_percol(w, a)
At_mul_B!(r, a, a .* w)
for j = 1 : n
for i = 1 : j-1
@inbounds r[i,j] = r[j,i]
end
@inbounds r[j,j] = 0
for i = j+1 : n
@inbounds r[i,j] = sa2[i] + sa2[j] - 2 * r[i,j]
end
end
r
end
# Weighted Euclidean
function evaluate{T<:FloatingPoint}(dist::WeightedEuclidean{T}, a::AbstractVector, b::AbstractVector)
sqrt(evaluate(WeightedSqEuclidean(dist.weights), a, b))
end
weuclidean(a::AbstractVector, b::AbstractVector, w::AbstractVector) = evaluate(WeightedEuclidean(w), a, b)
function colwise!{T<:FloatingPoint}(r::AbstractArray, dist::WeightedEuclidean{T}, a::AbstractMatrix, b::AbstractMatrix)
sqrt!(colwise!(r, WeightedSqEuclidean(dist.weights), a, b))
end
function colwise!{T<:FloatingPoint}(r::AbstractArray, dist::WeightedEuclidean{T}, a::AbstractVector, b::AbstractMatrix)
sqrt!(colwise!(r, WeightedSqEuclidean(dist.weights), a, b))
end
function pairwise!{T<:FloatingPoint}(r::AbstractMatrix, dist::WeightedEuclidean{T}, a::AbstractMatrix, b::AbstractMatrix)
sqrt!(pairwise!(r, WeightedSqEuclidean(dist.weights), a, b))
end
function pairwise!{T<:FloatingPoint}(r::AbstractMatrix, dist::WeightedEuclidean{T}, a::AbstractMatrix)
sqrt!(pairwise!(r, WeightedSqEuclidean(dist.weights), a))
end
# Weighted Cityblock
function evaluate{T<:FloatingPoint}(dist::WeightedCityblock{T}, a::AbstractVector, b::AbstractVector)
w = dist.weights
n = get_common_len(w, a, b)::Int
s = 0.
for i = 1:n
@inbounds s += w[i] * abs(a[i] - b[i])
end
s
end
wcityblock(a::AbstractVector, b::AbstractVector, w::AbstractVector) = evaluate(WeightedCityblock(w), a, b)
# WeightedMinkowski
function evaluate{T<:FloatingPoint}(dist::WeightedMinkowski{T}, a::AbstractVector, b::AbstractVector)
w = dist.weights
p = dist.p
n = get_common_len(w, a, b)
s = 0.
for i = 1:n
@inbounds s += w[i] * (abs(a[i] - b[i]) .^ p)
end
s .^ inv(p)
end
wminkowski(a::AbstractVector, b::AbstractVector, w::AbstractVector, p::Real) = evaluate(WeightedMinkowski(w, p), a, b)
# WeightedHamming
function evaluate{T<:FloatingPoint}(dist::WeightedHamming{T}, a::AbstractVector, b::AbstractVector)
n = length(a)
w = dist.weights
r = zero(T)
for i = 1 : n
@inbounds if a[i] != b[i]
r += w[i]
end
end
return r
end
whamming(a::AbstractVector, b::AbstractVector, w::AbstractVector) = evaluate(WeightedHamming(w), a, b)