Bayesian inference: error when fields of prior are different from field of discrepancy options

Hi all,

I get an error in UQLab when the number of fields of the prior distribution do not match with the number of fields of the prior for the discrepancy options.
Here’s a short working example:


%% model
Model.mString = 'X(:,1)';
myModel = uq_createModel(Model);

%% prior
PriorOpts.Marginals(1).Type = 'Gaussian';
PriorOpts.Marginals(1).Parameters = [0 1];
PriorOpts.Marginals(1).Bounds = [-3 3];
myPrior = uq_createInput(PriorOpts);

%% data 
myData(1).y = [0; 1; 2];
myData(1).MOMap = 1;

%% likelihood and hyperparameters
DiscrepancyPriorOpts1.Name = 'Prior of sigma 1';
DiscrepancyPriorOpts1.Marginals(1).Name = 'Sigma1';
DiscrepancyPriorOpts1.Marginals(1).Type = 'Uniform';
DiscrepancyPriorOpts1.Marginals(1).Parameters = [0 2];
DiscrepancyPrior1 = uq_createInput(DiscrepancyPriorOpts1);

DiscrepancyOpts(1).Type = 'Gaussian';
DiscrepancyOpts(1).Prior = DiscrepancyPrior1;

%% Bayes
BayesOpts.Prior = myPrior;                  % Prior
BayesOpts.Data  = myData;                     % Measurement data
BayesOpts.Type  = 'Inversion';              % Calibration
BayesOpts.Discrepancy = DiscrepancyOpts;    % Likelihood
BayesianAnalysis = uq_createAnalysis(BayesOpts);

The resulting error is:

Number of fields in structure arrays being concatenated do not match. Concatenation
of structure arrays requires that these arrays have the same set of fields.

When I comment the line
PriorOpts.Marginals(1).Bounds = [-3 3];
everything works as expected.
It seems that in uq_mergeInputs it is expected that the Marginals for PriorOpts and DiscrepancyOpts have the same number of fields. Why is this? I would expect it is possible to have a truncated Gaussian for the prior, while I have a regular Gaussian for the hyperparameters?
Is there a fix or workaround for this? Or am I simply overlooking something here?



Hi @bsanderse

This looks like a bug inside the uq_mergeInputs function of the Input module. The two random variables you defined cannot be merged because the first one (myPrior) has a .Bounds field, while the second one does not. As a quick fix, you can just assign meaningless bounds to the discrepancy prior like:

DiscrepancyPriorOpts1.Marginals(1).Bounds= [0 2];

We will fix this bug in the upcoming version of UQLab.

Thanks for reporting this issue!

Thanks @paulremo !
I’ll go ahead and to that. In my case, I actually added some more fields to the prior marginals for internal use in my code, so I will have to see if I either delete them or add them also to the discrepancy options.