# Polynomial chaos expansions

**URL:** <https://uqworld.org/t/polynomial-chaos-expansions/779>\
**Category:** Community Q&A and How To\
**Created:** [July 30, 2020, 2:48pm UTC](https://uqworld.org/t/polynomial-chaos-expansions/779 "2020-07-30T14:48:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sstore23](https://uqworld.org/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@sstore23](https://uqworld.org/u/sstore23)\
**Post date:** [July 30, 2020, 2:48pm UTC](https://uqworld.org/t/polynomial-chaos-expansions/779/1 "2020-07-30T14:48:18Z")

</div>

Hey everyone!

I am new to this subject so I would really appreciate if anyone could help me 🙂 I study civil engineering and I am currently working on my master thesis, where I wanted to implement PCE. However even though I followed the examples and read the user manual on PCE, i still can’t seem to get a pretty decent prediction based on my data. I just want to make sure that the problem is the data and not the code. Since I am a new user, I can’t upload attachments.

```
clearvars

```

uqlab

load X.mat  
load Y.mat

X=X\_ED(1:639,:);  
Xval=X\_ED(640:end,:);  
Y=Y\_ED(1:639);  
Yval=Y\_ED(640:end,:);

[R,C]=size(X);

for i=1:C  
InputOpts.Marginals(i).Name = sprintf(‘X%d’,i);  
InputOpts.Marginals(i).Type = ‘Uniform’;  
InputOpts.Marginals(i).Moments = [mean(X(:,i)) std(X(:,i))];  
end

myInput = uq\_createInput(InputOpts);

MetaOpts.Type = ‘Metamodel’;  
MetaOpts.MetaType = ‘PCE’;  
MetaOpts.ExpDesign.X = X;  
MetaOpts.ExpDesign.Y = Y;  
MetaOpts.Degree = 1:5;  
MetaOpts.ValidationSet.X = Xval;  
MetaOpts.ValidationSet.Y = Yval;

myPCE = uq\_createModel(MetaOpts);

uq\_print(myPCE)

%validation  
YPCE = uq\_evalModel(myPCE,Xval);

![imgonline-com-ua-twotoone-6aIzanLlzQJ4ZxKb](https://uqworld.org/uploads/default/original/1X/dc4fa0f10845fdac7e4df9df0f17d9a8032a56bd.jpeg)  
these pictures show the difference between my pce and polynomial regression models

Thank you so much, I really appreciate anyones help 🙂

---

<div class="post-metadata">

**Author:** ![ali](https://uqworld.org/user_avatar/uqworld.org/ali/32/487_2.png) [@ali](https://uqworld.org/u/ali)\
**Post date:** [July 30, 2020, 3:02pm UTC](https://uqworld.org/t/polynomial-chaos-expansions/779/2 "2020-07-30T15:02:16Z")

</div>

Dear @sstore23

Welcome to UQLab!  
Could you please report the LOO error?  
You can find a fruitful discussion in [Acceptable LOO Error](https://uqworld.org/t/acceptable-loo-error/681) to learn more about this magic test.  
Besides, try a bigger adaptive degree range by using:

> MetaOpts.Degree = 1:30;

and q-norm-Adaptive PCE by:

> MetaOpts.TruncOptions.qNorm = 0.5:0.1:1;

I hope this helps  
Ali

---

<div class="post-metadata">

**Author:** ![sstore23](https://uqworld.org/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@sstore23](https://uqworld.org/u/sstore23)\
**Post date:** [July 30, 2020, 3:55pm UTC](https://uqworld.org/t/polynomial-chaos-expansions/779/3 "2020-07-30T15:55:54Z")

</div>

> ------------ Polynomial chaos output ------------  
> Number of input variables: 2  
> Maximal degree: 5  
> q-norm: 1.00  
> Size of full basis: 21  
> Size of sparse basis: 10  
> Full model evaluations: 639  
> Leave-one-out error: 3.5708562e-02  
> Validation error: 1.0688788e+00  
> Mean value: 48.2893  
> Standard deviation: 1.2510  
> Coef. of variation: 2.591%  
> --------------------------------------------------

and after i changed the degree and the q-norm

> ------------ Polynomial chaos output ------------  
> Number of input variables: 2  
> Maximal degree: 9  
> q-norm: 0.70  
> Size of full basis: 31  
> Size of sparse basis: 11  
> Full model evaluations: 639  
> Leave-one-out error: 3.3236391e-02  
> Validation error: 1.1930977e+00  
> Mean value: 48.2682  
> Standard deviation: 1.3155  
> Coef. of variation: 2.725%  
> --------------------------------------------------

my data doesn’t exactly fit any distribution, so it might be that. I was just confused why i was getting a better approximation with the polynomial regression method

---

<div class="post-metadata">

**Author:** ![ali](https://uqworld.org/user_avatar/uqworld.org/ali/32/487_2.png) [@ali](https://uqworld.org/u/ali)\
**Post date:** [July 30, 2020, 8:18pm UTC](https://uqworld.org/t/polynomial-chaos-expansions/779/4 "2020-07-30T20:18:15Z")

</div>

Dear @sstore23

In order to address this problem, please follow my suggestions:

Due to the validation error and the YY plot, your analysis suffers from lack of accuracy. Based on Sec 2.7 in UserManual\_PCE. I cant understand the following codes:

> [@sstore23](#):
>
> for i=1:C  
> InputOpts.Marginals(i).Name = sprintf(‘X%d’,i);  
> InputOpts.Marginals(i).Type = ‘Uniform’;  
> InputOpts.Marginals(i).Moments = [mean(X(:,i)) std(X(:,i))];  
> end

It seems you assume inappropriate marginals. Please change your code based on Sec 2.7 and report the changes.

I hope this helps  
Ali

---

<div class="post-metadata">

**Author:** ![nluethen](https://uqworld.org/user_avatar/uqworld.org/nluethen/32/218_2.png) [@nluethen](https://uqworld.org/u/nluethen)\
**Post date:** [August 3, 2020, 11:27am UTC](https://uqworld.org/t/polynomial-chaos-expansions/779/5 "2020-08-03T11:27:58Z")

</div>

Hi @sstore23,

In addition to Ali’s suggestions, I would suggest the following:

- It is unusual that the LOO error is so small, and then the validation error is larger than 1. There might be an issue with your validation set. How large is it? Are the training set and the validation set sampled from the same distribution? Your data is two-dimensional, so it is easy to visualize: Plot your training data (`X`) and the validation data (`Xval`) in the same figure and confirm visually that they follow the same distribution. How does the distribution look like? Is there maybe dependence in the X-data?
- Make a [scatter plot of your data and the corresponding model output](https://de.mathworks.com/help/matlab/ref/scatter.html#btrli6o-1_1) by using the Matlab command `scatter` with filled circles that are colored according to the output value. Does the color vary smoothly over the input domain, or are there jumps in the data (or outliers)? Polynomial approximation works best when the model is smooth and varying slowly.
- Is your model [deterministic - or maybe stochastic](https://uqworld.org/t/stochastic-simulators-an-overview/158)?
- You mention

> [@sstore23](#):
>
> these pictures show the difference between my pce and polynomial regression models

Note that regression-based PCE _is_ a form of polynomial regression, just with a special polynomial family, and in the case of UQLab, by default using a sparsity constraint (LARS). The pictures that you show (the left one from PCE, the right one from “polynomial regression”?) use a very different set of points, which might explain the difference.

Good luck! 🙂

---

<div class="post-metadata">

**Author:** ![Aep93](https://uqworld.org/user_avatar/uqworld.org/aep93/32/507_2.png) [@Aep93](https://uqworld.org/u/Aep93)\
**Post date:** [August 4, 2020, 4:27pm UTC](https://uqworld.org/t/polynomial-chaos-expansions/779/6 "2020-08-04T16:27:22Z")

</div>

Hello. Sorry I have a question regarding this topic:  
What are the differences between "Leave-one-out error " and “Validation error”?  
I am able to find "leave-one-out error " from my PCE model, however, how can I find “Validation error”?  
Thanks

---

<div class="post-metadata">

**Author:** ![ali](https://uqworld.org/user_avatar/uqworld.org/ali/32/487_2.png) [@ali](https://uqworld.org/u/ali)\
**Post date:** [August 8, 2020, 6:43am UTC](https://uqworld.org/t/polynomial-chaos-expansions/779/7 "2020-08-08T06:43:55Z")

</div>

Dear @Aep93

Thanks for your question!  
The LOO error is discussed comprehensively in the mentioned question,[Acceptable LOO Error](https://uqworld.org/t/acceptable-loo-error/681).  
The main reasons to apply this magic tool are:  
1- Overcoming the problem of “overfitting”  
2- Scare validation set due to the high computational cost of the real model (e.g. Nonlinear FEM)  
As [UserManual\_PCE](https://www.uqlab.com/pce-user-manual) discussed, its important to use **independent** validation set:  
 ![PCE](https://uqworld.org/uploads/default/original/1X/595a44af6e4015c42bf5bc87dc6d2612de96d6fe.jpeg)  
About your question:  
If you run your metamodels in UQLab, the _LOO error_ is automatically calculated but for the “_Validation Error_” you **must have dataset**!  
In this context, you could run examples from your UQ\_Example such as:

- [PCE Truss Example](https://www.uqlab.com/pce-truss-data-set)
- [Kriging Truss Example](https://www.uqlab.com/kriging-truss-data-set)
- [PC-Kriging Truss Example](https://www.uqlab.com/pc-kriging-truss-data-set)

I hope this helps but please let me know if you have any other questions.  
Best regards  
Ali
