예제 #1
0
/**
 * emulate the model at the ith entry in results->new_x
 */
void emulate_ith_location(modelstruct *the_model, optstruct *options, resultstruct *results,int i, gsl_matrix* h_matrix, gsl_matrix* cinverse, gsl_vector *beta_vector){
	double kappa;
	double temp_mean, temp_var;
	gsl_vector_view new_x_row;
	gsl_vector *kplus = gsl_vector_alloc(options->nmodel_points);
	gsl_vector *h_vector = gsl_vector_alloc(options->nregression_fns);

	// read the new x location 
	new_x_row = gsl_matrix_row(results->new_x, i);
	//fprintf(stderr, "i(%d) new_x_row: ", i);
	//print_vector_quiet(&new_x_row.vector, options->nparams);

	
	makeKVector(kplus, the_model->xmodel, &new_x_row.vector, the_model->thetas, options->nmodel_points, options->nthetas, options->nparams);

	makeHVector(h_vector, &new_x_row.vector, options->nparams);
	
	temp_mean = makeEmulatedMean(cinverse, the_model->training_vector, kplus, h_vector, h_matrix, beta_vector, options->nmodel_points);


	kappa = covariance_fn(&new_x_row.vector, &new_x_row.vector, the_model->thetas, options->nthetas, options->nparams);

	temp_var = makeEmulatedVariance(cinverse, kplus, h_vector, h_matrix, kappa, options->nmodel_points, options->nregression_fns);

	//fprintf(stderr, "temp_mean %lf\ttemp_var %lf\n", temp_mean, temp_var);

	gsl_vector_set(results->emulated_mean, i, temp_mean);
	gsl_vector_set(results->emulated_var, i, temp_var);

	gsl_vector_free(kplus);
	gsl_vector_free(h_vector);
}
예제 #2
0
void emulateQuick( modelstruct *the_model, gsl_vector* the_point, optstruct* options, 
									 double* mean_out, double* var_out, gsl_matrix *h_matrix, gsl_matrix *cinverse, gsl_vector* beta_vector ){

	double kappa;
	double temp_mean, temp_var;
	gsl_vector_view new_x_row;
	gsl_vector *kplus = gsl_vector_alloc(options->nmodel_points);
	gsl_vector *h_vector = gsl_vector_alloc(options->nregression_fns);

	
	makeKVector(kplus, the_model->xmodel, the_point, the_model->thetas, options->nmodel_points, options->nthetas, options->nparams);

	makeHVector(h_vector, the_point, options->nparams);
	
	temp_mean = makeEmulatedMean(cinverse, the_model->training_vector, kplus, h_vector, h_matrix, beta_vector, options->nmodel_points);

	kappa = covariance_fn(the_point, the_point, the_model->thetas, options->nthetas, options->nparams);

	temp_var = makeEmulatedVariance(cinverse, kplus, h_vector, h_matrix, kappa, options->nmodel_points, options->nregression_fns);

	//fprintf(stderr, "temp_mean %lf\ttemp_var %lf\n", temp_mean, temp_var);
	// save the results
	*mean_out = temp_mean;
	*var_out = temp_var;

	gsl_vector_free(kplus);
	gsl_vector_free(h_vector);

}
예제 #3
0
/**
 * emulate the model at the location of the_point
 * @return *the_variance is set to the variance at this location
 * @return *the_mean is set to the emulated mean at this loc
 */
