Bayesian parameter reconstruction using Bayesian optimization
- Driver:
Download script: bayesian_reconstruction.m
The target of the study is to showcase the solution of a Bayesian parameter reconstruction problembased on a set ot measurements \(\mathbf{t} \in \mathbb{R}^K\).
We assume that the measurement process can be accurately modeled by a function \(\mathbf{f}(\mathbf{b}) \in \mathbb{R}^K\). As an example we consider the MGH17 problem which consists of fitting a vectorial model \(\mathbf{f}(\mathbf{b}) \in \mathbb{R}^{33}\) with
to a measurement vector with \(K = 33\) entries.
Due to random measurement noise \(\mathbf{w} \sim \mathcal{N}(0, {\rm diag}{[\eta_1^2,...,\eta_K^2]})\), the vector of measurements \(\mathbf{t} = \mathbf{f}(\mathbf{b}) + \mathbf{w}\) is a random vector with probability density
The noise variances \(\eta_i^2\) shall be unknown and estimated from the measurement data. To this end we assume that the variance is composed of a background term \(c_1^2\) and a noise contribution which scales linearly with \(f_i(\mathbf{b})\), i.e.
Taking non-uniform prior distributions for the design parameter vector \(P_\text{prior}(\mathbf{b})\) and the error model parameters \(P_\text{prior}(\mathbf{c})\) into account the posterior distribution is then proportional to \(P(\mathbf{b}, \mathbf{c} | \mathbf{t}) \propto P(\mathbf{t} | \mathbf{b}, \mathbf{c}) P_\text{prior}(\mathbf{b}) P_\text{prior}(\mathbf{c})\).
Alltogether, the target of finding the parameters with maximum posterior probability density is equivalent of minimizing the value of the negative log-probability
In the following, we show how this negative log-probability can be minimized using the BayesianReconstruction driver. We compare the results of the dirver’s MCMC sampling of the posterior probability to an MCMC sampling of the analytic value.
1client = jcmoptimizer.Client();
2
3% Definition of the search domain
4design_space = { ...
5 struct('name', 'b1', 'type', 'continuous', 'domain', [0,10]), ...
6 struct('name', 'b2', 'type', 'continuous', 'domain', [0.1,4]), ...
7 struct('name', 'b3', 'type', 'continuous', 'domain', [-4,-0.1]), ...
8 struct('name', 'b4', 'type', 'continuous', 'domain', [0.05,1]), ...
9 struct('name', 'b5', 'type', 'continuous', 'domain', [0.05,1]) ...
10};
11
12 % Creation of the study object with study_id 'bayesian_reconstruction'
13study = client.create_study( ...
14 'design_space', design_space, ...
15 'driver','BayesianReconstruction',...
16 'study_name','Bayesian parameter reconstruction using Bayesian optimization',...
17 'study_id', 'bayesian_reconstruction');
18%The vectorial model function of the MGH17 problem
19function val = model(x)
20 s = 0:32;
21 val = x(1) + x(2)*exp(-s.*x(4)) + x(3)*exp(-s.*x(5));
22end
23
24%The forward model parameters b to be reconstructed
25b_true = [3.7541005211E-01, 1.9358469127E+00, -1.4646871366E+00, ...
26 1.2867534640E-01,2.2122699662E-01];
27
28%The error model parameters in log-space to be reconstructed
29log_c1 = log(0.005);
30log_c2 = log(0.01);
31
32%The error model, i.e. the noise stddev depending on the model value y=f(b)
33function stddev = error_model(log_c1, log_c2, y)
34 stddev = sqrt( exp(log_c1)^2 + (exp(log_c2).*y).^2);
35end
36
37%Generate a rantom target vector of measurements
38rng("default");
39model_vector = model(b_true);
40err = error_model(log_c1,log_c2,model_vector);
41measurement_vector = model_vector + err.*randn(size(model_vector));
42
43%Configuration of the error model
44err_model_conf = struct();
45%error model expression
46err_model_conf.expression = 'sqrt(exp(log_c1)^2 + (exp(log_c2)*y_model)^2)';
47%distribution of error model parameters
48err_model_conf.distributions = { ...
49 struct('type', 'normal', 'parameter', 'log_c1', 'mean', -5.0, 'stddev', 1), ...
50 struct('type', 'normal', 'parameter', 'log_c2', 'mean', -4.0, 'stddev', 1), ...
51};
52%initial values and parameter bounds for fitting the error model parameters
53err_model_conf.initial_parameters = [-5.0, -4.0];
54err_model_conf.parameter_bounds = [-7,-2; -6.0,-1];
55
56%Multivariate normal prior distribution of forward model parameters.
57%Unspecified parameters (b1,b4,b5) are uniformly distributed in the design space
58distribution = struct();
59distribution.type='mvn';
60distribution.parameters = {'b2','b3'};
61distribution.mean = [2.25,-2.0];
62distribution.covariance = [0.5,-0.01;-0.01,0.5];
63parameter_distribution = struct();
64parameter_distribution.distributions = {distribution};
65
66study.configure( ...
67 'max_iter', 80, ...
68 'target_vector', measurement_vector, ...
69 'error_model', err_model_conf, ...
70 'parameter_distribution', parameter_distribution ...
71);
72% Evaluation of the black-box function for specified design parameters
73function observation = evaluate(study, sample)
74
75 observation = study.new_observation();
76 %array of design values
77 x = [sample.b1, sample.b2, sample.b3, sample.b4, sample.b5];
78 observation.add(model(x));
79
80end
81
82% Run the minimization
83study.set_evaluator(@evaluate);
84study.run();
85
86best_b_sample = study.driver.best_sample;
87min_neg_log_prob = study.driver.min_objective;
88
89%determine sample [b1, b2, b3, b4, b5, log_c1, log_c2] that minimizes the negative
90%log-probability
91minimum = struct2array(best_b_sample);
92
93%path to negative log-probability variable
94path = 'driver.acquisition_function.main_objective.variable';
95neg_log_probs = study.historic_parameter_values([path, '.observed_value']);
96[min_val, idx] = min(neg_log_probs);
97
98logs_c1 = study.historic_parameter_values([path, '.error_model_parameters.log_c1']);
99logs_c2 = study.historic_parameter_values([path, '.error_model_parameters.log_c2']);
100
101minimum = [minimum, logs_c1(idx), logs_c2(idx)];
102
103% Before running a Markov-chain Monte-Carlo (MCMC) sampling we converge the surrogate
104% models by sampling around the minimum. To make the study more explorative, the
105% scaling parameter is increased and the effective degrees of freedom is set to one.
106study.configure( ...
107 'scaling', 10.0, ...
108 'effective_DOF', 1.0, ...
109 'min_uncertainty', abs(min_neg_log_prob)*1e-8, ...
110 'max_iter', 120 ...
111);
112while(not(study.is_done))
113 sug = study.get_suggestion();
114 obs = evaluate(study, sug.sample);
115 study.add_observation(obs, sug.id);
116end
117
118% Run the MCMC sampling with 32 walkers
119num_walkers = 32; max_iter = 10000;
120mcmc_result = study.driver.run_mcmc( ...
121 'rel_error', 0.01, ...
122 'num_walkers', num_walkers, ...
123 'max_iter', max_iter ...
124);
125% This Matlab code does not contain a detailed analysis of the MCMC sampling by corner plots.
126% Please, see the corresponding Python tutorial for a code example.
127
128
129client.shutdown_server();
Markov-Chain Monte-Carlo (MCMC) sampling of the probability density of the model parameters \(b_1,\dots,b_5\) and the log value of the error model parameters \(c_1,\dots,c_2\) based on the analytic model function.
Markov-Chain Monte-Carlo (MCMC) sampling of the probability density of the model parameters \(b_1,\dots,b_5\) and the log value of the error model parameters \(c_1,\dots,c_2\) based on the trained surrogate of the study. A comparison between the analytic and the surrogate model function shows a good quantitative agreement.