%% Bayesian Study on my Kriging Model
clear all

%% Step 1: Start the UQLab framework
rng(100,'twister')
uqlab;

%% Step 2: Create the Model
% Just load the MYKRIGING model from KRIGING_METAMODEL you previously 
% computed
load('Kriging_MetaModel')
myForwardModel = uq_createModel(myKriging.Options);

%% Step 3: Create prior parameter info
% The model takes in 6 parameter values, each with a uniform distribution
% between a lower and upper bound
a = [150, 4, 0.0001, 0.1, 0, 0.4];
b = [200, 48, 0.5, 4, 10, 1.8];
name = {'ro2ut', 'Ic', 'KPHY', 'muZOO', 'kZOO', 'b'};
for i=1:6
    PriorOpts.Marginals(i).Name = name{i};
    PriorOpts.Marginals(i).Type = 'Uniform';
    PriorOpts.Marginals(i).Parameters = [a(i) b(i)];
end
myPriorDist = uq_createInput(PriorOpts);


%% Step 4: Create prior discrepancies info
% Define discrepancy prior
discrepancyPriorOpts.Marginals.Type = 'Uniform';
discrepancyPriorOpts.Marginals.Parameters = [0, 20];
myDiscrepancyPrior = uq_createInput(discrepancyPriorOpts);

% Set discrepancy options with newly defined prior
myDiscrepancyOptions.Type = 'Gaussian';
myDiscrepancyOptions.Prior = myDiscrepancyPrior;

%% Step 5: Data
% In your case, you would use artificial data of 0, although generally I
% would strongly recommend to use the actual measurements instead.
myData.Name = 'Target';
myData.y = 0;

%% Step 6: Solver
Solver.Type = 'MCMC';
Solver.MCMC.Sampler = 'AIES';
Solver.MCMC.NChains = 50;
Solver.MCMC.Steps = 200;

Solver.MCMC.Visualize.Parameters = 1:6;
Solver.MCMC.Visualize.Interval = 20;

%% Step 7: Run Bayes Analysis
BayesOpts.Type = 'Inversion';
BayesOpts.Data = myData;
BayesOpts.Solver = Solver;
BayesOpts.Prior = myPriorDist;
BayesOpts.Discrepancy = myDiscrepancyOptions;

myBayesianAnalysis = uq_createAnalysis(BayesOpts);
uq_print(myBayesianAnalysis)
uq_display(myBayesianAnalysis)
