Solution of a non-expensive least-square problem based on a scipy implementation

Driver:

ScipyLeastSquares

Download script:

scipy_least_squares.py

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} \]
 1import sys,os
 2import numpy as np
 3import time
 4import pandas as pd
 5import torch
 6
 7from jcmoptimizer import Client, Study, Obseravtion
 8client = Client()
 9
10
11# Definition of the search domain
12design_space = [
13    {'name': 'b1', 'type': 'continuous', 'domain': (0,10)}, 
14    {'name': 'b2', 'type': 'continuous', 'domain': (0.1,4)},
15    {'name': 'b3', 'type': 'continuous', 'domain': (-4,-0.1)},
16    {'name': 'b4', 'type': 'continuous', 'domain': (0.05,1)},
17    {'name': 'b5', 'type': 'continuous', 'domain': (0.05,1)}
18]
19constraints = [
20    {'name': 'test', 'expression': 'b2 + b3 <= 1.0'}
21]
22
23# Creation of the study object with study_id 'scipy_least_squares'
24study = client.create_study(
25    design_space=design_space,
26    constraints=constraints,
27    driver="ScipyLeastSquares",
28    study_name="Solution of a non-expensive least-square problem based on a scipy implementation",
29    study_id="scipy_least_squares"
30)
31#The vectorial model function of the MGH17 problem
32def model(x: torch.Tensor) -> torch.Tensor:
33    s = torch.arange(33)
34    return x[0] + x[1]*torch.exp(-s*x[3]) + x[2]*torch.exp(-s*x[4])
35
36#Target vector of the MGH17
37target=torch.tensor([
38    8.44E-01, 9.08E-01, 9.32E-01, 9.36E-01, 9.25E-01,
39    9.08E-01, 8.81E-01, 8.50E-01, 8.18E-01, 7.84E-01,
40    7.51E-01, 7.18E-01, 6.85E-01, 6.58E-01, 6.28E-01,
41    6.03E-01, 5.80E-01, 5.58E-01, 5.38E-01, 5.22E-01,
42    5.06E-01, 4.90E-01, 4.78E-01, 4.67E-01, 4.57E-01,
43    4.48E-01, 4.38E-01, 4.31E-01, 4.24E-01, 4.20E-01,
44    4.14E-01, 4.11E-01, 4.06E-01
45])
46
47study.configure(
48    target_vector=target.tolist(),
49    method="trf",
50    max_iter=150,
51    num_parallel=2,
52    num_initial=2,
53    jac=True
54)
55
56# Evaluation of the black-box function for specified design parameters
57def evaluate(study: Study, b1: float, b2: float, b3: float, b4: float, b5: float) -> Observation:
58
59    observation = study.new_observation()
60    #tensor of design values to reconstruct
61    x = torch.tensor([b1, b2, b3, b4, b5], requires_grad=True)
62    
63    observation.add(model(x).tolist())
64
65    #determine Jacobian matrix
66    jac = torch.autograd.functional.jacobian(
67        func=model,
68        inputs=x
69    )
70
71    for idx, param in enumerate(design_space):
72        observation.add(jac[:, idx].tolist(), derivative=param["name"])
73    return observation
74
75# Run the minimization
76study.set_evaluator(evaluate)
77study.run()
78best_sample = study.driver.best_sample
79uncertainties = study.driver.uncertainties
80print("Reconstructed parameters:")
81for param in design_space:
82    name = param['name']
83    print(f"  {name} = {best_sample[name]:.3f} +/- {uncertainties[name]:.3f}")
84
85
86client.shutdown_server()