-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
Merged
SteveBronder
merged 5 commits into
stan-dev:develop
from
florence-bockting:laplace/return-cov-chol
Jul 14, 2026
+198
−9
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc1693e
refactor: support additional return argument dependening on ReturnMea…
87e2972
feat: add new laplace_latent(_tol)_solve() functions
5f9fe8f
tests: add new tests for laplace_latent_solve
ebff9a7
chore: add laplace_latent_solve to prob.hpp
031e4a0
refactor: remove rng from laplace_latent_*_solve() and add internal d…
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)
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.
Thank you @WardBrian for pointing this out.
I used now the option with minimal code change by introducing a dummy rng that allows to remove the rng argument from the
laplace_latent_tol_solve()\laplace_latent_solve()function.However, I am happy to change it and rather do some refactoring as I am aware that this solution is not very clean.
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.
I think this is probably the best we can do, C++ templating is powerful but not very good at "arguments that only exist in some configurations"