Results get worse by adding invariable parameters

22 views
Skip to first unread message

wenke ren

unread,
Mar 5, 2024, 1:47:48 PMMar 5
to lmfit-py
Dear Matt and other developers,

I'm using your code fitting emission line spectra. By combining a few gaussian model (lmfit.models.GaussianModel) with some specific parameters fixed, I use the Model.fit() method to fit to my spectra data. I'm using an increasing number of Gaussian models to decide how many models are needed to recover the lines but I catched some glitch during the procedure. 

I simplified some procedures and narrowed this problem as much as I am possible:
The code can give a good fit for Model1 (sum of a few Gaussian). I add an anther Gaussian to Model1 and got Model2. I set the newly added Gaussian as completely invariable, three free parameters are all set to vary=False and the amplitude is fixed as 0. Then I use the best fit parameter of the first step as the initial of the Model2 and fit the same data again. The chisquare this time is even larger than the last one.

I didn't set any bounds for any parameters during the fitting. Only thing I set is value fix. All models are from build_in model so there shouldn't be any apparent logical bugs in the functions. All three parameters of the newly added Gaussian is fixed so there are even no changes in free parameters. I even set the initial value of the secend fitting as the best fit results in the first cycle. I would expect a exactly the same best fit. Current results have already beyond my knowledge. Is that comes from some mathematical design defect or any bugs that I didn't noticed in the code?

I attached my fit_report here:
  • Firstly, with a bunch of setted Gaussian models, I can well fit to a spectrum. The fit_report is as follow:

[[Model]]
    (((((Model(gaussian, prefix='Ha_na_main_') + Model(gaussian, prefix='NII_6548_main_')) + Model(gaussian, prefix='NII_6584_main_')) + Model(gaussian, prefix='SII_6717_main_')) + Model(gaussian, prefix='SII_6731_main_')) + ((((Model(gaussian, prefix='Ha_na_comp0_') + Model(gaussian, prefix='NII_6548_comp0_')) + Model(gaussian, prefix='NII_6584_comp0_')) + Model(gaussian, prefix='SII_6717_comp0_')) + Model(gaussian, prefix='SII_6731_comp0_')))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 287
    # data points      = 542
    # variables        = 10
    chi-square         = 1277.76291
    reduced chi-square = 2.40180997
    Akaike info crit   = 484.819253
    Bayesian info crit = 527.771913
    R-squared          = 0.96013172
