void FreeFragment(struct s_polymer *fragment,struct s_mc_parms *parms) { int i; for(i=0; i<parms->npol; i++) { FreeDoubleMatrix((fragment+i)->A,parms->nmul_local); FreeDoubleMatrix((fragment+i)->G,parms->nmul_local); FreeDoubleMatrix((fragment+i)->L,parms->nmul_local); FreeDoubleMatrix((fragment+i)->Y,parms->nmul_local); free((fragment+i)->d_ang ); free((fragment+i)->g_ang); free((fragment+i)->back); } free(fragment); }
ScaleHMM::~ScaleHMM() { //FILE_LOG(logDEBUG2) << __PRETTY_FUNCTION__; FreeDoubleMatrix(this->A, this->N); Free(this->scalefactoralpha); FreeDoubleMatrix(this->scalealpha, this->T); FreeDoubleMatrix(this->scalebeta, this->T); FreeDoubleMatrix(this->densities, this->N); // if (this->xvariate==MULTIVARIATE) // { // FreeDoubleMatrix(this->tdensities, this->T); // } FreeDoubleMatrix(this->gamma, this->N); FreeDoubleMatrix(this->sumxi, this->N); Free(this->proba); Free(this->sumgamma); for (int iN=0; iN<this->N; iN++) { //FILE_LOG(logDEBUG1) << "Deleting density functions"; delete this->densityFunctions[iN]; } // Free(this->num_nonzero_A_into_state); // FreeIntMatrix(this->index_nonzero_A_into_state, this->N); }
ScaleHMM::~ScaleHMM() { //FILE_LOG(logDEBUG2) << __PRETTY_FUNCTION__; FreeDoubleMatrix(this->A, this->N); Free(this->scalefactoralpha); FreeDoubleMatrix(this->scalealpha, this->T); FreeDoubleMatrix(this->scalebeta, this->T); // FreeDoubleMatrix(this->tdensities, this->T); FreeDoubleMatrix(this->gamma, this->N); FreeDoubleMatrix(this->sumxi, this->N); Free(this->proba); Free(this->sumgamma); if (this->xvariate == UNIVARIATE) { FreeDoubleMatrix(this->densities, this->N); for (int iN=0; iN<this->N; iN++) { //FILE_LOG(logDEBUG1) << "Deleting density functions"; delete this->densityFunctions[iN]; } } }
void ScaleHMM::EM(int* maxiter, int* maxtime, double* eps) { //FILE_LOG(logDEBUG2) << __PRETTY_FUNCTION__; double logPold = -INFINITY; double logPnew; double** gammaold = CallocDoubleMatrix(this->N, this->T); // Parallelization settings // omp_set_nested(1); // measuring the time this->EMStartTime_sec = time(NULL); // Print some initial information if (this->xvariate == UNIVARIATE) { //FILE_LOG(logINFO) << ""; //FILE_LOG(logINFO) << "INITIAL PARAMETERS"; // this->print_uni_params(); this->print_uni_iteration(0); } else if (this->xvariate == MULTIVARIATE) { this->print_multi_iteration(0); } R_CheckUserInterrupt(); // Do the Baum-Welch and updates int iteration = 0; while (((this->EMTime_real < *maxtime) or (*maxtime < 0)) and ((iteration < *maxiter) or (*maxiter < 0))) { iteration++; try { this->baumWelch(); } catch(...) { throw; } logPnew = this->logP; this->dlogP = logPnew - logPold; if (this->xvariate == UNIVARIATE) { // clock_t clocktime = clock(), dtime; // difference in posterior //FILE_LOG(logDEBUG1) << "Calculating differences in posterior in EM()"; double postsum = 0.0; for (int t=0; t<this->T; t++) { for (int iN=0; iN<this->N; iN++) { postsum += fabs(this->gamma[iN][t] - gammaold[iN][t]); gammaold[iN][t] = this->gamma[iN][t]; } } this->sumdiff_posterior = postsum; // dtime = clock() - clocktime; // //FILE_LOG(logDEBUG) << "differences in posterior: " << dtime << " clicks"; } R_CheckUserInterrupt(); // Print information about current iteration if (this->xvariate == UNIVARIATE) { this->print_uni_iteration(iteration); } else if (this->xvariate == MULTIVARIATE) { this->print_multi_iteration(iteration); } // Check convergence if((fabs(this->dlogP) < *eps) && (this->dlogP < INFINITY)) //it has converged { //FILE_LOG(logINFO) << "Convergence reached!\n"; Rprintf("Convergence reached!\n"); break; } else { // not converged this->EMTime_real = difftime(time(NULL),this->EMStartTime_sec); if (iteration == *maxiter) { //FILE_LOG(logINFO) << "Maximum number of iterations reached!"; Rprintf("Maximum number of iterations reached!\n"); break; } else if ((this->EMTime_real >= *maxtime) and (*maxtime >= 0)) { //FILE_LOG(logINFO) << "Exceeded maximum time!"; Rprintf("Exceeded maximum time!\n"); break; } logPold = logPnew; } // // Check weights // double weights [this->N]; // double sumweights=0; // this->calc_weights(weights); // for (int iN=0; iN<this->N; iN++) // { // //FILE_LOG(logERROR) << "weights["<<iN<<"] = " << weights[iN]; // sumweights += weights[iN]; // } // //FILE_LOG(logERROR) << "sumweights = " << sumweights; // // Updating initial probabilities proba and transition matrix A for (int iN=0; iN<this->N; iN++) { this->proba[iN] = this->gamma[iN][0]; //FILE_LOG(logDEBUG4) << "sumgamma["<<iN<<"] = " << sumgamma[iN]; if (this->sumgamma[iN] == 0) { //FILE_LOG(logINFO) << "Not reestimating A["<<iN<<"][x] because sumgamma["<<iN<<"] = 0"; // Rprintf("Not reestimating A[%d][x] because sumgamma[%d] = 0\n", iN, iN); } else { for (int jN=0; jN<this->N; jN++) { //FILE_LOG(logDEBUG4) << "sumxi["<<iN<<"]["<<jN<<"] = " << sumxi[iN][jN]; this->A[iN][jN] = this->sumxi[iN][jN] / this->sumgamma[iN]; if (std::isnan(this->A[iN][jN])) { //FILE_LOG(logERROR) << "updating transition probabilities"; //FILE_LOG(logERROR) << "A["<<iN<<"]["<<jN<<"] = " << A[iN][jN]; //FILE_LOG(logERROR) << "sumxi["<<iN<<"]["<<jN<<"] = " << sumxi[iN][jN]; //FILE_LOG(logERROR) << "sumgamma["<<iN<<"] = " << sumgamma[iN]; throw nan_detected; } } } } if (this->xvariate == UNIVARIATE) { // clock_t clocktime = clock(), dtime; // // // Update all distributions independently // for (int iN=0; iN<this->N; iN++) // { // this->densityFunctions[iN]->update(this->gamma[iN]); // } // Update distribution of independent states first, set others as multiples of 'monosomy' // This loop assumes that the dependent negative binomial states come last and are consecutive int xsomy = 1; for (int iN=0; iN<this->N; iN++) { if (this->densityFunctions[iN]->get_name() == ZERO_INFLATION) {} if (this->densityFunctions[iN]->get_name() == GEOMETRIC) { this->densityFunctions[iN]->update(this->gamma[iN]); } if (this->densityFunctions[iN]->get_name() == NEGATIVE_BINOMIAL) { if (xsomy==1) { //FILE_LOG(logDEBUG1) << "mean(state="<<iN<<") = " << this->densityFunctions[iN]->get_mean() << ", var(state="<<iN<<") = " << this->densityFunctions[iN]->get_variance(); this->densityFunctions[iN]->update_constrained(this->gamma, iN, this->N); double mean1 = this->densityFunctions[iN]->get_mean(); double variance1 = this->densityFunctions[iN]->get_variance(); //FILE_LOG(logDEBUG1) << "mean(state="<<iN<<") = " << this->densityFunctions[iN]->get_mean() << ", var(state="<<iN<<") = " << this->densityFunctions[iN]->get_variance(); // Set others as multiples for (int jN=iN+1; jN<this->N; jN++) { this->densityFunctions[jN]->set_mean(mean1 * (jN-iN+1)); this->densityFunctions[jN]->set_variance(variance1 * (jN-iN+1)); //FILE_LOG(logDEBUG1) << "mean(state="<<jN<<") = " << this->densityFunctions[jN]->get_mean() << ", var(state="<<jN<<") = " << this->densityFunctions[jN]->get_variance(); } break; } xsomy++; } } // dtime = clock() - clocktime; // //FILE_LOG(logDEBUG) << "updating distributions: " << dtime << " clicks"; R_CheckUserInterrupt(); } } /* main loop end */ //Print the last results //FILE_LOG(logINFO) << ""; //FILE_LOG(logINFO) << "FINAL ESTIMATION RESULTS"; // this->print_uni_params(); // free memory FreeDoubleMatrix(gammaold, this->N); // Return values *maxiter = iteration; *eps = this->dlogP; this->EMTime_real = difftime(time(NULL),this->EMStartTime_sec); *maxtime = this->EMTime_real; }