Skip to content

Commit 031e4a0

Browse files
author
Florence Bockting
committed
refactor: remove rng from laplace_latent_*_solve() and add internal dummy rng
1 parent ebff9a7 commit 031e4a0

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

stan/math/mix/prob/laplace_latent_solve.hpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
namespace stan {
77
namespace math {
88

9+
namespace internal {
10+
// Placeholder RNG type: used whenever laplace_base_rng's
11+
// sampling branch is compiled out via `if constexpr`. In this case
12+
// no rng is needed as no sampling is performed.
13+
struct laplace_unused_rng {};
14+
15+
} // namespace internal
16+
917
/**
1018
* In a latent gaussian model,
1119
*
@@ -23,23 +31,24 @@ namespace math {
2331
* @param[in] hessian_block_size Block size for the Hessian approximation with
2432
* respect to the latent gaussian variable theta.
2533
* \laplace_options
26-
* \rng_arg
2734
* \msg_arg
2835
*/
2936
template <typename LLFunc, typename LLArgs, typename CovarFun,
30-
typename CovarArgs, typename RNG, typename OpsTuple>
37+
typename CovarArgs, typename OpsTuple>
3138
inline auto laplace_latent_tol_solve(LLFunc&& ll_fun, LLArgs&& ll_args,
3239
int hessian_block_size,
3340
CovarFun&& covariance_function,
3441
CovarArgs&& covar_args, OpsTuple&& ops,
35-
RNG& rng, std::ostream* msgs) {
42+
std::ostream* msgs) {
3643
auto options
3744
= internal::tuple_to_laplace_options(std::forward<OpsTuple>(ops));
3845
options.hessian_block_size = hessian_block_size;
39-
return laplace_base_rng<true>(
40-
std::forward<LLFunc>(ll_fun), std::forward<LLArgs>(ll_args),
41-
std::forward<CovarFun>(covariance_function),
42-
std::forward<CovarArgs>(covar_args), std::move(options), rng, msgs);
46+
internal::laplace_unused_rng unused_rng;
47+
return laplace_base_rng<true>(std::forward<LLFunc>(ll_fun),
48+
std::forward<LLArgs>(ll_args),
49+
std::forward<CovarFun>(covariance_function),
50+
std::forward<CovarArgs>(covar_args),
51+
std::move(options), unused_rng, msgs);
4352
}
4453

4554
/**
@@ -59,21 +68,21 @@ inline auto laplace_latent_tol_solve(LLFunc&& ll_fun, LLArgs&& ll_args,
5968
* \laplace_common_args
6069
* @param[in] hessian_block_size Block size for the Hessian approximation with
6170
* respect to the latent gaussian variable theta.
62-
* \rng_arg
6371
* \msg_arg
6472
*/
6573
template <typename LLFunc, typename LLArgs, typename CovarFun,
66-
typename CovarArgs, typename RNG>
74+
typename CovarArgs>
6775
inline auto laplace_latent_solve(LLFunc&& ll_fun, LLArgs&& ll_args,
6876
int hessian_block_size,
6977
CovarFun&& covariance_function,
70-
CovarArgs&& covar_args, RNG& rng,
71-
std::ostream* msgs) {
78+
CovarArgs&& covar_args, std::ostream* msgs) {
7279
auto options = laplace_options_default{hessian_block_size};
73-
return laplace_base_rng<true>(
74-
std::forward<LLFunc>(ll_fun), std::forward<LLArgs>(ll_args),
75-
std::forward<CovarFun>(covariance_function),
76-
std::forward<CovarArgs>(covar_args), std::move(options), rng, msgs);
80+
internal::laplace_unused_rng unused_rng;
81+
return laplace_base_rng<true>(std::forward<LLFunc>(ll_fun),
82+
std::forward<LLArgs>(ll_args),
83+
std::forward<CovarFun>(covariance_function),
84+
std::forward<CovarArgs>(covar_args),
85+
std::move(options), unused_rng, msgs);
7786
}
7887

7988
} // namespace math

test/unit/math/laplace/laplace_latent_solve_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ struct poisson_log_likelihood {
2020

2121
TEST_F(laplace_count_two_dim_diag_test, latent_solve_mean_and_cov) {
2222
using stan::math::laplace_latent_solve;
23-
auto [mean_est, chol_est] = laplace_latent_solve(
24-
poisson_log_likelihood{}, std::forward_as_tuple(y), 1,
25-
stan::math::test::diagonal_kernel_functor{},
26-
std::forward_as_tuple(phi(0), phi(1)), rng, nullptr);
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);
2727
constexpr double tol = 1e-6;
2828
EXPECT_EQ(2, mean_est.size());
2929
EXPECT_NEAR(theta_root(0), mean_est(0), tol);
@@ -49,7 +49,7 @@ TEST_F(laplace_count_two_dim_diag_test, latent_tol_solve_mean_and_cov) {
4949
std::forward_as_tuple(phi(0), phi(1)),
5050
std::make_tuple(theta_0, tolerance, max_num_steps, solver,
5151
max_steps_line_search, true),
52-
rng, nullptr);
52+
nullptr);
5353
constexpr double tol = 1e-6;
5454
EXPECT_EQ(2, mean_est.size());
5555
EXPECT_NEAR(theta_root(0), mean_est(0), tol);
@@ -70,7 +70,7 @@ TEST_F(laplace_count_two_dim_diag_test,
7070
poisson_log_likelihood{}, std::forward_as_tuple(y), 1,
7171
stan::math::test::diagonal_kernel_functor{},
7272
std::forward_as_tuple(0.0, phi(1)), // singular covariance
73-
rng, nullptr);
73+
nullptr);
7474
}),
7575
std::domain_error);
7676
}

0 commit comments

Comments
 (0)