[[Variables]]
    Ha_na_main_amplitude:      197.297629 +/- 7.30126008 (3.70%) (init = 487.0571)
    Ha_na_main_center:         6564.68018 +/- 0.03362371 (0.00%) (init = 6564.485)
    Ha_na_main_sigma:          1.35127039 +/- 0.03901561 (2.89%) (init = 3.880078)
    NII_6548_main_amplitude:   22.7629439 +/- 1.90187665 (8.36%) (init = 96.23085)
    NII_6548_main_center:      6549.92595 +/- 0.03354814 (0.00%) == 'Ha_na_main_center*0.9977524836959835'
    NII_6548_main_sigma:       1.34823339 +/- 0.03892792 (2.89%) == 'Ha_na_main_sigma*0.9977524836959835'
    NII_6584_main_amplitude:   41.4529139 +/- 1.85314143 (4.47%) (init = 114.0512)
    NII_6584_main_center:      6585.33609 +/- 0.03372950 (0.00%) == 'Ha_na_main_center*1.003146522825623'
    NII_6584_main_sigma:       1.35552219 +/- 0.03913837 (2.89%) == 'Ha_na_main_sigma*1.003146522825623'
    SII_6717_main_amplitude:   24.9052218 +/- 1.66762664 (6.70%) (init = 67.27554)
    SII_6717_main_center:      6718.36419 +/- 0.03441086 (0.00%) == 'Ha_na_main_center*1.0234107393185834'
    SII_6717_main_sigma:       1.38290463 +/- 0.03992900 (2.89%) == 'Ha_na_main_sigma*1.0234107393185834'
    SII_6731_main_amplitude:   19.4301073 +/- 1.48792813 (7.66%) (init = 46.83112)
    SII_6731_main_center:      6732.74831 +/- 0.03448453 (0.00%) == 'Ha_na_main_center*1.0256018772475162'
    SII_6731_main_sigma:       1.38586545 +/- 0.04001448 (2.89%) == 'Ha_na_main_sigma*1.0256018772475162'
    Ha_na_comp0_amplitude:     389.466051 +/- 8.25058339 (2.12%) (init = 1)
    Ha_na_comp0_center:        6563.97371 +/- 0.20984070 (0.00%) (init = 6562.8)
    Ha_na_comp0_sigma:         6.98454013 +/- 0.23425958 (3.35%) (init = 328.14)
    NII_6548_comp0_amplitude:  44.9341125 +/- 4.00059952 (8.90%) == 'Ha_na_comp0_amplitude*NII_6548_main_amplitude/Ha_na_main_amplitude'
    NII_6548_comp0_center:     6549.22107 +/- 0.20936908 (0.00%) == 'Ha_na_comp0_center*0.9977524836959835'
    NII_6548_comp0_sigma:      6.96884216 +/- 0.23373307 (3.35%) == 'Ha_na_comp0_sigma*0.9977524836959835'
    NII_6584_comp0_amplitude:  81.8281635 +/- 3.25390490 (3.98%) == 'Ha_na_comp0_amplitude*NII_6584_main_amplitude/Ha_na_main_amplitude'
    NII_6584_comp0_center:     6584.62740 +/- 0.21050097 (0.00%) == 'Ha_na_comp0_center*1.003146522825623'
    NII_6584_comp0_sigma:      7.00651704 +/- 0.23499668 (3.35%) == 'Ha_na_comp0_sigma*1.003146522825623'
    SII_6717_comp0_amplitude:  49.1629749 +/- 3.09106365 (6.29%) == 'Ha_na_comp0_amplitude*SII_6717_main_amplitude/Ha_na_main_amplitude'
    SII_6717_comp0_center:     6717.64119 +/- 0.21475323 (0.00%) == 'Ha_na_comp0_center*1.0234107393185834'
    SII_6717_comp0_sigma:      7.14805327 +/- 0.23974377 (3.35%) == 'Ha_na_comp0_sigma*1.0234107393185834'
    SII_6731_comp0_amplitude:  38.3550840 +/- 2.76242493 (7.20%) == 'Ha_na_comp0_amplitude*SII_6731_main_amplitude/Ha_na_main_amplitude'
    SII_6731_comp0_center:     6732.02376 +/- 0.21521302 (0.00%) == 'Ha_na_comp0_center*1.0256018772475162'
    SII_6731_comp0_sigma:      7.16335736 +/- 0.24025706 (3.35%) == 'Ha_na_comp0_sigma*1.0256018772475162'
    Ha_na_main_fwhm:           3.18199853 +/- 0.09187474 (2.89%) == '2.3548200*Ha_na_main_sigma'
    Ha_na_main_height:         58.2491637 +/- 1.63400227 (2.81%) == '0.3989423*Ha_na_main_amplitude/max(1e-15, Ha_na_main_sigma)'
    NII_6548_main_fwhm:        3.17484694 +/- 0.09166825 (2.89%) == '2.3548200*NII_6548_main_sigma'
    NII_6548_main_height:      6.73555579 +/- 0.58294278 (8.65%) == '0.3989423*NII_6548_main_amplitude/max(1e-15, NII_6548_main_sigma)'
    NII_6584_main_fwhm:        3.19201076 +/- 0.09216383 (2.89%) == '2.3548200*NII_6584_main_sigma'
    NII_6584_main_height:      12.1999632 +/- 0.48306277 (3.96%) == '0.3989423*NII_6584_main_amplitude/max(1e-15, NII_6584_main_sigma)'
    SII_6717_main_fwhm:        3.25649147 +/- 0.09402560 (2.89%) == '2.3548200*SII_6717_main_sigma'
    SII_6717_main_height:      7.18469392 +/- 0.45371085 (6.31%) == '0.3989423*SII_6717_main_amplitude/max(1e-15, SII_6717_main_sigma)'
    SII_6731_main_fwhm:        3.26346367 +/- 0.09422691 (2.89%) == '2.3548200*SII_6731_main_sigma'
    SII_6731_main_height:      5.59324984 +/- 0.40813406 (7.30%) == '0.3989423*SII_6731_main_amplitude/max(1e-15, SII_6731_main_sigma)'
    Ha_na_comp0_fwhm:          16.4473345 +/- 0.55163914 (3.35%) == '2.3548200*Ha_na_comp0_sigma'
    Ha_na_comp0_height:        22.2454853 +/- 0.89115074 (4.01%) == '0.3989423*Ha_na_comp0_amplitude/max(1e-15, Ha_na_comp0_sigma)'
    NII_6548_comp0_fwhm:       16.4103689 +/- 0.55039932 (3.35%) == '2.3548200*NII_6548_comp0_sigma'
    NII_6548_comp0_height:     2.57232375 +/- 0.29110357 (11.32%) == '0.3989423*NII_6548_comp0_amplitude/max(1e-15, NII_6548_comp0_sigma)'
    NII_6584_comp0_fwhm:       16.4990865 +/- 0.55337489 (3.35%) == '2.3548200*NII_6584_comp0_sigma'
    NII_6584_comp0_height:     4.65919309 +/- 0.26894514 (5.77%) == '0.3989423*NII_6584_comp0_amplitude/max(1e-15, NII_6584_comp0_sigma)'
    SII_6717_comp0_fwhm:       16.8323788 +/- 0.56455342 (3.35%) == '2.3548200*SII_6717_comp0_sigma'
    SII_6717_comp0_height:     2.74385060 +/- 0.20343691 (7.41%) == '0.3989423*SII_6717_comp0_amplitude/max(1e-15, SII_6717_comp0_sigma)'
    SII_6731_comp0_fwhm:       16.8684172 +/- 0.56576213 (3.35%) == '2.3548200*SII_6731_comp0_sigma'
    SII_6731_comp0_height:     2.13607456 +/- 0.17290790 (8.09%) == '0.3989423*SII_6731_comp0_amplitude/max(1e-15, SII_6731_comp0_sigma)'
