**3.2 Modeling through a Bayesian approach**

Let us assume that measured data can be described by a chosen model specified by the vector Θ ¼ ð Þ *θ*1, *θ*2, ⋯, *θ<sup>m</sup>* , whose generic component *θ<sup>m</sup>* is a model parameter. For the sake of generality, we include the possibility that some Θ components, instead of being the parameter of a well-identified model, designate instead one model option among several competitive ones whose reliability is to be concurrently tested [10, 19]. The vector *y* ¼ *y*1, *y*2, ⋯, *yn* indicates instead the measured dataset with *n* being the sample size that is the number of data points. With this notation in mind, we can express the Bayes theorem [20] as follows:

$$P(\Theta|\mathbf{y}) = \frac{P(\mathbf{y}|\Theta)P(\Theta)}{P(\mathbf{y})},\tag{2}$$

where *P*ð Þ Θj*y* is the posterior distribution of the parameters built according to the experimental outcome, *P*ð Þ Θ is the prior distribution (or simply prior) of the parameters, that is, that in one's hands before any data measurement, *P y*ð Þ jΘ is the likelihood of the data, that is, the probability of observing the data conditional on a certain parameter vector, and *P y*ð Þ is the marginal probability of the data, which plays the role of normalizing constant, so that Eq. (2) has a unit integral over the variable Θ. We stress that the prior probability includes all our initial knowledge (or ignorance) and can be more or less informative depending on the preliminary insight we got on the problem at hand. Bayes' theorem is therefore a prescription on how to learn from experience, insofar as it gives a golden rule to update one's beliefs in light of the accrued data.

Now let us imagine to have achieved a portrayal of *S Q*ð Þ , *E* of a given sample from a measurement run of a certain duration *t*1. We can ideally try to fit this first rough *S Q*ð Þ , *E* . This will provide a first joint multi-dimensional posterior distribution of the parameter vector Θ, which likewise improves our knowledge of model parameters with respect to the prior we started with. It is meaningful to think about this posterior as an updated prior to feed back into the Bayes theorem, as we keep gathering new data in the experiment.

Unfortunately, the posterior distribution has no explicit analytical expression, thus being of hardly any use to feed back in the Bayes theorem again. We then measure a second run, which, with no loss of generality, can be assumed for simplicity of the same duration *t*<sup>2</sup> ¼ *t*<sup>1</sup> of the first one. New data can be certainly add to the old ones to get a new, more accurate, dataset.

Upon indicating data gathered during the first and second run, respectively, as *y* ¼ *y*1, *y*2, ⋯, *yn* and *<sup>y</sup>*<sup>0</sup> <sup>¼</sup> *<sup>y</sup>*<sup>0</sup> <sup>1</sup>, *y*<sup>0</sup> <sup>2</sup>, ⋯, *y*<sup>0</sup> *n* , we can formally express the posterior distribution of the parameters vector, conditionally on the complete collection of data, as

$$P(\Theta|\mathcal{y}, \mathcal{y}') = \frac{P(\mathcal{y}'|\Theta, \mathcal{y})P(\Theta|\mathcal{y})}{P(\mathcal{y}'|\mathcal{y})} \tag{3}$$

which is a mere formulation of the Bayes theorem. We observe how the prior we have now is just the posterior distribution for the vector parameter Θ having already observed the dataset *y*.

The datasets *y*<sup>0</sup> and *y* being independent, we have that *P y*<sup>0</sup> ð Þ¼ jΘ, *y P y*<sup>0</sup> ð Þ jΘ and *P y*<sup>0</sup> ð Þ¼ j*y P y*<sup>0</sup> ð Þ. On the other hand, we can apply again the Bayes theorem to get *P*ð Þ¼ Θj*y P y*ð Þ jΘ *P*ð Þ Θ *=P y*ð Þ. Doing the substitutions, Eq. 3 becomes:

$$P(\Theta|\mathbf{y}, \mathbf{y}') = \frac{P(\mathbf{y}'|\Theta)P(\mathbf{y}|\Theta)P(\Theta)}{P(\mathbf{y}')P(\mathbf{y})} \tag{4}$$

Once again, *y*<sup>0</sup> and *y* being independent, we have *P y*<sup>0</sup> ð Þ jΘ *P y*ð Þ¼ jΘ *P y*<sup>0</sup> ð Þ , *y*jΘ and *P y*<sup>0</sup> ð Þ*P y*ð Þ¼ *P y*<sup>0</sup> ð Þ , *y* for the property of the joint probability of independent variables. Eq. (4) becomes:

$$P(\Theta|\mathcal{y}, \mathcal{y}') = \frac{P(\mathcal{y}, \mathcal{y}'|\Theta)P(\Theta)}{P(\mathcal{y}, \mathcal{y}')} \tag{5}$$

We finally observe that the posterior probability for the vector Θ given the datasets *y*<sup>0</sup> and *y* can be obtained *via* Eq. (3) provided the posterior we derived after the first measurement is used as a new prior. This is equivalent to using as a prior the one we started with, yet multiplied for the likelihood pertinent to (inclusive of) all data collected thus far.

We can thus apply Eq. (5) in a recursive fashion to analyze on the fly neutron scattering data as we collect them. We would like to determine the most appropriate total acquisition time based on solid statistical arguments and with the prospect of inferring something about the quality of the data collected. The ultimate goal would be to have the possibility of ending the acquisition when the maximum level of information that can be obtained from the measurement has already been reached. Further prolonging the acquisition would not bring any extra relevant information. Certainly deciding when the measurement can reasonably be interrupted is at the discretion of the experimenter who may still want a specific precision from the measurement.
