|
| 1 | +using Distributions |
| 2 | +using PosteriorStats |
| 3 | +using Random |
| 4 | +using RCall |
| 5 | +using Test |
| 6 | + |
| 7 | +# R loo with our API |
| 8 | +function loo_r(log_likelihood; reff=nothing) |
| 9 | + R"require('loo')" |
| 10 | + if reff === nothing |
| 11 | + reff = rcopy(R"loo::relative_eff(exp($(log_likelihood)))") |
| 12 | + end |
| 13 | + result = R"loo::loo($log_likelihood, r_eff=$reff)" |
| 14 | + estimates = rcopy(R"$(result)$estimates") |
| 15 | + estimates = ( |
| 16 | + elpd=estimates[1, 1], |
| 17 | + se_elpd=estimates[1, 2], |
| 18 | + p=estimates[2, 1], |
| 19 | + se_p=estimates[2, 2], |
| 20 | + ) |
| 21 | + pointwise = rcopy(R"$(result)$pointwise") |
| 22 | + pointwise = ( |
| 23 | + elpd=pointwise[:, 1], |
| 24 | + se_elpd=pointwise[:, 2], |
| 25 | + p=pointwise[:, 3], |
| 26 | + reff=reff, |
| 27 | + pareto_shape=pointwise[:, 5], |
| 28 | + ) |
| 29 | + return (; estimates, pointwise) |
| 30 | +end |
| 31 | + |
| 32 | +# R loo with our API |
| 33 | +function waic_r(log_likelihood) |
| 34 | + R"require('loo')" |
| 35 | + result = R"loo::waic($log_likelihood)" |
| 36 | + estimates = rcopy(R"$(result)$estimates") |
| 37 | + estimates = ( |
| 38 | + elpd=estimates[1, 1], |
| 39 | + se_elpd=estimates[1, 2], |
| 40 | + p=estimates[2, 1], |
| 41 | + se_p=estimates[2, 2], |
| 42 | + ) |
| 43 | + pointwise = rcopy(R"$(result)$pointwise") |
| 44 | + pointwise = (elpd=pointwise[:, 1], p=pointwise[:, 2]) |
| 45 | + return (; estimates, pointwise) |
| 46 | +end |
| 47 | + |
| 48 | +function generate_log_likelihoods(proposal, target, ndraws, nchains, nparams) |
| 49 | + draws = rand(proposal, ndraws, nchains, nparams) |
| 50 | + log_likelihood = loglikelihood.(target, draws) |
| 51 | + return log_likelihood |
| 52 | +end |
| 53 | + |
| 54 | +Random.seed!(24) |
| 55 | + |
| 56 | +@testset "Consistency with R loo" begin |
| 57 | + proposal = Normal() |
| 58 | + target = TDist(7) |
| 59 | + |
| 60 | + @testset "loo" begin |
| 61 | + log_likelihood = generate_log_likelihoods(proposal, target, 1000, 4, 10) |
| 62 | + reff_rand = rand(size(log_likelihood, 3)) |
| 63 | + @testset for reff in (nothing, reff_rand) |
| 64 | + result_r = loo_r(log_likelihood; reff) |
| 65 | + result = loo(log_likelihood; reff) |
| 66 | + @test result.estimates.elpd ≈ result_r.estimates.elpd |
| 67 | + @test result.estimates.se_elpd ≈ result_r.estimates.se_elpd |
| 68 | + @test result.estimates.p ≈ result_r.estimates.p |
| 69 | + @test result.estimates.se_p ≈ result_r.estimates.se_p |
| 70 | + @test result.pointwise.elpd ≈ result_r.pointwise.elpd |
| 71 | + # increased tolerance for se_elpd, since we use a different approach |
| 72 | + @test result.pointwise.se_elpd ≈ result_r.pointwise.se_elpd rtol = 0.01 |
| 73 | + @test result.pointwise.p ≈ result_r.pointwise.p |
| 74 | + @test result.pointwise.reff ≈ result_r.pointwise.reff |
| 75 | + @test result.pointwise.pareto_shape ≈ result_r.pointwise.pareto_shape |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + @testset "waic" begin |
| 80 | + log_likelihood = generate_log_likelihoods(proposal, target, 1000, 4, 10) |
| 81 | + reff_rand = rand(size(log_likelihood, 3)) |
| 82 | + result_r = waic_r(log_likelihood) |
| 83 | + result = waic(log_likelihood) |
| 84 | + @test result.estimates.elpd ≈ result_r.estimates.elpd |
| 85 | + @test result.estimates.se_elpd ≈ result_r.estimates.se_elpd |
| 86 | + @test result.estimates.p ≈ result_r.estimates.p |
| 87 | + @test result.estimates.se_p ≈ result_r.estimates.se_p |
| 88 | + @test result.pointwise.elpd ≈ result_r.pointwise.elpd |
| 89 | + @test result.pointwise.p ≈ result_r.pointwise.p |
| 90 | + end |
| 91 | +end |
0 commit comments