double log_fact_table::cum_hyper_right(int kin_node, int kout_g, int tm, int degree_node) { //cout<<"kin_node... "<<kin_node<<" "<<kout_g<<" "<<tm<<" "<<degree_node<<endl; // this is bigger or equal p(x >= kin_node) *** EQUAL *** if (kin_node > min(degree_node, kout_g)) return 0; if (tm - kout_g - degree_node + kin_node <= 0) return 1; if (kin_node <= 0) return 1; if (kin_node < double(kout_g + 1) / double(tm + 2) * double(degree_node + 1)) return (1. - cum_hyper_left(kin_node, kout_g, tm, degree_node)); int x = kin_node; double pzero = hyper(x, kout_g, tm, degree_node); //* if (pzero <= 1e-40) return 0; //*/ double ga = tm - kout_g - degree_node; int kout_g_p = kout_g + 1; double degree_node_p = degree_node + 1; double z_zero = 1.; double sum = z_zero; while (true) { ++x; z_zero *= double(kout_g_p - x) / (x * (ga + x)) * (degree_node_p - x); if (z_zero < log_table_pr * sum) break; if (pzero * sum > 1) return pzero; sum += z_zero; } return pzero * sum; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Check for proper number of arguments if (nrhs != NUMARGS) { mexErrMsgTxt("Incorrect number of input arguments required."); } else if (nlhs > NUMOUT) { mexErrMsgTxt("Too many output arguments expected."); } if (!mxIsSparse(prhs[0])) mexErrMsgTxt("Not sparse!\n"); checkInput(prhs[1], VECTOR, "uint32"); //phi checkInput(prhs[2], STRUCT); // params int N = mxGetN(prhs[0]); int D = mxGetM(prhs[0]); const mxArray* x = prhs[0]; arr(unsigned int) z = getArrayInput<unsigned int>(prhs[1]); const mxArray* params = prhs[2]; double alphah = getInput<double>(getField(params,0,"alpha")); double diralphah = getInput<double>(getField(params,0,"diralpha")); double logalphah = log(alphah); int its_crp = getInput<double>(getField(params,0,"its_crp")); int its_ms = getInput<double>(getField(params,0,"its_ms")); int its = max(its_crp, its_ms); int Mproc = getInput<double>(getField(params,0,"Mproc")); int K = getInput<double>(getField(params,0,"K")); dir_sampled hyper(Mproc, D, diralphah); hyper.update_posteriors(); // processor temporary variables //Mproc = min(Mproc,omp_get_max_threads()); omp_set_num_threads(Mproc); clusters_FSD_mn model(N, D, K, x, z, alphah, hyper, Mproc); model.initialize(); plhs[0] = mxCreateNumericMatrix(its_crp,1,mxDOUBLE_CLASS,mxREAL); plhs[1] = mxCreateNumericMatrix(its_crp,1,mxDOUBLE_CLASS,mxREAL); arr(double) timeArr = getArrayInput<double>(plhs[0]); arr(double) EArr = getArrayInput<double>(plhs[1]); stopwatch timer; for (int it=0; it<its_crp; it++) { timer.tic(); model.sample_params(); model.sample_labels(); double time = timer.toc(); timeArr[it] = time; EArr[it] = model.joint_loglikelihood(); } }
double log_fact_table::cum_hyper_left(int kin_node, int kout_g, int tm, int degree_node) { // this is strictly less p(x < kin_node) *** NOT EQUAL *** //cout<<kin_node<<" node: "<<degree_node<<" group: "<<tm<<" "<<degree_node<<endl; if (kin_node <= 0) return 0; if (tm - kout_g - degree_node + kin_node <= 0) return 0; if (kin_node > min(degree_node, kout_g)) return 1; if (kin_node > double(kout_g + 1) / double(tm + 2) * double(degree_node + 1)) return (1. - cum_hyper_right(kin_node, kout_g, tm, degree_node)); int x = kin_node - 1; double pzero = hyper(x, kout_g, tm, degree_node); //cout<<"pzero: "<<pzero<<" "<<log_hyper(x, kout_g, tm, degree_node)<<" gsl: "<<(gsl_ran_hypergeometric_pdf(x, kout_g, tm - kout_g, degree_node))<<endl; //* if (pzero <= 1e-40) return 0; //*/ double ga = tm - kout_g - degree_node; int kout_g_p = kout_g + 1; double degree_node_p = degree_node + 1; double z_zero = 1.; double sum = z_zero; //cout<<"pzero "<<pzero<<" "<<z_zero<<" "<<kin_node<<endl; while (true) { z_zero *= (ga + x) / ((degree_node_p - x) * (kout_g_p - x)) * x; --x; //cout<<"zzero sum "<<z_zero<<" "<<sum<<" "<<(ga + x)<<endl; if (z_zero < log_table_pr * sum) break; if (pzero * sum > 1) return pzero; sum += z_zero; } return pzero * sum; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Check for proper number of arguments if (nrhs != NUMARGS) { mexErrMsgTxt("Incorrect number of input arguments required."); } else if (nlhs > NUMOUT) { mexErrMsgTxt("Too many output arguments expected."); } checkInput(prhs[0], "double"); //x checkInput(prhs[1], VECTOR, "uint32"); //phi checkInput(prhs[2], STRUCT); // params const int *dims = mxGetDimensions(prhs[0]); int N = mxGetNumberOfElements(prhs[0]); int D = 1; if (mxGetNumberOfDimensions(prhs[0])>1) D = dims[0]; N /= D; int D2 = D*D; arr(double) x = getArrayInput<double>(prhs[0]); arr(unsigned int) z = getArrayInput<unsigned int>(prhs[1]); const mxArray* params = prhs[2]; double alphah = getInput<double>(getField(params,0,"alpha")); double logalphah = log(alphah); double kappah = getInput<double>(getField(params,0,"kappa")); double nuh = getInput<double>(getField(params,0,"nu")); arr(double) thetah = getArrayInput<double>(getField(params,0,"theta")); arr(double) deltah = getArrayInput<double>(getField(params,0,"delta")); int its_crp = getInput<double>(getField(params,0,"its_crp")); int its_ms = getInput<double>(getField(params,0,"its_ms")); int its = max(its_crp, its_ms); int Mproc = getInput<double>(getField(params,0,"Mproc")); int K = getInput<double>(getField(params,0,"K")); niw_sampled hyper(D, kappah, nuh, thetah, deltah); hyper.update_posteriors_sample(); // processor temporary variables //Mproc = min(Mproc,omp_get_max_threads()); omp_set_num_threads(Mproc); clusters_FSD model(N, D, K, x, z, alphah, hyper, Mproc); model.initialize(); plhs[0] = mxCreateNumericMatrix(its_crp,1,mxDOUBLE_CLASS,mxREAL); plhs[1] = mxCreateNumericMatrix(its_crp,1,mxDOUBLE_CLASS,mxREAL); arr(double) timeArr = getArrayInput<double>(plhs[0]); arr(double) EArr = getArrayInput<double>(plhs[1]); stopwatch timer; for (int it=0; it<its_crp; it++) { timer.tic(); model.sample_params(); model.sample_labels(); double time = timer.toc(); timeArr[it] = time; EArr[it] = model.joint_loglikelihood(); } }