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