void emulateAtPoint(modelstruct *the_model, gsl_vector* the_point, optstruct* options, double* the_mean, double* the_variance){

	int i;
	double determinant_c = 0.0;

	gsl_matrix *c_matrix = gsl_matrix_alloc(options->nmodel_points, options->nmodel_points);
	gsl_matrix *cinverse = gsl_matrix_alloc(options->nmodel_points, options->nmodel_points);
	gsl_vector *beta_vector = gsl_vector_alloc(options->nregression_fns);
	gsl_matrix *h_matrix = gsl_matrix_alloc(options->nmodel_points, options->nregression_fns);
	gsl_matrix *temp_matrix = gsl_matrix_alloc(options->nmodel_points, options->nmodel_points);
	double kappa;
	double temp_mean, temp_var;
	gsl_vector_view new_x_row;
	gsl_vector *kplus = gsl_vector_alloc(options->nmodel_points);
	gsl_vector *h_vector = gsl_vector_alloc(options->nregression_fns);

	
	// create the covariance matrix and save a copy in temp_matrix
	makeCovMatrix(c_matrix, the_model->xmodel, the_model->thetas,options->nmodel_points, options->nthetas, options->nparams);
	gsl_matrix_memcpy(temp_matrix, c_matrix);
	
	chol_inverse_cov_matrix(options, temp_matrix, cinverse, &determinant_c);

	// regression cpts
	makeHMatrix(h_matrix, the_model->xmodel, options->nmodel_points, options->nparams, options->nregression_fns);
	estimateBeta(beta_vector, h_matrix, cinverse, the_model->training_vector, options->nmodel_points, options->nregression_fns);
	
	makeKVector(kplus, the_model->xmodel, the_point, the_model->thetas, options->nmodel_points, options->nthetas, options->nparams);
	makeHVector(h_vector, the_point, options->nparams);
	
	temp_mean = makeEmulatedMean(cinverse, the_model->training_vector, kplus, h_vector, h_matrix, beta_vector, options->nmodel_points);
	kappa = covariance_fn(the_point, the_point, the_model->thetas, options->nthetas, options->nparams);
	temp_var = makeEmulatedVariance(cinverse, kplus, h_vector, h_matrix, kappa, options->nmodel_points, options->nregression_fns);

	if(temp_mean > 100){
		printf("EF:mean:%lf\tvar:%lf\n", temp_mean, temp_var);
		print_vector_quiet(the_point,options->nparams);
		printf("kplus:\n");
		print_vector_quiet(kplus, options->nmodel_points);
		printf("hvector:\n");
		print_vector_quiet(h_vector, options->nregression_fns);
		printf("beta:\n");
		print_vector_quiet(beta_vector, options->nregression_fns);
		//exit(1);
	}
	

	*the_mean = temp_mean;
	*the_variance = temp_var;

	gsl_vector_free(kplus);
	gsl_vector_free(h_vector);
	gsl_matrix_free(c_matrix);
	gsl_matrix_free(cinverse);
	gsl_matrix_free(h_matrix);
	gsl_matrix_free(temp_matrix);
	gsl_vector_free(beta_vector);
	
}
예제 #4
0
double getLogLikelyhood(gsl_matrix *cinverse, double det_cmatrix,  gsl_matrix *xmodel, 
												gsl_vector *trainingvector, gsl_vector *thetas, gsl_matrix *h_matrix, int nmodel_points, 
												int nthetas, int nparams, int nregression_fns, 
												void(*makeHVector)(gsl_vector *, gsl_vector*, int))
{
	(void) thetas;
	(void) nthetas;
	int i;
	double the_likelyhood = 0.0;
	double vector_matrix_vector_product = 0.0;
	double log_2_pi = 1.83788;
	gsl_vector_view xmodel_row;
	gsl_vector *result_holder = gsl_vector_alloc(nmodel_points);
	gsl_vector *h_vector = gsl_vector_alloc(nregression_fns);
	gsl_vector *beta_vector = gsl_vector_alloc(nregression_fns);
	gsl_vector *estimated_mean = gsl_vector_alloc(nmodel_points);
	gsl_vector *train_sub_mean = gsl_vector_alloc(nmodel_points);
	double estimated_mean_val = 0.0;
	double log_det_c = log(det_cmatrix);

	//log_det_c = fabs(log_det_c);

	/* we need to calculate the mean vector for this set of thetas 
	 * estMean[i] = hvector(training[i]).betavector
	 */
	estimateBeta(beta_vector, h_matrix, cinverse,  trainingvector, nmodel_points, nregression_fns);
	for(i = 0; i < nmodel_points; i++){
		xmodel_row = gsl_matrix_row(xmodel, i);
		// can't call this...
		makeHVector(h_vector, &xmodel_row.vector, nparams);
		//print_vector_quiet(h_vector, nregression_fns);
		gsl_blas_ddot(beta_vector, h_vector, &estimated_mean_val);
		gsl_vector_set(estimated_mean, i, estimated_mean_val);
	}

	/* train_sub_mean = trainingvector- estimated_mean */
	gsl_vector_memcpy(train_sub_mean, trainingvector);
	gsl_vector_sub(train_sub_mean, estimated_mean);

	//print_vector_quiet(beta_vector, nregression_fns); <- verified
	//print_vector_quiet(trainingvector, nmodel_points); <- verified 
	//print_vector_quiet(estimated_mean, nmodel_points); 
	/* print_vector_quiet(train_sub_mean, nmodel_points); */
	// DEBUG 

	// the  log likelyhood is a given by
	// L = (-1/2)*Log[Det[cinverse]]  - (1/2)*trainingvector.cinverse.trainingvector - (nmodel_points/2)*Log[2*Pi]
	the_likelyhood = -(1.0/2.0)*log_det_c -  (nmodel_points/2.0)*log_2_pi; 
	
	gsl_blas_dgemv(CblasNoTrans, 1.0, cinverse, train_sub_mean, 0.0, result_holder);
	gsl_blas_ddot(train_sub_mean, result_holder, &vector_matrix_vector_product);
	
	/* printf("%g\n", (log_2_pi)*(nmodel_points/2.0)); */
	/* printf("parta:(-1/2)*log_det_c = %g\n", (-0.5)*log_det_c); */
	/* printf("partb:(training-mean).cinverse.(training-mean) = %g\n", (-0.5)*vector_matrix_vector_product); */
	/* printf("partc:(-nmodel_points/2.0)*log_2_pi = %g\n", -(nmodel_points/2.0)*log_2_pi); */

	the_likelyhood += vector_matrix_vector_product*(-1.0/2.0);

	gsl_vector_free(result_holder);
	gsl_vector_free(h_vector);
	gsl_vector_free(beta_vector);
	gsl_vector_free(estimated_mean);
	gsl_vector_free(train_sub_mean);
	return(the_likelyhood);
}