@@ -18,13 +18,11 @@ Cosine similarity
18
18
@doc raw """
19
19
cossim(x,y)
20
20
21
- Computes the cosine similarity between two inputs, `x` and `y `. Cosine similarity is defined as
21
+ Computes the cosine similarity between two inputs ``x`` and ``y` `. Cosine similarity is defined as
22
22
23
- ```\m ath
24
- cossim(x,y) = \f rac{\l eft\l angle x,y\r ight\r angle}{\| x\|\c dot\| y\| }
25
- ```
23
+ ``\t ext{cossim}(x,y) = \f rac{\l eft\l angle x,y\r ight\r angle}{\| x\|\c dot\| y\| }``
26
24
27
- where ``\l eft\l angle\c dot,\c dot\r ight\r angle`` is an inner product (e.g. dot product) and ``\|\c dot\| `` is its derived norm. This is roughly interpreted as being related to the angle between the inputs `x` and `y` : when `x` and `y` have low angle between them, `cossim(x,y)` is high (close to `1` ). Meanwhile, when `x` and `y` have large angle between them, `cossim(x,y)` is low (close to `-1 `).
25
+ where ``\l eft\l angle\c dot,\c dot\r ight\r angle`` is an inner product (e.g. dot product) and ``\|\c dot\| `` is its derived norm. This is roughly interpreted as being related to the angle between the inputs ``x`` and ``y`` : when ``x`` and ``y`` have low angle between them, `cossim(x,y)` is high (close to ``1`` ). When ``x`` and ``y`` have large angle between them, `cossim(x,y)` is low (close to ``-1` `).
28
26
29
27
# Arguments
30
28
- `x` and `y`: two inputs for which `dot(x,y)`, `norm(x)`, and `norm(y)` are defined.
@@ -73,17 +71,30 @@ L^p distance
73
71
====================#
74
72
75
73
@doc raw """
76
- ℓp(p, x , y)
77
- ℓ1(x, y)
78
- ℓ2(x, y)
74
+ ℓp(x::AbstractVector , y::AbstractVector, p::Real )
75
+ ℓ1(x::AbstractVector , y::AbstractVector )
76
+ ℓ2(x::AbstractVector , y::AbstractVector )
79
77
80
78
Computes the ``\e ll^p`` distance between a pair of vectors, given by
81
79
82
- ```\m ath
83
- \e ll^p(x,y) \c oloneqq \| x - y\| _p = \s um \l eft|x_i - y_i\r ight|^p
80
+ ``\e ll^p(x,y) \c oloneqq \| x - y\| _p = \l eft(\s um_i \l eft|x_i - y_i\r ight|^p\r ight)^{1/p}``
81
+
82
+ `ℓ1(x,y)` is the same as `ℓp(x,y,1)`, and `ℓ2(x,y)` is the same as `ℓp(x,y,2)`.
83
+
84
+ # Examples
85
+ ```jldoctest; setup = :(using LSH)
86
+ julia> x = [1, 2, 3];
87
+
88
+ julia> y = [4, 5, 6];
89
+
90
+ julia> ℓp(x,y,2) == (abs(1-4)^2 + abs(2-5)^2 + abs(3-6)^2)^(1/2)
91
+ true
92
+
93
+ julia> ℓp(x,y,3) == (abs(1-4)^3 + abs(2-5)^3 + abs(3-6)^3)^(1/3)
94
+ true
84
95
```
85
96
86
- Since `` \e ll^1`` and `` \e ll^2`` are both common cases of `` \e ll^p`` distance, they are given unique function names `ℓ1` and `ℓ2` that you can use to call them.
97
+ See also: [`ℓp_norm`](@ref)
87
98
"""
88
99
ℓp (x:: AbstractVector , y:: AbstractVector , p:: Real = 2 ) = Lp (x, y, p)
89
100
@@ -93,6 +104,13 @@ Since ``\ell^1`` and ``\ell^2`` are both common cases of ``\ell^p`` distance, th
93
104
@doc (@doc ℓp)
94
105
ℓ2 (x:: AbstractVector , y:: AbstractVector ) = L2 (x, y)
95
106
107
+ @doc raw """
108
+ Lp(x::AbstractVector, y::AbstractVector, p::Real)
109
+ L1(x::AbstractVector, y::AbstractVector)
110
+ L2(x::AbstractVector, y::AbstractVector)
111
+
112
+ Computes the ``ℓ^p`` distance between a pair of vectors ``x`` and ``y``. Identical to `ℓp(x,y,p)`, `ℓ1(x,y)`, and `ℓ2(x,y)`, respectively.
113
+ """
96
114
function Lp (x:: AbstractVector{T} , y:: AbstractVector{T} , p:: Real = 2 ) where {T}
97
115
if p ≤ 0
98
116
" p must be positive" |> ErrorException |> throw
@@ -108,6 +126,7 @@ function Lp(x::AbstractVector{T}, y::AbstractVector{T}, p::Real=2) where {T}
108
126
return result^ (1 / p)
109
127
end
110
128
129
+ @doc (@doc Lp)
111
130
function L1 (x:: AbstractVector{T} , y:: AbstractVector{T} ) where {T}
112
131
if length (x) != length (y)
113
132
" length(x) != length(y)" |> DimensionMismatch |> throw
@@ -121,6 +140,7 @@ function L1(x::AbstractVector{T}, y::AbstractVector{T}) where {T}
121
140
return result
122
141
end
123
142
143
+ @doc (@doc Lp)
124
144
function L2 (x:: AbstractVector{T} , y:: AbstractVector{T} ) where {T}
125
145
if length (x) != length (y)
126
146
" length(x) != length(y)" |> DimensionMismatch |> throw
@@ -135,9 +155,47 @@ function L2(x::AbstractVector{T}, y::AbstractVector{T}) where {T}
135
155
end
136
156
137
157
# Function space L^p distances
158
+
159
+ @doc raw """
160
+ Lp(f, g, interval::LSH.RealInterval, p)
161
+ L1(f, g, interval::LSH.RealInterval)
162
+ L2(f, g, interval::LSH.RealInterval)
163
+
164
+ Computes the ``L^p`` distance between two functions, given by
165
+
166
+ ``L^p(f,g) \c oloneqq \| f - g\| _p = \l eft(\i nt_a^b \l eft|f(x) - g(x)\r ight|^p \h space{0.15cm} dx\r ight)^{1/p}``
167
+
168
+ # Examples
169
+ Below we compute the ``L^1``, ``L^2``, and ``L^3`` distances between ``f(x) = x^2 + 1`` and ``g(x) = 2x`` over the interval ``[0,1]``. The distances are computed by evaluating the integral
170
+
171
+ ``\l eft(\i nt_0^1 \l eft|f(x) - g(x)\r ight|^p \h space{0.15cm}dx\r ight)^{1/p} = \l eft(\i nt_0^1 \l eft|x^2 - 2x + 1\r ight|^p \h space{0.15cm}dx\r ight)^{1/p} = \l eft(\i nt_0^1 (x - 1)^{2p} \h space{0.15cm}dx\r ight)^{1/p}``
172
+
173
+ for ``p = 1``, ``p = 2``, and ``p = 3``.
174
+
175
+ ```jldoctest; setup = :(using LSH)
176
+ julia> f(x) = x^2 + 1; g(x) = 2x;
177
+
178
+ julia> interval = LSH.@interval(0 ≤ x ≤ 1);
179
+
180
+ julia> Lp(f, g, interval, 1) ≈ L1(f, g, interval) ≈ 3^(-1)
181
+ true
182
+
183
+ julia> Lp(f, g, interval, 2) ≈ L2(f, g, interval) ≈ 5^(-1/2)
184
+ true
185
+
186
+ julia> Lp(f, g, interval, 3) ≈ 7^(-1/3)
187
+ true
188
+ ```
189
+
190
+ See also: [`Lp_norm`](@ref), [`ℓp`](@ref)
191
+ """
138
192
Lp (f, g, interval, p:: Real = 2 ) = Lp_norm (x -> f (x) - g (x), interval, p)
139
- L1 (f, g, interval) = L1_norm (x -> f (x) - g (x), interval)
140
- L2 (f, g, interval) = L2_norm (x -> f (x) - g (x), interval)
193
+
194
+ @doc (@doc Lp)
195
+ L1 (f, g, interval) = L1_norm (x -> f (x) - g (x), interval)
196
+
197
+ @doc (@doc Lp)
198
+ L2 (f, g, interval) = L2_norm (x -> f (x) - g (x), interval)
141
199
142
200
#= ===================
143
201
Jaccard similarity
@@ -146,11 +204,9 @@ Jaccard similarity
146
204
@doc raw """
147
205
jaccard(A::Set, B::Set) :: Float64
148
206
149
- Computes the Jaccard similarity between sets `A` and `B `, which is defined as
207
+ Computes the Jaccard similarity between sets ``A`` and ``B` `, which is defined as
150
208
151
- ```\m ath
152
- J(A,B) = \f rac{\l eft|A \c ap B\r ight|}{\l eft|A \c up B\r ight|}
153
- ```
209
+ ``\t ext{Jaccard}(A,B) = \f rac{\l eft|A \c ap B\r ight|}{\l eft|A \c up B\r ight|}``
154
210
155
211
# Arguments
156
212
- `A::Set`, `B::Set`: the two sets with which to compute Jaccard similarity.
@@ -186,25 +242,136 @@ Inner product and norms
186
242
187
243
# ## Inner products
188
244
# TODO : docs
245
+
246
+ @doc raw """
247
+ inner_prod(x::AbstractVector, y::AbstractVector)
248
+
249
+ Computes the ``\e ll^2`` inner product (dot product)
250
+
251
+ ``\l eft\l angle x, y\r ight\r angle = \s um_i x_iy_i``
252
+
253
+ # Examples
254
+ ```jldoctest; setup = :(using LSH)
255
+ julia> using LinearAlgebra: dot;
256
+
257
+ julia> x, y = randn(4), randn(4);
258
+
259
+ julia> inner_prod(x,y) ≈ dot(x,y)
260
+ true
261
+ ```
262
+ """
189
263
inner_prod (x:: AbstractVector , y:: AbstractVector ) = dot (x,y)
190
264
191
265
# 1-dimensional inner product between L^2 functions
266
+ @doc raw """
267
+ inner_prod(f, g, interval::LSH.RealInterval)
268
+
269
+ Computes the ``L^2`` inner product
270
+
271
+ ``\l eft\l angle f, g\r ight\r angle = \i nt_a^b f(x)g(x) \h space{0.15cm} dx``
272
+
273
+ where the interval we're integrating over is specified by the `interval` argument.
274
+
275
+ # Examples
276
+ ```jldoctest; setup = :(using LSH)
277
+ julia> f(x) = cos(x); g(x) = sin(x);
278
+
279
+ julia> inner_prod(f, g, LSH.@interval(0 ≤ x ≤ π/2)) ≈ 1/2
280
+ true
281
+ ```
282
+ """
192
283
inner_prod (f, g, interval:: LSH.RealInterval ) =
193
284
quadgk (x -> f (x)g (x), interval. lower, interval. upper)[1 ]
194
285
195
286
# ## L^p norms
287
+ @doc raw """
288
+ Lp_norm(x::AbstractVector, p::Real = 2)
289
+ L1_norm(x::AbstractVector)
290
+ L2_norm(x::AbstractVector)
291
+
292
+ Compute the ``\e ll^p`` norm of a vector ``x``. Identical to `ℓp_norm(x, p)`, `ℓ1_norm(x)`, and `ℓ2_norm(x)`, respectively.
293
+
294
+ See also: [`ℓp_norm`](@ref)
295
+ """
196
296
Lp_norm (x:: AbstractVector , p:: Real = 2 ) = norm (x,p)
297
+
298
+ @doc (@doc Lp_norm)
197
299
L1_norm (x:: AbstractVector ) = norm (x,1 )
300
+
301
+ @doc (@doc Lp_norm)
198
302
L2_norm (x:: AbstractVector ) = norm (x)
199
303
304
+ @doc raw """
305
+ ℓp_norm(x::AbstractVector, p::Real = 2)
306
+ ℓ1_norm(x::AbstractVector)
307
+ ℓ2_norm(x::AbstractVector)
308
+
309
+ Compute the ``\e ll^p`` norm of a point ``x``, defined as
310
+
311
+ ``\| x\| _p = \l eft(\s um_i \l eft|x_i\r ight|^p\r ight)^{1/p}``
312
+
313
+ # Examples
314
+
315
+ ```jldoctest; setup = :(using LSH)
316
+ julia> x = randn(4);
317
+
318
+ julia> ℓp_norm(x, 1) ≈ ℓ1_norm(x) ≈ (map(u -> abs(u)^1, x) |> sum)^(1/1)
319
+ true
320
+
321
+ julia> ℓp_norm(x, 2) ≈ ℓ2_norm(x) ≈ (map(u -> abs(u)^2, x) |> sum)^(1/2)
322
+ true
323
+
324
+ julia> ℓp_norm(x, 3) ≈ (map(u -> abs(u)^3, x) |> sum)^(1/3)
325
+ true
326
+ ```
327
+
328
+ See also: [`ℓp`](@ref), [`Lp_norm`](@ref)
329
+ """
200
330
ℓp_norm (x:: AbstractVector , p:: Real = 2 ) = Lp_norm (x, p)
201
- ℓ1_norm (x:: AbstractVector ) = L1_norm (x)
202
- ℓ2_norm (x:: AbstractVector ) = L2_norm (x)
331
+
332
+ @doc (@doc ℓp_norm)
333
+ ℓ1_norm (x:: AbstractVector ) = L1_norm (x)
334
+
335
+ @doc (@doc ℓp_norm)
336
+ ℓ2_norm (x:: AbstractVector ) = L2_norm (x)
203
337
204
338
# 1-dimensional L^p norms
339
+
340
+ @doc raw """
341
+ Lp_norm(f, interval::LSH.RealInterval, p::Real=2)
342
+ L1_norm(f, interval::LSH.RealInterval)
343
+ L2_norm(f, interval::LSH.RealInterval)
344
+
345
+ Computes the ``L^p`` function-space norm of a function ``f``, which is given by the equation
346
+
347
+ ``\| f\| _p = \l eft(\i nt_a^b \l eft|f(x)\r ight|^p \h space{0.15cm} dx\r ight)^{1/p}``
348
+
349
+ `L1_norm(f, interval)` is the same as `Lp_norm(f, interval, 1)`, and `L2_norm(f, interval)` is the same as `Lp_norm(f, interval, 2)`.
350
+
351
+ # Examples
352
+
353
+ ```jldoctest; setup = :(using LSH)
354
+ julia> f(x) = x;
355
+
356
+ julia> interval = LSH.@interval(0 ≤ x ≤ 1);
357
+
358
+ julia> Lp_norm(f, interval, 1) ≈ L1_norm(f, interval) ≈ 2^(-1/1)
359
+ true
360
+
361
+ julia> Lp_norm(f, interval, 2) ≈ L2_norm(f, interval) ≈ 3^(-1/2)
362
+ true
363
+
364
+ julia> Lp_norm(f, interval, 3) ≈ 4^(-1/3)
365
+ true
366
+ ```
367
+ """
205
368
Lp_norm (f, interval:: LSH.RealInterval , p:: Real = 2 ) = (quadgk (x -> abs (f (x)).^ p, interval. lower, interval. upper)[1 ])^ (1 / p)
206
- L1_norm (f, interval:: LSH.RealInterval ) = quadgk (x -> abs (f (x)), interval. lower, interval. upper)[1 ]
207
- L2_norm (f, interval:: LSH.RealInterval ) = √ quadgk (x -> abs2 (f (x)), interval. lower, interval. upper)[1 ]
369
+
370
+ @doc (@doc Lp_norm)
371
+ L1_norm (f, interval:: LSH.RealInterval ) = quadgk (x -> abs (f (x)), interval. lower, interval. upper)[1 ]
372
+
373
+ @doc (@doc Lp_norm)
374
+ L2_norm (f, interval:: LSH.RealInterval ) = √ quadgk (x -> abs2 (f (x)), interval. lower, interval. upper)[1 ]
208
375
209
376
#= ===================
210
377
1D Wasserstein distance
@@ -238,9 +405,12 @@ function wasserstein_1d(f, g, p::AbstractFloat)
238
405
error (" TODO" )
239
406
end
240
407
408
+ @doc (@doc wasserstein_1d)
241
409
wasserstein1_1d (f, g) = wasserstein_1d (f, g, 1 )
410
+ emd = wasserstein1_1d
411
+
412
+ @doc (@doc wasserstein_1d)
242
413
wasserstein2_2d (f, g) = wasserstein_1d (f, g, 2 )
243
- emd (f, g) = wasserstein1_1d (f, g)
244
414
245
415
#= ===================
246
416
Definitions for similarity function-related components of the AbstractLSHFunction
0 commit comments