1 #ifndef STAN__COMMON__DO_BFGS_OPTIMIZE_HPP
2 #define STAN__COMMON__DO_BFGS_OPTIMIZE_HPP
19 template<
typename ModelT,
typename BFGSOptimizerT,
typename RNGT,
20 typename StartIterationCallback>
24 std::vector<double> &cont_vector,
25 std::vector<int> &disc_vector,
26 std::fstream* output_stream,
27 std::ostream* notice_stream,
30 StartIterationCallback& callback) {
34 (*notice_stream) <<
"initial log joint probability = " << lp << std::endl;
35 if (output_stream && save_iterations) {
37 lp, cont_vector, disc_vector);
44 if (notice_stream &&
do_print(bfgs.iter_num(), 50*refresh)) {
45 (*notice_stream) <<
" Iter ";
46 (*notice_stream) <<
" log prob ";
47 (*notice_stream) <<
" ||dx|| ";
48 (*notice_stream) <<
" ||grad|| ";
49 (*notice_stream) <<
" alpha ";
50 (*notice_stream) <<
" alpha0 ";
51 (*notice_stream) <<
" # evals ";
52 (*notice_stream) <<
" Notes " << std::endl;
57 bfgs.params_r(cont_vector);
59 if (notice_stream && (
do_print(bfgs.iter_num(), ret != 0 || !bfgs.note().empty(),refresh))) {
60 (*notice_stream) <<
" " << std::setw(7) << bfgs.iter_num() <<
" ";
61 (*notice_stream) <<
" " << std::setw(12) << std::setprecision(6)
63 (*notice_stream) <<
" " << std::setw(12) << std::setprecision(6)
64 << bfgs.prev_step_size() <<
" ";
65 (*notice_stream) <<
" " << std::setw(12) << std::setprecision(6)
66 << bfgs.curr_g().norm() <<
" ";
67 (*notice_stream) <<
" " << std::setw(10) << std::setprecision(4)
68 << bfgs.alpha() <<
" ";
69 (*notice_stream) <<
" " << std::setw(10) << std::setprecision(4)
70 << bfgs.alpha0() <<
" ";
71 (*notice_stream) <<
" " << std::setw(7)
72 << bfgs.grad_evals() <<
" ";
73 (*notice_stream) <<
" " << bfgs.note() <<
" ";
74 (*notice_stream) << std::endl;
77 if (output_stream && save_iterations) {
79 lp, cont_vector, disc_vector);
86 (*notice_stream) <<
"Optimization terminated normally: " << std::endl;
90 (*notice_stream) <<
"Optimization terminated with error: " << std::endl;
94 (*notice_stream) <<
" " << bfgs.get_code_string(ret) << std::endl;
bool do_print(const int n, const bool special, const int refresh)
Indicates whether it should print on the current iteration.
void write_iteration(std::ostream &output_stream, Model &model, RNG &base_rng, double lp, std::vector< double > &cont_vector, std::vector< int > &disc_vector)
int do_bfgs_optimize(ModelT &model, BFGSOptimizerT &bfgs, RNGT &base_rng, double &lp, std::vector< double > &cont_vector, std::vector< int > &disc_vector, std::fstream *output_stream, std::ostream *notice_stream, bool save_iterations, int refresh, StartIterationCallback &callback)