예제 #1
0
static void load_data(mcmc * m, const char * filename) {
	FILE * input;
	int npoints = countlines(filename);
	int ndim = get_column_count(filename);
	gsl_matrix * data;
	IFDEBUGPARSER
	dump_i("lines", npoints);
	IFDEBUGPARSER
	dump_i("dimensions", ndim);

	data = gsl_matrix_alloc(npoints, ndim);

	input = openfile(filename);
	if (gsl_matrix_fscanf(input, data) != 0) {
		fprintf(stderr,
				"error reading input data. Perhaps inconsistent format?\n");
		fprintf(stderr, "tried to read %d x %d.\n", ndim, npoints);
		exit(3);
	}
	assert(fclose(input) == 0);

	m->data = data;

	IFDEBUGPARSER
	dump_i("loaded data points", npoints);
}
예제 #2
0
unsigned int fill_vector(gsl_vector * values, char * filename, double min,
                         double max) {
    FILE * input;
    int col;
    double v;
    int line = 0;
    unsigned int i = 0;

    input = openfile(filename);

    while (!feof(input)) {
        col = fscanf(input, "%lf", &v);
        if (col != 1) {
            if (feof(input))
                break;
            fprintf(stderr, "field could not be read: %d, line %d in %s\n", i
                    + 1, line + 1, filename);
            exit(1);
        }
        if (v >= min && v <= max) {
            gsl_vector_set(values, i, v);
            i++;
        }
        line++;
    }
    dump_i("read lines", line);
    dump_i("accepted lines", i);
    fclose(input);
    return i;
}
예제 #3
0
mcmc * mcmc_load_params(const char * filename) {
	mcmc * m;
	FILE * input;
	int currentline = 0;
	const int pretext = 0;
	int r;

	int nlines;
	nlines = countlines(filename);
	IFDEBUGPARSER
	dump_i("number of lines", nlines);

	m = mcmc_init(nlines - pretext);
	IFDEBUGPARSER
	debug("opening params file");
	input = openfile(filename);
	while (currentline < nlines) {
		IFDEBUGPARSER
		dump_i("reading parameter", currentline);
		if (load_parameter(m, input, currentline - pretext) != 0) {
			fprintf(stderr, "Line %d of %s is of incorrect format.\n",
					currentline + 1, filename);
			exit(1);
		}
		currentline++;
	}
	IFDEBUGPARSER
	debug("closing params file");
	r = fclose(input);
	assert (r == 0);
	IFDEBUGPARSER
	debug("finished reading params file");
	return m;
}
예제 #4
0
ndim_histogram * ndim_histogram_alloc(unsigned int nbins, gsl_vector * min,
		gsl_vector * max) {
	ndim_histogram * h;

	h = (ndim_histogram*) malloc(sizeof(ndim_histogram));
	assert(h!=NULL);
	h->min = min;
	h->max = max;
	h->nbins = nbins;
	assert(min->size == max->size);
	h->dimensions = min->size;
	if (pow(h->nbins, h->dimensions) > INT_MAX) {
		printf("I can not make %e cubes. choose less bins.\n", pow(h->nbins,
				h->dimensions));
		printf("Maximum: %d^%e, %d\n", INT_MAX, (1 / (double) h->dimensions),
				(int) pow((double) INT_MAX, 1 / (double) h->dimensions));
		exit(1);
	}
	h->size = pow(h->nbins, h->dimensions);

	dump_i("used size", h->size);

	h->values = (long*) calloc(h->size, sizeof(long));
	if (h->values == NULL) {
		perror("could not allocate that much");
		exit(1);
	} else {
		debug("allocating succeeded.");
	}

	return h;
}
예제 #5
0
void run(unsigned int nbins, char * filename) {
	gsl_vector * min;
	gsl_vector * max;
	ndim_histogram * h;
	unsigned int ncolumns;

	debug("looking for number of columns ...");
	ncolumns = get_column_count(filename);
	dump_i("number of columns", ncolumns);
	min = gsl_vector_alloc(ncolumns);
	max = gsl_vector_alloc(ncolumns);

	debug("finding minima/maxima ...");
	dump_s("in file", filename);
	find_min_max(filename, min, max);
	dump_v("minima", min);
	dump_v("maxima", max);
	debug("creating histogram cube ...");
	h = ndim_histogram_alloc(nbins, min, max);

	debug("filling histogram ... ");
	append_to_hist(h, filename);

	output_hist(h);
	ndim_histogram_free(h);
}
예제 #6
0
void append_to_hist(ndim_histogram * h, const char * filename) {
	FILE * input;
	unsigned int i;
	int col;
	double v;
	int line = 0;
	gsl_vector * current = gsl_vector_alloc(h->dimensions);

	input = openfile(filename);
	while (!feof(input)) {
		for (i = 0; i < h->dimensions; i++) {
			col = fscanf(input, "%lf", &v);
			if (col != 1) {
				if (feof(input))
					break;
				fprintf(stderr, "field could not be read: %d, line %d in %s\n",
						i + 1, line + 1, filename);
				exit(1);
			}
			gsl_vector_set(current, i, v);
		}
		ndim_histogram_increment(h, current);
		line++;
	}
	dump_i("read lines", line);
	fclose(input);
}
예제 #7
0
void append_to_hists(gsl_histogram ** hists, unsigned int n,
		const char * filename) {
	FILE * input;
	unsigned int i;
	int col;
	double v;
	int line = 0;

	input = openfile(filename);

	while (!feof(input)) {
		for (i = 0; i < n; i++) {
			col = fscanf(input, "%lf", &v);
			if (col != 1) {
				if (feof(input))
					break;
				fprintf(stderr, "field could not be read: %d, line %d in %s\n",
						i + 1, line + 1, filename);
				exit(1);
			}
			gsl_histogram_increment(hists[i], v);
		}
		line++;
	}
	dump_i("read lines", line);
	fclose(input);
}
예제 #8
0
void adapt(mcmc ** chains, const unsigned int n_beta, const unsigned int n_swap) {
	unsigned int i;

#ifdef RWM
	static double prob_old[100];
#endif

#ifdef RWM
	for (i = 0; i < n_beta; i++) {
		prob_old[i] = get_prob(chains[i]);
		markov_chain_step(chains[i], 0);
		rmw_adapt_stepwidth(chains[i], prob_old[i]);
	}
#endif
#ifdef ADAPT
	for (i = 0; i < n_beta; i++) {
		if (get_params_accepts_sum(chains[i]) + get_params_rejects_sum(
						chains[i]) < 20000) {
			continue;
		}
		if (get_params_accepts_sum(chains[i]) * 1.0
				/ get_params_rejects_sum(chains[i]) < TARGET_ACCEPTANCE_RATE - 0.05) {
			dump_i("too few accepts, scaling down", i);
			gsl_vector_scale(get_steps(chains[i]), 0.99);
		} else if (get_params_accepts_sum(chains[i]) * 1.0
				/ get_params_rejects_sum(chains[i]) > TARGET_ACCEPTANCE_RATE + 0.05) {
			dump_i("too many accepts, scaling up", i);
			gsl_vector_scale(get_steps(chains[i]), 1 / 0.99);
		}
		if (get_params_accepts_sum(chains[i]) + get_params_rejects_sum(
						chains[i]) > 100000) {
			reset_accept_rejects(chains[i]);
		}
	}
#endif
	/* avoid unused warnings if adapt is disabled */
	(void) chains;
	i = n_beta + n_swap;
}
예제 #9
0
/**
 * returns 0 on success.
 */
