Skip to content

Commit c99fbcf

Browse files
committed
Fix NewtonState and WolfeInfo constructors
1 parent 8dcfbaf commit c99fbcf

2 files changed

Lines changed: 10 additions & 52 deletions

File tree

stan/math/mix/functor/laplace_marginal_density_estimator.hpp

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -359,27 +359,6 @@ struct NewtonState {
359359
*/
360360
bool final_loop = false;
361361

362-
/**
363-
* @brief Constructs Newton state with given dimensions and functors.
364-
*
365-
* @tparam ThetaInitializer Type of the initial theta provider
366-
* @param theta_size Dimension of the latent space
367-
* @param obj_fun Objective function: (a, theta) -> double
368-
* @param theta_grad_f Gradient function: theta -> grad
369-
* @param theta_init Initial theta value or provider
370-
*/
371-
template <typename ObjFun, typename ThetaGradFun, typename ThetaInitializer>
372-
NewtonState(int theta_size, ObjFun&& obj_fun, ThetaGradFun&& theta_grad_f,
373-
ThetaInitializer&& theta_init)
374-
: wolfe_info(std::forward<ObjFun>(obj_fun), theta_size,
375-
std::forward<ThetaInitializer>(theta_init),
376-
std::forward<ThetaGradFun>(theta_grad_f)),
377-
b(theta_size),
378-
B(theta_size, theta_size),
379-
prev_g(theta_size) {
380-
wolfe_status.num_backtracks_ = -1; // Safe initial value for BB step
381-
}
382-
383362
/**
384363
* @brief Constructs Newton state with a consistent (a_init, theta_init) pair.
385364
*
@@ -392,12 +371,12 @@ struct NewtonState {
392371
* @param a_init Initial a value consistent with theta_init
393372
* @param theta_init Initial theta value
394373
*/
395-
template <typename ObjFun, typename ThetaGradFun, typename ThetaInitializer>
374+
template <typename ObjFun, typename ThetaGradFun, typename CovarianceT, typename ThetaInitializer>
396375
NewtonState(int theta_size, ObjFun&& obj_fun, ThetaGradFun&& theta_grad_f,
397-
const Eigen::VectorXd& a_init, ThetaInitializer&& theta_init)
398-
: wolfe_info(std::forward<ObjFun>(obj_fun), a_init,
376+
CovarianceT&& covariance, ThetaInitializer&& theta_init)
377+
: wolfe_info(std::forward<ObjFun>(obj_fun), covariance.llt().solve(theta_init),
399378
std::forward<ThetaInitializer>(theta_init),
400-
std::forward<ThetaGradFun>(theta_grad_f), 0),
379+
std::forward<ThetaGradFun>(theta_grad_f)),
401380
b(theta_size),
402381
B(theta_size, theta_size),
403382
prev_g(theta_size) {
@@ -1213,16 +1192,7 @@ inline auto laplace_marginal_density_est(
12131192
// the prior term -0.5 * a'*theta vanishes (a=0 while theta!=0), inflating
12141193
// the initial objective and causing the Wolfe line search to reject the
12151194
// first Newton step.
1216-
auto make_state = [&](auto&& theta_0) {
1217-
if constexpr (InitTheta) {
1218-
Eigen::VectorXd a_init = covariance.llt().solve(Eigen::VectorXd(theta_0));
1219-
return internal::NewtonState(theta_size, obj_fun, theta_grad_f, a_init,
1220-
theta_0);
1221-
} else {
1222-
return internal::NewtonState(theta_size, obj_fun, theta_grad_f, theta_0);
1223-
}
1224-
};
1225-
auto state = make_state(theta_init);
1195+
auto state = NewtonState(theta_size, obj_fun, theta_grad_f, covariance, theta_init);
12261196
// Start with safe step size
12271197
auto update_fun = create_update_fun(
12281198
std::move(obj_fun), std::move(theta_grad_f), covariance, options);

stan/math/mix/functor/wolfe_line_search.hpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -499,19 +499,7 @@ struct WolfeInfo {
499499
Eigen::VectorXd p_;
500500
// Initial directional derivative
501501
double init_dir_;
502-
template <typename ObjFun, typename Theta0, typename ThetaGradF>
503-
WolfeInfo(ObjFun&& obj_fun, Eigen::Index n, Theta0&& theta0,
504-
ThetaGradF&& theta_grad_f)
505-
: curr_(std::forward<ObjFun>(obj_fun), n, std::forward<Theta0>(theta0),
506-
std::forward<ThetaGradF>(theta_grad_f)),
507-
prev_(curr_),
508-
scratch_(n) {
509-
if (!std::isfinite(curr_.obj())) {
510-
throw std::domain_error(
511-
"laplace_marginal_density: log likelihood is not finite at initial "
512-
"theta and likelihood arguments.");
513-
}
514-
}
502+
515503
/**
516504
* Construct WolfeInfo with a consistent (a_init, theta_init) pair.
517505
*
@@ -521,10 +509,10 @@ struct WolfeInfo {
521509
* an inflated initial objective (the prior term -0.5 * a'*theta would
522510
* otherwise vanish when a is zero but theta is not).
523511
*/
524-
template <typename ObjFun, typename Theta0, typename ThetaGradF>
525-
WolfeInfo(ObjFun&& obj_fun, const Eigen::VectorXd& a_init, Theta0&& theta0,
526-
ThetaGradF&& theta_grad_f, int /*tag*/)
527-
: curr_(std::forward<ObjFun>(obj_fun), a_init,
512+
template <typename ObjFun, typename Theta0, typename AInit, typename ThetaGradF>
513+
WolfeInfo(ObjFun&& obj_fun, AInit&& a_init, Theta0&& theta0,
514+
ThetaGradF&& theta_grad_f)
515+
: curr_(std::forward<ObjFun>(obj_fun), std::forward<AInit>(a_init),
528516
std::forward<Theta0>(theta0),
529517
std::forward<ThetaGradF>(theta_grad_f)),
530518
prev_(curr_),

0 commit comments

Comments
 (0)