#define BDMLIB // not an ideal way to prevent double registration of UI factories... #include "test_util.h" #include "stat/exp_family.h" #include #include #include #include #include #include #include #include #include #include namespace bdm { using itpp::vec; using itpp::mat; std::string load_test_file(const char *fname) { char buffer[8192]; memset(buffer, 0, sizeof(buffer)); std::ifstream src(fname, std::ios_base::binary); src.read(buffer, sizeof(buffer) - 1); return std::string(buffer); } bool remove_all(const char *path) { DIR *dir; dirent *de; bool rv = true; if ((dir = opendir(path)) != 0) { try { std::string top(path); top += "/"; while ((de = readdir(dir)) != 0) { if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) { std::string subpath(top); subpath += de->d_name; remove_all(subpath.c_str()); } } } catch (...) { closedir(dir); throw; } closedir(dir); if (rmdir(path)) { std::string msg = "can't remove dir "; msg += path; throw std::runtime_error(msg); } } else { if (errno == ENOTDIR) { if (unlink(path)) { std::string msg = "can't remove file "; msg += path; throw std::runtime_error(msg); } } else { if (errno != ENOENT) { std::string msg = "can't remove "; msg += path; throw std::runtime_error(msg); } else { // it wasn't there in the first place rv = false; } } } return rv; } double normcoef(const epdf *ep, const vec &xb, const vec &yb, int xn, int yn) { mat Pdf(xn + 1, yn + 1); vec rgr(2); double xstep = (xb(1) - xb(0)) / xn; double ystep = (yb(1) - yb(0)) / yn; int i = 0; for (double x = xb(0); x <= xb(1); x += xstep, i++) { rgr(0) = x; int j = 0; for (double y = yb(0); y <= yb(1); y += ystep, j++) { rgr(1) = y; Pdf(i, j) = exp(ep->evallog(rgr)); } } return sumsum(Pdf) * xstep * ystep; } vec num_mean2(const epdf *ep, const vec &xb, const vec &yb, int xn, int yn) { mat Pdf(xn + 1, yn + 1); vec rgr(2); double xstep = (xb(1) - xb(0)) / xn; double ystep = (yb(1) - yb(0)) / yn; vec Mu(xn + 1); vec Si(yn + 1); int i = 0; for (double x = xb(0); x <= xb(1); x += xstep, i++) { Mu(i) = x; rgr(0) = x; int j = 0; for (double y = yb(0); y <= yb(1); y += ystep, j++) { Si(j) = y; rgr(1) = y; Pdf(i, j) = exp(ep->evallog(rgr)); } } vec fm = sum(Pdf, 2); vec fs = sum(Pdf, 1); return vec_2(Mu * fm / sum(fm), Si * fs / sum(fs)); } }