static int load_parameter(mcmc * m, FILE * input, int i) {
	int col = 0;
	double start;
	double min;
	double max;
	double step;
	char * descr = (char*) mem_calloc(MAX_LINE_LENGTH, sizeof(char));
	IFDEBUGPARSER
	dump_i("parsing line", i);

	col = fscanf(input, "%lf\t%lf\t%lf\t%s\t%lf\n", &start, &min, &max, descr,
			&step);
	if (col != 5) {
		fprintf(stderr, "only %d fields matched.\n", col);
		return 1;
	}
	if (!(descr != NULL && mystrnlen(descr, MAX_LINE_LENGTH) > 0 && mystrnlen(
			descr, MAX_LINE_LENGTH) < MAX_LINE_LENGTH)) {
		fprintf(stderr, "description invalid: %s\n", descr);
		return 1;
	}
	IFDEBUGPARSER
	debug("setting values");
	gsl_vector_set(m->params, i, start);
	gsl_vector_set(m->params_best, i, start);
	if (min > max) {
		fprintf(stderr, "min(%f) < max(%f)\n", min, max);
		return 1;
	}
	if (start > max) {
		fprintf(stderr, "start(%f) > max(%f)\n", start, max);
		return 1;
	}
	if (start < min) {
		fprintf(stderr, "start(%f) < min(%f)\n", start, min);
		return 1;
	}
	if (step < 0) {
		step = (max - min) * 0.1;
		dump_d("using auto step size, 10% of parameter space", step);
	}
	gsl_vector_set(m->params_min, i, min);
	gsl_vector_set(m->params_max, i, max);
	m->params_descr[i] = descr;
	gsl_vector_set(m->params_step, i, step /* * (max - min) */);
	IFDEBUGPARSER
	debug("setting values done.");
	return 0;
}
예제 #10
0
void find_min_max(char * filename, gsl_vector * min, gsl_vector * max) {
	int col;
	int i;
	int n = min->size;
	int line = 0;
	double v;
	FILE * input;
	assert(min->size == max->size);
	input = openfile(filename);
	for (i = 0; i < n; i++) {
		col = fscanf(input, "%lf", &v);
		if (col != 1) {
			fprintf(stderr, "field could not be read: %d, line %d in %s\n", i
					+ 1, line + 1, filename);
			exit(1);
		}
		gsl_vector_set(min, i, v);
		gsl_vector_set(max, i, v);
	}
	while (!feof(input)) {
		for (i = 0; i < n; i++) {
			col = fscanf(input, "%lf", &v);
			if (col != 1) {
				if (feof(input))
					break;
				fprintf(stderr, "field could not be read: %d, line %d in %s\n",
						i + 1, line + 1, filename);
				exit(1);
			}
			if (gsl_vector_get(min, i) > v)
				gsl_vector_set(min, i, v);
			if (gsl_vector_get(max, i) < v)
				gsl_vector_set(max, i, v);
		}
		line++;
	}
	dump_i("read lines", line);
	fclose(input);
}
예제 #11
0
/* find a local maximum (climb the hill)
 * one probe at a time */
