25 #include <queso/GPMSA.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
31 template <
class V,
class M>
38 const unsigned int m_numSimulations,
39 const unsigned int m_numExperiments,
49 const V & residual_in,
50 const M & BT_Wy_B_inv_in,
51 const M & KT_K_inv_in,
55 m_scenarioSpace(m_scenarioSpace),
56 m_parameterSpace(m_parameterSpace),
57 m_simulationOutputSpace(m_simulationOutputSpace),
58 m_experimentOutputSpace(m_experimentOutputSpace),
59 m_numSimulations(m_numSimulations),
60 m_numExperiments(m_numExperiments),
61 m_simulationScenarios(m_simulationScenarios),
62 m_simulationParameters(m_simulationParameters),
63 m_simulationOutputs(m_simulationOutputs),
64 m_experimentScenarios(m_experimentScenarios),
65 m_experimentOutputs(m_experimentOutputs),
66 m_discrepancyBases(m_discrepancyBases),
67 m_observationErrorMatrices(m_observationErrorMatrices),
68 m_observationErrorMatrix(m_observationErrorMatrix),
69 m_totalPrior(m_totalPrior),
70 residual(residual_in),
71 BT_Wy_B_inv(BT_Wy_B_inv_in),
72 KT_K_inv(KT_K_inv_in),
75 queso_assert_greater(m_numSimulations, 0);
80 const unsigned int numOutputs =
81 this->m_experimentOutputSpace.
dimLocal();
88 template <
class V,
class M>
94 template <
class V,
class M>
153 const unsigned int totalRuns = this->m_numExperiments + this->m_numSimulations;
154 const unsigned int numOutputs = this->m_experimentOutputSpace.dimLocal();
155 const unsigned int totalOutputs = totalRuns * numOutputs;
156 const unsigned int num_discrepancy_bases = m_discrepancyBases.size();
157 const unsigned int residualSize = (numOutputs == 1) ? totalOutputs :
158 totalRuns * num_svd_terms + m_numExperiments * num_discrepancy_bases;
160 double prodScenario = 1.0;
161 double prodParameter = 1.0;
162 double prodDiscrepancy = 1.0;
163 unsigned int dimScenario = (this->m_scenarioSpace).dimLocal();
164 unsigned int dimParameter = (this->m_parameterSpace).dimLocal();
167 unsigned int dimSum = 2 +
168 (this->num_svd_terms < numOutputs) +
170 m_opts.m_calibrateObservationalPrecision +
178 const unsigned int offset1 = (numOutputs == 1) ?
179 0 : m_numExperiments * num_discrepancy_bases;
182 const unsigned int offset1b = offset1 +
183 m_numExperiments * num_svd_terms;
186 const unsigned int offset2 = (numOutputs == 1) ?
187 0 : m_numExperiments * (num_discrepancy_bases + num_svd_terms);
191 Map z_map(residualSize, 0, comm);
192 M covMatrix(this->m_env, z_map, residualSize);
195 (
new V(*(this->m_simulationParameters[0])));
196 for (
unsigned int k = 0; k < dimParameter; k++) {
198 (*domainVectorParameter)[k] = domainVector[k];
202 for (
unsigned int i = 0; i < totalRuns; i++) {
210 if (i < this->m_numExperiments) {
212 scenario1 = (this->m_experimentScenarios)[i];
215 parameter1 = domainVectorParameter;
219 (this->m_simulationScenarios)[i-this->m_numExperiments];
221 (this->m_simulationParameters)[i-this->m_numExperiments];
224 for (
unsigned int j = 0; j < totalRuns; j++) {
230 if (j < this->m_numExperiments) {
231 scenario2 = (this->m_experimentScenarios)[j];
232 parameter2 = domainVectorParameter;
236 (this->m_simulationScenarios)[j-this->m_numExperiments];
238 (this->m_simulationParameters)[j-this->m_numExperiments];
243 unsigned int emulatorCorrStrStart =
244 dimParameter + (this->num_svd_terms < numOutputs) + num_svd_terms;
245 for (
unsigned int k = 0; k < dimScenario; k++) {
246 const double & emulator_corr_strength =
247 domainVector[emulatorCorrStrStart+k];
248 double scenario_param1 =
249 m_opts.normalized_scenario_parameter(k, (*scenario1)[k]);
250 double scenario_param2 =
251 m_opts.normalized_scenario_parameter(k, (*scenario2)[k]);
252 prodScenario *= std::pow(emulator_corr_strength,
253 4.0 * (scenario_param1 - scenario_param2) *
254 (scenario_param1 - scenario_param2));
261 for (
unsigned int k = 0; k < dimParameter; k++) {
262 queso_assert (!
queso_isnan(domainVector[emulatorCorrStrStart+dimScenario+k]));
265 const double & emulator_corr_strength =
266 domainVector[emulatorCorrStrStart+dimScenario+k];
267 double uncertain_param1 =
268 m_opts.normalized_uncertain_parameter(k, (*parameter1)[k]);
269 double uncertain_param2 =
270 m_opts.normalized_uncertain_parameter(k, (*parameter2)[k]);
271 prodParameter *= std::pow(
272 emulator_corr_strength,
273 4.0 * (uncertain_param1 - uncertain_param2) *
274 (uncertain_param1 - uncertain_param2));
281 for (
unsigned int basis = 0; basis != num_svd_terms; ++basis)
286 const double relevant_precision =
287 domainVector[dimParameter + basis +
288 (this->num_svd_terms<numOutputs) + (numOutputs>1)];
289 queso_assert_greater(relevant_precision, 0.0);
291 const unsigned int stridei =
292 (i < this->m_numExperiments) ? m_numExperiments : m_numSimulations;
293 const unsigned int offseti =
294 (i < this->m_numExperiments) ? offset1 : offset1b - m_numExperiments;
295 const unsigned int stridej =
296 (j < this->m_numExperiments) ? m_numExperiments : m_numSimulations;
297 const unsigned int offsetj =
298 (j < this->m_numExperiments) ? offset1 : offset1b - m_numExperiments;
300 covMatrix(offseti+basis*stridei+i,
301 offsetj+basis*stridej+j) =
302 prodScenario * prodParameter / relevant_precision;
307 if (i < this->m_numExperiments && j < this->m_numExperiments) {
310 prodDiscrepancy = 1.0;
311 unsigned int discrepancyCorrStrStart =
312 dimParameter + num_svd_terms + dimParameter + dimScenario + 1 +
313 (this->num_svd_terms<numOutputs) + (numOutputs > 1);
314 for (
unsigned int k = 0; k < dimScenario; k++) {
315 const double & discrepancy_corr_strength =
316 domainVector[discrepancyCorrStrStart+k];
317 double cross_scenario_param1 =
318 m_opts.normalized_scenario_parameter(k, (*cross_scenario1)[k]);
319 double cross_scenario_param2 =
320 m_opts.normalized_scenario_parameter(k, (*cross_scenario2)[k]);
322 std::pow(discrepancy_corr_strength, 4.0 *
323 (cross_scenario_param1 - cross_scenario_param2) *
324 (cross_scenario_param1 - cross_scenario_param2));
327 const double discrepancy_precision =
328 domainVector[discrepancyCorrStrStart-1];
329 queso_assert_greater(discrepancy_precision, 0);
334 const double R_v = prodDiscrepancy / discrepancy_precision;
335 for (
unsigned int disc = 0; disc != num_discrepancy_bases;
337 covMatrix(disc*m_numExperiments+i,
338 disc*m_numExperiments+j) += R_v;
348 const double experimentalError =
349 (*this->m_observationErrorMatrix)(i,j);
351 queso_assert_greater_equal (experimentalError, 0);
353 const double lambda_y =
354 m_opts.m_calibrateObservationalPrecision ?
355 domainVector[dimSum-1] : 1.0;
357 covMatrix(i,j) += lambda_y * experimentalError;
364 const double emulator_data_precision = domainVector[dimSum-1-(numOutputs>1)];
365 queso_assert_greater(emulator_data_precision, 0);
366 double nugget = 1.0 / emulator_data_precision;
368 for (
unsigned int disc = 0; disc != num_discrepancy_bases;
370 covMatrix(disc*m_numExperiments+i,
371 disc*m_numExperiments+i) += nugget;
378 const double lambda_y =
379 m_opts.m_calibrateObservationalPrecision ?
380 domainVector[dimSum-1] : 1.0;
381 const double inv_lambda_y = 1.0/lambda_y;
383 unsigned int BT_Wy_B_size = BT_Wy_B_inv.numCols();
384 for (
unsigned int i=0; i != BT_Wy_B_size; ++i)
385 for (
unsigned int j=0; j != BT_Wy_B_size; ++j)
386 covMatrix(i,j) += BT_Wy_B_inv(i,j) * inv_lambda_y;
388 const double emulator_precision =
389 domainVector[dimParameter+1];
390 const double inv_emulator_precision = 1.0/emulator_precision;
392 unsigned int KT_K_size = KT_K_inv.numCols();
393 for (
unsigned int i=0; i != KT_K_size; ++i)
394 for (
unsigned int j=0; j != KT_K_size; ++j)
395 covMatrix(i+offset2,j+offset2) +=
396 KT_K_inv(i,j) * inv_emulator_precision;
402 V sol(covMatrix.invertMultiply(residual));
405 double minus_2_log_lhd = 0.0;
407 for (
unsigned int i = 0; i < residualSize; i++) {
408 minus_2_log_lhd += sol[i] * residual[i];
412 for (
unsigned int i = 0; i < residualSize; i++) {
414 std::cout <<
"NaN sol[" << i <<
']' << std::endl;
416 std::cout <<
"NaN residual[" << i <<
']' << std::endl;
418 std::cout <<
"Covariance Matrix:" << std::endl;
419 covMatrix.print(std::cout);
424 queso_assert_greater(minus_2_log_lhd, 0);
426 double cov_det = covMatrix.determinant();
430 std::cout <<
"Non-positive determinant for covMatrix = " << std::endl;
431 covMatrix.print(std::cout);
435 minus_2_log_lhd += std::log(covMatrix.determinant());
438 return -0.5 * minus_2_log_lhd;
441 template <
class V,
class M>
453 template <
class V,
class M>
462 unsigned int numSimulations,
463 unsigned int numExperiments)
466 m_parameterPrior(parameterPrior),
467 m_scenarioSpace(scenarioSpace),
468 m_parameterSpace(parameterSpace),
469 m_simulationOutputSpace(simulationOutputSpace),
470 m_experimentOutputSpace(experimentOutputSpace),
471 m_numSimulations(numSimulations),
472 m_numExperiments(numExperiments),
473 m_simulationScenarios(numSimulations),
474 m_simulationParameters(numSimulations),
475 m_simulationOutputs(numSimulations),
476 m_experimentScenarios(numExperiments),
477 m_experimentOutputs(numExperiments),
478 m_numSimulationAdds(0),
479 m_numExperimentAdds(0),
482 m_constructedGP(false)
486 queso_assert_equal_to(simulationOutputSpace.
dimGlobal(),
490 const unsigned int numOutputs =
492 const Map & output_map = experimentOutputSpace.
map();
496 for (
unsigned int i=0; i != numOutputs; ++i)
497 (*all_ones_basis)[i] = 1;
510 queso_error_msg(
"Must options object or an input file");
527 template <
class V,
class M>
530 if (this->allocated_m_opts)
534 template <
class V,
class M>
538 return *this->m_opts;
542 template <
class V,
class M>
546 return *this->m_opts;
550 template <
class V,
class M>
554 return this->m_numSimulations;
557 template <
class V,
class M>
561 return this->m_numExperiments;
564 template <
class V,
class M>
568 return this->m_scenarioSpace;
571 template <
class V,
class M>
575 return this->m_parameterSpace;
578 template <
class V,
class M>
582 return this->m_simulationOutputSpace;
585 template <
class V,
class M>
589 return this->m_experimentOutputSpace;
592 template <
class V,
class M>
595 unsigned int simulationId)
const
597 queso_require_less_msg(simulationId, m_simulationScenarios.size(),
"simulationId is too large");
599 queso_require_msg(m_simulationScenarios[simulationId],
"vector is NULL");
601 return *(this->m_simulationScenarios[simulationId]);
604 template <
class V,
class M>
605 const std::vector<typename SharedPtr<V>::Type> &
608 return this->m_simulationScenarios;
611 template <
class V,
class M>
614 unsigned int simulationId)
const
616 queso_require_less_msg(simulationId, m_simulationParameters.size(),
"simulationId is too large");
618 queso_require_msg(m_simulationParameters[simulationId],
"vector is NULL");
620 return *(this->m_simulationParameters[simulationId]);
623 template <
class V,
class M>
624 const std::vector<typename SharedPtr<V>::Type> &
627 return this->m_simulationParameters;
630 template <
class V,
class M>
633 unsigned int simulationId)
const
635 queso_require_less_msg(simulationId, m_simulationOutputs.size(),
"simulationId is too large");
637 queso_require_msg(m_simulationOutputs[simulationId],
"vector is NULL");
639 return *(this->m_simulationOutputs[simulationId]);
642 template <
class V,
class M>
643 const std::vector<typename SharedPtr<V>::Type> &
646 return this->m_simulationOutputs;
649 template <
class V,
class M>
652 unsigned int experimentId)
const
654 queso_require_less_msg(experimentId, (this->m_experimentScenarios).
size(),
"experimentId is too large");
656 queso_require_msg(this->m_experimentScenarios[experimentId],
"vector is NULL");
658 return *(this->m_experimentScenarios[experimentId]);
661 template <
class V,
class M>
662 const std::vector<typename SharedPtr<V>::Type> &
665 return this->m_experimentScenarios;
668 template <
class V,
class M>
671 unsigned int experimentId)
const
673 queso_require_less_msg(experimentId, (this->m_experimentOutputs).
size(),
"experimentId is too large");
675 queso_require_msg(this->m_experimentOutputs[experimentId],
"vector is NULL");
677 return *(this->m_experimentOutputs[experimentId]);
680 template <
class V,
class M>
681 const std::vector<typename SharedPtr<V>::Type> &
684 return this->m_experimentOutputs;
687 template <
class V,
class M>
694 template <
class V,
class M>
698 return *(this->gpmsaEmulator);
701 template <
class V,
class M>
707 queso_require_less_msg(this->m_numSimulationAdds, this->m_numSimulations,
"too many simulation adds...");
709 this->m_simulationScenarios[this->m_numSimulationAdds] = simulationScenario;
710 this->m_simulationParameters[this->m_numSimulationAdds] = simulationParameter;
711 this->m_simulationOutputs[this->m_numSimulationAdds] = simulationOutput;
712 this->m_numSimulationAdds++;
714 if ((this->m_numSimulationAdds == this->m_numSimulations) &&
715 (this->m_numExperimentAdds == this->m_numExperiments) &&
716 (this->m_constructedGP ==
false)) {
717 this->setUpEmulator();
721 template <
class V,
class M>
728 for (
unsigned int i = 0; i < this->m_numSimulations; i++) {
729 this->addSimulation(simulationScenarios[i],
730 simulationParameters[i],
731 simulationOutputs[i]);
737 template <
class V,
class M>
741 this->m_opts->template set_final_scaling<V>
742 (m_simulationScenarios,
743 m_simulationParameters,
745 m_experimentScenarios,
746 m_experimentOutputs);
748 const unsigned int numOutputs =
749 this->m_experimentOutputSpace.dimLocal();
751 this->num_svd_terms = this->m_opts->m_maxEmulatorBasisVectors ?
752 std::min((
unsigned int)(this->m_opts->m_maxEmulatorBasisVectors), numOutputs) :
755 const Map & output_map = m_simulationOutputs[0]->map();
759 Map serial_map(m_numSimulations, 0, comm);
763 simulationOutputMeans.reset
764 (
new V (env, output_map));
766 for (
unsigned int i=0; i != m_numSimulations; ++i)
767 for (
unsigned int j=0; j != numOutputs; ++j)
768 (*simulationOutputMeans)[j] += (*m_simulationOutputs[i])[j];
770 for (
unsigned int j=0; j != numOutputs; ++j)
771 (*simulationOutputMeans)[j] /= m_numSimulations;
773 M simulation_matrix(env, serial_map, numOutputs);
775 for (
unsigned int i=0; i != m_numSimulations; ++i)
776 for (
unsigned int j=0; j != numOutputs; ++j)
777 simulation_matrix(i,j) =
778 (*m_simulationOutputs[i])[j] - (*simulationOutputMeans)[j];
783 M S_trans(simulation_matrix.transpose());
785 M SM_squared(S_trans*simulation_matrix);
787 M SM_singularVectors(env, SM_squared.map(), numOutputs);
788 V SM_singularValues(env, SM_squared.map());
790 SM_squared.eigen(SM_singularValues, &SM_singularVectors);
793 m_TruncatedSVD_simulationOutputs.reset
794 (
new M(env, output_map, num_svd_terms));
796 for (
unsigned int i=0; i != numOutputs; ++i)
797 for (
unsigned int k = 0; k != num_svd_terms; ++k)
798 (*m_TruncatedSVD_simulationOutputs)(i,k) = SM_singularVectors(i,k);
800 Map copied_map(numOutputs * m_numSimulations, 0, comm);
803 (
new M(env, copied_map, m_numSimulations * num_svd_terms));
804 for (
unsigned int k=0; k != num_svd_terms; ++k)
805 for (
unsigned int i1=0; i1 != m_numSimulations; ++i1)
806 for (
unsigned int i2=0; i2 != numOutputs; ++i2)
808 const unsigned int i = i1 * numOutputs + i2;
809 const unsigned int j = k * m_numSimulations + i1;
810 (*K)(i,j) = SM_singularVectors(i2,k);
814 (
new M((K->transpose() * *K).inverse()));
816 Map serial_output_map(numOutputs, 0, comm);
818 for (
unsigned int i = 0; i != m_numExperiments; ++i)
820 M D_i(env, serial_output_map,
821 (
unsigned int)(m_discrepancyBases.size()));
823 for (
unsigned int j=0; j != numOutputs; ++j)
824 for (
unsigned int k=0; k != m_discrepancyBases.size(); ++k)
825 D_i(j,k) = (*m_discrepancyBases[k])[j];
827 m_discrepancyMatrices.push_back(D_i);
843 const unsigned int num_discrepancy_bases = m_discrepancyBases.size();
844 const unsigned int Brows = m_numExperiments * numOutputs;
845 const unsigned int Bcols =
846 m_numExperiments * (num_discrepancy_bases + num_svd_terms);
848 const Map B_row_map(Brows, 0, comm);
851 (
new M(env, B_row_map, Bcols));
853 const unsigned int Wyrows = m_numExperiments * numOutputs;
855 const Map Wy_row_map(Wyrows, 0, comm);
857 m_observationErrorMatrix.reset
858 (
new M(env, Wy_row_map, Wyrows));
861 M& Wy = *m_observationErrorMatrix;
863 for (
unsigned int ex = 0; ex != m_numExperiments; ++ex)
865 const M & D_i = m_discrepancyMatrices[ex];
868 const M & W_i = (*m_observationErrorMatrices[ex]);
871 int rv = W_i_copy.chol();
872 queso_assert_msg(!rv,
"Observation error matrix W_" << ex <<
883 for (
unsigned int outi = 0; outi != numOutputs; ++outi)
885 unsigned int i = ex*numOutputs+outi;
886 for (
unsigned int outj = 0; outj != num_discrepancy_bases; ++outj)
888 unsigned int j = ex + m_numExperiments * outj;
890 B(i,j) = D_i(outi,outj);
893 for (
unsigned int outj = 0; outj != num_svd_terms; ++outj)
895 unsigned int j = ex +
896 m_numExperiments * (num_discrepancy_bases + outj);
898 B(i,j) = (*m_TruncatedSVD_simulationOutputs)(outi,outj);
901 for (
unsigned int outj = 0; outj != numOutputs; ++outj)
904 unsigned int j = ex*numOutputs+outj;
906 Wy(i,j) = W_i(outi,outj);
911 M BT_Wy_B (B.transpose() * Wy * B);
915 for (
unsigned int i=0; i != Brows; ++i)
916 BT_Wy_B(i,i) += 1.e-4;
918 BT_Wy_B_inv.reset(
new M(BT_Wy_B.inverse()));
920 this->setUpHyperpriors();
922 this->m_constructedGP =
true;
923 this->gpmsaEmulator.reset
925 this->prior().imageSet(),
926 this->m_scenarioSpace,
927 this->m_parameterSpace,
928 this->m_simulationOutputSpace,
929 this->m_experimentOutputSpace,
930 this->m_numSimulations,
931 this->m_numExperiments,
932 this->m_simulationScenarios,
933 this->m_simulationParameters,
934 this->m_simulationOutputs,
935 this->m_experimentScenarios,
936 this->m_experimentOutputs,
937 this->m_discrepancyBases,
938 this->m_observationErrorMatrices,
939 this->m_observationErrorMatrix,
940 *(this->m_totalPrior),
949 template <
class V,
class M>
956 queso_require_less_equal_msg(experimentScenarios.size(), this->m_numExperiments,
"too many experiments...");
958 unsigned int offset = 0;
959 for (
unsigned int i = 0; i < this->m_experimentScenarios.size(); i++) {
960 this->m_experimentScenarios[i] = experimentScenarios[i];
961 this->m_experimentOutputs[i] = experimentOutputs[i];
963 const unsigned int outsize =
964 this->m_experimentOutputs[i]->sizeGlobal();
965 for (
unsigned int outi = 0; outi != outsize; ++outi)
966 for (
unsigned int outj = 0; outj != outsize; ++outj)
967 (*this->m_observationErrorMatrices[i])(outi,outj) =
968 (*experimentErrors)(offset+outi, offset+outj);
973 this->m_numExperimentAdds += experimentScenarios.size();
975 if ((this->m_numSimulationAdds == this->m_numSimulations) &&
976 (this->m_numExperimentAdds == this->m_numExperiments) &&
977 (this->m_constructedGP ==
false)) {
978 this->setUpEmulator();
983 template <
class V,
class M>
990 queso_require_less_equal_msg(experimentScenarios.size(), this->m_numExperiments,
"too many experiments...");
991 queso_require_equal_to(experimentScenarios.size(),
992 experimentOutputs.size());
993 queso_require_equal_to(experimentScenarios.size(),
994 experimentErrors.size());
996 for (
unsigned int i = 0; i < this->m_experimentScenarios.size(); i++) {
997 this->m_experimentScenarios[i] = experimentScenarios[i];
998 this->m_experimentOutputs[i] = experimentOutputs[i];
999 this->m_observationErrorMatrices[i] = experimentErrors[i];
1001 this->m_numExperimentAdds += experimentScenarios.size();
1003 if ((this->m_numSimulationAdds == this->m_numSimulations) &&
1004 (this->m_numExperimentAdds == this->m_numExperiments) &&
1005 (this->m_constructedGP ==
false)) {
1006 this->setUpEmulator();
1011 template <
class V,
class M>
1016 m_discrepancyBases = discrepancyBases;
1019 queso_assert_equal_to(this->m_constructedGP,
false);
1024 template <
class V,
class M>
1027 (
unsigned int simulationNumber)
1029 queso_assert_less(simulationNumber, m_numSimulations);
1030 queso_assert_equal_to(m_observationErrorMatrices.size(), m_numSimulations);
1032 return *m_observationErrorMatrices[simulationNumber];
1036 template <
class V,
class M>
1039 (
unsigned int simulationNumber)
const
1041 queso_assert_less(simulationNumber, m_numSimulations);
1042 queso_assert_equal_to(m_observationErrorMatrices.size(), m_numSimulations);
1044 return *m_observationErrorMatrices[simulationNumber];
1049 template <
class V,
class M>
1053 return *(this->m_totalPrior);
1056 template <
class V,
class M>
1064 template <
class V,
class M>
1068 const unsigned int numOutputs =
1069 this->m_experimentOutputSpace.dimLocal();
1071 const unsigned int num_discrepancy_bases = m_discrepancyBases.size();
1073 const MpiComm & comm = m_simulationOutputs[0]->map().
Comm();
1075 unsigned int rank_B;
1076 if (m_BMatrix->numRowsGlobal() > m_BMatrix->numCols())
1077 rank_B = m_BMatrix->rank(0, 1.e-4);
1079 rank_B = m_BMatrix->transpose().rank(0, 1.e-4);
1081 double emulatorPrecisionShape = this->m_opts->m_emulatorPrecisionShape;
1082 double emulatorPrecisionScale = this->m_opts->m_emulatorPrecisionScale;
1084 double observationalPrecisionShape = this->m_opts->m_observationalPrecisionShape;
1085 double observationalPrecisionScale = this->m_opts->m_observationalPrecisionScale;
1089 Map y_map(m_numExperiments * numOutputs, 0, comm);
1090 Map eta_map(m_numSimulations * numOutputs, 0, comm);
1092 const unsigned int yhat_size =
1093 m_numExperiments * (num_discrepancy_bases + num_svd_terms);
1095 const unsigned int etahat_size =
1096 m_numSimulations * num_svd_terms;
1098 Map zhat_map(yhat_size + etahat_size, 0, comm);
1100 V y(this->m_env, y_map);
1101 V eta(this->m_env, eta_map);
1103 for (
unsigned int i = 0; i < this->m_numExperiments; i++) {
1104 for (
unsigned int k = 0; k != numOutputs; ++k)
1106 (*((this->m_experimentOutputs)[i]))[k] -
1107 (*simulationOutputMeans)[k];
1110 for (
unsigned int i = 0; i < this->m_numSimulations; i++) {
1111 for (
unsigned int k = 0; k != numOutputs; ++k)
1112 eta[i*numOutputs+k] =
1113 (*((this->m_simulationOutputs)[i]))[k] -
1114 (*simulationOutputMeans)[k];
1118 M& Wy = *m_observationErrorMatrix;
1120 V yhat(*BT_Wy_B_inv * (B.transpose() * (Wy * y)));
1122 queso_assert_equal_to(yhat.sizeGlobal(), yhat_size);
1124 V etahat(*KT_K_inv * (K->transpose() * eta));
1126 residual.reset(
new V(this->m_env, zhat_map));
1127 for (
unsigned int i = 0; i < yhat_size; ++i)
1128 (*residual)[i] = yhat[i];
1130 for (
unsigned int i = 0; i < etahat_size; ++i)
1131 (*residual)[yhat_size+i] = etahat[i];
1133 emulatorPrecisionShape +=
1134 (this->m_numSimulations * (numOutputs - num_svd_terms)) / 2.0;
1137 eta_temp -= *K * etahat;
1139 emulatorPrecisionScale +=
1142 observationalPrecisionShape +=
1143 (this->m_numExperiments * numOutputs - rank_B) / 2.0;
1146 y_temp -= Wy * B * yhat;
1148 observationalPrecisionScale +=
1153 const unsigned int totalRuns = this->m_numExperiments + this->m_numSimulations;
1154 Map z_map(totalRuns, 0, comm);
1155 residual.reset(
new V (this->m_env, z_map));
1160 for (
unsigned int i = 0; i < this->m_numExperiments; i++) {
1161 (*residual)[i] = (*((this->m_experimentOutputs)[i]))[0] -
1162 (*simulationOutputMeans)[0];
1164 for (
unsigned int i = 0; i < this->m_numSimulations; i++) {
1165 (*residual)[i+this->m_numExperiments] =
1166 (*((this->m_simulationOutputs)[i]))[0] -
1167 (*simulationOutputMeans)[0];
1172 double emulatorCorrelationStrengthAlpha = this->m_opts->m_emulatorCorrelationStrengthAlpha;
1173 double emulatorCorrelationStrengthBeta = this->m_opts->m_emulatorCorrelationStrengthBeta;
1174 double discrepancyPrecisionShape = this->m_opts->m_discrepancyPrecisionShape;
1175 double discrepancyPrecisionScale = this->m_opts->m_discrepancyPrecisionScale;
1176 double discrepancyCorrelationStrengthAlpha = this->m_opts->m_discrepancyCorrelationStrengthAlpha;
1177 double discrepancyCorrelationStrengthBeta = this->m_opts->m_discrepancyCorrelationStrengthBeta;
1178 double emulatorDataPrecisionShape = this->m_opts->m_emulatorDataPrecisionShape;
1179 double emulatorDataPrecisionScale = this->m_opts->m_emulatorDataPrecisionScale;
1181 this->oneDSpace.reset
1185 if (this->num_svd_terms < numOutputs)
1187 this->truncationErrorPrecisionMin.reset(
new V(this->oneDSpace->zeroVector()));
1188 this->truncationErrorPrecisionMax.reset(
new V(this->oneDSpace->zeroVector()));
1189 this->truncationErrorPrecisionMin->cwSet(-INFINITY);
1190 this->truncationErrorPrecisionMax->cwSet(INFINITY);
1192 this->truncationErrorPrecisionDomain.reset
1196 *(this->truncationErrorPrecisionMin),
1197 *(this->truncationErrorPrecisionMax)));
1199 this->m_truncationErrorPrecision.reset
1202 *(this->truncationErrorPrecisionDomain)));
1206 this->emulatorPrecisionSpace.reset
1210 num_svd_terms + (numOutputs > 1),
1213 this->emulatorPrecisionMin.reset
1214 (
new V(this->emulatorPrecisionSpace->zeroVector()));
1215 this->emulatorPrecisionMax.reset
1216 (
new V(this->emulatorPrecisionSpace->zeroVector()));
1217 this->m_emulatorPrecisionShapeVec.reset
1218 (
new V(this->emulatorPrecisionSpace->zeroVector()));
1219 this->m_emulatorPrecisionScaleVec.reset
1220 (
new V(this->emulatorPrecisionSpace->zeroVector()));
1221 this->emulatorPrecisionMin->cwSet(0.3);
1222 this->emulatorPrecisionMax->cwSet(INFINITY);
1223 this->m_emulatorPrecisionShapeVec->cwSet(emulatorPrecisionShape);
1224 this->m_emulatorPrecisionScaleVec->cwSet(emulatorPrecisionScale);
1226 this->emulatorPrecisionDomain.reset
1229 *(this->emulatorPrecisionSpace),
1230 *(this->emulatorPrecisionMin),
1231 *(this->emulatorPrecisionMax)));
1233 this->m_emulatorPrecision.reset
1236 *(this->emulatorPrecisionDomain),
1237 *(this->m_emulatorPrecisionShapeVec),
1238 *(this->m_emulatorPrecisionScaleVec)));
1241 unsigned int dimScenario = (this->scenarioSpace()).dimLocal();
1242 unsigned int dimParameter = (this->parameterSpace()).dimLocal();
1243 this->emulatorCorrelationSpace.reset
1247 dimScenario + dimParameter,
1250 this->emulatorCorrelationMin.reset
1251 (
new V(this->emulatorCorrelationSpace->zeroVector()));
1252 this->emulatorCorrelationMax.reset
1253 (
new V(this->emulatorCorrelationSpace->zeroVector()));
1254 this->m_emulatorCorrelationStrengthAlphaVec.reset
1255 (
new V(this->emulatorCorrelationSpace->zeroVector()));
1256 this->m_emulatorCorrelationStrengthBetaVec.reset
1257 (
new V(this->emulatorCorrelationSpace->zeroVector()));
1258 this->emulatorCorrelationMin->cwSet(0);
1259 this->emulatorCorrelationMax->cwSet(1);
1260 this->m_emulatorCorrelationStrengthAlphaVec->cwSet(emulatorCorrelationStrengthAlpha);
1261 this->m_emulatorCorrelationStrengthBetaVec->cwSet(emulatorCorrelationStrengthBeta);
1263 this->emulatorCorrelationDomain.reset
1266 *(this->emulatorCorrelationSpace),
1267 *(this->emulatorCorrelationMin),
1268 *(this->emulatorCorrelationMax)));
1270 this->m_emulatorCorrelationStrength.reset
1273 *(this->emulatorCorrelationDomain),
1274 *(this->m_emulatorCorrelationStrengthAlphaVec),
1275 *(this->m_emulatorCorrelationStrengthBetaVec)));
1278 this->observationalPrecisionSpace.reset
1285 this->observationalPrecisionMin.reset
1286 (
new V(this->observationalPrecisionSpace->zeroVector()));
1287 this->observationalPrecisionMax.reset
1288 (
new V(this->observationalPrecisionSpace->zeroVector()));
1289 this->m_observationalPrecisionShapeVec.reset
1290 (
new V(this->observationalPrecisionSpace->zeroVector()));
1291 this->m_observationalPrecisionScaleVec.reset
1292 (
new V(this->observationalPrecisionSpace->zeroVector()));
1293 this->observationalPrecisionMin->cwSet(0.3);
1294 this->observationalPrecisionMax->cwSet(INFINITY);
1295 this->m_observationalPrecisionShapeVec->cwSet
1296 (this->m_opts->m_observationalPrecisionShape);
1297 this->m_observationalPrecisionScaleVec->cwSet
1298 (this->m_opts->m_observationalPrecisionScale);
1300 this->observationalPrecisionDomain.reset
1303 *(this->observationalPrecisionSpace),
1304 *(this->observationalPrecisionMin),
1305 *(this->observationalPrecisionMax)));
1307 this->m_observationalPrecision.reset
1310 *(this->observationalPrecisionDomain),
1311 *(this->m_observationalPrecisionShapeVec),
1312 *(this->m_observationalPrecisionScaleVec)));
1315 this->discrepancyPrecisionMin.reset
1316 (
new V(this->oneDSpace->zeroVector()));
1317 this->discrepancyPrecisionMax.reset
1318 (
new V(this->oneDSpace->zeroVector()));
1319 this->m_discrepancyPrecisionShapeVec.reset
1320 (
new V(this->oneDSpace->zeroVector()));
1321 this->m_discrepancyPrecisionScaleVec.reset
1322 (
new V(this->oneDSpace->zeroVector()));
1323 this->discrepancyPrecisionMin->cwSet(0);
1324 this->discrepancyPrecisionMax->cwSet(INFINITY);
1325 this->m_discrepancyPrecisionShapeVec->cwSet(discrepancyPrecisionShape);
1326 this->m_discrepancyPrecisionScaleVec->cwSet(discrepancyPrecisionScale);
1328 this->discrepancyPrecisionDomain.reset
1332 *(this->discrepancyPrecisionMin),
1333 *(this->discrepancyPrecisionMax)));
1335 this->m_discrepancyPrecision.reset
1338 *(this->discrepancyPrecisionDomain),
1339 *(this->m_discrepancyPrecisionShapeVec),
1340 *(this->m_discrepancyPrecisionScaleVec)));
1343 this->discrepancyCorrelationSpace.reset
1350 this->discrepancyCorrelationMin.reset
1351 (
new V(this->discrepancyCorrelationSpace->zeroVector()));
1352 this->discrepancyCorrelationMax.reset
1353 (
new V(this->discrepancyCorrelationSpace->zeroVector()));
1354 this->m_discrepancyCorrelationStrengthAlphaVec.reset
1355 (
new V(this->discrepancyCorrelationSpace->zeroVector()));
1356 this->m_discrepancyCorrelationStrengthBetaVec.reset
1357 (
new V(this->discrepancyCorrelationSpace->zeroVector()));
1358 this->discrepancyCorrelationMin->cwSet(0);
1359 this->discrepancyCorrelationMax->cwSet(1);
1360 this->m_discrepancyCorrelationStrengthAlphaVec->cwSet(discrepancyCorrelationStrengthAlpha);
1361 this->m_discrepancyCorrelationStrengthBetaVec->cwSet(discrepancyCorrelationStrengthBeta);
1363 this->discrepancyCorrelationDomain.reset
1366 *(this->discrepancyCorrelationSpace),
1367 *(this->discrepancyCorrelationMin),
1368 *(this->discrepancyCorrelationMax)));
1370 this->m_discrepancyCorrelationStrength.reset
1373 *(this->discrepancyCorrelationDomain),
1374 *(this->m_discrepancyCorrelationStrengthAlphaVec),
1375 *(this->m_discrepancyCorrelationStrengthBetaVec)));
1378 this->emulatorDataPrecisionMin.reset
1379 (
new V(this->oneDSpace->zeroVector()));
1380 this->emulatorDataPrecisionMax.reset
1381 (
new V(this->oneDSpace->zeroVector()));
1382 this->m_emulatorDataPrecisionShapeVec.reset
1383 (
new V(this->oneDSpace->zeroVector()));
1384 this->m_emulatorDataPrecisionScaleVec.reset
1385 (
new V(this->oneDSpace->zeroVector()));
1386 this->emulatorDataPrecisionMin->cwSet(60.0);
1387 this->emulatorDataPrecisionMax->cwSet(1e5);
1388 this->m_emulatorDataPrecisionShapeVec->cwSet(emulatorDataPrecisionShape);
1389 this->m_emulatorDataPrecisionScaleVec->cwSet(emulatorDataPrecisionScale);
1391 this->emulatorDataPrecisionDomain.reset
1395 *(this->emulatorDataPrecisionMin),
1396 *(this->emulatorDataPrecisionMax)));
1398 this->m_emulatorDataPrecision.reset
1401 *(this->emulatorDataPrecisionDomain),
1402 *(this->m_emulatorDataPrecisionShapeVec),
1403 *(this->m_emulatorDataPrecisionScaleVec)));
1406 unsigned int dimSum = 2 +
1407 (this->num_svd_terms < numOutputs) +
1409 this->m_opts->m_calibrateObservationalPrecision +
1416 this->totalSpace.reset
1422 this->totalMins.reset(
new V(this->totalSpace->zeroVector()));
1423 this->totalMaxs.reset(
new V(this->totalSpace->zeroVector()));
1426 this->totalMins->cwSet(0);
1427 this->totalMaxs->cwSet(1);
1430 (*(this->totalMins))[dimParameter] = 0.3;
1432 (*(this->totalMaxs))[dimParameter] = INFINITY;
1435 for (
unsigned int basis = 0; basis != num_svd_terms; ++basis)
1438 (*(this->totalMins))[dimParameter+1+basis] = 0.3;
1440 (*(this->totalMaxs))[dimParameter+1+basis] = INFINITY;
1445 (*(this->totalMins))[dimParameter+(numOutputs>1)+num_svd_terms+dimScenario+dimParameter] = 0;
1447 (*(this->totalMaxs))[dimParameter+(numOutputs>1)+num_svd_terms+dimScenario+dimParameter] = INFINITY;
1449 const int emulator_data_precision_index =
1450 dimSum - 1 - this->m_opts->m_calibrateObservationalPrecision;
1451 (*(this->totalMins))[emulator_data_precision_index] = 60.0;
1452 (*(this->totalMaxs))[emulator_data_precision_index] = 1e5;
1454 if (this->m_opts->m_calibrateObservationalPrecision) {
1455 (*(this->totalMins))[dimSum-1] = 0.3;
1456 (*(this->totalMaxs))[dimSum-1] = INFINITY;
1459 this->totalDomain.reset
1462 *(this->totalSpace),
1464 *(this->totalMaxs)));
1466 this->priors.push_back(&(this->m_parameterPrior));
1468 if (this->num_svd_terms < numOutputs)
1469 this->priors.push_back(this->m_truncationErrorPrecision.get());
1471 this->priors.push_back(this->m_emulatorPrecision.get());
1472 this->priors.push_back(this->m_emulatorCorrelationStrength.get());
1473 this->priors.push_back(this->m_discrepancyPrecision.get());
1474 this->priors.push_back(this->m_discrepancyCorrelationStrength.get());
1475 this->priors.push_back(this->m_emulatorDataPrecision.get());
1476 if (this->m_opts->m_calibrateObservationalPrecision)
1477 this->priors.push_back(this->m_observationalPrecision.get());
1480 this->m_totalPrior.reset
1484 *(this->totalDomain)));
std::shared_ptr< T > Type
unsigned int dimGlobal() const
A templated (base) class for handling scalar functions.
std::vector< typename SharedPtr< M >::Type > m_observationErrorMatrices
const GPMSAOptions & options() const
Return GPMSAOptions structure.
A class for partitioning vectors and matrices.
std::vector< typename SharedPtr< V >::Type > m_discrepancyBases
unsigned int numSimulations() const
Return number of simulations.
const V & simulationParameter(unsigned int simulationId) const
Return the point in parameterSpace for simulation simulationId.
A class representing a vector space.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
const BaseEnvironment & m_env
M & getObservationErrorCovariance(unsigned int simulationNumber)
const BaseEnvironment & env() const
Return the QUESO environment.
const VectorSpace< V, M > & parameterSpace() const
Return the vector space in which parameters live.
A class representing a uniform vector RV.
const VectorSpace< V, M > & simulationOutputSpace() const
Return the vector space in which simulations live.
const GPMSAEmulator< V, M > & getGPMSAEmulator() const
Return the GPMSAEmulator likelihood object.
const VectorSpace< V, M > & experimentOutputSpace() const
Return the vector space in which experiments live.
const std::vector< typename SharedPtr< V >::Type > & experimentOutputs() const
Return all points in experimentOutputSpace for all experiments.
std::string optionsInputFileName() const
Access to the attribute m_optionsInputFileName, which stores the name of the input file passed by the...
A class representing a vector RV constructed via Beta distribution.
virtual double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Logarithm of the value of the scalar function. Deprecated.
int m_maxEmulatorBasisVectors
The maximum number of basis vectors to use for approximating.
A templated class for handling sets.
unsigned int numExperiments() const
Return number of experiments.
const std::vector< typename SharedPtr< V >::Type > & simulationOutputs() const
Return all points in simulationOutputSpace for all simulations.
double scalarProduct(const GslVector &x, const GslVector &y)
unsigned int num_svd_terms
A class representing concatenated vector RVs.
const V & experimentOutput(unsigned int experimentId) const
Return the experiment output for experiment experimentId.
void addSimulation(typename SharedPtr< V >::Type simulationScenario, typename SharedPtr< V >::Type simulationParameter, typename SharedPtr< V >::Type simulationOutput)
Add a simulation to this.
~GPMSAFactory()
Destructor.
Class representing a subset of a vector space shaped like a hypercube.
const GPMSAOptions & m_opts
void print(std::ostream &os) const
void addExperiments(const std::vector< typename SharedPtr< V >::Type > &experimentScenarios, const std::vector< typename SharedPtr< V >::Type > &experimentOutputs, const typename SharedPtr< M >::Type experimentErrors)
Add all experiments to this.
GPMSAEmulator(const VectorSet< V, M > &domain, const VectorSpace< V, M > &m_scenarioSpace, const VectorSpace< V, M > &m_parameterSpace, const VectorSpace< V, M > &m_simulationOutputSpace, const VectorSpace< V, M > &m_experimentOutputSpace, const unsigned int m_numSimulations, const unsigned int m_numExperiments, const std::vector< typename SharedPtr< V >::Type > &m_simulationScenarios, const std::vector< typename SharedPtr< V >::Type > &m_simulationParameters, const std::vector< typename SharedPtr< V >::Type > &m_simulationOutputs, const std::vector< typename SharedPtr< V >::Type > &m_experimentScenarios, const std::vector< typename SharedPtr< V >::Type > &m_experimentOutputs, const std::vector< typename SharedPtr< V >::Type > &m_discrepancyBases, const std::vector< typename SharedPtr< M >::Type > &m_observationErrorMatrices, const typename SharedPtr< M >::Type &m_observationErrorMatrix, const ConcatenatedVectorRV< V, M > &m_totalPrior, const V &residual_in, const M &BT_Wy_B_inv_in, const M &KT_K_inv_in, const GPMSAOptions &opts)
virtual double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Actual value of the scalar function.
GPMSAFactory(const BaseEnvironment &env, GPMSAOptions *opts, const BaseVectorRV< V, M > ¶meterPrior, const VectorSpace< V, M > &scenarioSpace, const VectorSpace< V, M > ¶meterSpace, const VectorSpace< V, M > &simulationOutputSpace, const VectorSpace< V, M > &experimentOutputSpace, unsigned int numSimulations, unsigned int numExperiments)
Constructor.
const ConcatenatedVectorRV< V, M > & prior() const
unsigned int dimLocal() const
void addSimulations(const std::vector< typename SharedPtr< V >::Type > &simulationScenarios, const std::vector< typename SharedPtr< V >::Type > &simulationParameters, const std::vector< typename SharedPtr< V >::Type > &simulationOutputs)
Adds multiple simulations to this.
A templated base class for handling vector RV.
const Map & map() const
Map.
void setDiscrepancyBases(const std::vector< typename SharedPtr< V >::Type > &discrepancyBases)
Add all discrepancy bases to this.
The QUESO MPI Communicator Class.
const std::vector< typename SharedPtr< V >::Type > & experimentScenarios() const
Return all points in scenarioSpace for all experiments.
const std::vector< typename SharedPtr< V >::Type > & m_simulationOutputs
A class representing a vector RV constructed via Gamma distribution.
const std::vector< typename SharedPtr< V >::Type > & simulationScenarios() const
Return all points in scenarioSpace for all simulations.
const std::vector< typename SharedPtr< V >::Type > & simulationParameters() const
Return all points in parameterSpace for all simulations.
const MpiComm & Comm() const
Access function for MpiComm communicator.
const VectorSpace< V, M > & scenarioSpace() const
Return the vector space in which scenarios live.
const V & simulationOutput(unsigned int simulationId) const
Return the simulation output for simulation simulationId.
const V & experimentScenario(unsigned int experimentId) const
Return the point in scenarioSpace for experiment experimentId.
This class defines the options that specify the behaviour of the Gaussian process emulator...
const VectorSpace< V, M > & m_experimentOutputSpace
const V & simulationScenario(unsigned int simulationId) const
Return the point in scenarioSpace for simulation simulationId.
RawType_MPI_Comm Comm() const
Extract MPI Communicator from a MpiComm object.