// New function: compute quantile: // (quite slow because we allocate memory to keep indexes) double my_quantile(double *vals, long len, double quantile) { if((quantile<0) || (quantile>1)) { //printf("Error! must supply quantile value between 0 and 1!\n"); return -9999999999999999.999; } //printf("Before Copy: Min: vals[0]=%lf, Max: vals[%ld]=%lf\n", vals[0], len-1, vals[len-1]); double *vals_copy = new double[len]; long *indexes = new long[len]; //printf("Before Copy2: Min: vals[0]=%lf, Max: vals[%ld]=%lf\n", vals[0], len-1, vals[len-1]); memcpy(vals_copy, vals, len*8); // first copy without noise. Double takes 8 (not 4) bytes !!! DoQuicksort(vals_copy, len, indexes); // sort long i = floor((len-1)*quantile); // set index double delta = (len-1)*quantile-double(i); //printf("PRINT SORTED VALUES:\n"); //for(j=0; j<len; j++) //printf("vals_copy[%ld]=%lf\n", j, vals_copy[j]); //printf("My Quantile, len=%ld, quantile=%lf\n", len, quantile); fflush(stdout); //printf("Min: vals[0]=%lf, Max: vals[%ld]=%lf\n", vals[0], len-1, vals[len-1]); //printf("Min: vals_copy[0]=%lf, Max: vals_copy[%ld]=%lf\n", vals_copy[0], len-1, vals_copy[len-1]); //printf("Index=%ld, delta=%lf, vals_copy[%ld]=%lf, vals_copy[%ld]=%lf\n", i, delta, i, vals_copy[i], i+1, vals_copy[i+1]); fflush(stdout); double quantile_val = (1 - delta)* vals_copy[i]; if(i < len-1) // not last quantile_val += delta*vals_copy[i+1]; //printf("My Quantile output=%lf\n", quantile_val); fflush(stdout); delete [] vals_copy; delete [] indexes;//delete vals_copy, indexes; return quantile_val; }
// permute the indexes of the model, thus enabling a more fair // comparison of the trained and the original model !!! long PermuteModelIncreasingMean(hmm_model *hmm) { long i, j; double mean_vec[MAX_X_VALS]; long indexes[MAX_X_VALS]; double helperx[MAX_X_VALS][MAX_X_VALS]; double helpery[MAX_X_VALS][MAX_Y_VALS]; if(hmm->y_type == DISCRETE) // here permute according to conditional means, assuming the Y's are 0,1,.. { for(i = 0; i < hmm->x_dim; i++) { mean_vec[i] = 0; for(j = 0; j < hmm->y_dim; j++) mean_vec[i] += hmm->N[i][j] * j; } } else // here permute according to the conditional means { for(i = 0; i < hmm->x_dim; i++) { mean_vec[i] = 0; for(j = 0; j < hmm->y_dim; j++) mean_vec[i] += hmm->N[i][j] * hmm->MEW[i][j]; } } /* printf("start quicksort\n mean : "); print_double_vec(mean_vec, hmm->x_dim); */ // Now sort according to mean !! DoQuicksort(mean_vec, hmm->x_dim, indexes); /* printf("ned quicksort\n mean : "); print_double_vec(mean_vec, hmm->x_dim); printf("\nIndexes :\n"); print_vec(indexes, hmm->x_dim); */ // change the X's M's variables for(i = 0; i < hmm->x_dim; i++) for(j = 0; j < hmm->x_dim; j++) helperx[i][j] = hmm->M[indexes[i]][indexes[j]]; for(i = 0; i < hmm->x_dim; i++) for(j = 0; j < hmm->x_dim; j++) hmm->M[i][j] = helperx[i][j]; // Change the Y's N's variables for(i = 0; i < hmm->x_dim; i++) for(j = 0; j < hmm->y_dim; j++) helpery[i][j] = hmm->N[indexes[i]][j]; for(i = 0; i < hmm->x_dim; i++) for(j = 0; j < hmm->y_dim; j++) hmm->N[i][j] = helpery[i][j]; if(hmm->y_type == CONTINUOUS) // Change MEW and SIGMA { for(i = 0; i < hmm->x_dim; i++) for(j = 0; j < hmm->y_dim; j++) helpery[i][j] = hmm->MEW[indexes[i]][j]; for(i = 0; i < hmm->x_dim; i++) for(j = 0; j < hmm->y_dim; j++) hmm->MEW[i][j] = helpery[i][j]; for(i = 0; i < hmm->x_dim; i++) for(j = 0; j < hmm->y_dim; j++) helpery[i][j] = hmm->SIGMA[indexes[i]][j]; for(i = 0; i < hmm->x_dim; i++) for(j = 0; j < hmm->y_dim; j++) hmm->SIGMA[i][j] = helpery[i][j]; } // Change everything else ComputeModelAuxillaryParameters(hmm); return 0; }