Solution of a non-expensive least-square problem based on a scipy implementation
- Driver:
Download script: scipy_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} \]
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 'scipy_least_squares'
17study = client.create_study( ...
18 'design_space', design_space, ...
19 'constraints', constraints,...
20 'driver','ScipyLeastSquares',...
21 'study_name','Solution of a non-expensive least-square problem based on a scipy implementation',...
22 'study_id', 'scipy_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', 300, ...
41 'num_initial', 1, ...
42 'method', 'trf' ...
43);
44
45% Evaluation of the black-box function for specified design parameters
46function observation = evaluate(study, sample)
47
48 observation = study.new_observation();
49 %tensor of design values to reconstruct
50 x = [sample.b1, sample.b2, sample.b3, sample.b4, sample.b5];
51 observation.add(model(x));
52
53end
54
55% Run the minimization
56study.set_evaluator(@evaluate);
57study.run();
58
59best_sample = study.driver.best_sample;
60min_chisq = study.driver.min_objective;
61uncertainties = study.driver.uncertainties;
62fprintf("Reconstructed parameters with chi-squared value %e\n", min_chisq);
63fns = fieldnames(best_sample);
64for i = 1:length(fns)
65 fprintf(" %s = %f +/- %f\n", fns{i}, ...
66 best_sample.(fns{i}), uncertainties.(fns{i}));
67end