# PCE Validation set and error

**URL:** <https://uqworld.org/t/pce-validation-set-and-error/3673>\
**Category:** UQ Discussion Forum\
**Tags:** sensitivity, pce, metamodeling\
**Created:** [May 30, 2024, 12:16pm UTC](https://uqworld.org/t/pce-validation-set-and-error/3673 "2024-05-30T12:16:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![saikausik](https://uqworld.org/letter_avatar_proxy/v4/letter/s/85f322/32.png) [@saikausik](https://uqworld.org/u/saikausik)\
**Post date:** [May 30, 2024, 12:16pm UTC](https://uqworld.org/t/pce-validation-set-and-error/3673/1 "2024-05-30T12:16:02Z")

</div>

Hello,

I have been using the UQLab for performing sensitivity analysis on my models and I have recently started using PCE. I have an analytical model and I do not have experimental data, so I have been generating samples based on the approximated distributions and sampling techniques.

I understand that there are two types of errors in PCE, the leave-out-error and the validation error, and for the validation error you need a validation set XVal like in the [PCE Truss example](https://www.uqlab.com/pce-truss-data-set). However, I am not able to figure out how to use the generated samples and use send some of it into validation set. Is there some options that I have to include? Can I use uq\_getSample or uq\_enrichSobol as define that as my XVal?

Also, is there a possibility to calculate the R^2 error/coefficient as well?

I have included the code I use for PCE using the UQLab! Kindly help! 🙂

```matlab
%% SENSITIVITY: MULTIPLE OUTPUTS

%% 1 - INITIALIZE UQLAB
%
% Clear all variables from the workspace, set the random number generator
% for reproducible results, and initialize the UQLab framework:
clearvars
rng(100, 'twister')
ops = 'winhome';
if strcmp(ops, 'mac')
addpath '/Users/shadowfangs/Library/CloudStorage/OneDrive-KTH/PhD/Research/Codes/GSA/SensitivityAnalysisECO2Lab/Construct surrogates/PCE/UQLab_Rel2.0.0/core'
elseif strcmp(ops, 'server')
addpath 'C:\Users\SaKa\OneDrive - KTH\PhD\Research\Codes\Induction motor\UQLab_Rel2.0.0\core'
addpath 'C:\Users\SaKa\OneDrive - KTH\PhD\Research\Codes\Induction motor'
else 
addpath 'C:\Users\Saika\OneDrive - KTH\PhD\Research\Codes\Induction motor\UQLab_Rel2.0.0\core'
addpath 'C:\Users\Saika\OneDrive - KTH\PhD\Research\Codes\Induction motor'
end
uqlab

%% 2 - COMPUTATIONAL MODEL
%
tic
% Create a MODEL object from the function file:
ModelOpts.mFile = 'motormodelsobol';

myModel = uq_createModel(ModelOpts);

%% 3 - INPUT MARGINALS

%% Inputs for R_r_dash example

InputOpts.Marginals(1).Name = 'n'; % 
InputOpts.Marginals(1).Type = 'Uniform';
InputOpts.Marginals(1).Parameters = [3 12]; % (m)

InputOpts.Marginals(2).Name = 'b'; % 
InputOpts.Marginals(2).Type = 'Uniform';
InputOpts.Marginals(2).Parameters = [0.5e-2 10e-2]; % 

InputOpts.Marginals(3).Name = 'A'; % 
InputOpts.Marginals(3).Type = 'Uniform';
InputOpts.Marginals(3).Parameters = [40e3 50e3]; % (m)

InputOpts.Marginals(4).Name = 'P_{rated}'; % 
InputOpts.Marginals(4).Type = 'Uniform';
InputOpts.Marginals(4).Parameters = [2.5e5 5e5]; % (m)

InputOpts.Marginals(5).Name = 'Jr'; % 
InputOpts.Marginals(5).Type = 'Uniform';
InputOpts.Marginals(5).Parameters = [3e6 5e6]; % 

InputOpts.Marginals(6).Name = 'B_{\delta_i}'; % 
InputOpts.Marginals(6).Type = 'Uniform';
InputOpts.Marginals(6).Parameters = [0.4 0.9]; % 

InputOpts.Marginals(7).Name = 'f'; % 
InputOpts.Marginals(7).Type = 'Uniform';
InputOpts.Marginals(7).Parameters = [30 80]; % 

InputOpts.Marginals(8).Name = 'U'; % 
InputOpts.Marginals(8).Type = 'Uniform';
InputOpts.Marginals(8).Parameters = [400 800]; % 

InputOpts.Marginals(9).Name = '\eta_{eff_{in}}'; % 
InputOpts.Marginals(9).Type = 'Uniform';
InputOpts.Marginals(9).Parameters = [0.8 0.9]; % 

InputOpts.Marginals(10).Name = 'cos \phi'; % 
InputOpts.Marginals(10).Type = 'Uniform';
InputOpts.Marginals(10).Parameters = [0.6 0.8]; %

InputOpts.Marginals(11).Name = '\alpha_i'; %
InputOpts.Marginals(11).Type = 'Uniform';
InputOpts.Marginals(11).Parameters = [0.6 0.9]; %

InputOpts.Marginals(12).Name = 'cos \zeta'; % 
InputOpts.Marginals(12).Type = 'Uniform';
InputOpts.Marginals(12).Parameters = [0.5 0.9]; %

InputOpts.Marginals(13).Name = 'J_{ring}'; % 
InputOpts.Marginals(13).Type = 'Uniform';
InputOpts.Marginals(13).Parameters = [3.5e6 4.5e6]; %

InputOpts.Marginals(14).Name = 'h_{1r}'; % 
InputOpts.Marginals(14).Type = 'Uniform';
InputOpts.Marginals(14).Parameters = [0.3e-3 0.8e-3]; %

InputOpts.Marginals(15).Name = 'h_{2r}'; % 
InputOpts.Marginals(15).Type = 'Uniform';
InputOpts.Marginals(15).Parameters = [0.3e-3 0.8e-3];

InputOpts.Marginals(16).Name = 'n_{req}'; % 
InputOpts.Marginals(16).Type = 'Uniform';
InputOpts.Marginals(16).Parameters = [1250 2000]; %

%%
% Create an INPUT object based on the specified marginals:
myInput = uq_createInput(InputOpts);

%% 4 - SENSITIVITY ANALYSIS
%
% Sobol' indices are calculated first with a direct MC simulation 
% of the model and subsequently through post-processing of the
% coefficients of its PCE and LRA approximation.
%% 4.1 PCE-based Sobol' indices
%
% Select the metamodeling tool in UQLab
% and the polynomial chaos expansion (PCE) type:
PCEOpts.Type = 'Metamodel';
PCEOpts.MetaType = 'PCE';
PCEOpts.Input = myInput;

%%
% Assign the full computational model:
PCEOpts.FullModel = myModel;
%%
% The full model is used to generate an experimental design for the PCE. 

%%
% Specify the maximum polynomial degree (*default*: sparse PCE):
PCEOpts.Degree = 7;

%%
% Specify the size of the experimental design
% (i.e., the total computational cost of constructing the metamodel):
PCEOpts.ExpDesign.NSamples = 1000;
PCEOpts.ExpDesign.Sampling = 'sobol';

%%
% Calculate the PCE:
myPCE = uq_createModel(PCEOpts);

%%
% The same options structure |SobolOpts| can be re-used
% to create a new analysis on the PCE model:
% mySobolAnalysisPCE = uq_createAnalysis(SobolOpts);

PCESobol.Type = 'Sensitivity';
PCESobol.Method = 'Sobol';
PCESobol.Sobol.Order = 3;
PCESobolAnalysis = uq_createAnalysis(PCESobol);

PCESobolAnalysis.Results

```

---

<div class="post-metadata">

**Author:** ![styfen.schaer](https://uqworld.org/user_avatar/uqworld.org/styfen.schaer/32/849_2.png) [@styfen.schaer](https://uqworld.org/u/styfen.schaer)\
**Post date:** [June 11, 2024, 1:47pm UTC](https://uqworld.org/t/pce-validation-set-and-error/3673/2 "2024-06-11T13:47:49Z")

</div>

Dear @saikausik

You can generate samples from your input object e.g. as follows:

```matlab
Xval = uq_getSample(myInput, 1e6)

```

You can then evaluate your computational model on that input data:

```matlab
Yval = uq_evalModel(myModel, Xval)

```

Then you can provide the data to the PCE model as follows

```matlab
PCEOpts.ValidationSet.X = XVal;
PCEOpts.ValidationSet.Y = YVal;

```

Let me know if that worked for you!

Best regards  
Styfen

PS: Regarding the r2 you can have a look here: [Coefficient of Determination (R-Squared) - MATLAB & Simulink - MathWorks Switzerland](https://ch.mathworks.com/help/stats/coefficient-of-determination-r-squared.html)
