Skip to content

Commit c909bea

Browse files
committed
Add convergence information to Newton
1 parent 82ea1a9 commit c909bea

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/stan/services/optimize/newton.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stan/callbacks/logger.hpp>
66
#include <stan/callbacks/writer.hpp>
77
#include <stan/io/var_context.hpp>
8+
#include <stan/optimization/bfgs.hpp>
89
#include <stan/optimization/newton.hpp>
910
#include <stan/services/error_codes.hpp>
1011
#include <stan/services/util/initialize.hpp>
@@ -92,14 +93,16 @@ int newton(Model& model, const stan::io::var_context& init,
9293
parameter_writer(names);
9394

9495
double lastlp = lp;
96+
int ret = 0;
97+
9598
for (int m = 0; m < num_iterations; m++) {
9699
if (save_iterations) {
97100
std::vector<double> values;
98101
std::stringstream ss;
99102
model.write_array(rng, cont_vector, disc_vector, values, true, true, &ss);
100103
if (ss.str().length() > 0)
101104
logger.info(ss);
102-
values.insert(values.begin(), {lp, 0});
105+
values.insert(values.begin(), {lp, static_cast<double>(ret)});
103106
parameter_writer(values);
104107
}
105108
interrupt();
@@ -117,13 +120,19 @@ int newton(Model& model, const stan::io::var_context& init,
117120
break;
118121
}
119122

123+
if (std::fabs(lp - lastlp) <= 1e-8) {
124+
ret = optimization::TERM_ABSF;
125+
} else {
126+
ret = optimization::TERM_MAXIT;
127+
}
128+
120129
{
121130
std::vector<double> values;
122131
std::stringstream ss;
123132
model.write_array(rng, cont_vector, disc_vector, values, true, true, &ss);
124133
if (ss.str().length() > 0)
125134
logger.info(ss);
126-
values.insert(values.begin(), {lp, 0});
135+
values.insert(values.begin(), {lp, static_cast<double>(ret)});
127136
parameter_writer(values);
128137
}
129138
return error_codes::OK;

0 commit comments

Comments
 (0)