[[Correlations]] (unreported correlations are < 0.100)
    C(Ha_na_main_amplitude, Ha_na_comp0_sigma)          = +0.6719
    C(Ha_na_main_amplitude, Ha_na_main_sigma)           = +0.6627
    C(Ha_na_main_amplitude, NII_6584_main_amplitude)    = +0.5996
    C(NII_6548_main_amplitude, Ha_na_comp0_center)      = +0.5209
    C(Ha_na_main_sigma, NII_6584_main_amplitude)        = +0.4898
    C(NII_6584_main_amplitude, Ha_na_comp0_amplitude)   = -0.4692
    C(Ha_na_main_sigma, Ha_na_comp0_sigma)              = +0.4604
    C(NII_6548_main_amplitude, Ha_na_comp0_amplitude)   = -0.4406
    C(Ha_na_main_amplitude, SII_6717_main_amplitude)    = +0.4277
    C(Ha_na_main_sigma, Ha_na_comp0_amplitude)          = -0.4232
    C(Ha_na_main_amplitude, Ha_na_comp0_amplitude)      = -0.4063
    C(Ha_na_main_amplitude, SII_6731_main_amplitude)    = +0.4035
    C(Ha_na_comp0_center, Ha_na_comp0_sigma)            = -0.3942
    C(NII_6548_main_amplitude, Ha_na_comp0_sigma)       = -0.3669
    C(NII_6584_main_amplitude, Ha_na_comp0_sigma)       = +0.3579
    C(Ha_na_main_sigma, SII_6717_main_amplitude)        = +0.3438
    C(Ha_na_main_sigma, SII_6731_main_amplitude)        = +0.3106
    C(SII_6717_main_amplitude, Ha_na_comp0_amplitude)   = -0.3053
    C(NII_6584_main_amplitude, SII_6717_main_amplitude) = +0.2879
    C(NII_6584_main_amplitude, SII_6731_main_amplitude) = +0.2858
    C(SII_6717_main_amplitude, Ha_na_comp0_sigma)       = +0.2849
    C(SII_6731_main_amplitude, Ha_na_comp0_sigma)       = +0.2840
    C(Ha_na_main_amplitude, Ha_na_comp0_center)         = -0.2689
    C(NII_6584_main_amplitude, Ha_na_comp0_center)      = -0.2649
    C(SII_6731_main_amplitude, Ha_na_comp0_amplitude)   = -0.2605
    C(Ha_na_main_center, Ha_na_comp0_center)            = -0.1816
    C(SII_6731_main_amplitude, Ha_na_comp0_center)      = -0.1738
    C(Ha_na_main_sigma, Ha_na_comp0_center)             = -0.1705


  • I'm add another Gaussian to this setted model and set every parameter in the new one as fixed (vary=False and amplitude=0). Also I update the rest initial parameter to the bestfit results of last time. Then I fit the same data again and here's the fit_report:
[[Model]]
    (((((Model(gaussian, prefix='Ha_na_comp0_') + Model(gaussian, prefix='NII_6548_comp0_')) + Model(gaussian, prefix='NII_6584_comp0_')) + Model(gaussian, prefix='SII_6717_comp0_')) + Model(gaussian, prefix='SII_6731_comp0_')) + Model(gaussian, prefix='Ha_br_comp0_'))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 171
    # data points      = 542
    # variables        = 10
    chi-square         = 3840.27045
    reduced chi-square = 7.21855347
    Akaike info crit   = 1081.25338
    Bayesian info crit = 1124.20604
    R-squared          = 0.88017731
##  Warning: uncertainties could not be estimated:
    Ha_na_main_center:         at initial value
    Ha_na_main_sigma:          at initial value
[[Variables]]
    Ha_na_comp0_amplitude:     487.058257 (init = 389.4661)
    Ha_na_comp0_center:        6564.48458 (init = 6563.974)
    Ha_na_comp0_sigma:         3.88011439 (init = 6.98454)
    NII_6548_comp0_amplitude:  96.2310277 == 'Ha_na_comp0_amplitude*NII_6548_main_amplitude/Ha_na_main_amplitude'
    NII_6548_comp0_center:     6549.73079 == 'Ha_na_comp0_center*0.9977524836959835'
    NII_6548_comp0_sigma:      3.87139377 == 'Ha_na_comp0_sigma*0.9977524836959835'
    NII_6584_comp0_amplitude:  114.051544 == 'Ha_na_comp0_amplitude*NII_6584_main_amplitude/Ha_na_main_amplitude'
    NII_6584_comp0_center:     6585.13988 == 'Ha_na_comp0_center*1.003146522825623'
    NII_6584_comp0_sigma:      3.89232326 == 'Ha_na_comp0_sigma*1.003146522825623'
    SII_6717_comp0_amplitude:  67.2764155 == 'Ha_na_comp0_amplitude*SII_6717_main_amplitude/Ha_na_main_amplitude'
    SII_6717_comp0_center:     6718.16402 == 'Ha_na_comp0_center*1.0234107393185834'
    SII_6717_comp0_sigma:      3.97095074 == 'Ha_na_comp0_sigma*1.0234107393185834'
    SII_6731_comp0_amplitude:  46.8318501 == 'Ha_na_comp0_amplitude*SII_6731_main_amplitude/Ha_na_main_amplitude'
    SII_6731_comp0_center:     6732.54771 == 'Ha_na_comp0_center*1.0256018772475162'
    SII_6731_comp0_sigma:      3.97945260 == 'Ha_na_comp0_sigma*1.0256018772475162'
    Ha_br_comp0_amplitude:     0 (fixed)
    Ha_br_comp0_center:        6562.8 (fixed)
    Ha_br_comp0_sigma:         1 (fixed)
    Ha_na_comp0_fwhm:          9.13697097 == '2.3548200*Ha_na_comp0_sigma'
    Ha_na_comp0_height:        50.0779415 == '0.3989423*Ha_na_comp0_amplitude/max(1e-15, Ha_na_comp0_sigma)'
    NII_6548_comp0_fwhm:       9.11643548 == '2.3548200*NII_6548_comp0_sigma'
    NII_6548_comp0_height:     9.91648739 == '0.3989423*NII_6548_comp0_amplitude/max(1e-15, NII_6548_comp0_sigma)'
    NII_6584_comp0_fwhm:       9.16572065 == '2.3548200*NII_6584_comp0_sigma'
    NII_6584_comp0_height:     11.6896728 == '0.3989423*NII_6584_comp0_amplitude/max(1e-15, NII_6584_comp0_sigma)'
    SII_6717_comp0_fwhm:       9.35087421 == '2.3548200*SII_6717_comp0_sigma'
    SII_6717_comp0_height:     6.75893752 == '0.3989423*SII_6717_comp0_amplitude/max(1e-15, SII_6717_comp0_sigma)'
    SII_6731_comp0_fwhm:       9.37089458 == '2.3548200*SII_6731_comp0_sigma'
    SII_6731_comp0_height:     4.69491859 == '0.3989423*SII_6731_comp0_amplitude/max(1e-15, SII_6731_comp0_sigma)'
    Ha_br_comp0_fwhm:          2.35482000 == '2.3548200*Ha_br_comp0_sigma'
    Ha_br_comp0_height:        0.00000000 == '0.3989423*Ha_br_comp0_amplitude/max(1e-15, Ha_br_comp0_sigma)'
    Ha_na_main_amplitude:      9151.06792 (init = 197.2976)
    Ha_na_main_center:         6564.68018 (init = 6564.68)
    Ha_na_main_sigma:          1.35127039 (init = 1.35127)
    NII_6548_main_amplitude:   1808.03150 (init = 22.76294)
    NII_6548_main_center:      6549.92595 == 'Ha_na_main_center*0.9977524836959835'
    NII_6548_main_sigma:       1.34823339 == 'Ha_na_main_sigma*0.9977524836959835'
    NII_6584_main_amplitude:   2142.85131 (init = 41.45291)
    NII_6584_main_center:      6585.33609 == 'Ha_na_main_center*1.003146522825623'
    NII_6584_main_sigma:       1.35552219 == 'Ha_na_main_sigma*1.003146522825623'
    SII_6717_main_amplitude:   1264.01932 (init = 24.90522)
    SII_6717_main_center:      6718.36419 == 'Ha_na_main_center*1.0234107393185834'
    SII_6717_main_sigma:       1.38290463 == 'Ha_na_main_sigma*1.0234107393185834'
    SII_6731_main_amplitude:   879.897702 (init = 19.43011)
    SII_6731_main_center:      6732.74831 == 'Ha_na_main_center*1.0256018772475162'
    SII_6731_main_sigma:       1.38586545 == 'Ha_na_main_sigma*1.0256018772475162'
    Ha_na_main_fwhm:           3.18199853 == '2.3548200*Ha_na_main_sigma'
    Ha_na_main_height:         2701.71545 == '0.3989423*Ha_na_main_amplitude/max(1e-15, Ha_na_main_sigma)'
    NII_6548_main_fwhm:        3.17484694 == '2.3548200*NII_6548_main_sigma'
    NII_6548_main_height:      534.996576 == '0.3989423*NII_6548_main_amplitude/max(1e-15, NII_6548_main_sigma)'
    NII_6584_main_fwhm:        3.19201076 == '2.3548200*NII_6584_main_sigma'
    NII_6584_main_height:      630.660299 == '0.3989423*NII_6584_main_amplitude/max(1e-15, NII_6584_main_sigma)'
    SII_6717_main_fwhm:        3.25649147 == '2.3548200*SII_6717_main_sigma'
    SII_6717_main_height:      364.646098 == '0.3989423*SII_6717_main_amplitude/max(1e-15, SII_6717_main_sigma)'
    SII_6731_main_fwhm:        3.26346367 == '2.3548200*SII_6731_main_sigma'
    SII_6731_main_height:      253.291843 == '0.3989423*SII_6731_main_amplitude/max(1e-15, SII_6731_main_sigma)'


