-
-
Notifications
You must be signed in to change notification settings - Fork 209
Add laplace_latent_solve() and laplace_latent_tol_solve()
#3337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
florence-bockting
wants to merge
4
commits into
stan-dev:develop
Choose a base branch
from
florence-bockting:laplace/return-cov-chol
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−9
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #ifndef STAN_MATH_MIX_PROB_LAPLACE_LATENT_SOLVE_HPP | ||
| #define STAN_MATH_MIX_PROB_LAPLACE_LATENT_SOLVE_HPP | ||
|
|
||
| #include <stan/math/mix/functor/laplace_base_rng.hpp> | ||
|
|
||
| namespace stan { | ||
| namespace math { | ||
|
|
||
| /** | ||
| * In a latent gaussian model, | ||
| * | ||
| * theta ~ Normal(0, Sigma(phi)) | ||
| * y ~ p(y|theta,phi) | ||
| * | ||
| * returns the posterior mean and Cholesky factor from the Laplace | ||
| * approximation to p(theta|y,phi), where the log likelihood is given by L_f. | ||
| * @tparam LLFunc Type of likelihood function. | ||
| * @tparam LLArgs Tuple of arguments types of likelihood function. | ||
| * \laplace_common_template_args | ||
| * @param ll_fun Likelihood function. | ||
| * @param ll_args Arguments for likelihood function. | ||
| * \laplace_common_args | ||
| * @param[in] hessian_block_size Block size for the Hessian approximation with | ||
| * respect to the latent gaussian variable theta. | ||
| * \laplace_options | ||
| * \rng_arg | ||
| * \msg_arg | ||
| */ | ||
| template <typename LLFunc, typename LLArgs, typename CovarFun, | ||
| typename CovarArgs, typename RNG, typename OpsTuple> | ||
| inline auto laplace_latent_tol_solve(LLFunc&& ll_fun, LLArgs&& ll_args, | ||
| int hessian_block_size, | ||
| CovarFun&& covariance_function, | ||
| CovarArgs&& covar_args, OpsTuple&& ops, | ||
| RNG& rng, std::ostream* msgs) { | ||
| auto options | ||
| = internal::tuple_to_laplace_options(std::forward<OpsTuple>(ops)); | ||
| options.hessian_block_size = hessian_block_size; | ||
| return laplace_base_rng<true>( | ||
| std::forward<LLFunc>(ll_fun), std::forward<LLArgs>(ll_args), | ||
| std::forward<CovarFun>(covariance_function), | ||
| std::forward<CovarArgs>(covar_args), std::move(options), rng, msgs); | ||
| } | ||
|
|
||
| /** | ||
| * In a latent gaussian model, | ||
| * | ||
| * theta ~ Normal(0, Sigma(phi)) | ||
| * y ~ p(y|theta,phi) | ||
| * | ||
| * returns the posterior mean and Cholesky factor | ||
| * from the Laplace approximation of p(theta | y, phi). | ||
| * @tparam LLFunc Type of likelihood function. | ||
| * @tparam LLArgs Tuple of arguments types of likelihood function. | ||
| * \laplace_common_template_args | ||
| * @tparam RNG A valid boost rng type | ||
| * @param ll_fun Likelihood function. | ||
| * @param ll_args Arguments for likelihood function. | ||
| * \laplace_common_args | ||
| * @param[in] hessian_block_size Block size for the Hessian approximation with | ||
| * respect to the latent gaussian variable theta. | ||
| * \rng_arg | ||
| * \msg_arg | ||
| */ | ||
| template <typename LLFunc, typename LLArgs, typename CovarFun, | ||
| typename CovarArgs, typename RNG> | ||
| inline auto laplace_latent_solve(LLFunc&& ll_fun, LLArgs&& ll_args, | ||
| int hessian_block_size, | ||
| CovarFun&& covariance_function, | ||
| CovarArgs&& covar_args, RNG& rng, | ||
| std::ostream* msgs) { | ||
| auto options = laplace_options_default{hessian_block_size}; | ||
| return laplace_base_rng<true>( | ||
| std::forward<LLFunc>(ll_fun), std::forward<LLArgs>(ll_args), | ||
| std::forward<CovarFun>(covariance_function), | ||
| std::forward<CovarArgs>(covar_args), std::move(options), rng, msgs); | ||
| } | ||
|
|
||
| } // namespace math | ||
| } // namespace stan | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #include <stan/math.hpp> | ||
| #include <stan/math/mix.hpp> | ||
| #include <test/unit/math/laplace/laplace_utility.hpp> | ||
|
|
||
| #include <boost/random/mersenne_twister.hpp> | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <stdexcept> | ||
| #include <vector> | ||
|
|
||
| namespace { | ||
| struct poisson_log_likelihood { | ||
| template <typename Theta> | ||
| auto operator()(const Theta& theta, const std::vector<int>& y, | ||
| std::ostream* pstream) const { | ||
| return stan::math::poisson_log_lpmf(y, theta); | ||
| } | ||
| }; | ||
| } // namespace | ||
|
|
||
| TEST_F(laplace_count_two_dim_diag_test, latent_solve_mean_and_cov) { | ||
| using stan::math::laplace_latent_solve; | ||
| auto [mean_est, chol_est] = laplace_latent_solve( | ||
| poisson_log_likelihood{}, std::forward_as_tuple(y), 1, | ||
| stan::math::test::diagonal_kernel_functor{}, | ||
| std::forward_as_tuple(phi(0), phi(1)), rng, nullptr); | ||
| constexpr double tol = 1e-6; | ||
| EXPECT_EQ(2, mean_est.size()); | ||
| EXPECT_NEAR(theta_root(0), mean_est(0), tol); | ||
| EXPECT_NEAR(theta_root(1), mean_est(1), tol); | ||
| EXPECT_NEAR(0.0, chol_est(0, 1), 1e-12); // check lower triangular matrix | ||
| Eigen::MatrixXd Sigma_est = chol_est * chol_est.transpose(); | ||
| EXPECT_NEAR(K_laplace(0, 0), Sigma_est(0, 0), tol); | ||
| EXPECT_NEAR(K_laplace(1, 1), Sigma_est(1, 1), tol); | ||
| EXPECT_NEAR(K_laplace(0, 1), Sigma_est(0, 1), tol); | ||
| EXPECT_NEAR(K_laplace(1, 0), Sigma_est(1, 0), tol); | ||
| } | ||
|
|
||
| TEST_F(laplace_count_two_dim_diag_test, latent_tol_solve_mean_and_cov) { | ||
| using stan::math::laplace_latent_tol_solve; | ||
| constexpr double tolerance = 1e-12; | ||
| constexpr int max_num_steps = 1000; | ||
| constexpr int hessian_block_size = 1; | ||
| constexpr int solver = 1; | ||
| constexpr int max_steps_line_search = 0; | ||
| auto [mean_est, chol_est] = laplace_latent_tol_solve( | ||
| poisson_log_likelihood{}, std::forward_as_tuple(y), hessian_block_size, | ||
| stan::math::test::diagonal_kernel_functor{}, | ||
| std::forward_as_tuple(phi(0), phi(1)), | ||
| std::make_tuple(theta_0, tolerance, max_num_steps, solver, | ||
| max_steps_line_search, true), | ||
| rng, nullptr); | ||
| constexpr double tol = 1e-6; | ||
| EXPECT_EQ(2, mean_est.size()); | ||
| EXPECT_NEAR(theta_root(0), mean_est(0), tol); | ||
| EXPECT_NEAR(theta_root(1), mean_est(1), tol); | ||
| EXPECT_NEAR(0.0, chol_est(0, 1), 1e-12); // check lower triangular matrix | ||
| Eigen::MatrixXd Sigma_est = chol_est * chol_est.transpose(); | ||
| EXPECT_NEAR(K_laplace(0, 0), Sigma_est(0, 0), tol); | ||
| EXPECT_NEAR(K_laplace(1, 1), Sigma_est(1, 1), tol); | ||
| EXPECT_NEAR(K_laplace(0, 1), Sigma_est(0, 1), tol); | ||
| EXPECT_NEAR(K_laplace(1, 0), Sigma_est(1, 0), tol); | ||
| } | ||
|
|
||
| TEST_F(laplace_count_two_dim_diag_test, | ||
| latent_solve_singular_covariance_throws) { | ||
| using stan::math::laplace_latent_solve; | ||
| EXPECT_THROW(({ | ||
| laplace_latent_solve( | ||
| poisson_log_likelihood{}, std::forward_as_tuple(y), 1, | ||
| stan::math::test::diagonal_kernel_functor{}, | ||
| std::forward_as_tuple(0.0, phi(1)), // singular covariance | ||
| rng, nullptr); | ||
| }), | ||
| std::domain_error); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function would be painful to expose in the language at the moment because of this rng parameter. Looking in the base_rng code, it appears to be completely unused in this branch, so we should find a way to factor it out (or at the very least, pass a dummy value to avoid the need to propagate one into this function)