-
Notifications
You must be signed in to change notification settings - Fork 423
/
Copy pathmixturemodel.jl
555 lines (453 loc) · 17 KB
/
mixturemodel.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# finite mixture models
"""
All subtypes of `AbstractMixtureModel` should implement the following methods:
- ncomponents(d): the number of components
- component(d, k): return the k-th component
- probs(d): return a vector of prior probabilities over components.
"""
abstract type AbstractMixtureModel{VF<:VariateForm,VS<:ValueSupport,C<:Distribution} <: Distribution{VF, VS} end
"""
MixtureModel{VF<:VariateForm,VS<:ValueSupport,C<:Distribution,CT<:Real}
A mixture of distributions, parametrized on:
* `VF,VS` variate and support
* `C` distribution family of the mixture
* `CT` the type for probabilities of the prior
"""
struct MixtureModel{VF<:VariateForm,VS<:ValueSupport,C<:Distribution,CT<:Categorical} <: AbstractMixtureModel{VF,VS,C}
components::Vector{C}
prior::CT
function MixtureModel{VF,VS,C}(cs::Vector{C}, pri::CT) where {VF,VS,C,CT}
length(cs) == ncategories(pri) ||
error("The number of components does not match the length of prior.")
new{VF,VS,C,CT}(cs, pri)
end
end
const UnivariateMixture{S<:ValueSupport, C<:Distribution} = AbstractMixtureModel{Univariate,S,C}
const MultivariateMixture{S<:ValueSupport, C<:Distribution} = AbstractMixtureModel{Multivariate,S,C}
const MatrixvariateMixture{S<:ValueSupport,C<:Distribution} = AbstractMixtureModel{Matrixvariate,S,C}
# Interface
"""
component_type(d::AbstractMixtureModel)
The type of the components of `d`.
"""
component_type(d::AbstractMixtureModel{VF,VS,C}) where {VF,VS,C} = C
"""
components(d::AbstractMixtureModel)
Get a list of components of the mixture model `d`.
"""
components(d::AbstractMixtureModel) = [component(d, k) for k in 1:ncomponents(d)]
"""
probs(d::AbstractMixtureModel)
Get the vector of prior probabilities of all components of `d`.
"""
probs(d::AbstractMixtureModel)
"""
mean(d::Union{UnivariateMixture, MultivariateMixture})
Compute the overall mean (expectation).
"""
mean(d::AbstractMixtureModel)
"""
insupport(d::MultivariateMixture, x)
Evaluate whether `x` is within the support of mixture distribution `d`.
"""
insupport(d::AbstractMixtureModel, x::AbstractVector)
"""
pdf(d::Union{UnivariateMixture, MultivariateMixture}, x)
Evaluate the (mixed) probability density function over `x`. Here, `x` can be a single
sample or an array of multiple samples.
"""
pdf(d::AbstractMixtureModel, x::Any)
"""
logpdf(d::Union{UnivariateMixture, MultivariateMixture}, x)
Evaluate the logarithm of the (mixed) probability density function over `x`.
Here, `x` can be a single sample or an array of multiple samples.
"""
logpdf(d::AbstractMixtureModel, x::Any)
"""
gradlogpdf(d::Union{UnivariateMixture, MultivariateMixture}, x)
Evaluate the gradient of the logarithm of the (mixed) probability density function over a single sample `x`.
"""
gradlogpdf(d::AbstractMixtureModel, x::Any)
"""
rand(d::Union{UnivariateMixture, MultivariateMixture})
Draw a sample from the mixture model `d`.
rand(d::Union{UnivariateMixture, MultivariateMixture}, n)
Draw `n` samples from `d`.
"""
rand(d::AbstractMixtureModel)
"""
rand!(d::Union{UnivariateMixture, MultivariateMixture}, r::AbstractArray)
Draw multiple samples from `d` and write them to `r`.
"""
rand!(d::AbstractMixtureModel, r::AbstractArray)
#### Constructors
"""
MixtureModel(components, [prior])
Construct a mixture model with a vector of `components` and a `prior` probability vector.
If no `prior` is provided then all components will have the same prior probabilities.
"""
MixtureModel(components::Vector{C}) where {C<:Distribution} =
MixtureModel(components, Categorical(length(components)))
"""
MixtureModel(C, params, [prior])
Construct a mixture model with component type ``C``, a vector of parameters for constructing
the components given by ``params``, and a prior probability vector.
If no `prior` is provided then all components will have the same prior probabilities.
"""
function MixtureModel(::Type{C}, params::AbstractArray) where C<:Distribution
components = C[_construct_component(C, a) for a in params]
MixtureModel(components)
end
function MixtureModel(components::Vector{C}, prior::Categorical) where C<:Distribution
VF = variate_form(C)
VS = value_support(C)
MixtureModel{VF,VS,C}(components, prior)
end
MixtureModel(components::Vector{C}, p::VT) where {C<:Distribution,VT<:AbstractVector{<:Real}} =
MixtureModel(components, Categorical(p))
_construct_component(::Type{C}, arg) where {C<:Distribution} = C(arg)
_construct_component(::Type{C}, args::Tuple) where {C<:Distribution} = C(args...)
function MixtureModel(::Type{C}, params::AbstractArray, p::Vector{T}) where {C<:Distribution,T<:Real}
components = C[_construct_component(C, a) for a in params]
MixtureModel(components, p)
end
#### Basic properties
"""
length(d::MultivariateMixture)
The length of each sample (only for `Multivariate`).
"""
length(d::MultivariateMixture) = length(d.components[1])
size(d::MatrixvariateMixture) = size(d.components[1])
ncomponents(d::MixtureModel) = length(d.components)
components(d::MixtureModel) = d.components
component(d::MixtureModel, k::Int) = d.components[k]
probs(d::MixtureModel) = probs(d.prior)
params(d::MixtureModel) = ([params(c) for c in d.components], params(d.prior)[1])
partype(d::MixtureModel) = promote_type(partype(d.prior), map(partype, d.components)...)
minimum(d::MixtureModel) = minimum([minimum(dci) for dci in d.components])
maximum(d::MixtureModel) = maximum([maximum(dci) for dci in d.components])
function mean(d::UnivariateMixture)
p = probs(d)
m = sum(pi * mean(component(d, i)) for (i, pi) in enumerate(p) if !iszero(pi))
return m
end
function mean(d::MultivariateMixture)
K = ncomponents(d)
p = probs(d)
m = zeros(length(d))
for i = 1:K
pi = p[i]
if pi > 0.0
c = component(d, i)
BLAS.axpy!(pi, mean(c), m)
end
end
return m
end
"""
var(d::UnivariateMixture)
Compute the overall variance (only for `UnivariateMixture`).
"""
function var(d::UnivariateMixture)
K = ncomponents(d)
p = probs(d)
means = Vector{Float64}(undef, K)
m = 0.0
v = 0.0
for i = 1:K
pi = p[i]
if pi > 0.0
ci = component(d, i)
means[i] = mi = mean(ci)
m += pi * mi
v += pi * var(ci)
end
end
for i = 1:K
pi = p[i]
if pi > 0.0
v += pi * abs2(means[i] - m)
end
end
return v
end
function var(d::MultivariateMixture)
return diag(cov(d))
end
function cov(d::MultivariateMixture)
K = ncomponents(d)
p = probs(d)
m = zeros(length(d))
md = zeros(length(d))
V = zeros(length(d),length(d))
for i = 1:K
pi = p[i]
if pi > 0.0
c = component(d, i)
BLAS.axpy!(pi, mean(c), m)
BLAS.axpy!(pi, cov(c), V)
end
end
for i = 1:K
pi = p[i]
if pi > 0.0
c = component(d, i)
# todo: use more in-place operations
md = mean(c) - m
BLAS.axpy!(pi, md*md', V)
end
end
return V
end
#### show
function show(io::IO, d::MixtureModel)
K = ncomponents(d)
pr = probs(d)
println(io, "MixtureModel{$(component_type(d))}(K = $K)")
Ks = min(K, 8)
for i = 1:Ks
@printf(io, "components[%d] (prior = %.4f): ", i, pr[i])
println(io, component(d, i))
end
if Ks < K
println(io, "The rest are omitted ...")
end
end
#### Evaluation
function insupport(d::AbstractMixtureModel, x::AbstractVector)
p = probs(d)
return any(insupport(component(d, i), x) for (i, pi) in enumerate(p) if !iszero(pi))
end
function cdf(d::UnivariateMixture, x::Real)
p = probs(d)
r = sum(pi * cdf(component(d, i), x) for (i, pi) in enumerate(p) if !iszero(pi))
return r
end
function _mixpdf1(d::AbstractMixtureModel, x)
p = probs(d)
return sum(pi * pdf(component(d, i), x) for (i, pi) in enumerate(p) if !iszero(pi))
end
function _mixpdf!(r::AbstractArray, d::AbstractMixtureModel, x)
K = ncomponents(d)
p = probs(d)
fill!(r, 0.0)
t = Array{eltype(p)}(undef, size(r))
@inbounds for i in eachindex(p)
pi = p[i]
if pi > 0.0
if d isa UnivariateMixture
t .= Base.Fix1(pdf, component(d, i)).(x)
else
pdf!(t, component(d, i), x)
end
BLAS.axpy!(pi, t, r)
end
end
return r
end
function _mixlogpdf1(d::AbstractMixtureModel, x)
p = probs(d)
lp = logsumexp(log(pi) + logpdf(component(d, i), x) for (i, pi) in enumerate(p) if !iszero(pi))
return lp
end
function _mixlogpdf!(r::AbstractArray, d::AbstractMixtureModel, x)
K = ncomponents(d)
p = probs(d)
n = length(r)
Lp = Matrix{eltype(p)}(undef, n, K)
m = fill(-Inf, n)
@inbounds for i in eachindex(p)
pi = p[i]
if pi > 0.0
lpri = log(pi)
lp_i = view(Lp, :, i)
# compute logpdf in batch and store
if d isa UnivariateMixture
lp_i .= Base.Fix1(logpdf, component(d, i)).(x)
else
logpdf!(lp_i, component(d, i), x)
end
# in the mean time, add log(prior) to lp and
# update the maximum for each sample
for j = 1:n
lp_i[j] += lpri
if lp_i[j] > m[j]
m[j] = lp_i[j]
end
end
end
end
fill!(r, 0.0)
@inbounds for i = 1:K
if p[i] > 0.0
lp_i = view(Lp, :, i)
for j = 1:n
r[j] += exp(lp_i[j] - m[j])
end
end
end
@inbounds for j = 1:n
r[j] = log(r[j]) + m[j]
end
return r
end
pdf(d::UnivariateMixture, x::Real) = _mixpdf1(d, x)
logpdf(d::UnivariateMixture, x::Real) = _mixlogpdf1(d, x)
function gradlogpdf(d::UnivariateMixture, x::Real)
ps = probs(d)
cs = components(d)
# `d` is expected to have at least one distribution, otherwise this will just error
psi, idxps = iterate(ps)
csi, idxcs = iterate(cs)
pdfx1 = pdf(csi, x)
pdfx = psi * pdfx1
glp = pdfx * gradlogpdf(csi, x)
if iszero(psi) || iszero(pdfx)
glp = zero(glp)
end
while (iterps = iterate(ps, idxps)) !== nothing && (itercs = iterate(cs, idxcs)) !== nothing
psi, idxps = iterps
csi, idxcs = itercs
if !iszero(psi)
pdfxi = pdf(csi, x)
if !iszero(pdfxi)
pipdfxi = psi * pdfxi
pdfx += pipdfxi
glp += pipdfxi * gradlogpdf(csi, x)
end
end
end
if !iszero(pdfx) # else glp is already zero
glp /= pdfx
end
return glp
end
_pdf!(r::AbstractArray{<:Real}, d::UnivariateMixture{Discrete}, x::UnitRange) = _mixpdf!(r, d, x)
_pdf!(r::AbstractArray{<:Real}, d::UnivariateMixture, x::AbstractArray{<:Real}) = _mixpdf!(r, d, x)
_logpdf!(r::AbstractArray{<:Real}, d::UnivariateMixture, x::AbstractArray{<:Real}) = _mixlogpdf!(r, d, x)
_pdf(d::MultivariateMixture, x::AbstractVector{<:Real}) = _mixpdf1(d, x)
_logpdf(d::MultivariateMixture, x::AbstractVector{<:Real}) = _mixlogpdf1(d, x)
_pdf!(r::AbstractArray{<:Real}, d::MultivariateMixture, x::AbstractMatrix{<:Real}) = _mixpdf!(r, d, x)
_logpdf!(r::AbstractArray{<:Real}, d::MultivariateMixture, x::AbstractMatrix{<:Real}) = _mixlogpdf!(r, d, x)
function gradlogpdf(d::MultivariateMixture, x::AbstractVector{<:Real})
ps = probs(d)
cs = components(d)
# `d` is expected to have at least one distribution, otherwise this will just error
psi, idxps = iterate(ps)
csi, idxcs = iterate(cs)
pdfx1 = pdf(csi, x)
pdfx = psi * pdfx1
glp = pdfx * gradlogpdf(csi, x)
if iszero(psi) || iszero(pdfx)
fill!(glp, zero(eltype(glp)))
end
while (iterps = iterate(ps, idxps)) !== nothing && (itercs = iterate(cs, idxcs)) !== nothing
psi, idxps = iterps
csi, idxcs = itercs
if !iszero(psi)
pdfxi = pdf(csi, x)
if !iszero(pdfxi)
pipdfxi = psi * pdfxi
pdfx += pipdfxi
glp .+= pipdfxi .* gradlogpdf(csi, x)
end
end
end
if !iszero(pdfx) # else glp is already zero
glp ./= pdfx
end
return glp
end
## component-wise pdf and logpdf
function _cwise_pdf1!(r::AbstractVector, d::AbstractMixtureModel, x)
K = ncomponents(d)
length(r) == K || error("The length of r should match the number of components.")
for i = 1:K
r[i] = pdf(component(d, i), x)
end
r
end
function _cwise_logpdf1!(r::AbstractVector, d::AbstractMixtureModel, x)
K = ncomponents(d)
length(r) == K || error("The length of r should match the number of components.")
for i = 1:K
r[i] = logpdf(component(d, i), x)
end
r
end
function _cwise_pdf!(r::AbstractMatrix, d::AbstractMixtureModel, X)
K = ncomponents(d)
n = size(X, ndims(X))
size(r) == (n, K) || error("The size of r is incorrect.")
for i = 1:K
if d isa UnivariateMixture
view(r,:,i) .= Base.Fix1(pdf, component(d, i)).(X)
else
pdf!(view(r,:,i),component(d, i), X)
end
end
r
end
function _cwise_logpdf!(r::AbstractMatrix, d::AbstractMixtureModel, X)
K = ncomponents(d)
n = size(X, ndims(X))
size(r) == (n, K) || error("The size of r is incorrect.")
for i = 1:K
if d isa UnivariateMixture
view(r,:,i) .= Base.Fix1(logpdf, component(d, i)).(X)
else
logpdf!(view(r,:,i), component(d, i), X)
end
end
r
end
componentwise_pdf!(r::AbstractVector, d::UnivariateMixture, x::Real) = _cwise_pdf1!(r, d, x)
componentwise_pdf!(r::AbstractVector, d::MultivariateMixture, x::AbstractVector) = _cwise_pdf1!(r, d, x)
componentwise_pdf!(r::AbstractMatrix, d::UnivariateMixture, x::AbstractVector) = _cwise_pdf!(r, d, x)
componentwise_pdf!(r::AbstractMatrix, d::MultivariateMixture, x::AbstractMatrix) = _cwise_pdf!(r, d, x)
componentwise_logpdf!(r::AbstractVector, d::UnivariateMixture, x::Real) = _cwise_logpdf1!(r, d, x)
componentwise_logpdf!(r::AbstractVector, d::MultivariateMixture, x::AbstractVector) = _cwise_logpdf1!(r, d, x)
componentwise_logpdf!(r::AbstractMatrix, d::UnivariateMixture, x::AbstractVector) = _cwise_logpdf!(r, d, x)
componentwise_logpdf!(r::AbstractMatrix, d::MultivariateMixture, x::AbstractMatrix) = _cwise_logpdf!(r, d, x)
componentwise_pdf(d::UnivariateMixture, x::Real) = componentwise_pdf!(Vector{eltype(x)}(undef, ncomponents(d)), d, x)
componentwise_pdf(d::UnivariateMixture, x::AbstractVector) = componentwise_pdf!(Matrix{eltype(x)}(undef, length(x), ncomponents(d)), d, x)
componentwise_pdf(d::MultivariateMixture, x::AbstractVector) = componentwise_pdf!(Vector{eltype(x)}(undef, ncomponents(d)), d, x)
componentwise_pdf(d::MultivariateMixture, x::AbstractMatrix) = componentwise_pdf!(Matrix{eltype(x)}(undef, size(x,2), ncomponents(d)), d, x)
componentwise_logpdf(d::UnivariateMixture, x::Real) = componentwise_logpdf!(Vector{eltype(x)}(undef, ncomponents(d)), d, x)
componentwise_logpdf(d::UnivariateMixture, x::AbstractVector) = componentwise_logpdf!(Matrix{eltype(x)}(undef, length(x), ncomponents(d)), d, x)
componentwise_logpdf(d::MultivariateMixture, x::AbstractVector) = componentwise_logpdf!(Vector{eltype(x)}(undef, ncomponents(d)), d, x)
componentwise_logpdf(d::MultivariateMixture, x::AbstractMatrix) = componentwise_logpdf!(Matrix{eltype(x)}(undef, size(x,2), ncomponents(d)), d, x)
function quantile(d::UnivariateMixture{Continuous}, p::Real)
ps = probs(d)
min_q, max_q = extrema(quantile(component(d, i), p) for (i, pi) in enumerate(ps) if pi > 0)
quantile_bisect(d, p, min_q, max_q)
end
# we also implement `median` since `median` is implemented more efficiently than
# `quantile(d, 1//2)` for some distributions
function median(d::UnivariateMixture{Continuous})
ps = probs(d)
min_q, max_q = extrema(median(component(d, i)) for (i, pi) in enumerate(ps) if pi > 0)
quantile_bisect(d, 1//2, min_q, max_q)
end
## Sampling
struct MixtureSampler{VF,VS,Sampler} <: Sampleable{VF,VS}
csamplers::Vector{Sampler}
psampler::AliasTable
end
function MixtureSampler(d::MixtureModel{VF,VS}) where {VF,VS}
csamplers = map(sampler, d.components)
psampler = sampler(d.prior)
MixtureSampler{VF,VS,eltype(csamplers)}(csamplers, psampler)
end
Base.length(s::MixtureSampler) = length(first(s.csamplers))
rand(rng::AbstractRNG, s::MixtureSampler{Univariate}) =
rand(rng, s.csamplers[rand(rng, s.psampler)])
rand(rng::AbstractRNG, d::MixtureModel{Univariate}) =
rand(rng, component(d, rand(rng, d.prior)))
# multivariate mixture sampler for a vector
_rand!(rng::AbstractRNG, s::MixtureSampler{Multivariate}, x::AbstractVector{<:Real}) =
@inbounds rand!(rng, s.csamplers[rand(rng, s.psampler)], x)
# if only a single sample is requested, no alias table is created
_rand!(rng::AbstractRNG, d::MixtureModel{Multivariate}, x::AbstractVector{<:Real}) =
@inbounds rand!(rng, component(d, rand(rng, d.prior)), x)
sampler(d::MixtureModel) = MixtureSampler(d)