TEST(AgradRevMatrix, trace_gen_inv_quad_form_ldlt_grad_vvd) { using stan::math::sum; using stan::agrad::matrix_v; using stan::math::matrix_d; matrix_v av(4,4); matrix_d ad(4,4); matrix_d bd(4,2); matrix_d cd(2,2); matrix_v cv(2,2); AVAR res; AVEC vars; VEC grad; size_t i,j,pos; bd << 100, 10, 0, 1, -3, -3, 5, 2; ad << 9.0, 3.0, 3.0, 3.0, 3.0, 10.0, 2.0, 2.0, 3.0, 2.0, 7.0, 1.0, 3.0, 2.0, 1.0, 112.0; av << 9.0, 3.0, 3.0, 3.0, 3.0, 10.0, 2.0, 2.0, 3.0, 2.0, 7.0, 1.0, 3.0, 2.0, 1.0, 112.0; cd.setIdentity(2,2); cv.setIdentity(2,2); stan::math::LDLT_factor<stan::agrad::var,-1,-1> ldlt_av; ldlt_av.compute(av); ASSERT_TRUE(ldlt_av.success()); matrix_d ainv(ad.inverse()); matrix_d dqda(-ainv*bd*cd.transpose()*bd.transpose()*ainv); matrix_d dqdc(bd.transpose()*ainv.transpose()*bd); // var-var res = trace_gen_inv_quad_form_ldlt(cv,ldlt_av,bd); vars.clear(); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) vars.push_back(cv(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) vars.push_back(av(i,j)); grad = cgradvec(res,vars); pos = 0; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqdc(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqda(i,j)); }
TEST(AgradRevMatrix, trace_gen_quad_form_mat_grad_vdv) { using stan::math::trace_gen_quad_form; using stan::math::sum; using stan::agrad::matrix_v; using stan::math::matrix_d; matrix_v av(4,4); matrix_d ad(4,4); matrix_d bd(4,2); matrix_v bv(4,2); matrix_d cd(2,2); matrix_v cv(2,2); AVAR res; AVEC vars; VEC grad; size_t i,j,pos; bd << 100, 10, 0, 1, -3, -3, 5, 2; bv << 100, 10, 0, 1, -3, -3, 5, 2; ad << 2.0, 3.0, 4.0, 5.0, 6.0, 10.0, 2.0, 2.0, 7.0, 2.0, 7.0, 1.0, 8.0, 2.0, 1.0, 112.0; av << 2.0, 3.0, 4.0, 5.0, 6.0, 10.0, 2.0, 2.0, 7.0, 2.0, 7.0, 1.0, 8.0, 2.0, 1.0, 112.0; cd.setIdentity(2,2); cv.setIdentity(2,2); matrix_d dqdb(ad*bd*cd.transpose() + ad.transpose()*bd*cd); matrix_d dqdc(bd.transpose()*ad.transpose()*bd); res = trace_gen_quad_form(cv,ad,bv); vars.clear(); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) vars.push_back(cv(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 2; j++) vars.push_back(bv(i,j)); grad = cgradvec(res,vars); pos = 0; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqdc(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 2; j++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqdb(i,j)); }
TEST(AgradRevMatrix, quad_form_sym_mat_grad_vv) { using stan::math::quad_form_sym; using stan::math::sum; using stan::agrad::matrix_v; using stan::math::matrix_d; matrix_v av(4,4); matrix_d ad(4,4); matrix_d bd(4,2); matrix_v bv(4,2); AVAR res; AVEC vars; VEC grad; size_t i,j,pos; bd << 100, 10, 0, 1, -3, -3, 5, 2; bv << 100, 10, 0, 1, -3, -3, 5, 2; ad << 2.0, 3.0, 4.0, 5.0, 3.0, 10.0, 2.0, 2.0, 4.0, 2.0, 7.0, 1.0, 5.0, 2.0, 1.0, 112.0; av << 2.0, 3.0, 4.0, 5.0, 3.0, 10.0, 2.0, 2.0, 4.0, 2.0, 7.0, 1.0, 5.0, 2.0, 1.0, 112.0; matrix_d dqda(bd*matrix_d::Ones(2,2)*bd.transpose()); matrix_d dqdb((ad*bd + ad.transpose()*bd)*matrix_d::Ones(2,2)); // var-var res = sum(quad_form_sym(av,bv)); vars.clear(); for (i = 0; i < 4; i++) for (j = 0; j < 2; j++) vars.push_back(bv(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) vars.push_back(av(i,j)); grad = cgradvec(res,vars); pos = 0; for (i = 0; i < 4; i++) for (j = 0; j < 2; j++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqdb(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqda(i,j)); }
TEST(AgradRevMatrix, append_col_row_vector) { row_vector_v a(3); row_vector_v a_square(3); RowVectorXd b(3); a << 2.0, 3.0, 9.0; b << 4.0, 3.0, 0.0; AVEC x; for (int i = 0; i < 3; ++i) { x.push_back(a(i)); a_square(i) = square(a(i)); } AVAR append_col_ab = sum(append_col(a_square, b)); VEC g = cgradvec(append_col_ab, x); size_t idx = 0; for (int i = 0; i < 3; i++) EXPECT_FLOAT_EQ(a(i).val()*2.0, g[idx++]); set_zero_all_adjoints(); append_col_ab = sum(append_col(a_square, 2.1)); g = cgradvec(append_col_ab, x); idx = 0; for (int i = 0; i < 3; i++) EXPECT_FLOAT_EQ(a(i).val()*2.0, g[idx++]); set_zero_all_adjoints(); append_col_ab = sum(append_col(2.1, a_square)); g = cgradvec(append_col_ab, x); idx = 0; for (int i = 0; i < 3; i++) EXPECT_FLOAT_EQ(a(i).val()*2.0, g[idx++]); set_zero_all_adjoints(); append_col_ab = sum(append_col(a_square(2)*3.0, b)); append_col_ab.grad(); EXPECT_FLOAT_EQ(a(2).val()*6.0, x[2].adj()); set_zero_all_adjoints(); append_col_ab = sum(append_col(b, a_square(1)*3.0)); append_col_ab.grad(); EXPECT_FLOAT_EQ(a(1).val()*6.0, x[1].adj()); stan::math::recover_memory(); }
TEST(AgradRevMatrix, dot_product_vd) { AVEC a; VEC b; AVAR c; for (int i = -1; i < 2; i++) { // a = (-1, 0, 1), b = (1, 2, 3) a.push_back(i); b.push_back(i + 2); } c = dot_product(&a[0], &b[0], 3); EXPECT_EQ(2, c); VEC grad; c.grad(a, grad); EXPECT_EQ(grad[0], 1); EXPECT_EQ(grad[1], 2); EXPECT_EQ(grad[2], 3); }
TEST(AgradRevMatrix, dot_product_dv_vec) { VEC a; AVEC b; AVAR c; for (int i = -1; i < 2; i++) { // a = (-1, 0, 1), b = (1, 2, 3) a.push_back(i); b.push_back(i + 2); } c = dot_product(a, b); EXPECT_EQ(2, c); VEC grad; c.grad(b, grad); EXPECT_EQ(grad[0], -1); EXPECT_EQ(grad[1], 0); EXPECT_EQ(grad[2], 1); }
TEST(AgradRevMatrix, quad_form_sym_vec_grad_vv) { using stan::math::quad_form_sym; using stan::agrad::matrix_v; using stan::agrad::vector_v; using stan::math::matrix_d; using stan::math::vector_d; matrix_v av(4,4); matrix_d ad(4,4); vector_d bd(4); vector_v bv(4); stan::agrad::var res; AVEC vars; VEC grad; size_t pos, i, j; bd << 100, 0, -3, 5; bv << 100, 0, -3, 5; ad << 2.0, 3.0, 4.0, 5.0, 3.0, 10.0, 2.0, 2.0, 4.0, 2.0, 7.0, 1.0, 5.0, 2.0, 1.0, 112.0; av << 2.0, 3.0, 4.0, 5.0, 3.0, 10.0, 2.0, 2.0, 4.0, 2.0, 7.0, 1.0, 5.0, 2.0, 1.0, 112.0; matrix_d dqda(bd*bd.transpose()); vector_d dqdb(ad*bd + ad.transpose()*bd); // var-var res = quad_form_sym(av,bv); vars.clear(); for (size_t i = 0; i < 4; i++) vars.push_back(bv[i]); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) vars.push_back(av(i,j)); grad = cgradvec(res,vars); for (i = 0, pos = 0; i < 4; i++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqdb[i]); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqda(i,j)); }
TEST(AgradRevMatrix, trace_quad_form_ldlt_mat_grad_dv) { using stan::math::sum; using stan::math::matrix_v; using stan::math::matrix_d; matrix_d ad(4,4); matrix_d bd(4,2); matrix_v bv(4,2); AVAR res; AVEC vars; VEC grad; size_t i,j,pos; bd << 100, 10, 0, 1, -3, -3, 5, 2; bv << 100, 10, 0, 1, -3, -3, 5, 2; ad << 9.0, 3.0, 3.0, 3.0, 3.0, 10.0, 2.0, 2.0, 3.0, 2.0, 7.0, 1.0, 3.0, 2.0, 1.0, 112.0; stan::math::LDLT_factor<double,-1,-1> ldlt_ad; ldlt_ad.compute(ad); ASSERT_TRUE(ldlt_ad.success()); matrix_d ainv(ad.inverse()); matrix_d dqdb(ainv*bd + ainv.transpose()*bd); // var-var res = trace_inv_quad_form_ldlt(ldlt_ad,bv); vars.clear(); for (i = 0; i < 4; i++) for (j = 0; j < 2; j++) vars.push_back(bv(i,j)); grad = cgradvec(res,vars); pos = 0; for (i = 0; i < 4; i++) for (j = 0; j < 2; j++, pos++) EXPECT_FLOAT_EQ(grad[pos], dqdb(i,j)); }
void test_sort_indices_desc(VEC val) { AVEC x; for(size_t i=0U; i<val.size(); i++) x.push_back(AVAR(val[i])); std::vector<int> val_sorted = sort_indices_desc(val); std::vector<int> x_sorted = sort_indices_desc(x); for(size_t i=0U; i<val.size(); i++) EXPECT_EQ(val_sorted[i],x_sorted[i]); for(size_t i=0U; i<val.size(); i++) for(size_t j=0U; j<val.size(); j++) if(val_sorted[i] == val[j]) EXPECT_EQ(x_sorted[i],x[j]); else EXPECT_FALSE(x_sorted[i]==x[j]); }
void test_sort_asc(VEC val) { using stan::math::sort_asc; using stan::agrad::sort_asc; AVEC x; for(size_t i=0U; i<val.size(); i++) x.push_back(AVAR(val[i])); VEC val_sorted = sort_asc(val); AVEC x_sorted = sort_asc(x); for(size_t i=0U; i<val.size(); i++) EXPECT_EQ(val_sorted[i],x_sorted[i].val()); for(size_t i=0U; i<val.size(); i++) for(size_t j=0U; j<val.size(); j++) if(val_sorted[i] == val[j]) EXPECT_EQ(x_sorted[i],x[j]); else EXPECT_FALSE(x_sorted[i]==x[j]); }
TEST(AgradRevMatrix, distance_vd) { using stan::math::vector_d; using stan::math::vector_v; vector_v a(3); vector_d b(3); AVAR c; for (int i = -1; i < 2; i++) { // a = (-1, 0, 1), b = (1, 2, 3) a(i+1) = i; b(i+1) = i + 2; } c = stan::math::distance(a, b); EXPECT_FLOAT_EQ(3.464102, c.val()); AVEC av; VEC grad; for (size_t i = 0; i < 3; i++) { av.push_back(a[i]); } c.grad(av, grad); EXPECT_FLOAT_EQ((a(0).val() - b(0)) / 3.464102, grad[0]); EXPECT_FLOAT_EQ((a(1).val() - b(1)) / 3.464102, grad[1]); EXPECT_FLOAT_EQ((a(2).val() - b(2)) / 3.464102, grad[2]); }
TEST(AgradRevMatrix, dot_product_vv_vec) { AVEC a, b; AVAR c; for (int i = -1; i < 2; i++) { // a = (-1, 0, 1), b = (1, 2, 3) a.push_back(i); b.push_back(i + 2); } c = dot_product(a, b); EXPECT_EQ(2, c); AVEC ab; VEC grad; for (size_t i = 0; i < 3; i++) { ab.push_back(a[i]); ab.push_back(b[i]); } c.grad(ab, grad); EXPECT_EQ(grad[0], 1); EXPECT_EQ(grad[1], -1); EXPECT_EQ(grad[2], 2); EXPECT_EQ(grad[3], 0); EXPECT_EQ(grad[4], 3); EXPECT_EQ(grad[5], 1); }
AVEC createAVEC(AVAR x1, AVAR x2) { AVEC v; v.push_back(x1); v.push_back(x2); return v; }
TEST(AgradRevMatrix, trace_gen_inv_quad_form_ldlt_grad_vvv_basic) { using stan::math::sum; using stan::agrad::matrix_v; using stan::math::matrix_d; using stan::math::inverse; using stan::math::multiply; using stan::math::trace; using stan::math::transpose; matrix_v cv(2,2); matrix_v av(4,4); matrix_v bv(4,2); AVAR result, result_basic; double result_val, result_basic_val; AVEC vars; VEC grad, grad_basic; size_t i,j; stan::math::LDLT_factor<stan::agrad::var,-1,-1> ldlt_av; // calculate gradient using trace_gen_inv_quad_form_ldlt bv << 100, 10, 0, 1, -3, -3, 5, 2; av << 9.0, 3.0, 3.0, 3.0, 3.0, 10.0, 2.0, 2.0, 3.0, 2.0, 7.0, 1.0, 3.0, 2.0, 1.0, 112.0; cv << 1, 2, 3, 4; ldlt_av.compute(av); ASSERT_TRUE(ldlt_av.success()); result = trace_gen_inv_quad_form_ldlt(cv,ldlt_av,bv); vars.clear(); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) vars.push_back(cv(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 2; j++) vars.push_back(bv(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) vars.push_back(av(i,j)); grad = cgradvec(result,vars); result_val = result.val(); // calculate gradient using basic math bv << 100, 10, 0, 1, -3, -3, 5, 2; av << 9.0, 3.0, 3.0, 3.0, 3.0, 10.0, 2.0, 2.0, 3.0, 2.0, 7.0, 1.0, 3.0, 2.0, 1.0, 112.0; cv << 1, 2, 3, 4; matrix_v tmp = bv.transpose() * inverse(av) * bv; matrix_v gen_inv_quad_form = multiply(cv, tmp); result_basic = trace(gen_inv_quad_form); vars.clear(); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) vars.push_back(cv(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 2; j++) vars.push_back(bv(i,j)); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) vars.push_back(av(i,j)); grad_basic = cgradvec(result_basic,vars); result_basic_val = result_basic.val(); // check values; EXPECT_FLOAT_EQ(result_basic_val, result_val); ASSERT_EQ(grad_basic.size(), grad.size()); for (size_t n = 0; n < grad_basic.size(); n++) { EXPECT_FLOAT_EQ(grad_basic[n], grad[n]); } }
AVEC createAVEC(AVAR x1, AVAR x2, AVAR x3, AVAR x4) { AVEC v = createAVEC(x1,x2,x3); v.push_back(x4); return v; }
AVEC createAVEC(AVAR x) { AVEC v; v.push_back(x); return v; }
AVEC createAVEC(AVAR x1, AVAR x2, AVAR x3, AVAR x4, AVAR x5, AVAR x6, AVAR x7, AVAR x8) { AVEC v = createAVEC(x1,x2,x3,x4,x5,x6,x7); v.push_back(x8); return v; }
AVEC createAVEC(AVAR x1, AVAR x2, AVAR x3, AVAR x4, AVAR x5, AVAR x6) { AVEC v = createAVEC(x1,x2,x3,x4,x5); v.push_back(x6); return v; }