Commit b0b951d6 authored by Dahua Lin's avatar Dahua Lin

multivariate mixture tested

parent d5cb3fc5
......@@ -30,7 +30,7 @@ MixtureModel{C<:Distribution}(components::Vector{C}) =
#### Basic properties
length(d::MultivariateMixture) = dim(d.components[1])
length(d::MultivariateMixture) = length(d.components[1])
size(d::MatrixvariateMixture) = size(d.components[1])
components(d::MixtureModel) = d.components
......@@ -229,8 +229,3 @@ _rand!(s::MixtureSampler{Multivariate}, x::DenseVector) = _rand!(s.csamplers[ran
sampler(d::MixtureModel) = MixtureSampler(d)
......@@ -126,8 +126,15 @@ gradlogpdf(d::MvNormal, x::Vector{Float64}) = -(d.Σ \ (x - d.μ))
# Sampling (for GenericMvNormal)
_rand!(d::MvNormal, x::DenseVecOrMat{Float64}) = add!(unwhiten!(d.Σ, randn!(x)), d.μ)
_rand!(d::MvNormal, x::VecOrMat{Float64}) = add!(unwhiten!(d.Σ, randn!(x)), d.μ)
# Workaround: randn! only works for Array, but not generally for DenseArray
function _rand!(d::MvNormal, x::DenseVecOrMat{Float64})
for i = 1:length(x)
@inbounds x[i] = randn()
end
add!(unwhiten!(d.Σ, x), d.μ)
end
###########################################################
#
......
......@@ -48,9 +48,60 @@ function test_mixture(g::UnivariateMixture, n::Int, ns::Int)
# 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:10
X[:,i] = rand(g)
end
cs = components(g)
pr = priorprobs(g)
@assert length(cs) == length(pr)
# mean
mu = 0.0
for k = 1:length(cs)
mu += pr[k] * mean(cs[k])
end
@test_approx_eq mean(g) mu
# ground-truth
p0 = zeros(n)
for i = 1:n
p0_i = 0.0
x_i = X[:,i]
for k = 1:length(cs)
p0_i += pr[k] * pdf(cs[k], x_i)
end
p0[i] = p0_i
end
lp0 = log(p0)
for i = 1:n
@test_approx_eq pdf(g, X[:,i]) p0[i]
@test_approx_eq logpdf(g, X[:,i]) lp0[i]
end
p_e = pdf(g, X)
lp_e = logpdf(g, X)
@assert isa(p_e, Vector{Float64}) && length(p_e) == n
@assert isa(lp_e, Vector{Float64}) && length(lp_e) == n
@test_approx_eq p_e p0
@test_approx_eq lp_e 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
end
# Tests
......@@ -64,3 +115,14 @@ g_u = MixtureModel(
test_mixture(g_u, 1000, 10^6)
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(g_m) == 2
test_mixture(g_m, 1000, 10^6)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment