tBuLi / symfit

Symbolic Fitting; fitting as it should be.
http://symfit.readthedocs.org
MIT License
232 stars 17 forks source link

Bad result for default surface fit #338

Closed nfhktwrbq closed 2 years ago

nfhktwrbq commented 2 years ago

I use default surface fitting like fit = Fit(model, x=x_data, y=y_data, z=z_data) with poly model:

p42_00, p42_01, p42_02, p42_10, p42_11, p42_12, p42_20, p42_21, p42_22, p42_30, p42_31, p42_40 = parameters(
        'p00, p01, p02, p10, p11, p12, p20, p21, p22, p30, p31, p40')
    model_dict42 = {
        z: Poly({(0, 0): p42_00, (0, 1): p42_01, (0, 2): p42_02, (1, 0): p42_10, (1, 1): p42_11, (1, 2): p42_12,
                 (2, 0): p42_20, (2, 1): p42_21, (2, 2): p42_22, (3, 0): p42_30, (3, 1): p42_31, (4, 0): p42_40},
                 x,  y).as_expr()
    }
    model42 = Model(model_dict42)

For some data I get good fit result:

x = [100. 100. 100. 100. 100. 100. 100. 100. 100. 125. 125. 125. 125. 125.
 125. 125. 125. 125. -20. -20. -20. -20. -20. -20. -20. -20. -20. -40.
 -40. -40. -40. -40. -40. -40. -40. -40. -60. -60. -60. -60. -60. -60.
 -60. -60. -60.   0.   0.   0.   0.   0.   0.   0.   0.   0.  20.  20.
  20.  20.  20.  20.  20.  20.  20.  40.  40.  40.  40.  40.  40.  40.
  40.  40.  60.  60.  60.  60.  60.  60.  60.  60.  60.  80.  80.  80.
  80.  80.  80.  80.  80.  80.] 
y = [312. 312. 363. 238. 263. 287. 336. 359. 378. 287. 286. 304. 227. 246.
 265. 304. 305. 304. 325. 325. 394. 224. 258. 293. 357. 388. 418. 324.
 324. 395. 220. 256. 291. 357. 389. 418. 324. 323. 397. 215. 252. 288.
 358. 390. 422. 326. 326. 392. 230. 262. 295. 357. 385. 413. 327. 327.
 391. 234. 265. 296. 357. 384. 412. 327. 326. 388. 237. 267. 298. 356.
 382. 408. 326. 326. 384. 240. 269. 297. 352. 378. 403. 322. 322. 377.
 242. 269. 295. 348. 372. 395.] 
z = [0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.
 0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.    0.765 0.81  0.855
 0.945 0.99  1.035 0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035
 0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.
 0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.    0.765 0.81  0.855
 0.945 0.99  1.035 0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035
 0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.
 0.765 0.81  0.855 0.945 0.99  1.035]

Parameter Value        Standard Deviation
p00       5.558602e-01 5.586866e-02
p01       6.460956e-04 3.515970e-04
p02       1.262043e-06 5.404002e-07
p10       1.403957e-04 9.768420e-04
p11       -1.635300e-06 5.977600e-06
p12       4.144909e-09 9.129835e-09
p20       -2.576515e-05 1.573222e-05
p21       1.434869e-07 9.754735e-08
p22       -2.258422e-10 1.471868e-10
p30       -1.953423e-07 6.881279e-08
p31       4.933608e-10 1.888138e-10
p40       9.620778e-10 1.768323e-10
Status message         Desired error not necessarily achieved due to precision loss.
Number of iterations   38
Objective              <symfit.core.objectives.LeastSquares object at 0x000001ABE9E89C40>
Minimizer              <symfit.core.minimizers.BFGS object at 0x000001ABE7E5B520>

Goodness of fit qualifiers:
chi_squared            0.008946573810119391
objective_value        0.0044732869050596955
r_squared              0.9863596198050018

But sometimes fit return bad parameters:

x = [100. 100. 100. 100. 100. 100. 100. 100. 100. 125. 125. 125. 125. 125.
 125. 125. 125. 125. -20. -20. -20. -20. -20. -20. -20. -20. -20. -40.
 -40. -40. -40. -40. -40. -40. -40. -40. -60. -60. -60. -60. -60. -60.
 -60. -60. -60.   0.   0.   0.   0.   0.   0.   0.   0.   0.  20.  20.
  20.  20.  20.  20.  20.  20.  20.  40.  40.  40.  40.  40.  40.  40.
  40.  40.  60.  60.  60.  60.  60.  60.  60.  60.  60.  80.  80.  80.
  80.  80.  80.  80.  80.  80.] 
 y = [309. 310. 359. 238. 262. 286. 332. 355. 375. 283. 284. 301. 227. 246.
 264. 302. 302. 301. 317. 317. 383. 222. 255. 286. 348. 377. 404. 315.
 315. 383. 216. 250. 283. 347. 376. 405. 312. 312. 382. 210. 244. 279.
 344. 375. 404. 320. 320. 384. 228. 259. 290. 349. 377. 404. 322. 322.
 384. 232. 263. 293. 352. 377. 403. 323. 323. 382. 236. 265. 295. 351.
 377. 401. 322. 322. 378. 239. 268. 295. 349. 374. 398. 319. 319. 374.
 241. 267. 293. 344. 368. 390.] 
 z = [0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.
 0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.    0.765 0.81  0.855
 0.945 0.99  1.035 0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035
 0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.
 0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.    0.765 0.81  0.855
 0.945 0.99  1.035 0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035
 0.9   0.9   1.    0.765 0.81  0.855 0.945 0.99  1.035 0.9   0.9   1.
 0.765 0.81  0.855 0.945 0.99  1.035]

Parameter Value        Standard Deviation
p00       9.999917e-01 1.374857e+09
p01       9.971409e-01 8.805962e+06
p02       -1.486830e-02 1.379254e+04
p10       9.997671e-01 2.432815e+07
p11       9.646214e-01 1.521692e+05
p12       4.379723e-04 2.371712e+02
p20       9.920647e-01 3.770917e+05
p21       -2.279766e-02 2.392612e+03
p22       3.512629e-01 3.707486e+00
p30       3.923439e-02 1.611642e+03
p31       1.193505e-01 4.524911e+00
p40       4.684057e-02 4.143410e+00
Status message         Desired error not necessarily achieved due to precision loss.
Number of iterations   7
Objective              <symfit.core.objectives.LeastSquares object at 0x000001ABE9E89DF0>
Minimizer              <symfit.core.minimizers.BFGS object at 0x000001ABE9E89BE0>

Goodness of fit qualifiers:
chi_squared            4.987753192573911e+18
objective_value        2.4938765962869555e+18
r_squared              -7.604570342735088e+18

What can be done for resolve this problem?

pckroon commented 2 years ago

Most likely your fit got stuck in a local minimum. There's a few ways out (in order of computational expense):

You can also try switching to a different local optimization algorithm, or setting bounds on your parameters. But that could also hurt.

nfhktwrbq commented 2 years ago

SLSQP algorithm also suitable for my data