Skip to content

Commit

Permalink
avoid double calculation of intial likelihood for 'lbfgs'
Browse files Browse the repository at this point in the history
  • Loading branch information
fabsig committed Apr 3, 2024
1 parent 103985d commit 7764850
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
22 changes: 22 additions & 0 deletions external_libs/LBFGSpp/include/LBFGS.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ class LBFGSSolver
// Evaluate function and compute gradient
fx = f(x, m_grad, true, true);// ChangedForGPBoost

std::string init_coef_str = "";
if (f.HasCovariates())
{
init_coef_str = " and 'init_coef'";
}
std::string problem_str = "none";
if (std::isnan(fx))
{
problem_str = "NaN";
}
else if (std::isinf(fx))
{
problem_str = "Inf";
}
if (problem_str != "none")
{
Log::REFatal((problem_str + " occurred in initial approximate negative marginal log-likelihood. "
"Possible solutions: try other initial values ('init_cov_pars'" + init_coef_str + ") "
"or other tuning parameters in case you apply the GPBoost algorithm (e.g., learning_rate)").c_str());
}
Log::REDebug("Initial approximate negative marginal log-likelihood: %g", fx);

m_gnorm = m_grad.norm();
if (fpast > 0)
m_fx[0] = fx;
Expand Down
4 changes: 4 additions & 0 deletions include/GPBoost/optim_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ namespace GPBoost {
return neg_log_likelihood;
}

bool HasCovariates() {
return(re_model_templ_->HasCovariates());
}

/*!
* \brief Set the iteration number in re_model_templ_ (e.g. for correlation-based neighbor selection in Vecchia approximations)
* \param iter iteration number
Expand Down
62 changes: 32 additions & 30 deletions include/GPBoost/re_model_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -868,36 +868,38 @@ namespace GPBoost {
SetCovParsComps(cov_aux_pars.segment(0, num_cov_par_));
RedetermineNearestNeighborsVecchia();//called only if gp_approx == "vecchia" and neighbors are selected based on correlations and not distances
}
CalcCovFactorOrModeAndNegLL(cov_aux_pars.segment(0, num_cov_par_), fixed_effects_ptr);
// TODO: for likelihood evaluation we don't need y_aux = Psi^-1 * y but only Psi^-0.5 * y. So, if has_covariates_==true, we might skip this step here and save some time
string_t ll_str;
if (gauss_likelihood_) {
ll_str = "negative log-likelihood";
}
else {
ll_str = "approximate negative marginal log-likelihood";
}
string_t init_coef_str = "";
if (has_covariates_) {
init_coef_str = " and 'init_coef'";
}
string_t problem_str = "none";
if (std::isnan(neg_log_likelihood_)) {
problem_str = "NaN";
}
else if (std::isinf(neg_log_likelihood_)) {
problem_str = "Inf";
}
if (problem_str != "none") {
Log::REFatal((problem_str + " occurred in initial " + ll_str + ". "
"Possible solutions: try other initial values ('init_cov_pars'" + init_coef_str + ") "
"or other tuning parameters in case you apply the GPBoost algorithm (e.g., learning_rate)").c_str());
}
if (gauss_likelihood_) {
Log::REDebug("Initial negative log-likelihood: %g", neg_log_likelihood_);
}
else {
Log::REDebug("Initial approximate negative marginal log-likelihood: %g", neg_log_likelihood_);
if (optimizer_cov_pars_ != "lbfgs" && optimizer_cov_pars_ != "lbfgs_linesearch_nocedal_wright") {
CalcCovFactorOrModeAndNegLL(cov_aux_pars.segment(0, num_cov_par_), fixed_effects_ptr);
// TODO: for likelihood evaluation we don't need y_aux = Psi^-1 * y but only Psi^-0.5 * y. So, if has_covariates_==true, we might skip this step here and save some time
string_t ll_str;
if (gauss_likelihood_) {
ll_str = "negative log-likelihood";
}
else {
ll_str = "approximate negative marginal log-likelihood";
}
string_t init_coef_str = "";
if (has_covariates_) {
init_coef_str = " and 'init_coef'";
}
string_t problem_str = "none";
if (std::isnan(neg_log_likelihood_)) {
problem_str = "NaN";
}
else if (std::isinf(neg_log_likelihood_)) {
problem_str = "Inf";
}
if (problem_str != "none") {
Log::REFatal((problem_str + " occurred in initial " + ll_str + ". "
"Possible solutions: try other initial values ('init_cov_pars'" + init_coef_str + ") "
"or other tuning parameters in case you apply the GPBoost algorithm (e.g., learning_rate)").c_str());
}
if (gauss_likelihood_) {
Log::REDebug("Initial negative log-likelihood: %g", neg_log_likelihood_);
}
else {
Log::REDebug("Initial approximate negative marginal log-likelihood: %g", neg_log_likelihood_);
}
}
bool na_or_inf_occurred = false;
if (OPTIM_EXTERNAL_.find(optimizer_cov_pars_) != OPTIM_EXTERNAL_.end()) {
Expand Down

0 comments on commit 7764850

Please sign in to comment.