void generateData(int length, double a, double b, double std){

	genericFunctions gen;
	gen.initialiseRand();

	//generate data for the linear model calibration
	observations = new double[length];
	predictions = new double[length];
	double *residuals; residuals = new double[length];

	for (int i = 0; i < length; i++){ 
		linearModel(a, b,i+1,observations[i]);

		gen.sampleNormDist(std,residuals[i]);
		observations[i] += residuals[i];
	}

	double actualStd = 0;

	gen.calcStd(residuals,length,actualStd);

	delete [] residuals;
}
int _tmain(int argc, _TCHAR* argv[])
{	
	tObs = 1000;
	parameters = 3;
	retainedParameterSets = 15000;
	generateData(tObs, 0.6, 0.5, 2);

	//set prior parameter distributions for the model.
	double * minpar; minpar = new double[parameters];
	double *maxpar; maxpar = new double[parameters];
	minpar[0] = 0.5; maxpar[0] = 0.7;
	minpar[1] = 0.35; maxpar[1] = 0.65;
	minpar[2] = 1; maxpar[2] = 3;
	
	//initialise the calibration class.
	modelCalibration cal;
	cal.initialise(parameters, tObs, retainedParameterSets);
	
	//initialise the formal likelihood class.
	informalLikelihoods like;
	like.initialiseNSE(observations,tObs,2);

	//initialise the sampling class.
	mcSampling samp;
	samp.initialise(parameters, minpar, maxpar);

	informalBayesAnalysis iba;

	//loop through making model runs.
	for (int i = 0; i < 200000; i++){

		std::cout<<i<<std::endl;

		//sample from the specified prior distributions.
		samp.sample();

		//run the model.
		for (int i = 0; i < tObs; i++){
			linearModel(samp.par[0], samp.par[1], i+1, predictions[i]);
		}

		//calculate the result of the predictions.
		double result;
		like.runNSE(predictions,result);		
		
		//if sufficiently good, store in calibration master storage.
		cal.addSample(result, samp.par, predictions);
	}
	
	//intitialise the formal bayesian analysis class.
	iba.initialise(cal.par, cal.like, cal.tSamples, cal.tPar, samp.parMin, samp.parMax, cal.pred, cal.tObs,observations,0.95);
				
	//define the thresholds for running the informal bayesian analysis.
	int tThresh = 5;
	double *thresholds; thresholds = new double[tThresh];
	thresholds[0] = 0.1;
	thresholds[1] = 0.2;
	thresholds[2] = 0.4;
	thresholds[3] = 0.7;
	thresholds[4] = 1;

	//run model performance evaluation.
	iba.runAnalysis(tThresh,thresholds);

	//output summary tables of model parameter sensitivity to file.
	iba.outputTables("C:/Users/desktop/toolbox_demo/output tables.txt");

	//output parameter probability (and cumulative) distribution functions to file.
	iba.outputPDFCDF("C:/Users/desktop/toolbox_demo/output pdfcdf.txt");

	//output prediction intervals and confidence intervals to file.
	iba.outputPredInt("C:/Users/desktop/toolbox_demo/output confidence intervals.txt");
	
	return 0;
}
Пример #3
0
#include "catch.hpp"

#include <iterator>

#include "dataset.h"
#include "libnested.h"
#include "models.h"
#include "params.h"

#include "csv.h"

TEST_CASE( "Linear model correct", "[Linear model]") {
  vector<double> params {0.5, 0.5};
  vector<double> data {0, 1, 2, 3, 4, 5};

  vector<double> results = linearModel(data, params);
  vector<double> expected_results {0.5, 1, 1.5, 2, 2.5, 3};

  REQUIRE( results == expected_results );
}

TEST_CASE( "Uniform prior correct", "[Uniform prior]") {
  double r = uniformPrior(1.5, 0, 2);

  REQUIRE( r == 3 );
}

TEST_CASE ( "DataSet load correct", "[DataSet]") {
  DataSet ftdata("data/ftdata.csv");

  REQUIRE ( ftdata.t[0] == 0.923884 );