%% SVC METAMODELLING: VARIOUS METHODS
% This Example showcases how to perform SVC metamodelling with multiple
% outputs. 

%% 1 - CLEAR THE WORKSPACE AND INITIALIZE THE UQLAB FRAMEWORK 
clearvars
uqlab
rng(1000,'twister') % fix the random seed for repeatable results

%% 2 - PROBABILISTIC INPUT MODEL
% To generate an experimental design for the SVR surrogate a random
% sample from a 2D uniform distribution is generated:
%
% $X_i \in \mathcal{U}(0, 10)$, $i = 1,2$

%%
% Specify the distribution of the input variable:Input.Marginals(1).Type = 'Uniform';
Input.Marginals(1).Type = 'Uniform';
Input.Marginals(1).Parameters = [0 10];
Input.Marginals(2).Type = 'Uniform';
Input.Marginals(2).Parameters = [0 10];

%%
% Create and add an input object to UQLab:
myInput = uq_createInput(Input);

%% 3 - COMPUTATIONAL MODEL
% The computational model is a simple analytical function defined as:
%
% $Y_{val_1}(X) = X_2 - X_1 \sin(X_1) - 1$
% $Y_1(x) = -1$ if $Y_{val_1}(X) < 0$ and $Y_1(X) = +1$ if $Y_{val_1}(X) > 0$
% $Y_{val_2}(X) = X_2 - X_1$
% $Y_2(x) = -1$ if $Y_{val_2}(X) < 0$ and $Y_2(X) = +1$ if $Y_{val_2}(X) > 0$

%%
% This model is implemented in the function uq_cmsin.m
modelopts.mFile = 'uq_test_class_multi_outputs';
myModel = uq_createModel(modelopts);

%% 4 - GENERATION OF THE EXPERIMENTAL DESIGN OPTIONS
% The experimental design and the model responses will be calculated later
% when creating the SVC metamodel.

%%
% Use LHS of size 100
metaopts.Input = myInput ;
metaopts.ExpDesign.NSamples = 100 ;
metaopts.ExpDesign.Sampling = 'LHS' ;

%% 5 - SVC METAMODEL
metaopts.Type = 'Metamodel';
metaopts.MetaType = 'SVC';

%%
% Kernel: Handle defined in a m-file written by the user
% $K = \theta(3) * \exp( |X-X'|^2/ ( 2 \theta(1))^2 ] + (1-\theta(3)) * (X  X' + \theta(2) )^5$
metaopts.Kernel.Handle = @uq_MixedSVRKernel;
%% 
% Initial parameters of the kernel
metaopts.Hyperparameters.theta = [0.5 0.5 0];

%%
% Bounds of the search space for the hyperparameters calibration
metaopts.Optim.Bounds.C = [10; 1000] ;
metaopts.Optim.Bounds.theta = [ 0.1 1e-6 1e-6; 5 10 1] ;
%%
% Create the metamodel object and add it to UQLab
mySVC = uq_createModel(metaopts) ;

%% 6 - SHOW RESULTS

%% 6.1 First output
% A report about the resulting SVC object can be produced as follows
uq_print(mySVC,1);

%%
% Similarly, a visual representation the SVC predictor can be obtained (only for 2D functions)
uq_display(mySVC,1);

%% 6.2 Second output
% A report about the resulting SVC object can be produced as follows
uq_print(mySVC,2);

%%
% Similarly, a visual representation the SVC predictor can be obtained (only for 2D functions)
uq_display(mySVC,2);
