From 36fe174c140b7cef7a58e0cf2fc3a5f2365be3d2 Mon Sep 17 00:00:00 2001 From: Dave Kleinschmidt Date: Wed, 6 Dec 2017 16:00:41 -0500 Subject: [PATCH] modernize tests (#22) * use testset for all tests * use Compat.Test and import->using in tests --- test/REQUIRE | 1 + test/conjugates.jl | 214 +++++++++++++++++++----------------- test/conjugates_mvnormal.jl | 172 +++++++++++++++-------------- test/conjugates_normal.jl | 3 - test/runtests.jl | 19 ++-- 5 files changed, 214 insertions(+), 195 deletions(-) create mode 100644 test/REQUIRE diff --git a/test/REQUIRE b/test/REQUIRE new file mode 100644 index 0000000..9530d2e --- /dev/null +++ b/test/REQUIRE @@ -0,0 +1 @@ +Compat \ No newline at end of file diff --git a/test/conjugates.jl b/test/conjugates.jl index f997dcf..cd70c64 100644 --- a/test/conjugates.jl +++ b/test/conjugates.jl @@ -1,4 +1,3 @@ -using Base.Test using Distributions using ConjugatePriors @@ -26,144 +25,155 @@ function ccount(K, x, w) end -# Beta - Bernoulli +@testset "Beta - Bernoulli" begin -pri = Beta(1.0, 2.0) + pri = Beta(1.0, 2.0) -x = rand(Bernoulli(0.3), n) -p = posterior(pri, Bernoulli, x) -@test isa(p, Beta) -@test p.α ≈ pri.α + sum(x) -@test p.β ≈ pri.β + (n - sum(x)) + x = rand(Bernoulli(0.3), n) + p = posterior(pri, Bernoulli, x) + @test isa(p, Beta) + @test p.α ≈ pri.α + sum(x) + @test p.β ≈ pri.β + (n - sum(x)) -f = fit_map(pri, Bernoulli, x) -@test isa(f, Bernoulli) -@test succprob(f) ≈ mode(p) + f = fit_map(pri, Bernoulli, x) + @test isa(f, Bernoulli) + @test succprob(f) ≈ mode(p) -p = posterior(pri, Bernoulli, x, w) -@test isa(p, Beta) -@test p.α ≈ pri.α + sum(x .* w) -@test p.β ≈ pri.β + (sum(w) - sum(x .* w)) + p = posterior(pri, Bernoulli, x, w) + @test isa(p, Beta) + @test p.α ≈ pri.α + sum(x .* w) + @test p.β ≈ pri.β + (sum(w) - sum(x .* w)) -f = fit_map(pri, Bernoulli, x, w) -@test isa(f, Bernoulli) -@test succprob(f) ≈ mode(p) + f = fit_map(pri, Bernoulli, x, w) + @test isa(f, Bernoulli) + @test succprob(f) ≈ mode(p) -# posterior_rand & posterior_randmodel + @testset "posterior_rand & posterior_randmodel" begin -pri = Beta(1.0, 2.0) -x = rand(Bernoulli(0.3), n) -post = posterior(pri, Bernoulli, x) + pri = Beta(1.0, 2.0) + x = rand(Bernoulli(0.3), n) + post = posterior(pri, Bernoulli, x) -pv = posterior_rand(pri, Bernoulli, x) -@test isa(pv, Float64) -@test 0. <= pv <= 1. + pv = posterior_rand(pri, Bernoulli, x) + @test isa(pv, Float64) + @test 0. <= pv <= 1. -pv = posterior_rand(pri, Bernoulli, x, w) -@test isa(pv, Float64) -@test 0. <= pv <= 1. + pv = posterior_rand(pri, Bernoulli, x, w) + @test isa(pv, Float64) + @test 0. <= pv <= 1. -pm = posterior_randmodel(pri, Bernoulli, x) -@test isa(pm, Bernoulli) -@test 0. <= succprob(pm) <= 1. + pm = posterior_randmodel(pri, Bernoulli, x) + @test isa(pm, Bernoulli) + @test 0. <= succprob(pm) <= 1. -pm = posterior_randmodel(pri, Bernoulli, x, w) -@test isa(pm, Bernoulli) -@test 0. <= succprob(pm) <= 1. + pm = posterior_randmodel(pri, Bernoulli, x, w) + @test isa(pm, Bernoulli) + @test 0. <= succprob(pm) <= 1. + end + +end +@testset "Beta - Binomial" begin -# Beta - Binomial + pri = Beta(1.0, 2.0) -x = rand(Binomial(10, 0.3), n) -p = posterior(pri, Binomial, (10, x)) -@test isa(p, Beta) -@test p.α ≈ pri.α + sum(x) -@test p.β ≈ pri.β + (10n - sum(x)) + x = rand(Binomial(10, 0.3), n) + p = posterior(pri, Binomial, (10, x)) + @test isa(p, Beta) + @test p.α ≈ pri.α + sum(x) + @test p.β ≈ pri.β + (10n - sum(x)) -f = fit_map(pri, Binomial, (10, x)) -@test isa(f, Binomial) -@test ntrials(f) == 10 -@test succprob(f) ≈ mode(p) + f = fit_map(pri, Binomial, (10, x)) + @test isa(f, Binomial) + @test ntrials(f) == 10 + @test succprob(f) ≈ mode(p) -p = posterior(pri, Binomial, (10, x), w) -@test isa(p, Beta) -@test p.α ≈ pri.α + sum(x .* w) -@test p.β ≈ pri.β + (10 * sum(w) - sum(x .* w)) + p = posterior(pri, Binomial, (10, x), w) + @test isa(p, Beta) + @test p.α ≈ pri.α + sum(x .* w) + @test p.β ≈ pri.β + (10 * sum(w) - sum(x .* w)) -f = fit_map(pri, Binomial, (10, x), w) -@test isa(f, Binomial) -@test ntrials(f) == 10 -@test succprob(f) ≈ mode(p) + f = fit_map(pri, Binomial, (10, x), w) + @test isa(f, Binomial) + @test ntrials(f) == 10 + @test succprob(f) ≈ mode(p) +end -# Dirichlet - Categorical +@testset "Dirichlet - Categorical" begin -pri = Dirichlet([1., 2., 3.]) + pri = Dirichlet([1., 2., 3.]) -x = rand(Categorical([0.2, 0.3, 0.5]), n) -p = posterior(pri, Categorical, x) -@test isa(p, Dirichlet) -@test p.alpha ≈ pri.alpha + ccount(3, x) + x = rand(Categorical([0.2, 0.3, 0.5]), n) + p = posterior(pri, Categorical, x) + @test isa(p, Dirichlet) + @test p.alpha ≈ pri.alpha + ccount(3, x) -f = fit_map(pri, Categorical, x) -@test isa(f, Categorical) -@test probs(f) ≈ mode(p) + f = fit_map(pri, Categorical, x) + @test isa(f, Categorical) + @test probs(f) ≈ mode(p) -p = posterior(pri, Categorical, x, w) -@test isa(p, Dirichlet) -@test p.alpha ≈ pri.alpha + ccount(3, x, w) + p = posterior(pri, Categorical, x, w) + @test isa(p, Dirichlet) + @test p.alpha ≈ pri.alpha + ccount(3, x, w) -f = fit_map(pri, Categorical, x, w) -@test isa(f, Categorical) -@test probs(f) ≈ mode(p) + f = fit_map(pri, Categorical, x, w) + @test isa(f, Categorical) + @test probs(f) ≈ mode(p) +end -# Dirichlet - Multinomial +@testset "Dirichlet - Multinomial" begin -x = rand(Multinomial(100, [0.2, 0.3, 0.5]), 1) -p = posterior(pri, Multinomial, x) -@test isa(p, Dirichlet) -@test p.alpha ≈ pri.alpha + x + pri = Dirichlet([1., 2., 3.]) -r = posterior_mode(pri, Multinomial, x) -@test r ≈ mode(p) + x = rand(Multinomial(100, [0.2, 0.3, 0.5]), 1) + p = posterior(pri, Multinomial, x) + @test isa(p, Dirichlet) + @test p.alpha ≈ pri.alpha + x -x = rand(Multinomial(10, [0.2, 0.3, 0.5]), n) -p = posterior(pri, Multinomial, x) -@test isa(p, Dirichlet) -@test p.alpha ≈ pri.alpha + vec(sum(x, 2)) + r = posterior_mode(pri, Multinomial, x) + @test r ≈ mode(p) -r = posterior_mode(pri, Multinomial, x) -@test r ≈ mode(p) + x = rand(Multinomial(10, [0.2, 0.3, 0.5]), n) + p = posterior(pri, Multinomial, x) + @test isa(p, Dirichlet) + @test p.alpha ≈ pri.alpha + vec(sum(x, 2)) -p = posterior(pri, Multinomial, x, w) -@test isa(p, Dirichlet) -@test p.alpha ≈ pri.alpha + vec(x * w) + r = posterior_mode(pri, Multinomial, x) + @test r ≈ mode(p) -r = posterior_mode(pri, Multinomial, x, w) -@test r ≈ mode(p) + p = posterior(pri, Multinomial, x, w) + @test isa(p, Dirichlet) + @test p.alpha ≈ pri.alpha + vec(x * w) + r = posterior_mode(pri, Multinomial, x, w) + @test r ≈ mode(p) -# Gamma - Exponential +end -pri = Gamma(1.5, 2.0) +@testset "Gamma - Exponential" begin -x = rand(Exponential(2.0), n) -p = posterior(pri, Exponential, x) -@test isa(p, Gamma) -@test shape(p) ≈ shape(pri) + n -@test rate(p) ≈ rate(pri) + sum(x) + pri = Gamma(1.5, 2.0) -f = fit_map(pri, Exponential, x) -@test isa(f, Exponential) -@test rate(f) ≈ mode(p) + x = rand(Exponential(2.0), n) + p = posterior(pri, Exponential, x) + @test isa(p, Gamma) + @test shape(p) ≈ shape(pri) + n + @test rate(p) ≈ rate(pri) + sum(x) -p = posterior(pri, Exponential, x, w) -@test isa(p, Gamma) -@test shape(p) ≈ shape(pri) + sum(w) -@test rate(p) ≈ rate(pri) + sum(x .* w) + f = fit_map(pri, Exponential, x) + @test isa(f, Exponential) + @test rate(f) ≈ mode(p) -f = fit_map(pri, Exponential, x, w) -@test isa(f, Exponential) -@test rate(f) ≈ mode(p) + p = posterior(pri, Exponential, x, w) + @test isa(p, Gamma) + @test shape(p) ≈ shape(pri) + sum(w) + @test rate(p) ≈ rate(pri) + sum(x .* w) + + f = fit_map(pri, Exponential, x, w) + @test isa(f, Exponential) + @test rate(f) ≈ mode(p) + +end diff --git a/test/conjugates_mvnormal.jl b/test/conjugates_mvnormal.jl index 87bca41..51a08e3 100644 --- a/test/conjugates_mvnormal.jl +++ b/test/conjugates_mvnormal.jl @@ -2,115 +2,121 @@ using Distributions using ConjugatePriors -using Base.Test -import ConjugatePriors: posterior, posterior_randmodel, NormalWishart, NormalInverseWishart +using ConjugatePriors: posterior, posterior_randmodel, NormalWishart, NormalInverseWishart -# MvNormal -- Normal (known covariance) +@testset "Conjugate models for multivariate normal" begin -n = 3 -p = 4 -X = reshape(Float64[1:12;], p, n) -w = rand(n) -Xw = X * diagm(w) + @testset "MvNormal -- Normal (known covariance)" begin -Sigma = 0.75 * eye(p) + fill(0.25, 4, 4) -ss = suffstats(MvNormalKnownCov(Sigma), X) -ssw = suffstats(MvNormalKnownCov(Sigma), X, w) + n = 3 + p = 4 + X = reshape(Float64[1:12;], p, n) + w = rand(n) + Xw = X * diagm(w) -s_t = sum(X, 2) -ws_t = sum(Xw, 2) -tw_t = length(w) -wtw_t = sum(w) + Sigma = 0.75 * eye(p) + fill(0.25, 4, 4) + ss = suffstats(MvNormalKnownCov(Sigma), X) + ssw = suffstats(MvNormalKnownCov(Sigma), X, w) -@test ss.sx ≈ s_t -@test ss.tw ≈ tw_t + s_t = sum(X, 2) + ws_t = sum(Xw, 2) + tw_t = length(w) + wtw_t = sum(w) -@test ssw.sx ≈ ws_t -@test ssw.tw ≈ wtw_t + @test ss.sx ≈ s_t + @test ss.tw ≈ tw_t -# Posterior -n = 10 -# n = 100 -mu_true = [2., 3.] -Sig_true = eye(2) -Sig_true[1,2] = Sig_true[2,1] = 0.25 -mu0 = [2.5, 2.5] -Sig0 = eye(2) -Sig0[1,2] = Sig0[2,1] = 0.5 -X = rand(MultivariateNormal(mu_true, Sig_true), n) -pri = MultivariateNormal(mu0, Sig0) + @test ssw.sx ≈ ws_t + @test ssw.tw ≈ wtw_t -post = posterior((pri, Sig_true), MvNormal, X) -@test isa(post, FullNormal) + # Posterior + n = 10 + # n = 100 + mu_true = [2., 3.] + Sig_true = eye(2) + Sig_true[1,2] = Sig_true[2,1] = 0.25 + mu0 = [2.5, 2.5] + Sig0 = eye(2) + Sig0[1,2] = Sig0[2,1] = 0.5 + X = rand(MultivariateNormal(mu_true, Sig_true), n) + pri = MultivariateNormal(mu0, Sig0) -@test post.μ ≈ inv(inv(Sig0) + n*inv(Sig_true))*(n*inv(Sig_true)*mean(X,2) + inv(Sig0)*mu0) -@test post.Σ.mat ≈ inv(inv(Sig0) + n*inv(Sig_true)) + post = posterior((pri, Sig_true), MvNormal, X) + @test isa(post, FullNormal) -# posterior_sample + @test post.μ ≈ inv(inv(Sig0) + n*inv(Sig_true))*(n*inv(Sig_true)*mean(X,2) + inv(Sig0)*mu0) + @test post.Σ.mat ≈ inv(inv(Sig0) + n*inv(Sig_true)) -ps = posterior_randmodel((pri, Sig_true), MvNormal, X) -@test isa(ps, FullNormal) -@test insupport(ps, ps.μ) -@test insupport(InverseWishart, ps.Σ.mat) + # posterior_sample + ps = posterior_randmodel((pri, Sig_true), MvNormal, X) + @test isa(ps, FullNormal) + @test insupport(ps, ps.μ) + @test insupport(InverseWishart, ps.Σ.mat) -# NormalInverseWishart - MvNormal + end + + @testset "NormalInverseWishart - MvNormal" begin -mu_true = [2., 2.] -Sig_true = eye(2) -Sig_true[1,2] = Sig_true[2,1] = 0.25 + mu_true = [2., 2.] + Sig_true = eye(2) + Sig_true[1,2] = Sig_true[2,1] = 0.25 -X = rand(MultivariateNormal(mu_true, Sig_true), n) -Xbar = mean(X,2) -Xm = X .- mean(X,2) + X = rand(MultivariateNormal(mu_true, Sig_true), n) + Xbar = mean(X,2) + Xm = X .- mean(X,2) -mu0 = [2., 3.] -kappa0 = 3. -nu0 = 4. -T0 = eye(2) -T0[1,2] = T0[2,1] = .5 -pri = NormalInverseWishart(mu0, kappa0, T0, nu0) + mu0 = [2., 3.] + kappa0 = 3. + nu0 = 4. + T0 = eye(2) + T0[1,2] = T0[2,1] = .5 + pri = NormalInverseWishart(mu0, kappa0, T0, nu0) -post = posterior(pri, MvNormal, X) + post = posterior(pri, MvNormal, X) -@test post.mu ≈ (kappa0*mu0 + n*Xbar)./(kappa0 + n) -@test post.kappa ≈ kappa0 + n -@test post.nu ≈ nu0 + n -@test (post.Lamchol[:U]'*post.Lamchol[:U]) ≈ T0 + A_mul_Bt(Xm, Xm) + kappa0*n/(kappa0+n)*(Xbar-mu0)*(Xbar-mu0)' + @test post.mu ≈ (kappa0*mu0 + n*Xbar)./(kappa0 + n) + @test post.kappa ≈ kappa0 + n + @test post.nu ≈ nu0 + n + @test (post.Lamchol[:U]'*post.Lamchol[:U]) ≈ T0 + A_mul_Bt(Xm, Xm) + kappa0*n/(kappa0+n)*(Xbar-mu0)*(Xbar-mu0)' -ps = posterior_randmodel(pri, MultivariateNormal, X) + ps = posterior_randmodel(pri, MultivariateNormal, X) -@test isa(ps, MultivariateNormal) -@test insupport(ps, ps.μ) && insupport(InverseWishart, ps.Σ.mat) + @test isa(ps, MultivariateNormal) + @test insupport(ps, ps.μ) && insupport(InverseWishart, ps.Σ.mat) + end + + @testset "NormalWishart - MvNormal" begin -# NormalWishart - MvNormal + mu_true = [2., 2.] + Lam_true = eye(2) + Lam_true[1,2] = Lam_true[2,1] = 0.25 -mu_true = [2., 2.] -Lam_true = eye(2) -Lam_true[1,2] = Lam_true[2,1] = 0.25 + X = rand(MvNormal(mu_true, inv(Lam_true)), n) + Xbar = mean(X,2) + Xm = X .- Xbar -X = rand(MvNormal(mu_true, inv(Lam_true)), n) -Xbar = mean(X,2) -Xm = X .- Xbar + mu0 = [2., 3.] + kappa0 = 3. + nu0 = 4. + T0 = eye(2) + T0[1,2] = T0[2,1] = .5 + pri = NormalWishart(mu0, kappa0, T0, nu0) -mu0 = [2., 3.] -kappa0 = 3. -nu0 = 4. -T0 = eye(2) -T0[1,2] = T0[2,1] = .5 -pri = NormalWishart(mu0, kappa0, T0, nu0) + post = posterior(pri, MvNormal, X) -post = posterior(pri, MvNormal, X) + @test post.mu ≈ (kappa0*mu0 + n*Xbar)./(kappa0 + n) + @test post.kappa ≈ kappa0 + n + @test post.nu ≈ nu0 + n + @test (post.Tchol[:U]'*post.Tchol[:U]) ≈ T0 + A_mul_Bt(Xm, Xm) + kappa0*n/(kappa0+n)*(Xbar-mu0)*(Xbar-mu0)' + + ps = posterior_randmodel(pri, MvNormal, X) -@test post.mu ≈ (kappa0*mu0 + n*Xbar)./(kappa0 + n) -@test post.kappa ≈ kappa0 + n -@test post.nu ≈ nu0 + n -@test (post.Tchol[:U]'*post.Tchol[:U]) ≈ T0 + A_mul_Bt(Xm, Xm) + kappa0*n/(kappa0+n)*(Xbar-mu0)*(Xbar-mu0)' + @test isa(ps, MultivariateNormal) + @test insupport(ps, ps.μ) + @test insupport(InverseWishart, ps.Σ.mat) # InverseWishart on purpose -ps = posterior_randmodel(pri, MvNormal, X) - -@test isa(ps, MultivariateNormal) -@test insupport(ps, ps.μ) -@test insupport(InverseWishart, ps.Σ.mat) # InverseWishart on purpose + end +end diff --git a/test/conjugates_normal.jl b/test/conjugates_normal.jl index 6ed7a85..cd6bcb4 100644 --- a/test/conjugates_normal.jl +++ b/test/conjugates_normal.jl @@ -1,6 +1,3 @@ -# - -using Base.Test using Distributions using ConjugatePriors diff --git a/test/runtests.jl b/test/runtests.jl index db98391..539272e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,9 +1,14 @@ -tests = ["conjugates", - "conjugates_normal", - "conjugates_mvnormal"] +using Compat +using Compat.Test -for t in tests - fpath = "$t.jl" - println("running $fpath ...") - include(fpath) +@testset "Conjugate priors" begin + tests = ["conjugates", + "conjugates_normal", + "conjugates_mvnormal"] + + for t in tests + fpath = "$t.jl" + println("running $fpath ...") + include(fpath) + end end