int find_local_maximum_naive(unsigned int ndim, double exactness,
		gsl_vector * current_x) {
	unsigned int i = 0;
	unsigned int last_i = 0;
	unsigned int j = 0;
	unsigned int count = 0;
	double current_val;
	gsl_vector * current_probe = gsl_vector_alloc(ndim);
	gsl_vector * next_probe = gsl_vector_alloc(ndim);
	gsl_vector * scales = gsl_vector_alloc(ndim);
	/* did we switch direction in the last move? */
	int flaps = 0;
	double probe_value;
	gsl_vector_set_all(scales, START_SCALE);

	current_val = f(current_x);
	count++;

	dump_v("currently at", current_x)
	dump_d("current value", current_val);
	while (1) {
		for (j = 0; j < ndim; j++) {
			i = (last_i + j) % ndim;
			gsl_vector_memcpy(current_probe, current_x);
			gsl_vector_set(current_probe, i, gsl_vector_get(current_probe, i)
					+ gsl_vector_get(scales, i));
			limit(current_probe);
			if (calc_same(current_probe, current_x) == 1) {
				dump_i("we clashed a wall in", i);
				gsl_vector_set(scales, i, gsl_vector_get(scales, i) * -1);
				continue;
			}
			dump_v("will probe at", current_probe);
			probe_value = f(current_probe);
			count++;
			if (probe_value > current_val) {
				dump_i("we jump forward in", i);
				current_val = probe_value;
				gsl_vector_memcpy(current_x, current_probe);
				dump_v("currently at", current_x)
				dump_d("current value", current_val);
				break;
			} else {
				dump_i("we turn back in", i);
				gsl_vector_set(scales, i, gsl_vector_get(scales, i) * -1);
			}
		}
#ifndef NO_ROUNDROBIN
		last_i = i;
#else
		last_i = 0;
#endif
		if (j == ndim) {
			if (flaps == 1) {
				debug("all dimensions are ready, lets refine");
				dump_v("currently at", current_x)
				dump_d("exactness (min)", gsl_vector_min(scales));
				dump_d("exactness (max)", gsl_vector_max(scales));
				dump_d("exactness (desired)", exactness);
				if (gsl_vector_max(scales) < exactness
						&& gsl_vector_min(scales) > -exactness) {
					for (i = 0; i < ndim; i++) {
						gsl_vector_memcpy(current_probe, current_x);
						gsl_vector_set(current_probe, i, gsl_vector_get(
								current_probe, i) + abs(gsl_vector_get(scales,
								i)));
						assert(f(current_probe) >= current_val);
						gsl_vector_set(current_probe, i, gsl_vector_get(
								current_probe, i) - 2* abs (gsl_vector_get(
								scales, i)));
						assert(f(current_probe) >= current_val);
					}
					gsl_vector_free(scales);
					gsl_vector_free(current_probe);
					gsl_vector_free(next_probe);
					return count;
				}
				gsl_vector_scale(scales, ZOOM_IN_FACTOR);
				flaps = 0;
				dump_d("new exactness (min)", gsl_vector_min(scales));
				dump_d("new exactness (max)", gsl_vector_max(scales));
			} else {
				flaps = 1;
			}
		} else {
			flaps = 0;
		}
	}
	return count;
}
예제 #12
0
/* find a local maximum (climb the hill)
 * diagonals */
