TEST(ModelUtil, streams) { stan::test::capture_std_streams(); std::fstream data_stream(std::string("").c_str(), std::fstream::in); stan::io::dump data_var_context(data_stream); data_stream.close(); stan_model model(data_var_context, static_cast<std::stringstream*>(0)); std::vector<double> params_r(1); std::vector<int> params_i(0); std::vector<double> gradient; std::stringstream out; try { stan::model::log_prob_propto<true, stan_model>(model, params_r, params_i, 0); stan::model::log_prob_propto<false, stan_model>(model, params_r, params_i, 0); out.str(""); stan::model::log_prob_propto<true, stan_model>(model, params_r, params_i, &out); stan::model::log_prob_propto<false, stan_model>(model, params_r, params_i, &out); EXPECT_EQ("", out.str()); } catch (...) { FAIL() << "log_prob_propto"; } try { Eigen::VectorXd p(1); stan::model::log_prob_propto<true, stan_model>(model, p, 0); stan::model::log_prob_propto<false, stan_model>(model, p, 0); out.str(""); stan::model::log_prob_propto<true, stan_model>(model, p, &out); stan::model::log_prob_propto<false, stan_model>(model, p, &out); EXPECT_EQ("", out.str()); } catch (...) { FAIL() << "log_prob_propto"; } stan::test::reset_std_streams(); EXPECT_EQ("", stan::test::cout_ss.str()); EXPECT_EQ("", stan::test::cerr_ss.str()); }
TEST(ModelUtil, finite_diff_grad__false_false) { TestModel_uniform_01 model; std::vector<double> params_r(1); std::vector<int> params_i(0); std::vector<double> gradient; for (int i = 0; i < 10; i++) { params_r[0] = (i-5.0) * 10; stan::model::finite_diff_grad<false,false,TestModel_uniform_01> (model, params_r, params_i, gradient); ASSERT_EQ(1U, gradient.size()); EXPECT_FLOAT_EQ(0.0, gradient[0]); } }
TEST(ModelUtil, finite_diff_grad__true_true) { TestModel_uniform_01 model; std::vector<double> params_r(1); std::vector<int> params_i(0); std::vector<double> gradient; for (int i = 0; i < 10; i++) { double x = (i - 5.0) * 10; params_r[0] = x; stan::model::finite_diff_grad<true,true,TestModel_uniform_01> (model, params_r, params_i, gradient); ASSERT_EQ(1U, gradient.size()); double expected_gradient = -std::tanh(0.5 * x); EXPECT_FLOAT_EQ(expected_gradient, gradient[0]); } }
TEST(ModelUtil, streams) { stan::test::capture_std_streams(); std::fstream data_stream(std::string("").c_str(), std::fstream::in); stan::io::dump data_var_context(data_stream); data_stream.close(); stan_model model(data_var_context, static_cast<std::stringstream*>(0)); std::vector<double> params_r(1); std::vector<int> params_i(0); std::vector<double> gradient; stan::callbacks::interrupt interrupt; std::stringstream out; try { stan::callbacks::stream_writer writer(out); stan::test::unit::instrumented_logger logger; out.str(""); stan::model::test_gradients<true, true, stan_model>(model, params_r, params_i, 1e-6, 1e-6, interrupt, logger, writer); EXPECT_EQ("\n Log probability=0\n\n param idx value model finite diff error\n 0 0 0 0 0\n", out.str()); out.str(""); stan::model::test_gradients<true, false, stan_model>(model, params_r, params_i, 1e-6, 1e-6, interrupt, logger, writer); EXPECT_EQ("\n Log probability=0\n\n param idx value model finite diff error\n 0 0 0 0 0\n", out.str()); out.str(""); stan::model::test_gradients<false, true, stan_model>(model, params_r, params_i, 1e-6, 1e-6, interrupt, logger, writer); EXPECT_EQ("\n Log probability=0\n\n param idx value model finite diff error\n 0 0 0 0 0\n", out.str()); out.str(""); stan::model::test_gradients<false, false, stan_model>(model, params_r, params_i, 1e-6, 1e-6, interrupt, logger, writer); EXPECT_EQ("\n Log probability=0\n\n param idx value model finite diff error\n 0 0 0 0 0\n", out.str()); } catch (...) { FAIL() << "test_gradients"; } stan::test::reset_std_streams(); EXPECT_EQ("", stan::test::cout_ss.str()); EXPECT_EQ("", stan::test::cerr_ss.str()); }