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.
1server = jcmoptimizer.Server();
2client = jcmoptimizer.Client('host', server.host);
3
4% Definition of the search domain
5design_space = { ...
6 struct('name', 'b1', 'type', 'continuous', 'domain', [0,10]), ...
7 struct('name', 'b2', 'type', 'continuous', 'domain', [0.1,4]), ...
8 struct('name', 'b3', 'type', 'continuous', 'domain', [-4,-0.1]), ...
9 struct('name', 'b4', 'type', 'continuous', 'domain', [0.05,1]), ...
10 struct('name', 'b5', 'type', 'continuous', 'domain', [0.05,1]) ...
11};
12
13 % Creation of the study object with study_id 'bayesian_reconstruction'
14study = client.create_study( ...
15 'design_space', design_space, ...
16 'driver','BayesianReconstruction',...
17 'study_name','Bayesian parameter reconstruction using Bayesian optimization',...
18 'study_id', 'bayesian_reconstruction');
19%The vectorial model function of the MGH17 problem
20function val = model(x)
21 s = 0:32;
22 val = x(1) + x(2)*exp(-s.*x(4)) + x(3)*exp(-s.*x(5));
23end
24
25%The forward model parameters b to be reconstructed
26b_true = [3.7541005211E-01, 1.9358469127E+00, -1.4646871366E+00, ...
27 1.2867534640E-01,2.2122699662E-01];
28
29%The error model parameters in log-space to be reconstructed
30log_c1 = log(0.005);
31log_c2 = log(0.01);
32
33%The error model, i.e. the noise stddev depending on the model value y=f(b)
34function stddev = error_model(log_c1, log_c2, y)
35 stddev = sqrt( exp(log_c1)^2 + (exp(log_c2).*y).^2);
36end
37
38%Generate a rantom target vector of measurements
39rng("default");
40model_vector = model(b_true);
41err = error_model(log_c1,log_c2,model_vector);
42measurement_vector = model_vector + err.*randn(size(model_vector));
43
44%Configuration of the error model
45err_model_conf = struct();
46%error model expression
47err_model_conf.expression = 'sqrt(exp(log_c1)^2 + (exp(log_c2)*y_model)^2)';
48%distribution of error model parameters
49err_model_conf.distributions = { ...
50 struct('type', 'normal', 'parameter', 'log_c1', 'mean', -5.0, 'stddev', 1), ...
51 struct('type', 'normal', 'parameter', 'log_c2', 'mean', -4.0, 'stddev', 1), ...
52};
53%initial values and parameter bounds for fitting the error model parameters
54err_model_conf.initial_parameters = [-5.0, -4.0];
55err_model_conf.parameter_bounds = [-7,-2; -6.0,-1];
56
57%Multivariate normal prior distribution of forward model parameters.
58%Unspecified parameters (b1,b4,b5) are uniformly distributed in the design space
59distribution = struct();
60distribution.type='mvn';
61distribution.parameters = {'b2','b3'};
62distribution.mean = [2.25,-2.0];
63distribution.covariance = [0.5,-0.01;-0.01,0.5];
64parameter_distribution = struct();
65parameter_distribution.distributions = {distribution};
66
67study.configure( ...
68 'max_iter', 80, ...
69 'target_vector', measurement_vector, ...
70 'error_model', err_model_conf, ...
71 'parameter_distribution', parameter_distribution ...
72);
73% Evaluation of the black-box function for specified design parameters
74function observation = evaluate(study, sample)
75
76 observation = study.new_observation();
77 %array of design values
78 x = [sample.b1, sample.b2, sample.b3, sample.b4, sample.b5];
79 observation.add(model(x));
80
81end
82
83% Run the minimization
84study.set_evaluator(@evaluate);
85study.run();
86
87best_b_sample = study.driver.best_sample;
88min_neg_log_prob = study.driver.min_objective;
89
90%determine sample [b1, b2, b3, b4, b5, log_c1, log_c2] that minimizes the negative
91%log-probability
92minimum = struct2array(best_b_sample);
93
94%path to negative log-probability variable
95path = 'driver.acquisition_function.main_objective.variable';
96neg_log_probs = study.historic_parameter_values([path, '.observed_value']);
97[min_val, idx] = min(neg_log_probs);
98
99logs_c1 = study.historic_parameter_values([path, '.error_model_parameters.log_c1']);
100logs_c2 = study.historic_parameter_values([path, '.error_model_parameters.log_c2']);
101
102minimum = [minimum, logs_c1(idx), logs_c2(idx)];
103
104% Before running a Markov-chain Monte-Carlo (MCMC) sampling we converge the surrogate
105% models by sampling around the minimum. To make the study more explorative, the
106% scaling parameter is increased and the effective degrees of freedom is set to one.
107study.configure( ...
108 'scaling', 10.0, ...
109 'effective_DOF', 1.0, ...
110 'min_uncertainty', abs(min_neg_log_prob)*1e-8, ...
111 'max_iter', 120 ...
112);
113while(not(study.is_done))
114 sug = study.get_suggestion();
115 obs = evaluate(study, sug.sample);
116 study.add_observation(obs, sug.id);
117end
118
119% Run the MCMC sampling with 32 walkers
120num_walkers = 32; max_iter = 10000;
121mcmc_result = study.driver.run_mcmc( ...
122 'rel_error', 0.01, ...
123 'num_walkers', num_walkers, ...
124 'max_iter', max_iter ...
125);
126% This Matlab code does not contain a detailed analysis of the MCMC sampling by corner plots.
127% Please, see the corresponding Python tutorial for a code example.
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.