Solution of least-square problem using Bayesian optimization

Driver:

BayesianLeastSquares

Download script: bayesian_least_squares.m

The target of the study is to showcase the solution of a non-linear least-squares problem from the NIST statistical reference datasets. As an example, the MGH17 problem is considered which consists of fitting a vectorial model \(\mathbf{f}(\mathbf{b}) \in \mathbb{R}^{33}\) with

\[f_i(\mathbf{b}) = b_1 + b_2 \exp(-i \cdot b_4) + b_3 \exp(-i \cdot b_5),\, i = 0, \dots, 32\]

to a target vector with 33 entries. The certified best-fit values are

\[ \begin{align}\begin{aligned}b_1 &= 0.3754100521 \pm 2.0723153551 \cdot 10^{-3}&\\b_2 &= 1.9358469127 \pm 0.22031669222&\\b_3 &= -1.464687136 \pm 0.22175707739&\\b_4 &= 0.1286753464 \pm 4.4861358114\cdot 10^{-3}&\\b_5 &= 0.2212269966 \pm 8.9471996575 \cdot 10^{-3}&\end{aligned}\end{align} \]
 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};
11constraints = { ...
12    struct('name', 'test', 'expression', 'b2 + b3 <= 1.0') ...
13};
14
15 % Creation of the study object with study_id 'bayesian_least_squares'
16study = client.create_study( ...
17    'design_space', design_space, ...
18    'constraints', constraints,...
19    'driver','BayesianLeastSquares',...
20    'study_name','Solution of least-square problem using Bayesian optimization',...
21    'study_id', 'bayesian_least_squares');
22%The vectorial model function of the MGH17 problem
23function val = model(x)
24    s = 0:32;
25    val = x(1) + x(2)*exp(-s.*x(4)) + x(3)*exp(-s.*x(5));
26end
27
28%Target vector of the MGH17
29target=[8.44E-01, 9.08E-01, 9.32E-01, 9.36E-01, 9.25E-01, ...
30 9.08E-01, 8.81E-01, 8.50E-01, 8.18E-01, 7.84E-01, ...
31 7.51E-01, 7.18E-01, 6.85E-01, 6.58E-01, 6.28E-01, ...
32 6.03E-01, 5.80E-01, 5.58E-01, 5.38E-01, 5.22E-01, ...
33 5.06E-01, 4.90E-01, 4.78E-01, 4.67E-01, 4.57E-01, ...
34 4.48E-01, 4.38E-01, 4.31E-01, 4.24E-01, 4.20E-01, ...
35 4.14E-01, 4.11E-01, 4.06E-01];
36
37study.configure( ...
38    'target_vector', target, ...
39    'max_iter', 120 ...
40);
41% Evaluation of the black-box function for specified design parameters
42function observation = evaluate(study, sample)
43
44    observation = study.new_observation();
45    %array of design values
46    x = [sample.b1, sample.b2, sample.b3, sample.b4, sample.b5];    
47    observation.add(model(x));
48    
49end  
50
51% Run the minimization
52study.set_evaluator(@evaluate);
53study.run(); 
54
55best_sample = study.driver.best_sample;
56min_chisq = study.driver.min_objective;
57uncertainties = study.driver.uncertainties;
58fprintf("Reconstructed parameters with chi-squared value %e\n", min_chisq);
59fns = fieldnames(best_sample);
60for i = 1:length(fns)
61    fprintf("  %s = %f +/- %f\n", fns{i}, ...
62            best_sample.(fns{i}), uncertainties.(fns{i})); 
63end
64 
65% Before running a Markov-chain Monte-Carlo (MCMC) sampling we converge the surrogate
66% models by sampling around the minimum. To make the study more explorative, the
67% scaling parameter is increased and the effective degrees of freedom is set to one.
68study.configure( ...
69    'scaling', 10.0, ...
70    'effective_DOF', 1.0,  ...
71    'min_uncertainty', min_chisq*1e-8, ...
72    'max_iter', 150, ...
73    'min_val', 0.0 ...
74);
75while(not(study.is_done))
76   sug = study.get_suggestion();
77   obs = evaluate(study, sug.sample);
78   study.add_observation(obs, sug.id);
79end
80
81% Run the MCMC sampling with 32 walkers
82num_walkers = 32; max_iter = 10000;
83mcmc_result = study.driver.run_mcmc( ...
84    'rel_error', 0.01, ...
85    'num_walkers', num_walkers, ...
86    'max_iter', max_iter ...
87);
88% This Matlab code does not contain a detailed analysis of the MCMC sampling by corner plots.
89% Please, see the corresponding Python tutorial for a code example.
90
91
92client.shutdown_server();
MCMC sampling based on surrogate

Markov-Chain Monte-Carlo (MCMC) sampling of the probability density of the parameters \(b_1,\dots,b_5\) based on the analytic model function.

MCMC sampling based on surrogate

Markov-Chain Monte-Carlo (MCMC) sampling of the probability density of the parameters \(b_1,\dots,b_5\) based on the trained surrogate of the study. A comparison between the analytic and the surrogate model function shows a good quantitative agreement.