int main(int argv, char** argc) { printf("\n\nRunning code for question 1.11:\n\n"); int train_area1[4] = {150-1,330-1,264-1,328-1}; /*read images from file*/ pnm_img * kande1 = pnm_read(IMG_IN_DIR "kande1.pnm"), * kande2 = pnm_read(IMG_IN_DIR "kande2.pnm"); pnm_img * train_img =pnm_subimage(kande1, train_area1); mtrx * train_set = img2train_set(train_img); vect * s_mean = sample_mean(train_set); mtrx * s_cov = sample_cov(train_set, s_mean); double* map = (double*)malloc(kande2->width*kande2->height*sizeof(double)); pdf_map(kande2, s_mean,s_cov,&map); vect* w_mean = weighted_avg_pos(kande2, map); pnm_pixmap p = {0,255,0}; for (int x = -2; x<4; x++) for (int y=-2; y<4; y++) { pnm_set_pixel(kande2, (*w_mean->data)+x, *(w_mean->data+1)+y, &p); } // pnm_write(kande1, IMG_OUT_DIR"cas10center.pnm"); mtrx* w_cov = weighted_2dcov(map, w_mean, kande2); FILE* fp = fopen(TEX_OUT_DIR"c11.tex", "w"); vect2tex(w_mean, "celevenwmean", fp); mtrx2tex(w_cov, "celevenwcov", fp); fclose(fp); gplot_img2splot(kande2, 0 ,TEX_OUT_DIR"case11.kande2.gnuplot.dat"); gplot_pdf2splot(w_mean, w_cov, kande2, 5, TEX_OUT_DIR"case11.pdf.gnuplot.dat"); gsl_matrix_free(train_set); gsl_matrix_free(s_cov); gsl_vector_free(s_mean); pnm_destroy(kande1); pnm_destroy(train_img); }
TEST(McmcDenseEMetric, sample_p) { rng_t base_rng(0); Eigen::Matrix2d m(2,2); m(0, 0) = 3.0; m(1, 0) = -2.0; m(0, 1) = -2.0; m(1, 1) = 4.0; Eigen::Matrix2d m_inv = m.inverse(); stan::mcmc::mock_model model(2); stan::mcmc::dense_e_metric<stan::mcmc::mock_model, rng_t> metric(model); stan::mcmc::dense_e_point z(2); z.set_metric(m_inv); int n_samples = 1000; Eigen::Matrix2d sample_cov(2,2); sample_cov(0, 0) = 0.0; sample_cov(0, 1) = 0.0; sample_cov(1, 0) = 0.0; sample_cov(1, 1) = 0.0; for (int i = 0; i < n_samples; ++i) { metric.sample_p(z, base_rng); sample_cov(0, 0) += z.p[0] * z.p[0] / n_samples; sample_cov(0, 1) += z.p[0] * z.p[1] / n_samples; sample_cov(1, 0) += z.p[1] * z.p[0] / n_samples; sample_cov(1, 1) += z.p[1] * z.p[1] / n_samples; } Eigen::Matrix2d var(2,2); var(0, 0) = 2 * m(0, 0); var(1, 0) = m(1, 0) * m(1, 0) + m(1, 1) * m(0, 0); var(0, 1) = m(0, 1) * m(0, 1) + m(1, 1) * m(0, 0); var(1, 1) = 2 * m(1, 1); // Covariance matrix within 5sigma of expected value (comes from a Wishart distribution) EXPECT_TRUE(std::fabs(m(0, 0) - sample_cov(0, 0)) < 5.0 * sqrt(var(0, 0) / n_samples)); EXPECT_TRUE(std::fabs(m(1, 0) - sample_cov(1, 0)) < 5.0 * sqrt(var(1, 0) / n_samples)); EXPECT_TRUE(std::fabs(m(0, 1) - sample_cov(0, 1)) < 5.0 * sqrt(var(0, 1) / n_samples)); EXPECT_TRUE(std::fabs(m(1, 1) - sample_cov(1, 1)) < 5.0 * sqrt(var(1, 1) / n_samples)); }