int find_local_maximum_multi(unsigned int ndim, double exactness,
		gsl_vector * start) {
	unsigned int i;
	unsigned int count = 0;
	int possibly_circle_jump;
	double current_val;
	gsl_vector * current_probe = gsl_vector_alloc(ndim);
	gsl_vector * next_probe = gsl_vector_alloc(ndim);
	gsl_vector * current_x = dup_vector(start);
	gsl_vector * scales = gsl_vector_alloc(ndim);
	/* did we switch direction in the last move? */
	gsl_vector * flaps = gsl_vector_alloc(ndim);
	gsl_vector * probe_values = gsl_vector_alloc(ndim);
	gsl_vector_set_all(scales, START_SCALE);
	gsl_vector_set_all(flaps, 0);
	assert(exactness < 1);

	while (1) {
		dump_v("currently at", current_x)
		current_val = f(current_x);
		count++;
		dump_d("current value", current_val);

		gsl_vector_memcpy(next_probe, current_x);
		gsl_vector_add(next_probe, scales);
		limit(next_probe);
		dump_v("will probe at", next_probe);

		for (i = 0; i < ndim; i++) {
			gsl_vector_memcpy(current_probe, current_x);
			gsl_vector_set(current_probe, i, gsl_vector_get(next_probe, i));

			gsl_vector_set(probe_values, i, f(current_probe) - current_val);
			if (gsl_vector_get(probe_values, i) < 0)
				gsl_vector_set(probe_values, i, 0);
			count++;
		}
		if(gsl_vector_max(probe_values) != 0)
			gsl_vector_scale(probe_values, 1 / gsl_vector_max(probe_values));
		dump_v("probe results", probe_values);
		gsl_vector_memcpy(start, current_x);

		possibly_circle_jump = detect_circle_jump(flaps, probe_values);

		for (i = 0; i < ndim; i++) {
			if (gsl_vector_get(probe_values, i) > 0) {
				dump_i("we jump forward in", i);
				gsl_vector_set(
						current_x,
						i,
						gsl_vector_get(current_x, i)
								+ gsl_vector_get(scales, i) * JUMP_SCALE
										*
#ifdef ADAPTIVE
										gsl_vector_get(probe_values, i) *
#endif
										(1
												+ (gsl_rng_uniform(
														get_rng_instance())
														- 0.5) * 2
														* (RANDOM_SCALE
																+ possibly_circle_jump
																		* RANDOM_SCALE_CIRCLE_JUMP)));
				limit(current_x);
				if (gsl_vector_get(current_x, i) == gsl_vector_get(start, i)) {
					/* we clashed against a wall. That means we are ready to
					 * refine */
					gsl_vector_set(flaps, i, 2);
				} else {
					gsl_vector_set(flaps, i, 0);
				}
			} else {
				if (gsl_vector_get(flaps, i) == 0) {
					dump_i("we turn back in", i);
					gsl_vector_set(flaps, i, 1);
					/* TODO: should we step back a little?
					 * no we can't, otherwise our double-turnback is tainted */
					gsl_vector_set(scales, i, gsl_vector_get(scales, i) * -1);
				} else {
					dump_i("we turned back twice in", i);
					gsl_vector_set(flaps, i, 2);
				}
			}
		}
		if (gsl_vector_min(flaps) == 2) {
			debug("all dimensions are ready, lets refine");
			dump_d("exactness (min)", gsl_vector_min(scales));
			dump_d("exactness (max)", gsl_vector_max(scales));
			dump_d("exactness (desired)", exactness);
			if (gsl_vector_max(scales) < exactness && gsl_vector_min(scales)
					> -exactness) {
				for (i = 0; i < ndim; i++) {
					gsl_vector_memcpy(current_probe, start);
					gsl_vector_set(current_probe, i, gsl_vector_get(
							current_probe, i) + abs(gsl_vector_get(scales, i)));
					assert(f(current_probe) >= current_val);
					gsl_vector_set(current_probe, i, gsl_vector_get(
							current_probe, i) - 2* abs (gsl_vector_get(scales,
							i)));
					assert(f(current_probe) >= current_val);
				}
				gsl_vector_free(scales);
				gsl_vector_free(flaps);
				gsl_vector_free(probe_values);
				gsl_vector_free(current_probe);
				gsl_vector_free(next_probe);
				gsl_vector_free(current_x);
				return count;
			}
			gsl_vector_scale(scales, ZOOM_IN_FACTOR);
			gsl_vector_set_all(flaps, 0);
			dump_d("new exactness (min)", gsl_vector_min(scales));
			dump_d("new exactness (max)", gsl_vector_max(scales));
		}
	}
}