-
Notifications
You must be signed in to change notification settings - Fork 409
/
mixture.jl
164 lines (138 loc) · 3.9 KB
/
mixture.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
using Distributions
using Base.Test
# Core testing procedure
function test_mixture(g::UnivariateMixture, n::Int, ns::Int)
X = zeros(n)
for i = 1:n
X[i] = rand(g)
end
K = ncomponents(g)
pr = probs(g)
@assert length(pr) == K
# mean
mu = 0.0
for k = 1:K
mu += pr[k] * mean(component(g, k))
end
@test_approx_eq mean(g) mu
# evaluation of cdf
cf = zeros(n)
for k = 1:K
c_k = component(g, k)
for i = 1:n
cf[i] += pr[k] * cdf(c_k, X[i])
end
end
for i = 1:n
@test_approx_eq cdf(g, X[i]) cf[i]
end
@test_approx_eq cdf(g, X) cf
# evaluation
P0 = zeros(n, K)
LP0 = zeros(n, K)
for k = 1:K
c_k = component(g, k)
for i = 1:n
x_i = X[i]
P0[i,k] = pdf(c_k, x_i)
LP0[i,k] = logpdf(c_k, x_i)
end
end
mix_p0 = P0 * pr
mix_lp0 = log(mix_p0)
for i = 1:n
@test_approx_eq pdf(g, X[i]) mix_p0[i]
@test_approx_eq logpdf(g, X[i]) mix_lp0[i]
@test_approx_eq componentwise_pdf(g, X[i]) vec(P0[i,:])
@test_approx_eq componentwise_logpdf(g, X[i]) vec(LP0[i,:])
end
@test_approx_eq pdf(g, X) mix_p0
@test_approx_eq logpdf(g, X) mix_lp0
@test_approx_eq componentwise_pdf(g, X) P0
@test_approx_eq componentwise_logpdf(g, X) LP0
# sampling
Xs = rand(g, ns)
@test isa(Xs, Vector{Float64})
@test length(Xs) == ns
@test_approx_eq_eps mean(Xs) mean(g) 0.01
end
function test_mixture(g::MultivariateMixture, n::Int, ns::Int)
X = zeros(length(g), n)
for i = 1:n
X[:,i] = rand(g)
end
K = ncomponents(g)
pr = probs(g)
@assert length(pr) == K
# mean
mu = 0.0
for k = 1:K
mu += pr[k] * mean(component(g, k))
end
@test_approx_eq mean(g) mu
# evaluation
P0 = zeros(n, K)
LP0 = zeros(n, K)
for k = 1:K
c_k = component(g, k)
for i = 1:n
x_i = X[:,i]
P0[i,k] = pdf(c_k, x_i)
LP0[i,k] = logpdf(c_k, x_i)
end
end
mix_p0 = P0 * pr
mix_lp0 = log(mix_p0)
for i = 1:n
x_i = X[:,i]
@test_approx_eq pdf(g, x_i) mix_p0[i]
@test_approx_eq logpdf(g, x_i) mix_lp0[i]
@test_approx_eq componentwise_pdf(g, x_i) vec(P0[i,:])
@test_approx_eq componentwise_logpdf(g, x_i) vec(LP0[i,:])
end
@test_approx_eq pdf(g, X) mix_p0
@test_approx_eq logpdf(g, X) mix_lp0
@test_approx_eq componentwise_pdf(g, X) P0
@test_approx_eq componentwise_logpdf(g, X) LP0
# sampling
Xs = rand(g, ns)
@test isa(Xs, Matrix{Float64})
@test size(Xs) == (length(g), ns)
@test_approx_eq_eps vec(mean(Xs, 2)) mean(g) 0.01
@test_approx_eq_eps cov(Xs, 2) cov(g) 0.01
end
function test_params(g::AbstractMixtureModel)
C = eltype(g.components)
pars = params(g)
mm = MixtureModel(C, pars...)
@test g.prior == mm.prior
@test g.components == mm.components
end
function test_params(g::UnivariateGMM)
pars = params(g)
mm = UnivariateGMM(pars...)
@test g == mm
end
# Tests
println(" testing UnivariateMixture")
g_u = MixtureModel(Normal, [(0.0, 1.0), (2.0, 1.0), (-4.0, 1.5)], [0.2, 0.5, 0.3])
@test isa(g_u, MixtureModel{Univariate, Continuous, Normal})
@test ncomponents(g_u) == 3
test_mixture(g_u, 1000, 10^6)
test_params(g_u)
g_u = UnivariateGMM([0.0, 2.0, -4.0], [1.0, 1.2, 1.5], Categorical([0.2, 0.5, 0.3]))
@test isa(g_u, UnivariateGMM)
@test ncomponents(g_u) == 3
test_mixture(g_u, 1000, 10^6)
test_params(g_u)
println(" testing MultivariateMixture")
g_m = MixtureModel(
IsoNormal[ MvNormal([0.0, 0.0], 1.0),
MvNormal([0.2, 1.0], 1.0),
MvNormal([-0.5, -3.0], 1.6) ],
[0.2, 0.5, 0.3])
@test isa(g_m, MixtureModel{Multivariate, Continuous, IsoNormal})
@test length(components(g_m)) == 3
@test length(g_m) == 2
test_mixture(g_m, 1000, 10^6)
test_params(g_m)