This could be a naive question but it really annoying me sometimes in my study. If you have any idea about this quesition, please enlighten me.

Note that the [Model] in the second report is not correctly printted, but the parameters are right. Only the gaussian with prefix='Ha_br_comp0' is added. I think that is not a fatal problem here.

Best wishes,
Wenke

Matt Newville

unread,
Mar 5, 2024, 2:17:21 PMMar 5
to lmfi...@googlegroups.com
Hi Wenke, 

Without more information (say, does the "fixed component" that you added even remotely match the data), it is hard to speculate much further.   We always ask for a simple example code that shows the problem. Without that, we just do not know what you did.  Anyway, sure, adding a random, fixed Gaussian to any model could easily make it worse.


You say:

       Note that the [Model] in the second report is not correctly printted,


In what way is it wrong?  

It is very hard to guess what is not working well for you.


--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/6d11eeae-3f28-4df1-aaa9-7ae3702bc813n%40googlegroups.com.


wenke ren

unread,
Mar 5, 2024, 10:11:52 PMMar 5
to lmfit-py
Hi Matt,

Thanks for your prompt reply. 

When I'm making a demo for this problem, I reallize that the model is not updated properly. I do miss some Gaussian in last run. The reason why the fitted results in this round still contain all parameters is because I update the parameters like:
init_param = new_model.make_params()
init_param.update(old_fit.params)

It really teaches me a lesson. Sorry for the interruption. I will prepare a full demo next time.

Thank you again and hope you have a good day.

Wenke

Reply all
Reply to author
Forward
0 new messages