# Creating new Input from Bayesian Inversion

**URL:** <https://uqworld.org/t/creating-new-input-from-bayesian-inversion/862>\
**Category:** Community Q&A and How To\
**Created:** [September 25, 2020, 10:30am UTC](https://uqworld.org/t/creating-new-input-from-bayesian-inversion/862 "2020-09-25T10:30:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisP](https://uqworld.org/user_avatar/uqworld.org/chrisp/32/646_2.png) [@ChrisP](https://uqworld.org/u/ChrisP)\
**Post date:** [September 25, 2020, 10:30am UTC](https://uqworld.org/t/creating-new-input-from-bayesian-inversion/862/1 "2020-09-25T10:30:23Z")

</div>

When performing a Bayesian Inversion, it is possible to retrieve the moments (mean and variance) of the posterior distribution in order to create a new input that will be used for other purposes, such as sensitivity analysis. However, that means that I have to manually check the posterior distribution shapes in order to decide the kind of distribution each parameter follows (gaussian, lognormal etc.)  
Is there any way to automate this proccess, by creating a custom input from the posterior distributions? The available documentation on custom distributions has not been of much help (most probably I am not familiar enough with the concepts). I hope that you can help shed light on this issue, since it can drastically decrease the time spent on a rather tedious task.

---

<div class="post-metadata">

**Author:** ![paulremo](https://uqworld.org/user_avatar/uqworld.org/paulremo/32/192_2.png) [@paulremo](https://uqworld.org/u/paulremo)\
**Post date:** [September 26, 2020, 9:12pm UTC](https://uqworld.org/t/creating-new-input-from-bayesian-inversion/862/2 "2020-09-26T21:12:54Z")

</div>

Hi @ChrisP

I suggest you have a look at my response to the following post:

> [@Looking for documentation/example of using UQLab results of Bayesian inference for forward UQ or further Bayesian inference](https://uqworld.org/t/looking-for-documentation-example-of-using-uqlab-results-of-bayesian-inference-for-forward-uq-or-further-bayesian-inference/690/2):
>
> Hi Olaf There is currently no example that explains this - but using the .Results.PostProc.PostSample is the correct starting point. There are a couple of things you can do with this sample, but first of all, you will have to transform it to a 2D array with PostSample2D = reshape(permute(PostSample3D, [2 1 3]), size(PostSample3D, 2), []).' You could then, of course, directly propagate this sample through a different forward model, but to use advanced UQ methods (or for another round of infe…

In short: There is no example that directly does this for a Bayesian problem, but the [Input User Manual](https://www.uqlab.com/input-user-manual), the [Statistical Inference Manual](https://www.uqlab.com/inference-user-manual) and especially this [Example](https://www.uqlab.com/input-inference-of-marginals-copula) are your friends.

If this solves your problem, it would be great if you could share the answer here for others to see as well.

---

<div class="post-metadata">

**Author:** ![paulremo](https://uqworld.org/user_avatar/uqworld.org/paulremo/32/192_2.png) [@paulremo](https://uqworld.org/u/paulremo)\
**Post date:** [October 5, 2020, 3:12pm UTC](https://uqworld.org/t/creating-new-input-from-bayesian-inversion/862/3 "2020-10-05T15:12:36Z")

</div>

Split this topic on October 5, 2020, 3:12pm UTC

3 posts were split to a new topic: [How many samples per variable are needed in order to accurately infer their distributions](https://uqworld.org/t/how-many-samples-per-variable-are-needed-in-order-to-accurately-infer-their-distributions/878)

---

<div class="post-metadata">

**Author:** ![M\_Groslambert](https://uqworld.org/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@M\_Groslambert](https://uqworld.org/u/M_Groslambert)\
**Post date:** [September 29, 2020, 8:27am UTC](https://uqworld.org/t/creating-new-input-from-bayesian-inversion/862/5 "2020-09-29T08:27:36Z")

</div>

Hello @ChrisP and @paulremo,

I had the same question, and thanks to the former discussion on UQlab, I sorted it out.  
I adapted the former response of Paul to my own problem, and here is a description of the case.

I have **one input** , which follows a (prior) _Lognormal_ distribution. After the Bayesian inversion (without postprocessing like burn-in or so), I have two options :

1. To get the _post_ **mean** and **standard deviation** (in the results of the Bayesian Analysis), and to create a _post_ Lognormal distribution, based on those parameters.
2. The other option is to create a _post_ input, based on the **samples of MCMC** and an **inference**.

Finally a graphical comparison is plot, based on the PDF of the two _post_ inputs to find out if the two methods leads to different _post_ distributions.

Here is the piece of code I used (after the bayesian inversion) :

```matlab
% Option 1 : Get the postprocessing results, ie mean and standard deviation
dist_post_pro.Name = 'distribution inversion postpro';
dist_post_pro.Marginals(1).Name = 'E';
dist_post_pro.Marginals(1).Type = 'LogNormal';
dist_post_pro.Marginals(1).Moments = [myBayes.Results.PostProc.Percentiles.Mean(1) sqrt(myBayes.Results.PostProc.Percentiles.Var(1))];
mydist_post_pro = uq_createInput(dist_post_pro); %the distribution based on mean and std

%Option 2 : Inference of the samples created in MCMC
PostSample3D = myBayes.Results.PostProc.PostSample ; 
PostSample2D = reshape(permute(PostSample3D, [2 1 3]), size(PostSample3D, 2), [])';
iOpts.Inference.Data = PostSample2D ; 
iOpts.Copula.Type = 'Independent';
iOpts.Inference.Criterion = 'KS';
mydist_infer = uq_createInput(iOpts);

%Creation of samples from the two post-inputs 
Xtest_bayes= uq_getSample(mydist_post_pro , 10000, 'LHS');
Xtest_inf= uq_getSample(mydist_infer , 10000, 'LHS');

% Comparison with a Normalized histogram (a kind of PDF) 
histogram(Xtest_bayes, 'Normalization','probability')
hold on 
histogram(Xtest_inf,'Normalization','probability')

```

I decided not to use the inference after the comparison, because the results were almost the same with the postproc, but I think it is _highly problem dependent_.

Let me know if you have further questions or remarks on the code I provided 😀

---

<div class="post-metadata">

**Author:** ![paulremo](https://uqworld.org/user_avatar/uqworld.org/paulremo/32/192_2.png) [@paulremo](https://uqworld.org/u/paulremo)\
**Post date:** [October 4, 2021, 12:02pm UTC](https://uqworld.org/t/creating-new-input-from-bayesian-inversion/862/6 "2021-10-04T12:02:46Z")

</div>

Closed on October 4, 2021, 12:02pm UTC


