Ejemplo n.º 1
0
/*
 * Do the bootstrap calculation for a pair of trees.
 * Returns 0. It should return whether the tree passes the t-student test
 */
int accept_tree(char** resample, Tau* current_tree, Tau* next_tree, int depth, int number_resamples) {

    double db[number_resamples];
    char* sample[2];
    sample[1] = NULL;

    // calculates each db[i]
    for (int i = 0; i < number_resamples; i++) {
        db[i] = 0.0;
        sample[0] = resample[i];
        db[i] += delta_tau(sample, current_tree, next_tree, depth);
        sample[0] = resample[number_resamples + i];
        db[i] -= delta_tau(sample, current_tree, next_tree, depth);
    }


    double d_avg = 0.0;
    double d_squared = 0.0;
    for (int i = 0; i< number_resamples; i++) {
        d_avg += db[i];
        d_squared += db[i] * db[i];
    }
    d_avg = d_avg / number_resamples;

    double Sd = sqrt((d_squared - d_avg * number_resamples)/(number_resamples - 1));
    double t_b1 = d_avg / (Sd / sqrt(number_resamples));


    // since the t-student test is not implemented, it displays the value which should be checked
    printf("t_b1=%g tree=", t_b1);
    print_Tau(current_tree);
    printf("\n");



    // the method should check if t_1b passes a t-student check and return proper value
    // currently it returns 0, so further calculations are done
    return 0;
}
Ejemplo n.º 2
0
/*
 * Main method.
 * Read arguments, alphabet and samples.
 * Starts the BIC calculator.
 * Runs the champion trees selector.
 * Then runs the bootstrap method to return the selected Context Tree.
 */
int main(int argc, char** argv) {
    /* Parameters and defaults */
    char* filename = NULL;
    unsigned int depth = 5;
    unsigned int size_resample_small = 30;
    unsigned int size_resample_large = 90;
    unsigned int number_resample_small = 10;
    unsigned int number_resample_large = 10;
    char *renewalstr = NULL;
    unsigned int seed = 0;
    char *c_method = NULL;
    int print_champs = 0;

    /* Process options */
    int c, e = 0;
    while (1) {
	static struct option long_options[] =  {
	    {"file",  required_argument, 0, 'f'},
	    {"depth",  required_argument, 0, 'd'},
	    {"small-sample-percentage",  required_argument, 0, 'p'},
	    {"large-sample-percentage",    required_argument, 0, 'P'},
	    {"number-samples",    required_argument, 0, 'n'},
	    {"number-large-samples",    required_argument, 0, 'N'},
	    {"renewal-string",    required_argument, 0, 'r'},
	    {"seed",    required_argument, 0, 's'},
	    {"scale",    required_argument, 0, 'k'},
	    {"champ-method",    required_argument, 0, 'c'},
	    {"print-champs", no_argument, 0, 'C'},
	    {0, 0, 0, 0}
	};
	int option_index = 0;
	c = getopt_long (argc, argv, "f:d:p:P:n:N:r:s:c:k:C", long_options, &option_index);
     
	if ( c == -1)
	    break;
	switch ( c ) {
	    SOPT('f', "filename", filename);
	    IOPT('d', "depth", depth);
	    IOPT('p', "small sample percentage", size_resample_small);
	    IOPT('P', "large sample percentage", size_resample_large);
	    IOPT('n', "small resample number", number_resample_small);
	    IOPT('N', "large resample number", number_resample_large);
	    IOPT('s', "seed", seed);
	    FOPT('k', "scale", scale);
	    SOPT('r', "renewal string", renewalstr);
	    SOPT('c', "champ method", c_method);
	    NOPT('C', print_champs);
	case '?':
	    e = 1;
	}
    }
    DEB("file %s\ndepth %d\nsmall p %d\nlarge p %d\nsmall num %d\nlarge num %d\nseed %d\nren %s\n",
	   filename,depth,size_resample_small,size_resample_large,number_resample_small,number_resample_large,seed,renewalstr);
    champ_method = !c_method || c_method[0] != 'o';
    
    if(e){
	usage();
	exit(0);
    }
    if (optind < argc && !filename)
	filename = strdup(argv[optind]);

    if(!filename)
	fatal_error(MISSING_FILENAME);
    
    Tree_node prob_root = Tree_create(PROB);
    Tree_node bic_root = Tree_create(BIC);
    
    char** sample = read_lines(filename);

    setup_BIC(sample, depth, prob_root, bic_root); // pre calculations
    
    // champions set calculation
    Champion_item champion_bics = champion_set(bic_root, Max_c(prob_root), Eps(prob_root)); 

    if (print_champs){
	ITERA(Champion_item, champion_item, champion_bics, next) {
	    printf("c=%f tree=[ ", champion_item->tau->c);
	    print_Tau(champion_item->tau);
	    printf("]\n");
	}
    }