|
| 1 | +#include <stan/math.hpp> |
| 2 | +#include <stan/math/mix.hpp> |
| 3 | +#include <test/unit/math/laplace/laplace_utility.hpp> |
| 4 | + |
| 5 | +#include <boost/random/mersenne_twister.hpp> |
| 6 | + |
| 7 | +#include <gtest/gtest.h> |
| 8 | +#include <stdexcept> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +namespace { |
| 12 | +struct poisson_log_likelihood { |
| 13 | + template <typename Theta> |
| 14 | + auto operator()(const Theta& theta, const std::vector<int>& y, |
| 15 | + std::ostream* pstream) const { |
| 16 | + return stan::math::poisson_log_lpmf(y, theta); |
| 17 | + } |
| 18 | +}; |
| 19 | +} // namespace |
| 20 | + |
| 21 | +TEST_F(laplace_count_two_dim_diag_test, latent_solve_mean_and_cov) { |
| 22 | + using stan::math::laplace_latent_solve; |
| 23 | + auto [mean_est, chol_est] |
| 24 | + = laplace_latent_solve(poisson_log_likelihood{}, std::forward_as_tuple(y), |
| 25 | + 1, stan::math::test::diagonal_kernel_functor{}, |
| 26 | + std::forward_as_tuple(phi(0), phi(1)), nullptr); |
| 27 | + constexpr double tol = 1e-6; |
| 28 | + EXPECT_EQ(2, mean_est.size()); |
| 29 | + EXPECT_NEAR(theta_root(0), mean_est(0), tol); |
| 30 | + EXPECT_NEAR(theta_root(1), mean_est(1), tol); |
| 31 | + EXPECT_NEAR(0.0, chol_est(0, 1), 1e-12); // check lower triangular matrix |
| 32 | + Eigen::MatrixXd Sigma_est = chol_est * chol_est.transpose(); |
| 33 | + EXPECT_NEAR(K_laplace(0, 0), Sigma_est(0, 0), tol); |
| 34 | + EXPECT_NEAR(K_laplace(1, 1), Sigma_est(1, 1), tol); |
| 35 | + EXPECT_NEAR(K_laplace(0, 1), Sigma_est(0, 1), tol); |
| 36 | + EXPECT_NEAR(K_laplace(1, 0), Sigma_est(1, 0), tol); |
| 37 | +} |
| 38 | + |
| 39 | +TEST_F(laplace_count_two_dim_diag_test, latent_tol_solve_mean_and_cov) { |
| 40 | + using stan::math::laplace_latent_tol_solve; |
| 41 | + constexpr double tolerance = 1e-12; |
| 42 | + constexpr int max_num_steps = 1000; |
| 43 | + constexpr int hessian_block_size = 1; |
| 44 | + constexpr int solver = 1; |
| 45 | + constexpr int max_steps_line_search = 0; |
| 46 | + auto [mean_est, chol_est] = laplace_latent_tol_solve( |
| 47 | + poisson_log_likelihood{}, std::forward_as_tuple(y), hessian_block_size, |
| 48 | + stan::math::test::diagonal_kernel_functor{}, |
| 49 | + std::forward_as_tuple(phi(0), phi(1)), |
| 50 | + std::make_tuple(theta_0, tolerance, max_num_steps, solver, |
| 51 | + max_steps_line_search, true), |
| 52 | + nullptr); |
| 53 | + constexpr double tol = 1e-6; |
| 54 | + EXPECT_EQ(2, mean_est.size()); |
| 55 | + EXPECT_NEAR(theta_root(0), mean_est(0), tol); |
| 56 | + EXPECT_NEAR(theta_root(1), mean_est(1), tol); |
| 57 | + EXPECT_NEAR(0.0, chol_est(0, 1), 1e-12); // check lower triangular matrix |
| 58 | + Eigen::MatrixXd Sigma_est = chol_est * chol_est.transpose(); |
| 59 | + EXPECT_NEAR(K_laplace(0, 0), Sigma_est(0, 0), tol); |
| 60 | + EXPECT_NEAR(K_laplace(1, 1), Sigma_est(1, 1), tol); |
| 61 | + EXPECT_NEAR(K_laplace(0, 1), Sigma_est(0, 1), tol); |
| 62 | + EXPECT_NEAR(K_laplace(1, 0), Sigma_est(1, 0), tol); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F(laplace_count_two_dim_diag_test, |
| 66 | + latent_solve_singular_covariance_throws) { |
| 67 | + using stan::math::laplace_latent_solve; |
| 68 | + EXPECT_THROW(({ |
| 69 | + laplace_latent_solve( |
| 70 | + poisson_log_likelihood{}, std::forward_as_tuple(y), 1, |
| 71 | + stan::math::test::diagonal_kernel_functor{}, |
| 72 | + std::forward_as_tuple(0.0, phi(1)), // singular covariance |
| 73 | + nullptr); |
| 74 | + }), |
| 75 | + std::domain_error); |
| 76 | +} |
0 commit comments