Example #1
0
void mcmc_reuse_data(mcmc * m, const mcmc * m_orig) {
	IFDEBUGPARSER
	debug("reusing data from other struct");
	assert(m_orig->data != NULL);
	m->data = m_orig->data;
	mcmc_check(m);
}
Example #2
0
void mcmc_load_data(mcmc * m, const char * datafilename) {
	IFDEBUGPARSER
	dump_s("looking for data in file", datafilename);
	load_data(m, datafilename);
	mcmc_check(m);
	IFDEBUGPARSER
	debug("loading data successful")
}
/**
 * needs:
 * params file
 * BURN_IN_ITERATIONS
 * start values (in params file)
 **
 * does:
 * calibrate first chain (beta = 1)
 * writes beta, stepwidths and start values as first line in file
 *   calibration_result
 **
 * provides:
 * stepwidths of first chain (calibration_result)
 * new params file (params_suggest)
 * new start values (calibration_result)
 **/
void calibrate_first() {
	mcmc ** chains = setup_chains();
	const double desired_acceptance_rate = TARGET_ACCEPTANCE_RATE;
	const double max_ar_deviation = MAX_AR_DEVIATION;
	const unsigned long burn_in_iterations = BURN_IN_ITERATIONS;
	const unsigned long iter_limit = ITER_LIMIT;
	const double mul = MUL;

	printf("Starting markov chain calibration\n");
	fflush(stdout);
	calc_model(chains[0], NULL);
	mcmc_check(chains[0]);
	markov_chain_calibrate(chains[0], burn_in_iterations,
			desired_acceptance_rate, max_ar_deviation, iter_limit, mul,
			DEFAULT_ADJUST_STEP);
	write_calibrations_file(chains, 1);
	write_params_file(chains[0]);
}
/**
 * needs:
 * params file
 * BURN_IN_ITERATIONS
 * first line in calibration_result
 * BETA_ALIGNMENT
 * BETA_0
 * SKIP_CALIBRATE_ALLCHAINS
 **
 * does:
 * calibrate remaining chains (beta < 1)
 * writes all betas, stepwidths and start values in file calibration_result
 **
 * provides:
 * stepwidths of first chain (calibration_result)
 * new params file (params_suggest)
 * new start values (calibration_result)
 **/
void calibrate_rest() {
	int n_beta = N_BETA;
	const double desired_acceptance_rate = TARGET_ACCEPTANCE_RATE;
	const double max_ar_deviation = MAX_AR_DEVIATION;
	double beta_0 = BETA_0;
	const unsigned long burn_in_iterations = BURN_IN_ITERATIONS;
	const unsigned long iter_limit = ITER_LIMIT;
	const double mul = MUL;
	unsigned int n_par;
	int i;
	gsl_vector * stepwidth_factors;
	mcmc ** chains = setup_chains();

	read_calibration_file(chains, 1);

	printf("Calibrating chains\n");
	fflush(stdout);
	n_par = get_n_par(chains[0]);
	stepwidth_factors = gsl_vector_alloc(n_par);
	gsl_vector_set_all(stepwidth_factors, 1);

	i = 1;
	if (n_beta > 1) {
		if (beta_0 < 0)
			set_beta(chains[i], get_chain_beta(i, n_beta, calc_beta_0(
					chains[0], stepwidth_factors)));
		else
			set_beta(chains[i], get_chain_beta(i, n_beta, beta_0));
		gsl_vector_free(get_steps(chains[i]));
		chains[i]->params_step = dup_vector(get_steps(chains[0]));
		gsl_vector_scale(get_steps(chains[i]), pow(get_beta(chains[i]), -0.5));
		set_params(chains[i], dup_vector(get_params_best(chains[0])));
		calc_model(chains[i], NULL);
		mcmc_check(chains[i]);
		printf("Calibrating second chain to infer stepwidth factor\n");
		printf("\tChain %2d - ", i);
		printf("beta = %f\tsteps: ", get_beta(chains[i]));
		dump_vectorln(get_steps(chains[i]));
		fflush(stdout);
		markov_chain_calibrate(chains[i], burn_in_iterations,
				desired_acceptance_rate, max_ar_deviation, iter_limit, mul,
				DEFAULT_ADJUST_STEP);
		gsl_vector_scale(stepwidth_factors, pow(get_beta(chains[i]), -0.5));
		gsl_vector_mul(stepwidth_factors, get_steps(chains[0]));
		gsl_vector_div(stepwidth_factors, get_steps(chains[i]));
		mem_free(chains[i]->additional_data);
	}

	printf("stepwidth factors: ");
	dump_vectorln(stepwidth_factors);

	if (beta_0 < 0) {
		beta_0 = calc_beta_0(chains[0], stepwidth_factors);
		printf("automatic beta_0: %f\n", beta_0);
	}

	fflush(stdout);

#pragma omp parallel for
	for (i = 1; i < n_beta; i++) {
		printf("\tChain %2d - ", i);
		fflush(stdout);
		chains[i]->additional_data
				= mem_malloc(sizeof(parallel_tempering_mcmc));
		set_beta(chains[i], get_chain_beta(i, n_beta, beta_0));
		gsl_vector_free(get_steps(chains[i]));
		chains[i]->params_step = dup_vector(get_steps(chains[0]));
		gsl_vector_scale(get_steps(chains[i]), pow(get_beta(chains[i]), -0.5));
		gsl_vector_mul(get_steps(chains[i]), stepwidth_factors);
		set_params(chains[i], dup_vector(get_params_best(chains[0])));
		calc_model(chains[i], NULL);
		mcmc_check(chains[i]);
		printf("beta = %f\tsteps: ", get_beta(chains[i]));
		dump_vectorln(get_steps(chains[i]));
		fflush(stdout);
#ifndef SKIP_CALIBRATE_ALLCHAINS
		markov_chain_calibrate(chains[i], burn_in_iterations,
				desired_acceptance_rate, max_ar_deviation, iter_limit, mul,
				DEFAULT_ADJUST_STEP);
#else
		burn_in(chains[i], burn_in_iterations);
#endif
	}
	gsl_vector_free(stepwidth_factors);
	fflush(stdout);
	printf("all chains calibrated.\n");
	for (i = 0; i < n_beta; i++) {
		printf("\tChain %2d - beta = %f \tsteps: ", i, get_beta(chains[i]));
		dump_vectorln(get_steps(chains[i]));
	}
	write_calibration_summary(chains, n_beta);
	write_calibrations_file(chains, n_beta);
}