Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
do_bfgs_optimize.hpp
Go to the documentation of this file.
1 #ifndef STAN__COMMON__DO_BFGS_OPTIMIZE_HPP
2 #define STAN__COMMON__DO_BFGS_OPTIMIZE_HPP
3 
4 #include <fstream>
5 #include <iostream>
6 #include <iomanip>
7 #include <vector>
8 
10 
11 #include <stan/common/do_print.hpp>
14 
15 namespace stan {
16 
17  namespace common {
18 
19  template<typename ModelT,typename BFGSOptimizerT,typename RNGT,
20  typename StartIterationCallback>
21  int do_bfgs_optimize(ModelT &model, BFGSOptimizerT &bfgs,
22  RNGT &base_rng,
23  double &lp,
24  std::vector<double> &cont_vector,
25  std::vector<int> &disc_vector,
26  std::fstream* output_stream,
27  std::ostream* notice_stream,
28  bool save_iterations,
29  int refresh,
30  StartIterationCallback& callback) {
31  lp = bfgs.logp();
32 
33  if (notice_stream)
34  (*notice_stream) << "initial log joint probability = " << lp << std::endl;
35  if (output_stream && save_iterations) {
36  write_iteration(*output_stream, model, base_rng,
37  lp, cont_vector, disc_vector);
38  }
39 
40  int ret = 0;
41 
42  while (ret == 0) {
43  callback();
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;
53  }
54 
55  ret = bfgs.step();
56  lp = bfgs.logp();
57  bfgs.params_r(cont_vector);
58 
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)
62  << lp << " ";
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;
75  }
76 
77  if (output_stream && save_iterations) {
78  write_iteration(*output_stream, model, base_rng,
79  lp, cont_vector, disc_vector);
80  }
81  }
82 
83  int return_code;
84  if (ret >= 0) {
85  if (notice_stream)
86  (*notice_stream) << "Optimization terminated normally: " << std::endl;
87  return_code = stan::gm::error_codes::OK;
88  } else {
89  if (notice_stream)
90  (*notice_stream) << "Optimization terminated with error: " << std::endl;
91  return_code = stan::gm::error_codes::SOFTWARE;
92  }
93  if (notice_stream)
94  (*notice_stream) << " " << bfgs.get_code_string(ret) << std::endl;
95 
96  return return_code;
97  }
98 
99  }
100 
101 }
102 #endif
103 
bool do_print(const int n, const bool special, const int refresh)
Indicates whether it should print on the current iteration.
Definition: do_print.hpp:25
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)

     [ Stan Home Page ] © 2011–2014, Stan Development Team.