UQTk: Uncertainty Quantification Toolkit 3.1.5
dfi.h
Go to the documentation of this file.
1/* =====================================================================================
2
3 The UQ Toolkit (UQTk) version 3.1.5
4 Copyright (2024) NTESS
5 https://www.sandia.gov/UQToolkit/
6 https://github.com/sandialabs/UQTk
7
8 Copyright 2024 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
9 Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10 retains certain rights in this software.
11
12 This file is part of The UQ Toolkit (UQTk)
13
14 UQTk is open source software: you can redistribute it and/or modify
15 it under the terms of BSD 3-Clause License
16
17 UQTk is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 BSD 3 Clause License for more details.
21
22 You should have received a copy of the BSD 3 Clause License
23 along with UQTk. If not, see https://choosealicense.com/licenses/bsd-3-clause/.
24
25 Questions? Contact the UQTk Developers at https://github.com/sandialabs/UQTk/discussions
26 Sandia National Laboratories, Livermore, CA, USA
27===================================================================================== */
28#ifndef DFI_H_
29#define DFI_H_
30
31#include "Array1D.h"
32#include "Array2D.h"
33#include "Array3D.h"
34#include "mcmc.h"
35#include "amcmc.h"
36#include "quad.h"
37#include "math.h"
38#include "arrayio.h"
39
40#include "dsfmt_add.h"
41#include <sys/time.h>
42#include "arraytools.h"
43#include <string>
44#include <iostream>
45//for setting stdout precision
46#include <iomanip>
47
48#include "sampling.hpp"
49
50//for UQTk KDE functionality
51#include "tools.h"
52
53//for PCE surrogate construction
54#ifndef PCSET_H_SEEN
55#define __wsu
56#include "PCSet.h"
57#endif //PCSET_H_SEEN
58
59#include <stdlib.h>
60#include <sstream>
61
62//data container for surrogate model
63class DFIsurr{
64
65 public:
66
67 //model surrogate containers
68
69 //PCE dimension
70 int PCEdim;
71
72 //surrogate limits
75
76 //surrogate defined flag
78
79 //PCset defines and initializes polynomial chaos basis function set
81
82 //number of PCE terms
84 //cotainer for coeefficients
86 //container for evaluated PCE basis functions
88
89 // surrogate evaulation function
90 void evaluateSurr(Array1D<double> & modelOutput, Array1D<double> & params);
91};
92
93
94
95
96//data container
98
99 public:
100
101 int seed;
102 //dimension of the data space
104 //parameter dimension
106 //the number of constraints to impose
108
109
110 //boolean flag indicating that the data likelihood function is running for noise optimzation
112 //boolean flag indicating that the the data chain has achieved burn-in
114 //counter for entry in data chain
116
117
118 //container for passing noisy data sample to 1D noise optimization chain
120
121 //containers for data signal, i.e. y=f(x)
124 //container for error signal (e.g. noise + bias)
126 //container for nominal parameters, which define the nominal true data (trueDatay)
128 //container for nominal error parameters
130 //container for additional parameters needed to run the model (but not inferred)
132 //container for parameters for surrogate data model
134
135 //ABC data likelihood containers
136 //container to store labels describing the statistics to enforce as constraints
137 vector<string> statLabels;
138 //container to store the values of the statistics to enforce as constraints
139 vector<double> statValues;
140 //container to store the values of the ABC deltas for approximately enforcing statistics as contraints
141 vector<double> statDeltas;
142
143
144 //parameter chain write options
146 //burn in chain file
148 //parameter chainfile
150
151 //length of parameter chains
154
155
156 //surrogate model
159 vector<DFIsurr> surrModels;
160
161};
162
163//data container
165
166 public:
168 //container for passing optimal error parameters to inner likelihood during error optimation
172 //the input 'x' possibly required to compute the model function y=f(x)
174
175 // pointer to surrogate model object
177
178 vector<DFIsurr> * surrModels_;
179
180};
181
182//============================================
183//user defined functions
184
185//run the true data model
186void userRunModel(Array1D<double> &modelDataY, Array1D<double> &modelDataX, Array1D<double> & parameters, Array1D<double> &hyperparameters);
187//compute parameter (truth model and error model) parameter posterior
189//compute parameter (truth model and error model) likelihood
190double userComputeParamLogLikelihood(parameterPosteriorInformation * paramPostInfo, Array1D<double> modelDataOut, Array1D<double> parameters, Array1D<double> hyperparameters);
191//compute statistics from inner parameter chain
192void userComputeStatistics(Array1D<double> &parameterStatistics, Array1D<MCMC::chainstate> & parameterChain);
193//define the target data signal
195//define the constraints
197//specify the nominal values of the parameters
199//=============================================
200
201//data log postrior function to be passed to UQTk MCMC object for data chain
202double dataInferenceLogPosterior(Array1D<double>& m, void *info);
203//parameter log posterior function to be passed to UQTk MCMC object for parameter chains
205
206//define inner parameter inference
207void parameterInference(dataPosteriorInformation *dataPostInfo, Array1D<double> &m, Array1D<MCMC::chainstate>& parameterChainEntries);
208
209//compute parameter (truth model and error model) parameter posterior
211//compute parameter (truth model and error model) likelihood
212double computeParamLogLikelihood(parameterPosteriorInformation * paramPostInfo, Array1D<double> modelDataOut, Array1D<double> parameters, Array1D<double> hyperparameters);
213//compute statistics from inner parameter chain
214void computeStatistics(Array1D<double> &parameterStatistics, Array1D<MCMC::chainstate> & parameterChain);
215
216
217
218class DFI{
219
220 private:
221 //seed
222 int seed;
223
224
225 //metadata container to pass by argument to data MCMC chain
227 //metadata container to pass by argument to parameter MCMC chain
229
230 //I/O ===============
231 //log file name
232 stringstream logFileName;
233 //log file
234 ofstream logFile;
235 //===================
236
237 //container for noisy data
239 //data scale (defines an approximate scale for the noise to be added)
240 double dataScale;
241
242
243 //length of main data chain after burn-in
245 //length of burn-in chainlets
247 //length of error model parameter optimization chain
249 //target data chain acceptance ratio (a burn-in parameter)
251 //data chain acceptance ratio
253 //data chain mode ratio
255
256 //intial data chain (Gaussian) proposal covariance
258 //scale for setting proposal covariance
260
261
262 //data chain proposal covariance matrix
264
265 //=====wrappers for user specified functions=========
266 //define the target data signal
270 //define the constraints
274 //specify the nominal values of the parameters
278 //run the true data model
279 void runModel(Array1D<double> &modelDataY, Array1D<double> &modelDataX, Array1D<double> & parameters, Array1D<double> &hyperparameters){
280
281 //if a surrogate is defined, use it
283
284 /* map parameters of interest to surrogate parameters */
285 //userSurrMap(dataPostInfo.surrParameters, parameters, hyperparameters);
286
287 /* check if parameters are within surrogate bounds */
288 bool inBounds=true;
289 for (int i=0; i<parameters.XSize();i++){
290 if ( (parameters(i) < dataPostInfo.surrModelObj.surrLo(i)) || (parameters(i) >dataPostInfo.surrModelObj.surrHi(i)) ){
291 inBounds=false;
292 std::cout<<"Out of bounds: param("<<i+1<<"<<) = "<<parameters(i)<<std::endl;
293 }
294 }
295
296 if (inBounds){
297 dataPostInfo.surrModelObj.evaluateSurr(modelDataY, parameters);
298 }else{
299 userRunModel(modelDataY, modelDataX, parameters, hyperparameters);
300 }
301
302 }else{
303 //run user specified detailed model
304 userRunModel(modelDataY, modelDataX, parameters, hyperparameters);
305 }
306
307 };
308 //===================================================
309
310
311 public:
312 //constructor
313 DFI();
314 //constructor
315 DFI(string inputfile);
316 //destructor
317 ~DFI();
318 //perform data inference
319 void dataInference();
320 //perform data refitting
321 void dataRefit();
322 //build KDE densities
323 void buildKDE(Array1D<int> KDEdim);
324 //generate samples from a pdf
325 void genSamples(Array2D<double> &pdf);
326 //build surrogate model
327 void buildSurrogateModel();
328 //load surrogate model
329 void loadSurrogateModel();
330 //test surrogate model
331 void testSurrogateModel();
332
333
334};
335
336#endif //DFI_H_
1D Array class for any type T
2D Array class for any type T
3D Array class for any type T
Header file for the Multivariate PC class.
Header file for array read/write utilities.
Header file for array tools.
Stores data of any type T in a 1D array.
Definition Array1D.h:61
int XSize() const
Returns size in the x-direction.
Definition Array1D.h:103
Stores data of any type T in a 2D array.
Definition Array2D.h:60
Definition dfi.h:218
DFI()
Definition dfi.cpp:33
ofstream logFile
Definition dfi.h:234
void buildKDE(Array1D< int > KDEdim)
Definition dfi.cpp:914
stringstream logFileName
Definition dfi.h:232
int dataChainNumSamples
Definition dfi.h:244
double dataChainPropCov_fac
Definition dfi.h:259
dataPosteriorInformation dataPostInfo
Definition dfi.h:226
void specifyNominalParams(dataPosteriorInformation &dataPostInfo)
Definition dfi.h:275
double targetDataChainAcceptanceRatio
Definition dfi.h:250
int seed
Definition dfi.h:222
Array1D< double > noisyData
Definition dfi.h:238
void genSamples(Array2D< double > &pdf)
Definition dfi.cpp:1092
void buildSurrogateModel()
Definition dfi.cpp:521
void testSurrogateModel()
Definition dfi.cpp:845
double dataChainAcceptanceRatio
Definition dfi.h:252
parameterPosteriorInformation paramPostInfo
Definition dfi.h:228
void loadSurrogateModel()
Definition dfi.cpp:698
void dataInference()
Definition dfi.cpp:208
void dataRefit()
Definition dfi.cpp:401
~DFI()
Definition dfi.cpp:200
double errorOptChainNumSamples
Definition dfi.h:248
Array2D< double > dataChainPropCovMatrix
Definition dfi.h:263
double dataScale
Definition dfi.h:240
void defineConstraints(dataPosteriorInformation &dataPostInfo)
Definition dfi.h:271
void runModel(Array1D< double > &modelDataY, Array1D< double > &modelDataX, Array1D< double > &parameters, Array1D< double > &hyperparameters)
Definition dfi.h:279
double dataPosteriorMode
Definition dfi.h:254
int dataChainNumSamples_burnin
Definition dfi.h:246
double dataChainPropCov_init
Definition dfi.h:257
void defineData(dataPosteriorInformation &dataPostInfo)
Definition dfi.h:267
Definition dfi.h:63
Array2D< double > PCEcoefficients
Definition dfi.h:85
Array1D< double > surrLo
Definition dfi.h:73
PCSet * surrModel
Definition dfi.h:80
int numPCETerms
Definition dfi.h:83
void evaluateSurr(Array1D< double > &modelOutput, Array1D< double > &params)
Definition dfi.cpp:1106
bool surrDefined
Definition dfi.h:77
Array2D< double > psiPCE
Definition dfi.h:87
Array1D< double > surrHi
Definition dfi.h:74
int PCEdim
Definition dfi.h:70
Defines and initializes PC basis function set and provides functions to manipulate PC expansions defi...
Definition PCSet.h:73
Definition dfi.h:97
vector< double > statValues
Definition dfi.h:139
bool errorOpt
Definition dfi.h:111
Array1D< double > trueDatax
Definition dfi.h:122
bool dataChainBurnedIn
Definition dfi.h:113
string burninParamWriteFile
Definition dfi.h:147
int seed
Definition dfi.h:101
int numConstraints
Definition dfi.h:107
int numSurr
Definition dfi.h:158
Array1D< double > hyperparameters
Definition dfi.h:131
int parameterBurnInNumSamples
Definition dfi.h:152
Array1D< double > error
Definition dfi.h:125
DFIsurr surrModelObj
Definition dfi.h:157
Array1D< double > nominalParameters
Definition dfi.h:127
Array1D< double > optErrorParameters
Definition dfi.h:119
vector< string > statLabels
Definition dfi.h:137
int dataDim
Definition dfi.h:103
vector< DFIsurr > surrModels
Definition dfi.h:159
Array1D< double > surrParameters
Definition dfi.h:133
int parameterChainNumSamples
Definition dfi.h:153
vector< double > statDeltas
Definition dfi.h:141
Array1D< double > trueDatay
Definition dfi.h:123
int paramWriteFlag
Definition dfi.h:145
int dataChain_count
Definition dfi.h:115
string mainParamWriteFile
Definition dfi.h:149
Array1D< double > nominalErrorParameters
Definition dfi.h:129
int paramDim
Definition dfi.h:105
Definition dfi.h:164
bool errorOpt
Definition dfi.h:171
vector< DFIsurr > * surrModels_
Definition dfi.h:178
Array1D< double > dataChainState
Definition dfi.h:167
Array1D< double > hyperparameters
Definition dfi.h:170
Array1D< double > trueDatax
Definition dfi.h:173
Array1D< double > optErrorParams
Definition dfi.h:169
DFIsurr * surrModelObj_
Definition dfi.h:176
double beta(const double z, const double w)
Compute the Beta function at the point pair (z,w)
Definition combin.cpp:218
void computeStatistics(Array1D< double > &parameterStatistics, Array1D< MCMC::chainstate > &parameterChain)
Definition dfi.cpp:1410
double dataInferenceLogPosterior(Array1D< double > &m, void *info)
Definition dfi.cpp:1149
void userComputeStatistics(Array1D< double > &parameterStatistics, Array1D< MCMC::chainstate > &parameterChain)
double parameterInferenceLogPosterior(Array1D< double > &beta, void *info)
Definition dfi.cpp:1387
double computeParamLogPosterior(parameterPosteriorInformation *paramPostInfo, Array1D< double > parameters)
Definition dfi.cpp:1402
void userDefineConstraints(dataPosteriorInformation &dataPostInfo)
double userComputeParamLogPosterior(parameterPosteriorInformation *paramPostInfo, Array1D< double > parameters)
void userDefineData(dataPosteriorInformation &dataPostInfo)
double computeParamLogLikelihood(parameterPosteriorInformation *paramPostInfo, Array1D< double > modelDataOut, Array1D< double > parameters, Array1D< double > hyperparameters)
Definition dfi.cpp:1406
void parameterInference(dataPosteriorInformation *dataPostInfo, Array1D< double > &m, Array1D< MCMC::chainstate > &parameterChainEntries)
Definition dfi.cpp:1232
double userComputeParamLogLikelihood(parameterPosteriorInformation *paramPostInfo, Array1D< double > modelDataOut, Array1D< double > parameters, Array1D< double > hyperparameters)
void userRunModel(Array1D< double > &modelDataY, Array1D< double > &modelDataX, Array1D< double > &parameters, Array1D< double > &hyperparameters)
void userSpecifyNominalParams(dataPosteriorInformation &dataPostInfo)
Header file for the Markov chain Monte Carlo class.
Header file for the quadrature class.
A header function that includes all tools.