[AstroPy] Chi-square problems with lmfit and scipy

Ivan Valtchanov ivvv68 at gmail.com
Wed May 19 16:19:01 EDT 2021


Hi Rudolf,

I would recommend to normalise the y-values within [0,1], I suspect
the results you see are due to numerical accuracy limits.

Cheers,
Ivan V


On Wed, 19 May 2021 at 17:43, Rudolf Baer <rbaer25 at gmail.com> wrote:
>
> I am fitting the continuum of a NIR spectrum with a blackbody plus power law using lmfit and scipy. The plot of the fit looks good
> and the resulting parameters agree reasonably with previous results.
>
> T:      1109.29505 +/- 3.08340774 (0.28%) (init = 1100)
> const:  4.7240e-21 +/- 6.3014e-23 (1.33%) (init = 2e-21)
> A:      2.4188e-11 +/- 6.3181e-14 (0.26%) (init = 2e-11)
> p:     -1.06704890 +/- 0.01701493 (1.59%) (init = -1)
>
> However the chi-square and reduced chi-square values
> (chi-square = 1.1268e-20 reduced chi-square = 2.1039e-24)
> are clearly wrong. The problem is apparently known as the scipy guide says Note that the calculation of chi-square and
> reduced chi-square assume that the returned residual function is scaled properly to the uncertainties in the data.
> For these statistics to be meaningful, the person writing the function to be minimized must scale them properly.
>
>  I do not know how to do this. Any advice will be appreciated.
>
> With kind regards
> Rudolf Baer
> Zurich
>
>
>
> The input data for the code are x: lambda in AA, y: flux density in erg/cm^{2}/s/{\AA)
> Code:
>
> x=x_AA/10000. #<---------conversion AA to um
>
> y=y*1e4
>
> y=y*x
> #=========================================================================================
>
> def bb(x, T, const):
>
>     from scipy.constants import h,k,c
>
>     x = 1e-6 * x # convert to metres from um
>
>     return const*2*h*c**2 / (x**5 * (np.exp(h*c / (x*k*T)) - 1)) #J/s/m2/m
>
>
> def powerlaw(x,A,p):
>
>     return A*x**p
>
>
> mod= Model(bb) + Model(powerlaw)
>
> pars  = mod.make_params(T=1100,const=2*1e-21,A=2*1e-11,p=-1.0)
>
>  result = mod.fit(y,pars,x=x)
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy


More information about the AstroPy mailing list