@@ -29,7 +29,7 @@ namespace math {
2929 */
3030struct laplace_options_base {
3131 /* Size of the blocks in block diagonal hessian*/
32- int hessian_block_size{1 }; // 0
32+ int hessian_block_size{1 }; // 0
3333 /* *
3434 * Which linear solver to use inside the Newton step.
3535 *
@@ -45,19 +45,19 @@ struct laplace_options_base {
4545 * `Sigma = K_root * K_root^T` and form `B = I + K_root^T * W * K_root`.
4646 * 3. General LU: form `B = I + Sigma * W` and factorize with LU.
4747 */
48- int solver{1 }; // 1
48+ int solver{1 }; // 1
4949 /* *
5050 * Iterations end when the absolute change in the optimization objective
5151 * is less than this tolerance.
5252 *
5353 * Note: the objective used for convergence is the one optimized by the
5454 * Newton/Wolfe loop (not the final Laplace-corrected log marginal density).
5555 */
56- double tolerance{1.49012e-08 }; // 2
56+ double tolerance{1.49012e-08 }; // 2
5757 /* Maximum number of steps*/
58- int max_num_steps{500 }; // 3
59- int allow_fallthrough{true }; // 4
60- laplace_line_search_options line_search; // 5
58+ int max_num_steps{500 }; // 3
59+ int allow_fallthrough{true }; // 4
60+ laplace_line_search_options line_search; // 5
6161 laplace_options_base () = default ;
6262 laplace_options_base (int hessian_block_size_, int solver_, double tolerance_,
6363 int max_num_steps_, bool allow_fallthrough_,
@@ -79,16 +79,16 @@ struct laplace_options<false> : public laplace_options_base {};
7979template <>
8080struct laplace_options <true > : public laplace_options_base {
8181 /* Value for user supplied initial theta */
82- Eigen::VectorXd theta_0{0 }; // 6
82+ Eigen::VectorXd theta_0{0 }; // 6
8383
8484 template <typename ThetaVec>
8585 laplace_options (ThetaVec&& theta_0_, int hessian_block_size_, int solver_,
8686 double tolerance_, int max_num_steps_,
8787 bool allow_fallthrough_, int max_steps_line_search_)
8888 : laplace_options_base(hessian_block_size_, solver_, tolerance_,
89- max_num_steps_, allow_fallthrough_, max_steps_line_search_),
90- theta_0 (std::forward<ThetaVec>(theta_0_)) {
91- }
89+ max_num_steps_, allow_fallthrough_,
90+ max_steps_line_search_),
91+ theta_0 (std::forward<ThetaVec>(theta_0_)) { }
9292};
9393
9494using laplace_options_default = laplace_options<false >;
@@ -101,84 +101,80 @@ using laplace_options_user_supplied = laplace_options<true>;
101101 */
102102inline auto generate_laplace_options (int theta_0_size) {
103103 auto ops = laplace_options_default{};
104- return std::make_tuple (Eigen::VectorXd::Zero (theta_0_size).eval (), // 0 -> 6
105- ops.tolerance ,
106- ops.max_num_steps ,
107- ops.hessian_block_size ,
108- ops.solver ,
109- ops.line_search .max_iterations ,
110- static_cast <int >(ops.allow_fallthrough )
111- );
104+ return std::make_tuple (
105+ Eigen::VectorXd::Zero (theta_0_size).eval (), // 0 -> 6
106+ ops.tolerance , ops.max_num_steps , ops.hessian_block_size , ops.solver ,
107+ ops.line_search .max_iterations , static_cast <int >(ops.allow_fallthrough ));
112108}
113109
114-
115110namespace internal {
116111
117112template <std::size_t N, typename Tuple, typename CheckType>
118- inline constexpr bool is_tuple_type_v = std::is_same_v<std::decay_t <CheckType>, std::tuple_element_t <N, std::decay_t <Tuple>>>;
113+ inline constexpr bool is_tuple_type_v
114+ = std::is_same_v<std::decay_t <CheckType>,
115+ std::tuple_element_t <N, std::decay_t <Tuple>>>;
119116
120117template <typename Ops>
121118inline constexpr auto tuple_to_laplace_options (Ops&& ops) {
122119 if constexpr (is_tuple_v<Ops>) {
123120 if constexpr (!is_eigen_v<std::tuple_element_t <0 , std::decay_t <Ops>>>) {
124121 static_assert (
125- sizeof (std::decay_t <Ops>*) == 0 ,
126- " ERROR:(laplace_marginal_lpdf) The first laplace argument is "
127- " expected to be an Eigen vector of dynamic size representing the "
128- " initial theta_0." );
122+ sizeof (std::decay_t <Ops>*) == 0 ,
123+ " ERROR:(laplace_marginal_lpdf) The first laplace argument is "
124+ " expected to be an Eigen vector of dynamic size representing the "
125+ " initial theta_0." );
129126 }
130127 if constexpr (!is_tuple_type_v<1 , Ops, double >) {
131128 static_assert (
132- sizeof (std::decay_t <Ops>*) == 0 ,
133- " ERROR:(laplace_marginal_lpdf) The second laplace argument is "
134- " expected to be a double representing the tolerance." );
135-
129+ sizeof (std::decay_t <Ops>*) == 0 ,
130+ " ERROR:(laplace_marginal_lpdf) The second laplace argument is "
131+ " expected to be a double representing the tolerance." );
136132 }
137133 if constexpr (!is_tuple_type_v<2 , Ops, int >) {
138134 static_assert (
139- sizeof (std::decay_t <Ops>*) == 0 ,
140- " ERROR:(laplace_marginal_lpdf) The third laplace argument is "
141- " expected to be an int representing the maximum number of steps." );
135+ sizeof (std::decay_t <Ops>*) == 0 ,
136+ " ERROR:(laplace_marginal_lpdf) The third laplace argument is "
137+ " expected to be an int representing the maximum number of steps." );
142138 }
143139 if constexpr (!is_tuple_type_v<3 , Ops, int >) {
144140 static_assert (
145- sizeof (std::decay_t <Ops>*) == 0 ,
146- " ERROR:(laplace_marginal_lpdf) The fourth laplace argument is "
147- " expected to be an int representing the solver." );
141+ sizeof (std::decay_t <Ops>*) == 0 ,
142+ " ERROR:(laplace_marginal_lpdf) The fourth laplace argument is "
143+ " expected to be an int representing the solver." );
148144 }
149145 if constexpr (!is_tuple_type_v<4 , Ops, int >) {
150146 static_assert (
151- sizeof (std::decay_t <Ops>*) == 0 ,
152- " ERROR:(laplace_marginal_lpdf) The fifth laplace argument is "
153- " expected to be an int representing the hessian block size." );
147+ sizeof (std::decay_t <Ops>*) == 0 ,
148+ " ERROR:(laplace_marginal_lpdf) The fifth laplace argument is "
149+ " expected to be an int representing the hessian block size." );
154150 }
155151 if constexpr (!is_tuple_type_v<5 , Ops, int >) {
156152 static_assert (
157- sizeof (std::decay_t <Ops>*) == 0 ,
158- " ERROR:(laplace_marginal_lpdf) The sixth laplace argument is "
159- " expected to be an int representing the max steps line search." );
153+ sizeof (std::decay_t <Ops>*) == 0 ,
154+ " ERROR:(laplace_marginal_lpdf) The sixth laplace argument is "
155+ " expected to be an int representing the max steps line search." );
160156 }
161- if constexpr (!(is_tuple_type_v<6 , Ops, int > || is_tuple_type_v<6 , Ops, bool >)) {
157+ if constexpr (!(is_tuple_type_v<6 , Ops,
158+ int > || is_tuple_type_v<6 , Ops, bool >)) {
162159 static_assert (
163- sizeof (std::decay_t <Ops>*) == 0 ,
164- " ERROR:(laplace_marginal_lpdf) The seventh laplace argument is "
165- " expected to be an int representing allow fallthrough (0/1)." );
160+ sizeof (std::decay_t <Ops>*) == 0 ,
161+ " ERROR:(laplace_marginal_lpdf) The seventh laplace argument is "
162+ " expected to be an int representing allow fallthrough (0/1)." );
166163 }
167164 return laplace_options_user_supplied{
168- value_of (std::get<0 >(std::forward<Ops>(ops))),
169- std::get<3 >(ops),
170- std::get<4 >(ops),
171- std::get<1 >(ops),
172- std::get<2 >(ops),
173- (std::get<6 >(ops) > 0 ) ? true : false ,
174- std::get<5 >(ops),
165+ value_of (std::get<0 >(std::forward<Ops>(ops))),
166+ std::get<3 >(ops),
167+ std::get<4 >(ops),
168+ std::get<1 >(ops),
169+ std::get<2 >(ops),
170+ (std::get<6 >(ops) > 0 ) ? true : false ,
171+ std::get<5 >(ops),
175172 };
176173 } else {
177174 return std::forward<Ops>(ops);
178175 }
179176}
180177
181-
182178template <typename ThetaVec, typename WR , typename L_t, typename A_vec,
183179 typename ThetaGrad, typename LU_t, typename KRoot>
184180struct laplace_density_estimates {
@@ -1094,12 +1090,12 @@ inline auto run_newton_loop(SolverPolicy& solver, NewtonStateT& state,
10941090 * @param next_solver Name of the solver being attempted next
10951091 * @param e Exception that caused the fallback
10961092 */
1097- inline void log_solver_fallback (const bool allow_fallthrough, std::ostream* msgs, std::string_view context,
1093+ inline void log_solver_fallback (const bool allow_fallthrough,
1094+ std::ostream* msgs, std::string_view context,
10981095 Eigen::Index iter,
10991096 std::string_view failed_solver,
11001097 std::string_view next_solver,
11011098 const std::exception& e) {
1102-
11031099 // Build once so we don't interleave with other logs.
11041100 std::ostringstream os;
11051101 os << " [" << context << " ] WARNING: solver fallback\n "
@@ -1109,13 +1105,10 @@ inline void log_solver_fallback(const bool allow_fallthrough, std::ostream* msgs
11091105 << " " << std::left << std::setw (12 ) << " action:"
11101106 << " trying " << next_solver << " \n " ;
11111107 if (!allow_fallthrough) {
1112- throw std::domain_error (
1113- std::string (" [" ) + std::string (context)
1114- + " ]" );
1108+ throw std::domain_error (std::string (" [" ) + std::string (context) + " ]" );
11151109 } else if (msgs) {
11161110 (*msgs) << os.str ();
11171111 }
1118-
11191112}
11201113
11211114/* *
@@ -1297,7 +1290,8 @@ inline auto laplace_marginal_density_est(
12971290 std::string solver_type
12981291 = (options.hessian_block_size == 1 ) ? " Diagonal" : " Block" ;
12991292 std::string failed = " solver 1 (" + solver_type + " Hessian-root Cholesky)" ;
1300- log_solver_fallback (options.allow_fallthrough , msgs, " laplace_marginal_density" , step_iter, failed,
1293+ log_solver_fallback (options.allow_fallthrough , msgs,
1294+ " laplace_marginal_density" , step_iter, failed,
13011295 " solver 2 (Covariance-root Cholesky)" , e);
13021296 }
13031297 try {
@@ -1308,7 +1302,8 @@ inline auto laplace_marginal_density_est(
13081302 throw_overstep, msgs);
13091303 }
13101304 } catch (const std::exception& e) {
1311- log_solver_fallback (options.allow_fallthrough , msgs, " laplace_marginal_density" , step_iter,
1305+ log_solver_fallback (options.allow_fallthrough , msgs,
1306+ " laplace_marginal_density" , step_iter,
13121307 " solver 2 (Covariance-root Cholesky)" ,
13131308 " solver 3 (General LU solver)" , e);
13141309 }
0 commit comments