void compute(const QUESO::FullEnvironment& env) { // Step 1 of 5: Instantiate the parameter space QUESO::VectorSpace<QUESO::GslVector,QUESO::GslMatrix> paramSpace(env, "param_", 2, NULL); // Step 2 of 5: Instantiate the parameter domain QUESO::GslVector paramMins(paramSpace.zeroVector()); paramMins.cwSet(-INFINITY); QUESO::GslVector paramMaxs(paramSpace.zeroVector()); paramMaxs.cwSet( INFINITY); QUESO::BoxSubset<QUESO::GslVector,QUESO::GslMatrix> paramDomain("param_",paramSpace,paramMins,paramMaxs); // Step 3 of 5: Instantiate the likelihood function object QUESO::GslVector meanVector(paramSpace.zeroVector()); meanVector[0] = -1; meanVector[1] = 2; QUESO::GslMatrix covMatrix(paramSpace.zeroVector()); covMatrix(0,0) = 4.; covMatrix(0,1) = 0.; covMatrix(1,0) = 0.; covMatrix(1,1) = 1.; likelihoodRoutine_DataType likelihoodRoutine_Data; likelihoodRoutine_Data.meanVector = &meanVector; likelihoodRoutine_Data.covMatrix = &covMatrix; QUESO::GenericScalarFunction<QUESO::GslVector,QUESO::GslMatrix> likelihoodFunctionObj("like_", paramDomain, likelihoodRoutine, (void *) &likelihoodRoutine_Data, true); // routine computes [ln(function)] // Step 4 of 5: Instantiate the inverse problem QUESO::UniformVectorRV<QUESO::GslVector,QUESO::GslMatrix> priorRv("prior_", paramDomain); QUESO::GenericVectorRV<QUESO::GslVector,QUESO::GslMatrix> postRv("post_", paramSpace); QUESO::StatisticalInverseProblem<QUESO::GslVector,QUESO::GslMatrix> ip("", NULL, priorRv, likelihoodFunctionObj, postRv); // Step 5 of 5: Solve the inverse problem QUESO::GslVector paramInitials(paramSpace.zeroVector()); paramInitials[0] = 0.1; paramInitials[1] = -1.4; QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0,0) = 8.; proposalCovMatrix(0,1) = 4.; proposalCovMatrix(1,0) = 4.; proposalCovMatrix(1,1) = 16.; ip.solveWithBayesMetropolisHastings(NULL,paramInitials, &proposalCovMatrix); return; }
int main(int argc, char ** argv) { MPI_Init(&argc, &argv); // Step 0 of 5: Set up environment QUESO::FullEnvironment env(MPI_COMM_WORLD, argv[1], "", NULL); // Step 1 of 5: Instantiate the parameter space QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> paramSpace(env, "param_", 1, NULL); // Step 2 of 5: Set up the prior QUESO::GslVector paramMins(paramSpace.zeroVector()); paramMins.cwSet(min_val); QUESO::GslVector paramMaxs(paramSpace.zeroVector()); paramMaxs.cwSet(max_val); QUESO::BoxSubset<QUESO::GslVector, QUESO::GslMatrix> paramDomain("param_", paramSpace, paramMins, paramMaxs); // Uniform prior here. Could be a different prior. QUESO::UniformVectorRV<QUESO::GslVector, QUESO::GslMatrix> priorRv("prior_", paramDomain); // Step 3 of 5: Set up the likelihood using the class above Likelihood<QUESO::GslVector, QUESO::GslMatrix> lhood("llhd_", paramDomain); // Step 4 of 5: Instantiate the inverse problem QUESO::GenericVectorRV<QUESO::GslVector, QUESO::GslMatrix> postRv("post_", paramSpace); QUESO::StatisticalInverseProblem<QUESO::GslVector, QUESO::GslMatrix> ip("", NULL, priorRv, lhood, postRv); // Step 5 of 5: Solve the inverse problem QUESO::GslVector paramInitials(paramSpace.zeroVector()); // Initial condition of the chain paramInitials[0] = 0.0; paramInitials[1] = 0.0; QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); for (unsigned int i = 0; i < 2; i++) { // Might need to tweak this proposalCovMatrix(i, i) = 0.1; } ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); MPI_Finalize(); return 0; }
int main(int argc, char ** argv) { MPI_Init(&argc, &argv); QUESO::FullEnvironment env(MPI_COMM_WORLD, argv[1], "", NULL); QUESO::VectorSpace<> paramSpace(env, "param_", 1, NULL); QUESO::GslVector paramMins(paramSpace.zeroVector()); QUESO::GslVector paramMaxs(paramSpace.zeroVector()); double min_val = 0.0; double max_val = 1.0; paramMins.cwSet(min_val); paramMaxs.cwSet(max_val); QUESO::BoxSubset<> paramDomain("param_", paramSpace, paramMins, paramMaxs); QUESO::UniformVectorRV<> priorRv("prior_", paramDomain); Likelihood<> lhood("llhd_", paramDomain); QUESO::GenericVectorRV<> postRv("post_", paramSpace); QUESO::StatisticalInverseProblem<> ip("", NULL, priorRv, lhood, postRv); QUESO::GslVector paramInitials(paramSpace.zeroVector()); paramInitials[0] = 0.0; QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0, 0) = 0.1; ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); MPI_Finalize(); return 0; }
int main(int argc, char ** argv) { MPI_Init(&argc, &argv); QUESO::FullEnvironment env(MPI_COMM_WORLD, argv[1], "", NULL); QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> paramSpace(env, "param_", 2, NULL); double min_val = 0.0; double max_val = 1.0; QUESO::GslVector paramMins(paramSpace.zeroVector()); QUESO::GslVector paramMaxs(paramSpace.zeroVector()); // Model parameter between 0 and 1 paramMins[0] = 0.0; paramMaxs[0] = 1.0; // Hyperparameter (multiplicative coefficient of observational error // covariance matrix) between 0.0 and \infty paramMins[1] = 0.0; paramMaxs[1] = INFINITY; QUESO::BoxSubset<QUESO::GslVector, QUESO::GslMatrix> paramDomain("param_", paramSpace, paramMins, paramMaxs); QUESO::UniformVectorRV<QUESO::GslVector, QUESO::GslMatrix> priorRv("prior_", paramDomain); // Set up observation space QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> obsSpace(env, "obs_", 2, NULL); // Fill up observation vector QUESO::GslVector observations(obsSpace.zeroVector()); observations[0] = 1.0; observations[1] = 1.0; // Fill up covariance 'matrix' QUESO::GslMatrix covariance(obsSpace.zeroVector()); covariance(0, 0) = 1.0; covariance(0, 1) = 0.0; covariance(1, 0) = 0.0; covariance(1, 1) = 1.0; // Pass in observations to Gaussian likelihood object Likelihood<QUESO::GslVector, QUESO::GslMatrix> lhood("llhd_", paramDomain, observations, covariance); QUESO::GenericVectorRV<QUESO::GslVector, QUESO::GslMatrix> postRv("post_", paramSpace); QUESO::StatisticalInverseProblem<QUESO::GslVector, QUESO::GslMatrix> ip("", NULL, priorRv, lhood, postRv); QUESO::GslVector paramInitials(paramSpace.zeroVector()); paramInitials[0] = 0.0; QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); for (unsigned int i = 0; i < 1; i++) { proposalCovMatrix(i, i) = 0.1; } ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); MPI_Finalize(); return 0; }
int main(int argc, char ** argv) { #ifdef QUESO_HAS_MPI MPI_Init(&argc, &argv); #endif QUESO::EnvOptionsValues envOptions; envOptions.m_numSubEnvironments = 1; envOptions.m_subDisplayFileName = "test_outputNoInputFile/display"; envOptions.m_subDisplayAllowAll = 1; envOptions.m_displayVerbosity = 2; envOptions.m_syncVerbosity = 0; envOptions.m_seed = 0; #ifdef QUESO_HAS_MPI QUESO::FullEnvironment env(MPI_COMM_WORLD, "", "", &envOptions); #else QUESO::FullEnvironment env("", "", &envOptions); #endif unsigned int dim = 2; QUESO::VectorSpace<> paramSpace(env, "param_", dim, NULL); QUESO::GslVector paramMins(paramSpace.zeroVector()); QUESO::GslVector paramMaxs(paramSpace.zeroVector()); double min_val = -10.0; double max_val = 10.0; paramMins.cwSet(min_val); paramMaxs.cwSet(max_val); QUESO::BoxSubset<> paramDomain("param_", paramSpace, paramMins, paramMaxs); QUESO::UniformVectorRV<> priorRv("prior_", paramDomain); Likelihood<> lhood("llhd_", paramDomain); QUESO::GenericVectorRV<> postRv("post_", paramSpace); QUESO::SipOptionsValues sipOptions; sipOptions.m_computeSolution = 1; sipOptions.m_dataOutputFileName = "test_outputNoInputFile/sipOutput"; sipOptions.m_dataOutputAllowedSet.clear(); sipOptions.m_dataOutputAllowedSet.insert(0); QUESO::StatisticalInverseProblem<> ip("", &sipOptions, priorRv, lhood, postRv); QUESO::GslVector paramInitials(paramSpace.zeroVector()); paramInitials[0] = 0.0; paramInitials[1] = 0.0; QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0, 0) = 1.0; proposalCovMatrix(0, 1) = 0.0; proposalCovMatrix(1, 0) = 0.0; proposalCovMatrix(1, 1) = 1.0; QUESO::MhOptionsValues mhOptions; mhOptions.m_dataOutputFileName = "test_outputNoInputFile/sipOutput"; mhOptions.m_dataOutputAllowAll = 1; mhOptions.m_rawChainGenerateExtra = 0; mhOptions.m_rawChainDisplayPeriod = 50000; mhOptions.m_rawChainMeasureRunTimes = 1; mhOptions.m_rawChainDataOutputFileName = "test_outputNoInputFile/ip_raw_chain"; mhOptions.m_rawChainDataOutputAllowAll = 1; mhOptions.m_displayCandidates = 0; mhOptions.m_tkUseLocalHessian = 0; mhOptions.m_tkUseNewtonComponent = 1; mhOptions.m_filteredChainGenerate = 0; mhOptions.m_rawChainSize = 1000; mhOptions.m_putOutOfBoundsInChain = false; mhOptions.m_drMaxNumExtraStages = 1; mhOptions.m_drScalesForExtraStages.resize(1); mhOptions.m_drScalesForExtraStages[0] = 5.0; mhOptions.m_amInitialNonAdaptInterval = 100; mhOptions.m_amAdaptInterval = 100; mhOptions.m_amEta = (double) 2.4 * 2.4 / dim; // From Gelman 95 mhOptions.m_amEpsilon = 1.e-8; mhOptions.m_doLogitTransform = false; mhOptions.m_algorithm = "random_walk"; mhOptions.m_tk = "random_walk"; ip.solveWithBayesMetropolisHastings(&mhOptions, paramInitials, &proposalCovMatrix); #ifdef QUESO_HAS_MPI MPI_Finalize(); #endif return 0; }
void computeGravityAndTraveledDistance(const QUESO::FullEnvironment& env) { struct timeval timevalNow; gettimeofday(&timevalNow, NULL); if (env.fullRank() == 0) { std::cout << "\nBeginning run of 'Gravity + Projectile motion' example at " << ctime(&timevalNow.tv_sec) << "\n my fullRank = " << env.fullRank() << "\n my subEnvironmentId = " << env.subId() << "\n my subRank = " << env.subRank() << "\n my interRank = " << env.inter0Rank() << std::endl << std::endl; } // Just examples of possible calls if ((env.subDisplayFile() ) && (env.displayVerbosity() >= 2)) { *env.subDisplayFile() << "Beginning run of 'Gravity + Projectile motion' example at " << ctime(&timevalNow.tv_sec) << std::endl; } env.fullComm().Barrier(); env.subComm().Barrier(); // Just an example of a possible call //================================================================ // Statistical inverse problem (SIP): find posterior PDF for 'g' //================================================================ gettimeofday(&timevalNow, NULL); if (env.fullRank() == 0) { std::cout << "Beginning 'SIP -> Gravity estimation' at " << ctime(&timevalNow.tv_sec) << std::endl; } //------------------------------------------------------ // SIP Step 1 of 6: Instantiate the parameter space //------------------------------------------------------ QUESO::VectorSpace<QUESO::GslVector,QUESO::GslMatrix> paramSpace(env, "param_", 1, NULL); //------------------------------------------------------ // SIP Step 2 of 6: Instantiate the parameter domain //------------------------------------------------------ QUESO::GslVector paramMinValues(paramSpace.zeroVector()); QUESO::GslVector paramMaxValues(paramSpace.zeroVector()); paramMinValues[0] = 8.; paramMaxValues[0] = 11.; QUESO::BoxSubset<QUESO::GslVector,QUESO::GslMatrix> paramDomain("param_", paramSpace, paramMinValues, paramMaxValues); //------------------------------------------------------ // SIP Step 3 of 6: Instantiate the likelihood function // object to be used by QUESO. //------------------------------------------------------ likelihoodRoutine_Data likelihoodRoutine_Data(env); QUESO::GenericScalarFunction<QUESO::GslVector,QUESO::GslMatrix> likelihoodFunctionObj("like_", paramDomain, likelihoodRoutine, (void *) &likelihoodRoutine_Data, true); // the routine computes [ln(function)] //------------------------------------------------------ // SIP Step 4 of 6: Define the prior RV //------------------------------------------------------ #ifdef PRIOR_IS_GAUSSIAN QUESO::GslVector meanVector( paramSpace.zeroVector() ); meanVector[0] = 9; QUESO::GslMatrix covMatrix = QUESO::GslMatrix(paramSpace.zeroVector()); covMatrix(0,0) = 1.; // Create a Gaussian prior RV QUESO::GaussianVectorRV<QUESO::GslVector,QUESO::GslMatrix> priorRv("prior_",paramDomain,meanVector,covMatrix); #else // Create an uniform prior RV QUESO::UniformVectorRV<QUESO::GslVector,QUESO::GslMatrix> priorRv("prior_",paramDomain); #endif //------------------------------------------------------ // SIP Step 5 of 6: Instantiate the inverse problem //------------------------------------------------------ QUESO::GenericVectorRV<QUESO::GslVector,QUESO::GslMatrix> postRv("post_", // Extra prefix before the default "rv_" prefix paramSpace); QUESO::StatisticalInverseProblem<QUESO::GslVector,QUESO::GslMatrix> ip("", // No extra prefix before the default "ip_" prefix NULL, priorRv, likelihoodFunctionObj, postRv); //------------------------------------------------------ // SIP Step 6 of 6: Solve the inverse problem, that is, // set the 'pdf' and the 'realizer' of the posterior RV //------------------------------------------------------ std::cout << "Solving the SIP with Metropolis Hastings" << std::endl << std::endl; QUESO::GslVector paramInitials(paramSpace.zeroVector()); priorRv.realizer().realization(paramInitials); QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0,0) = std::pow( fabs(paramInitials[0])/20. , 2. ); ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); //================================================================ // Statistical forward problem (SFP): find the max distance // traveled by an object in projectile motion; input pdf for 'g' // is the solution of the SIP above. //================================================================ gettimeofday(&timevalNow, NULL); std::cout << "Beginning 'SFP -> Projectile motion' at " << ctime(&timevalNow.tv_sec) << std::endl; //------------------------------------------------------ // SFP Step 1 of 6: Instantiate the parameter *and* qoi spaces. // SFP input RV = FIP posterior RV, so SFP parameter space // has been already defined. //------------------------------------------------------ QUESO::VectorSpace<QUESO::GslVector,QUESO::GslMatrix> qoiSpace(env, "qoi_", 1, NULL); //------------------------------------------------------ // SFP Step 2 of 6: Instantiate the parameter domain //------------------------------------------------------ // Not necessary because input RV of the SFP = output RV of SIP. // Thus, the parameter domain has been already defined. //------------------------------------------------------ // SFP Step 3 of 6: Instantiate the qoi function object // to be used by QUESO. //------------------------------------------------------ qoiRoutine_Data qoiRoutine_Data; qoiRoutine_Data.m_angle = M_PI/4.0; //45 degrees (radians) qoiRoutine_Data.m_initialVelocity= 5.; //initial speed (m/s) qoiRoutine_Data.m_initialHeight = 0.; //initial height (m) QUESO::GenericVectorFunction<QUESO::GslVector,QUESO::GslMatrix,QUESO::GslVector,QUESO::GslMatrix> qoiFunctionObj("qoi_", paramDomain, qoiSpace, qoiRoutine, (void *) &qoiRoutine_Data); //------------------------------------------------------ // SFP Step 4 of 6: Define the input RV //------------------------------------------------------ // Not necessary because input RV of SFP = output RV of SIP // (postRv). //------------------------------------------------------ // SFP Step 5 of 6: Instantiate the forward problem //------------------------------------------------------ QUESO::GenericVectorRV<QUESO::GslVector,QUESO::GslMatrix> qoiRv("qoi_", qoiSpace); QUESO::StatisticalForwardProblem<QUESO::GslVector,QUESO::GslMatrix,QUESO::GslVector,QUESO::GslMatrix> fp("", NULL, postRv, qoiFunctionObj, qoiRv); //------------------------------------------------------ // SFP Step 6 of 6: Solve the forward problem //------------------------------------------------------ std::cout << "Solving the SFP with Monte Carlo" << std::endl << std::endl; fp.solveWithMonteCarlo(NULL); //------------------------------------------------------ gettimeofday(&timevalNow, NULL); if ((env.subDisplayFile() ) && (env.displayVerbosity() >= 2)) { *env.subDisplayFile() << "Ending run of 'Gravity + Projectile motion' example at " << ctime(&timevalNow.tv_sec) << std::endl; } if (env.fullRank() == 0) { std::cout << "Ending run of 'Gravity + Projectile motion' example at " << ctime(&timevalNow.tv_sec) << std::endl; } return; }
int main(int argc, char ** argv) { #ifdef QUESO_HAS_MPI MPI_Init(&argc, &argv); QUESO::FullEnvironment env(MPI_COMM_WORLD, argv[1], "", NULL); #else QUESO::FullEnvironment env(argv[1], "", NULL); #endif QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> paramSpace(env, "param_", 1, NULL); double min_val = 0.0; double max_val = 1.0; QUESO::GslVector paramMins(paramSpace.zeroVector()); paramMins.cwSet(min_val); QUESO::GslVector paramMaxs(paramSpace.zeroVector()); paramMaxs.cwSet(max_val); QUESO::BoxSubset<QUESO::GslVector, QUESO::GslMatrix> paramDomain("param_", paramSpace, paramMins, paramMaxs); QUESO::UniformVectorRV<QUESO::GslVector, QUESO::GslMatrix> priorRv("prior_", paramDomain); // Set up observation space QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> obsSpace(env, "obs_", 2, NULL); // Fill up observation vector QUESO::GslVector observations(obsSpace.zeroVector()); observations[0] = 1.0; observations[1] = 1.0; // Pass in observations to Gaussian likelihood object Likelihood<QUESO::GslVector, QUESO::GslMatrix> lhood("llhd_", paramDomain, observations, 1.0); QUESO::GenericVectorRV<QUESO::GslVector, QUESO::GslMatrix> postRv("post_", paramSpace); QUESO::StatisticalInverseProblem<QUESO::GslVector, QUESO::GslMatrix> ip("", NULL, priorRv, lhood, postRv); QUESO::GslVector paramInitials(paramSpace.zeroVector()); paramInitials[0] = 0.0; QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); for (unsigned int i = 0; i < 1; i++) { proposalCovMatrix(i, i) = 0.1; } ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); #ifdef QUESO_HAS_MPI MPI_Finalize(); #endif return 0; }
int main(int argc, char ** argv) { // Step 0: Set up some variables unsigned int numExperiments = 10; // Number of experiments unsigned int numUncertainVars = 5; // Number of things to calibrate unsigned int numSimulations = 50; // Number of simulations unsigned int numConfigVars = 1; // Dimension of configuration space unsigned int numEta = 1; // Number of responses the model is returning unsigned int experimentSize = 1; // Size of each experiment #ifdef QUESO_HAS_MPI MPI_Init(&argc, &argv); // Step 1: Set up QUESO environment QUESO::FullEnvironment env(MPI_COMM_WORLD, argv[1], "", NULL); #else QUESO::FullEnvironment env(argv[1], "", NULL); #endif // Step 2: Set up prior for calibration parameters QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> paramSpace(env, "param_", numUncertainVars, NULL); // Parameter (theta) bounds: // descriptors 'k_tmasl' 'k_xkle' 'k_xkwew' 'k_xkwlx' 'k_cd' // upper_bounds 1.05 1.1 1.1 1.1 1.1 // lower_bounds 0.95 0.9 0.9 0.9 0.9 // // These bounds are dealt with when reading in the data QUESO::GslVector paramMins(paramSpace.zeroVector()); QUESO::GslVector paramMaxs(paramSpace.zeroVector()); paramMins.cwSet(0.0); paramMaxs.cwSet(1.0); QUESO::BoxSubset<QUESO::GslVector, QUESO::GslMatrix> paramDomain("param_", paramSpace, paramMins, paramMaxs); QUESO::UniformVectorRV<QUESO::GslVector, QUESO::GslMatrix> priorRv("prior_", paramDomain); // Step 3: Instantiate the 'scenario' and 'output' spaces for simulation QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> configSpace(env, "scenario_", numConfigVars, NULL); QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> nEtaSpace(env, "output_", numEta, NULL); // Step 4: Instantiate the 'output' space for the experiments QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> experimentSpace(env, "experimentspace_", experimentSize, NULL); QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> totalExperimentSpace(env, "experimentspace_", experimentSize * numExperiments, NULL); // Step 5: Instantiate the Gaussian process emulator object // // Regarding simulation scenario input values, the user should standardise // them so that they exist inside a hypercube. // // Regarding simulation output data, the user should transform it so that the // mean is zero and the variance is one. // // Regarding experimental scenario input values, the user should standardize // them so that they exist inside a hypercube. // // Regarding experimental data, the user should transformed it so that it has // zero mean and variance one. // GPMSA stores all the information about our simulation // data and experimental data. It also stores default information about the // hyperparameter distributions. QUESO::GPMSAFactory<QUESO::GslVector, QUESO::GslMatrix> gpmsaFactory(env, NULL, priorRv, configSpace, paramSpace, nEtaSpace, experimentSpace, numSimulations, numExperiments); // std::vector containing all the points in scenario space where we have // simulations std::vector<QUESO::GslVector *> simulationScenarios(numSimulations, (QUESO::GslVector *) NULL); // std::vector containing all the points in parameter space where we have // simulations std::vector<QUESO::GslVector *> paramVecs(numSimulations, (QUESO::GslVector *) NULL); // std::vector containing all the simulation output data std::vector<QUESO::GslVector *> outputVecs(numSimulations, (QUESO::GslVector *) NULL); // std::vector containing all the points in scenario space where we have // experiments std::vector<QUESO::GslVector *> experimentScenarios(numExperiments, (QUESO::GslVector *) NULL); // std::vector containing all the experimental output data std::vector<QUESO::GslVector *> experimentVecs(numExperiments, (QUESO::GslVector *) NULL); // The experimental output data observation error covariance matrix QUESO::GslMatrix experimentMat(totalExperimentSpace.zeroVector()); // Instantiate each of the simulation points/outputs for (unsigned int i = 0; i < numSimulations; i++) { simulationScenarios[i] = new QUESO::GslVector(configSpace.zeroVector()); // 'x_{i+1}^*' in paper paramVecs [i] = new QUESO::GslVector(paramSpace.zeroVector()); // 't_{i+1}^*' in paper outputVecs [i] = new QUESO::GslVector(nEtaSpace.zeroVector()); // 'eta_{i+1}' in paper } for (unsigned int i = 0; i < numExperiments; i++) { experimentScenarios[i] = new QUESO::GslVector(configSpace.zeroVector()); // 'x_{i+1}' in paper experimentVecs[i] = new QUESO::GslVector(experimentSpace.zeroVector()); } // Read in data and store the standard deviation of the simulation data. We // will need the standard deviation when we pass the experiment error // covariance matrix to QUESO. double stdsim = readData(simulationScenarios, paramVecs, outputVecs, experimentScenarios, experimentVecs); for (unsigned int i = 0; i < numExperiments; i++) { // Passing in error of experiments (standardised). experimentMat(i, i) = (0.025 / stdsim) * (0.025 / stdsim); } // Add simulation and experimental data gpmsaFactory.addSimulations(simulationScenarios, paramVecs, outputVecs); gpmsaFactory.addExperiments(experimentScenarios, experimentVecs, &experimentMat); QUESO::GenericVectorRV<QUESO::GslVector, QUESO::GslMatrix> postRv( "post_", gpmsaFactory.prior().imageSet().vectorSpace()); QUESO::StatisticalInverseProblem<QUESO::GslVector, QUESO::GslMatrix> ip("", NULL, gpmsaFactory, postRv); QUESO::GslVector paramInitials( gpmsaFactory.prior().imageSet().vectorSpace().zeroVector()); // Initial condition of the chain // Have to set each of these by hand, *and* the sampler is sensitive to these // values paramInitials[0] = 0.5; // param 1 paramInitials[1] = 0.5; // param 2 paramInitials[2] = 0.5; // param 3 paramInitials[3] = 0.5; // param 4 paramInitials[4] = 0.5; // param 5 paramInitials[5] = 0.4; // not used. emulator mean paramInitials[6] = 0.4; // emulator precision paramInitials[7] = 0.97; // emulator corr str paramInitials[8] = 0.97; // emulator corr str paramInitials[9] = 0.97; // emulator corr str paramInitials[10] = 0.97; // emulator corr str paramInitials[11] = 0.20; // emulator corr str paramInitials[12] = 0.80; // emulator corr str paramInitials[13] = 10.0; // discrepancy precision paramInitials[14] = 0.97; // discrepancy corr str paramInitials[15] = 8000.0; // emulator data precision QUESO::GslMatrix proposalCovMatrix( gpmsaFactory.prior().imageSet().vectorSpace().zeroVector()); // Setting the proposal covariance matrix by hand. This requires great // forethough, and can generally be referred to as a massive hack. These // values were taken from the gpmsa matlab code and fiddled with. double scale = 600.0; proposalCovMatrix(0, 0) = 3.1646 / 10.0; // param 1 proposalCovMatrix(1, 1) = 3.1341 / 10.0; // param 2 proposalCovMatrix(2, 2) = 3.1508 / 10.0; // param 3 proposalCovMatrix(3, 3) = 0.3757 / 10.0; // param 4 proposalCovMatrix(4, 4) = 0.6719 / 10.0; // param 5 proposalCovMatrix(5, 5) = 0.1 / scale; // not used. emulator mean proposalCovMatrix(6, 6) = 0.4953 / scale; // emulator precision proposalCovMatrix(7, 7) = 0.6058 / scale; // emulator corr str proposalCovMatrix(8, 8) = 7.6032e-04 / scale; // emulator corr str proposalCovMatrix(9, 9) = 8.3815e-04 / scale; // emulator corr str proposalCovMatrix(10, 10) = 7.5412e-04 / scale; // emulator corr str proposalCovMatrix(11, 11) = 0.2682 / scale; // emulator corr str proposalCovMatrix(12, 12) = 0.0572 / scale; // emulator corr str proposalCovMatrix(13, 13) = 1.3417 / scale; // discrepancy precision proposalCovMatrix(14, 14) = 0.3461 / scale; // discrepancy corr str proposalCovMatrix(15, 15) = 495.3 / scale; // emulator data precision // Square to get variances for (unsigned int i = 0; i < 16; i++) { proposalCovMatrix(i, i) = proposalCovMatrix(i, i) * proposalCovMatrix(i, i); } ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); #ifdef QUESO_HAS_MPI MPI_Finalize(); #endif return 0; }
void infer_slope(const QUESO::FullEnvironment & env) { // Statistical Inverse Problem: Compute posterior pdf for slope 'm' and y-intercept c // Step 1: Instantiate the parameter space QUESO::VectorSpace<> paramSpace(env, "param_", 2, NULL); // 2 since we have a 2D problem // Step 2: Parameter domain QUESO::GslVector paramMinValues(paramSpace.zeroVector()); QUESO::GslVector paramMaxValues(paramSpace.zeroVector()); paramMinValues[0] = 2.; paramMaxValues[0] = 5.; paramMinValues[1] = 3.; paramMaxValues[1] = 7.; QUESO::BoxSubset<> paramDomain("param_", paramSpace, paramMinValues, paramMaxValues); // Step 3: Instantiate likelihood Likelihood<> lhood("like_", paramDomain); // Step 4: Define the prior RV QUESO::UniformVectorRV<> priorRv("prior_", paramDomain); // Step 5: Instantiate the inverse problem QUESO::GenericVectorRV<> postRv("post_", paramSpace); QUESO::StatisticalInverseProblem<> ip("", NULL, priorRv, lhood, postRv); // Step 6: Solve the inverse problem // Randomly sample for the initial state? QUESO::GslVector paramInitials(paramSpace.zeroVector()); priorRv.realizer().realization(paramInitials); // Initialize the Cov matrix: QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0,0) = std::pow(std::abs(paramInitials[0]) / 20.0, 2.0); proposalCovMatrix(1,1) = std::pow(std::abs(paramInitials[1]) / 20.0, 2.0); ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); // Using the posterior pdfs for m and c, compute 'y' at a given 'x' // Step 1: Instantiate the qoi space QUESO::VectorSpace<> qoiSpace(env, "qoi_", 1, NULL); // Step 2: Instantiate the parameter domain // Not necessary here because the posterior from SIP is used as the RV for SFP // Step 3: Instantiate the qoi object to be used by QUESO Qoi<> qoi("qoi_", paramDomain, qoiSpace); // Step 4: Define the input RV // Not required because we use the posterior as RV // Step 5: Instantiate the forward problem QUESO::GenericVectorRV<> qoiRv("qoi_", qoiSpace); QUESO::StatisticalForwardProblem<> fp("", NULL, postRv, qoi, qoiRv); // Step 6: Solve the forward problem std::cout << "Solving the SFP with Monte Carlo" << std::endl << std::endl; fp.solveWithMonteCarlo(NULL); system("mv outputData/sfp_lineSlope_qoi_seq.txt outputData/sfp_lineSlope_qoi_seq_post.txt"); // SENSITIVITY ANALYSIS // For m Qoi_m<> qoi_m("qoi_", paramDomain, qoiSpace); // Step 4: Define the input RV // Not required because we use the prior as RV for sensitivity analysis // Step 5: Instantiate the forward problem QUESO::StatisticalForwardProblem<> fp_m("", NULL, priorRv, qoi_m, qoiRv); // Step 6: Solve the forward problem fp_m.solveWithMonteCarlo(NULL); system("mv outputData/sfp_lineSlope_qoi_seq.txt outputData/sense_m.txt"); // For c Qoi_c<> qoi_c("qoi_", paramDomain, qoiSpace); // Step 4: Define the input RV // Not required because we use the prior as RV for sensitivity analysis // Step 5: Instantiate the forward problem QUESO::StatisticalForwardProblem<> fp_c("", NULL, priorRv, qoi_c, qoiRv); // Step 6: Solve the forward problem fp_c.solveWithMonteCarlo(NULL); system("mv outputData/sfp_lineSlope_qoi_seq.txt outputData/sense_c.txt"); // For both, m and c Qoi_mc<> qoi_mc("qoi_", paramDomain, qoiSpace); // Step 4: Define the input RV // Not required because we use the prior as RV for sensitivity analysis // Step 5: Instantiate the forward problem QUESO::StatisticalForwardProblem<> fp_mc("", NULL, priorRv, qoi_mc, qoiRv); // Step 6: Solve the forward problem fp_mc.solveWithMonteCarlo(NULL); system("mv outputData/sfp_lineSlope_qoi_seq.txt outputData/sense_mc.txt"); }
int main(int argc, char* argv[]) { #ifndef QUESO_HAS_MPI // Skip this test if we're not in parallel return 77; #else MPI_Init(&argc, &argv); std::string inputFileName = argv[1]; const char * test_srcdir = std::getenv("srcdir"); if (test_srcdir) inputFileName = test_srcdir + ('/' + inputFileName); // Initialize QUESO environment QUESO::FullEnvironment env(MPI_COMM_WORLD, inputFileName, "", NULL); //================================================================ // Statistical inverse problem (SIP): find posterior PDF for 'g' //================================================================ //------------------------------------------------------ // SIP Step 1 of 6: Instantiate the parameter space //------------------------------------------------------ QUESO::VectorSpace<> paramSpace(env, "param_", 1, NULL); //------------------------------------------------------ // SIP Step 2 of 6: Instantiate the parameter domain //------------------------------------------------------ QUESO::GslVector paramMinValues(paramSpace.zeroVector()); QUESO::GslVector paramMaxValues(paramSpace.zeroVector()); paramMinValues[0] = 8.; paramMaxValues[0] = 11.; QUESO::BoxSubset<> paramDomain("param_", paramSpace, paramMinValues, paramMaxValues); //------------------------------------------------------ // SIP Step 3 of 6: Instantiate the likelihood function // object to be used by QUESO. //------------------------------------------------------ Likelihood<> lhood("like_", paramDomain); //------------------------------------------------------ // SIP Step 4 of 6: Define the prior RV //------------------------------------------------------ QUESO::UniformVectorRV<> priorRv("prior_", paramDomain); //------------------------------------------------------ // SIP Step 5 of 6: Instantiate the inverse problem //------------------------------------------------------ // Extra prefix before the default "rv_" prefix QUESO::GenericVectorRV<> postRv("post_", paramSpace); // No extra prefix before the default "ip_" prefix QUESO::StatisticalInverseProblem<> ip("", NULL, priorRv, lhood, postRv); //------------------------------------------------------ // SIP Step 6 of 6: Solve the inverse problem, that is, // set the 'pdf' and the 'realizer' of the posterior RV //------------------------------------------------------ QUESO::GslVector paramInitials(paramSpace.zeroVector()); priorRv.realizer().realization(paramInitials); QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0,0) = std::pow(std::abs(paramInitials[0]) / 20.0, 2.0); ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); //================================================================ // Statistical forward problem (SFP): find the max distance // traveled by an object in projectile motion; input pdf for 'g' // is the solution of the SIP above. //================================================================ //------------------------------------------------------ // SFP Step 1 of 6: Instantiate the parameter *and* qoi spaces. // SFP input RV = FIP posterior RV, so SFP parameter space // has been already defined. //------------------------------------------------------ QUESO::VectorSpace<> qoiSpace(env, "qoi_", 1, NULL); //------------------------------------------------------ // SFP Step 2 of 6: Instantiate the parameter domain //------------------------------------------------------ // Not necessary because input RV of the SFP = output RV of SIP. // Thus, the parameter domain has been already defined. //------------------------------------------------------ // SFP Step 3 of 6: Instantiate the qoi object // to be used by QUESO. //------------------------------------------------------ Qoi<> qoi("qoi_", paramDomain, qoiSpace); //------------------------------------------------------ // SFP Step 4 of 6: Define the input RV //------------------------------------------------------ // Not necessary because input RV of SFP = output RV of SIP // (postRv). //------------------------------------------------------ // SFP Step 5 of 6: Instantiate the forward problem //------------------------------------------------------ QUESO::GenericVectorRV<> qoiRv("qoi_", qoiSpace); QUESO::StatisticalForwardProblem<> fp("", NULL, postRv, qoi, qoiRv); //------------------------------------------------------ // SFP Step 6 of 6: Solve the forward problem //------------------------------------------------------ fp.solveWithMonteCarlo(NULL); MPI_Finalize(); return 0; #endif // QUESO_HAS_MPI }