newton_test.cpp
Go to the documentation of this file.
2 #include <gtest/gtest.h>
4 #include <test/test-models/good/optimization/rosenbrock.hpp>
7 
8 
10  int n;
11  mock_callback() : n(0) { }
12 
13  void operator()() {
14  n++;
15  }
16 };
17 
18 
19 class values
21 public:
22  std::vector<std::string> names_;
23  std::vector<std::vector<double> > states_;
24 
25  values(std::ostream& stream)
26  : stan::callbacks::stream_writer(stream) {
27  }
28 
29  /**
30  * Writes a set of names.
31  *
32  * @param[in] names Names in a std::vector
33  */
34  void operator()(const std::vector<std::string>& names) {
35  names_ = names;
36  }
37 
38  /**
39  * Writes a set of values.
40  *
41  * @param[in] state Values in a std::vector
42  */
43  void operator()(const std::vector<double>& state) {
44  states_.push_back(state);
45  }
46 
47 };
48 
49 
50 class ServicesOptimizeNewton : public testing::Test {
51 public:
53  : init(init_ss),
54  parameter(parameter_ss),
55  model(context, &model_ss) {}
56 
57  std::stringstream init_ss, parameter_ss, model_ss;
62  stan_model model;
63 };
64 
65 
67  unsigned int seed = 0;
68  unsigned int chain = 1;
69  double init_radius = 0;
70 
71  int num_interations = 1000;
72  bool save_iterations = true;
73  mock_callback callback;
74 
76  seed, chain, init_radius,
77  num_interations, save_iterations,
78  callback,
79  logger,
80  init,
81  parameter);
82 
83  EXPECT_EQ(0, return_code);
84  EXPECT_EQ(logger.call_count(), logger.call_count_info()) << "all output to info";
85  EXPECT_EQ(1, logger.find("Initial log joint probability = -1"));
86  EXPECT_EQ(1, logger.find("Iteration 1. Log joint probability ="));
87 
88  ASSERT_EQ(3, parameter.names_.size());
89  EXPECT_EQ("lp__", parameter.names_[0]);
90  EXPECT_EQ("x", parameter.names_[1]);
91  EXPECT_EQ("y", parameter.names_[2]);
92 
93  EXPECT_GT(parameter.states_.size(), 0);
94  EXPECT_FLOAT_EQ(0, parameter.states_.front()[1])
95  << "initial value should be (0, 0)";
96  EXPECT_FLOAT_EQ(0, parameter.states_.front()[2])
97  << "initial value should be (0, 0)";
98  EXPECT_NEAR(1, parameter.states_.back()[1], 1e-3)
99  << "optimal value should be (1, 1)";
100  EXPECT_NEAR(1, parameter.states_.back()[2], 1e-3)
101  << "optimal value should be (1, 1)";
102  EXPECT_FLOAT_EQ(return_code, 0);
103  EXPECT_GT(callback.n, 0);
104 }
105 
106 TEST_F(ServicesOptimizeNewton, rosenbrock_no_save_iterations) {
107  unsigned int seed = 0;
108  unsigned int chain = 1;
109  double init_radius = 0;
110 
111  int num_interations = 1000;
112  bool save_iterations = false;
113  mock_callback callback;
114 
116  seed, chain, init_radius,
117  num_interations, save_iterations,
118  callback,
119  logger,
120  init,
121  parameter);
122 
123  EXPECT_EQ(0, return_code);
124  EXPECT_EQ(logger.call_count(), logger.call_count_info()) << "all output to info";
125  EXPECT_EQ(1, logger.find("Initial log joint probability = -1"));
126  EXPECT_EQ(1, logger.find("Iteration 1. Log joint probability ="));
127 
128  EXPECT_EQ("0,0\n", init_ss.str());
129 
130  ASSERT_EQ(3, parameter.names_.size());
131  EXPECT_EQ("lp__", parameter.names_[0]);
132  EXPECT_EQ("x", parameter.names_[1]);
133  EXPECT_EQ("y", parameter.names_[2]);
134 
135  EXPECT_EQ(1, parameter.states_.size());
136  EXPECT_NEAR(1, parameter.states_.back()[1], 1e-3)
137  << "optimal value should be (1, 1)";
138  EXPECT_NEAR(1, parameter.states_.back()[2], 1e-3)
139  << "optimal value should be (1, 1)";
140  EXPECT_FLOAT_EQ(return_code, 0);
141  EXPECT_GT(callback.n, 0);
142 }
void operator()(const std::vector< double > &state)
Definition: newton_test.cpp:43
void operator()()
Definition: newton_test.cpp:13
stan::callbacks::stream_writer init
Definition: newton_test.cpp:59
TEST_F(ServicesOptimizeNewton, rosenbrock)
Definition: newton_test.cpp:66
std::stringstream parameter_ss
Definition: newton_test.cpp:57
stan::io::empty_var_context context
Definition: newton_test.cpp:61
unsigned int seed
Definition: runWimpSim.h:102
def callbacks(model_name, group, tensorboard=True)
Definition: regression.py:123
chain
Check that an output directory exists.
values(std::ostream &stream)
Definition: newton_test.cpp:25
const XML_Char * context
Definition: expat.h:434
int newton(Model &model, stan::io::var_context &init, unsigned int random_seed, unsigned int chain, double init_radius, int num_iterations, bool save_iterations, callbacks::interrupt &interrupt, callbacks::logger &logger, callbacks::writer &init_writer, callbacks::writer &parameter_writer)
Definition: newton.hpp:42
Float_t e
Definition: plot.C:35
void operator()(const std::vector< std::string > &names)
Definition: newton_test.cpp:34
const XML_Char XML_Content * model
Definition: expat.h:151
stan::test::unit::instrumented_logger logger
Definition: newton_test.cpp:58