vector<int> Recoloring::MatchGaussians(CvEM& source_model, CvEM& target_model) { int num_g = source_model.get_nclusters(); Mat sMu(source_model.get_means()); Mat tMu(target_model.get_means()); const CvMat** target_covs = target_model.get_covs(); const CvMat** source_covs = source_model.get_covs(); double best_dist = std::numeric_limits<double>::max(); vector<int> best_res(num_g); vector<int> prmt(num_g); for(int itr = 0; itr < 10; itr++) { for(int i=0;i<num_g;i++) prmt[i] = i; //make a permutation randShuffle(Mat(prmt)); //Greedy selection vector<int> res(num_g); vector<bool> taken(num_g); for(int sg = 0; sg < num_g; sg++) { double min_dist = std::numeric_limits<double>::max(); int minv = -1; for(int tg = 0; tg < num_g; tg++) { if(taken[tg]) continue; //TODO: can save on re-calculation of pairs - calculate affinity matrix ahead //double d = norm(sMu(Range(prmt[sg],prmt[sg]+1),Range(0,3)), tMu(Range(tg,tg+1),Range(0,3))); //symmetric kullback-leibler Mat diff = Mat(sMu(Range(prmt[sg],prmt[sg]+1),Range(0,3)) - tMu(Range(tg,tg+1),Range(0,3))); Mat d = diff * Mat(Mat(source_covs[prmt[sg]]).inv() + Mat(target_covs[tg]).inv()) * diff.t(); Scalar tr = trace(Mat( Mat(Mat(source_covs[prmt[sg]])*Mat(target_covs[tg])) + Mat(Mat(target_covs[tg])*Mat(source_covs[prmt[sg]]).inv()) + Mat(Mat::eye(3,3,CV_64FC1)*2) )); double kl_dist = ((double*)d.data)[0] + tr[0]; if(kl_dist<min_dist) { min_dist = kl_dist; minv = tg; } } res[prmt[sg]] = minv; taken[minv] = true; } double dist = 0; for(int i=0;i<num_g;i++) { dist += norm(sMu(Range(prmt[i],prmt[i]+1),Range(0,3)), tMu(Range(res[prmt[i]],res[prmt[i]]+1),Range(0,3))); } if(dist < best_dist) { best_dist = dist; best_res = res; } } return best_res; }
// [[Rcpp::export]] VectorXd bootR2pred(const MatrixXd X, const VectorXd y, int nBoot){ RNGScope scope; const int n(X.rows()); // const int p(X.cols()); VectorXd R2s(nBoot); MatrixXd Xti(X); VectorXd yti(y); MatrixXd Xvi(X); VectorXd yvi(y); IntegerVector prmt(n); IntegerVector prmv(n); // double R2i(R2pred(Xti, yti, Xvi, yvi)); for(int i = 0; i < nBoot; ++i) { prmt = bootPerm(n); prmv = bootPerm(n); Xti = shuffleMatrix(X, prmt); yti = shuffleVector(y, prmt); Xvi = shuffleMatrix(X, prmv); yvi = shuffleVector(y, prmv); R2s(i) = R2pred(Xti, yti, Xvi, yvi); } return R2s; }
/* main */ int main(int argc, char *argv[]) { char *filename = NULL; char *command = NULL; /* Parse arguments */ if (argc >= 2) { arg1_eval(argv[1]); /* catch and print help/version and exit */ if (*(argv[1]) == '-') { command = argv[1]; if (argc >= 3) filename = argv[2]; } else { filename = argv[1]; } } /* Commands found */ if (command != NULL) { int i; for (i = 'a'; i <= 'z'; ++i) { if (strchr(command, i) && commands[i - 'a'] != __na) (commands[i - 'a'])(); } /* -e EXPR on command line, rest of args into buffer expr */ if (strchr(command, 'e')) { int i; int len = 0; for (i = 2; i < argc; ++i) len += strlen(argv[i]) + 1; expr = malloc(len + 2); expr[0] = 0; for (i = 2; i < argc; ++i) { strcat(expr, argv[i]); strcat(expr, i < argc-1 ? " " : "\n"); } strcat(expr, "\0\0"); filename = NULL; } } /* Open input stream; Prompt: tty - yes, script/pipe - no */ if (!filename) { yyin = stdin; if (!expr && isatty(fileno(yyin))) prmt(); } else { yyin = fopen(filename, "r"); if (!yyin) { yyerror("Cannot open input file"); exit(1); } } /* readline or not? */ use_readline = isatty(fileno(yyin)); #ifdef NO_READLINE use_readline = 0; #endif /* Run the actual calculator */ ps1 = num_display(&output, 0); if (prompt && !use_readline) { fflush(NULL); } yyparse(); if (filename) fclose(yyin); quit(); }