/
ProbabilitySimplex.jl
379 lines (317 loc) · 11.1 KB
/
ProbabilitySimplex.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
@doc raw"""
ProbabilitySimplex{n} <: AbstractEmbeddedManifold{ℝ,DefaultEmbeddingType}
The (relative interior of) the probability simplex is the set
````math
Δ^n := \biggl\{ p ∈ ℝ^{n+1}\ \big|\ p_i > 0 \text{ for all } i=1,…,n+1,
\text{ and } ⟨\mathbb{1},p⟩ = \sum_{i=1}^{n+1} p_i = 1\biggr\},
````
where $\mathbb{1}=(1,…,1)^{\mathrm{T}}∈ ℝ^{n+1}$ denotes the vector containing only ones.
This set is also called the unit simplex or standard simplex.
The tangent space is given by
````math
T_pΔ^n = \biggl\{ X ∈ ℝ^{n+1}\ \big|\ ⟨\mathbb{1},X⟩ = \sum_{i=1}^{n+1} X_i = 0 \biggr\}
````
The manifold is implemented assuming the Fisher-Rao metric for the multinomial distribution,
which is equivalent to the induced metric from isometrically embedding the probability
simplex in the $n$-sphere of radius 2.
The corresponding diffeomorphism $\varphi: \mathbb Δ^n → \mathcal N$,
where $\mathcal N \subset 2𝕊^n$ is given by $\varphi(p) = 2\sqrt{p}$.
This implementation follows the notation in [^ÅströmPetraSchmitzerSchnörr2017].
[^ÅströmPetraSchmitzerSchnörr2017]:
> F. Åström, S. Petra, B. Schmitzer, C. Schnörr: “Image Labeling by Assignment”,
> Journal of Mathematical Imaging and Vision, 58(2), pp. 221–238, 2017.
> doi: [10.1007/s10851-016-0702-4](https://doi.org/10.1007/s10851-016-0702-4)
> arxiv: [1603.05285](https://arxiv.org/abs/1603.05285).
"""
struct ProbabilitySimplex{n} <: AbstractEmbeddedManifold{ℝ,DefaultEmbeddingType} end
ProbabilitySimplex(n::Int) = ProbabilitySimplex{n}()
"""
SoftmaxRetraction <: AbstractRetractionMethod
Describes a retraction that is based on the softmax function.
"""
struct SoftmaxRetraction <: AbstractRetractionMethod end
"""
SoftmaxInverseRetraction <: AbstractInverseRetractionMethod
Describes an inverse retraction that is based on the softmax function.
"""
struct SoftmaxInverseRetraction <: AbstractInverseRetractionMethod end
"""
FisherRaoMetric <: Metric
The Fisher-Rao metric or Fisher information metric is a particular Riemannian metric which
can be defined on a smooth statistical manifold, i.e., a smooth manifold whose points are
probability measures defined on a common probability space.
See for example the [`ProbabilitySimplex`](@ref).
"""
struct FisherRaoMetric <: Metric end
"""
check_manifold_point(M::ProbabilitySimplex, p; kwargs...)
Check whether `p` is a valid point on the [`ProbabilitySimplex`](@ref) `M`, i.e. is a point in
the embedding with positive entries that sum to one
The tolerance for the last test can be set using the `kwargs...`.
"""
function check_manifold_point(M::ProbabilitySimplex, p; kwargs...)
mpv = invoke(
check_manifold_point,
Tuple{(typeof(get_embedding(M))),typeof(p)},
get_embedding(M),
p;
kwargs...,
)
mpv === nothing || return mpv
if minimum(p) <= 0
return DomainError(
minimum(p),
"The point $(p) does not lie on the $(M) since it has nonpositive entries.",
)
end
if !isapprox(sum(p), 1.0; kwargs...)
return DomainError(
sum(p),
"The point $(p) does not lie on the $(M) since its sum is not 1.",
)
end
return nothing
end
"""
check_tangent_vector(M::ProbabilitySimplex, p, X; check_base_point = true, kwargs... )
Check whether `X` is a tangent vector to `p` on the [`ProbabilitySimplex`](@ref) `M`, i.e.
after [`check_manifold_point`](@ref check_manifold_point(::ProbabilitySimplex, ::Any))`(M,p)`,
`X` has to be of same dimension as `p` and its elements have to sum to one.
The optional parameter `check_base_point` indicates, whether to call
[`check_manifold_point`](@ref check_manifold_point(::ProbabilitySimplex, ::Any)) for `p` or not.
The tolerance for the last test can be set using the `kwargs...`.
"""
function check_tangent_vector(
M::ProbabilitySimplex,
p,
X;
check_base_point = true,
kwargs...,
)
if check_base_point
mpe = check_manifold_point(M, p; kwargs...)
mpe === nothing || return mpe
end
mpv = invoke(
check_tangent_vector,
Tuple{typeof(get_embedding(M)),typeof(p),typeof(X)},
get_embedding(M),
p,
X;
check_base_point = false, # already checked above
kwargs...,
)
mpv === nothing || return mpv
if !isapprox(sum(X), 0.0; kwargs...)
return DomainError(
sum(X),
"The vector $(X) is not a tangent vector to $(p) on $(M), since its elements to not sum up to 0.",
)
end
return nothing
end
decorated_manifold(M::ProbabilitySimplex) = Euclidean(representation_size(M)...; field = ℝ)
default_metric_dispatch(::ProbabilitySimplex, ::FisherRaoMetric) = Val(true)
@doc raw"""
distance(M,p,q)
Compute the distance between two points on the [`ProbabilitySimplex`](@ref) `M`.
The formula reads
````math
d_{Δ^n}(p,q) = 2\arccos \biggl( \sum_{i=1}^{n+1} \sqrt{p_i q_i} \biggr)
````
"""
function distance(::ProbabilitySimplex, p, q)
sumsqrt = zero(Base.promote_eltype(p, q))
@inbounds for i in eachindex(p, q)
sumsqrt += sqrt(p[i] * q[i])
end
return 2 * acos(sumsqrt)
end
embed!(::ProbabilitySimplex, q, p) = copyto!(q, p)
embed!(::ProbabilitySimplex, Y, p, X) = copyto!(Y, X)
@doc raw"""
exp(M::ProbabilitySimplex,p,X)
Compute the exponential map on the probability simplex.
````math
\exp_pX = \frac{1}{2}\Bigl(p+\frac{X_p^2}{\lVert X_p \rVert^2}\Bigr)
+ \frac{1}{2}\Bigl(p - \frac{X_p^2}{\lVert X_p \rVert^2}\Bigr)\cos(\lVert X_p\rVert)
+ \frac{1}{\lVert X_p \rVert}\sqrt{p}\sin(\lVert X_p\rVert),
````
where $X_p = \frac{X}{\sqrt{p}}$, with its division meant elementwise, as well as for the
operations $X_p^2$ and $\sqrt{p}$.
"""
exp(::ProbabilitySimplex, ::Any...)
function exp!(::ProbabilitySimplex, q, p, X)
s = sqrt.(p)
Xs = X ./ s ./ 2
θ = norm(Xs)
q .= (cos(θ) .* s .+ usinc(θ) .* Xs) .^ 2
return q
end
@doc raw"""
injectivity_radius(M,p)
compute the injectivity radius on the [`ProbabilitySimplex`](@ref) `M` at the point `p`,
i.e. the distanceradius to a point near/on the boundary, that could be reached by following the
geodesic.
"""
function injectivity_radius(::ProbabilitySimplex{n}, p) where {n}
i = argmin(p)
s = sum(p) - p[i]
return 2 * acos(sqrt(s))
end
function injectivity_radius(M::ProbabilitySimplex, p, ::ExponentialRetraction)
return injectivity_radius(M, p)
end
injectivity_radius(M::ProbabilitySimplex, p, ::SoftmaxRetraction) = injectivity_radius(M, p)
injectivity_radius(M::ProbabilitySimplex) = 0
injectivity_radius(M::ProbabilitySimplex, ::SoftmaxRetraction) = 0
injectivity_radius(M::ProbabilitySimplex, ::ExponentialRetraction) = 0
eval(
quote
@invoke_maker 1 Manifold injectivity_radius(
M::ProbabilitySimplex,
rm::AbstractRetractionMethod,
)
end,
)
@doc raw"""
inner(M::ProbabilitySimplex,p,X,Y)
Compute the inner product of two tangent vectors `X`, `Y` from the tangent space $T_pΔ^n$ at
`p`. The formula reads
````math
g_p(X,Y) = \sum_{i=1}^{n+1}\frac{X_iY_i}{p_i}
````
"""
function inner(::ProbabilitySimplex, p, X, Y)
d = zero(Base.promote_eltype(p, X, Y))
@inbounds for i in eachindex(p, X, Y)
d += X[i] * Y[i] / p[i]
end
return d
end
@doc raw"""
inverse_retract(M::ProbabilitySimplex, p, q, ::SoftmaxInverseRetraction)
Compute a first order approximation by projection. The formula reads
````math
\operatorname{retr}^{-1}_p q = \bigl( I_{n+1} - \frac{1}{n}\mathbb{1}^{n+1,n+1} \bigr)(\log(q)-\log(p))
````
where $\mathbb{1}^{m,n}$ is the size `(m,n)` matrix containing ones, and $\log$ is applied elementwise.
"""
inverse_retract(::ProbabilitySimplex, ::Any, ::Any, ::SoftmaxInverseRetraction)
function inverse_retract!(
::ProbabilitySimplex{n},
X,
p,
q,
::SoftmaxInverseRetraction,
) where {n}
X .= log.(q) .- log.(p)
meanlogdiff = mean(X)
X .-= meanlogdiff
return X
end
@doc raw"""
log(M::ProbabilitySimplex, p, q)
Compute the logarithmic map of `p` and `q` on the [`ProbabilitySimplex`](@ref) `M`.
````math
\log_pq = \frac{d_{Δ^n}(p,q)}{\sqrt{1-⟨\sqrt{p},\sqrt{q}⟩}}(\sqrt{pq} - ⟨\sqrt{p},\sqrt{q}⟩p),
````
where $pq$ and $\sqrt{p}$ is meant elementwise.
"""
log(::ProbabilitySimplex, ::Any...)
function log!(M::ProbabilitySimplex, X, p, q)
if p ≈ q
fill!(X, 0)
else
z = sqrt.(p .* q)
s = sum(z)
X .= 2 * acos(s) / sqrt(1 - s^2) .* (z .- s .* p)
end
return X
end
@doc raw"""
manifold_dimension(M::ProbabilitySimplex{n})
Returns the manifold dimension of the probability simplex in $ℝ^{n+1}$, i.e.
````math
\dim_{Δ^n} = n.
````
"""
manifold_dimension(::ProbabilitySimplex{n}) where {n} = n
@doc raw"""
mean(
M::ProbabilitySimplex,
x::AbstractVector,
[w::AbstractWeights,]
method = GeodesicInterpolation();
kwargs...,
)
Compute the Riemannian [`mean`](@ref mean(M::Manifold, args...)) of `x` using
[`GeodesicInterpolation`](@ref).
"""
mean(::ProbabilitySimplex, ::Any...)
function Statistics.mean!(
M::ProbabilitySimplex,
p,
x::AbstractVector,
w::AbstractVector;
kwargs...,
)
return mean!(M, p, x, w, GeodesicInterpolation(); kwargs...)
end
@doc raw"""
project(M::ProbabilitySimplex, p, Y)
project `Y` from the embedding onto the tangent space at `p` on
the [`ProbabilitySimplex`](@ref) `M`. The formula reads
````math
\operatorname{proj}_{Δ^n}(p,Y) = Y - ⟨p,Y⟩p.
````
"""
project(::ProbabilitySimplex, ::Any, ::Any)
function project!(::ProbabilitySimplex, q, p)
if any(x -> x <= 0, p)
throw(DomainError(
p,
"All coordinates of point from the embedding, that should be projected, must be positive, otherwise the projection is not well defined.",
))
end
q .= p ./ sum(p)
return q
end
function project!(::ProbabilitySimplex, X, p, Y)
X .= Y .- sum(Y) .* p
return X
end
@doc raw"""
representation_size(::ProbabilitySimplex{n})
return the representation size of points in the $n$-dimensional probability simplex,
i.e. an array size of `(n+1,)`.
"""
representation_size(::ProbabilitySimplex{n}) where {n} = (n + 1,)
@doc raw"""
retract(M::ProbabilitySimplex, p, X, ::SoftmaxRetraction)
Compute a first order approximation by applying the softmax function. The formula reads
````math
\operatorname{retr}_p X = \frac{p\mathrm{e}^X}{⟨p,\mathrm{e}^X⟩},
````
where multiplication, exponentiation and division are meant elementwise.
"""
retract(::ProbabilitySimplex, ::Any, ::Any, ::SoftmaxRetraction)
function retract!(::ProbabilitySimplex, q, p, X, ::SoftmaxRetraction)
s = zero(eltype(q))
@inbounds for i in eachindex(q, p, X)
q[i] = p[i] * exp(X[i])
s += q[i]
end
q ./= s
return q
end
function Base.show(io::IO, ::ProbabilitySimplex{n}) where {n}
return print(io, "ProbabilitySimplex($(n))")
end
@doc raw"""
zero_tangent_vector(M::ProbabilitySimplex,p)
returns the zero tangent vector in the tangent space of the point `p` from the
[`ProbabilitySimplex`](@ref) `M`, i.e. its representation by the zero vector in the embedding.
"""
zero_tangent_vector(::ProbabilitySimplex, ::Any)
zero_tangent_vector!(M::ProbabilitySimplex, v, p) = fill!(v, 0)