Merge branch 'develop' into documentation

This commit is contained in:
fjosw 2023-03-09 15:33:17 +00:00
commit 6fbf4d30c4
2 changed files with 54 additions and 4 deletions

View file

@ -358,11 +358,11 @@ def least_squares(x, y, func, priors=None, silent=False, **kwargs):
if not fit_result.success:
raise Exception('The minimization procedure did not converge.')
if x_all.shape[-1] - n_parms > 0:
output.chisquare = chisquare
output.dof = x_all.shape[-1] - n_parms + len(loc_priors)
output.chisquare = chisquare
output.dof = x_all.shape[-1] - n_parms + len(loc_priors)
output.p_value = 1 - scipy.stats.chi2.cdf(output.chisquare, output.dof)
if output.dof > 0:
output.chisquare_by_dof = output.chisquare / output.dof
output.p_value = 1 - scipy.stats.chi2.cdf(output.chisquare, output.dof)
else:
output.chisquare_by_dof = float('nan')

View file

@ -1039,6 +1039,29 @@ def test_constrained_and_prior_fit():
assert np.isclose(out.chisquare_by_dof, alt_out.chisquare_by_dof, atol=1e-5, rtol=1e-6)
def test_prior_fit_different_methods():
dim = 5
x = np.arange(dim)
y = 2 * x + 0.5 + np.random.normal(0.0, 0.3, dim) + 0.02 * x ** 5
yerr = [0.3] * dim
oy = []
for i, item in enumerate(x):
oy.append(pe.pseudo_Obs(y[i], yerr[i], 'test'))
def func(a, x):
return a[0] * x + a[1] + a[2] * x ** 5
for priors in [None, {1: "0.5(4)"}, ["2(1)", "0.6(3)", "0(5)"]]:
chisquare_list = []
for method in ["Levenberg-Marquardt", "migrad", "Powell"]:
fr = pe.least_squares(x, oy, func, silent=True, priors=priors, method=method)
print(fr.iterations)
chisquare_list.append(fr.chisquare)
assert np.allclose(chisquare_list[0], chisquare_list[1:])
def test_resplot_lists_in_dict():
xd = {
'a': [1, 2, 3],
@ -1057,6 +1080,33 @@ def test_resplot_lists_in_dict():
fitp = pe.fits.least_squares(xd, yd, fd, resplot=True)
def test_fit_dof():
def func(a, x):
return a[1] * anp.exp(-x * a[0])
dof = []
cd = []
for dim in [2, 3]:
x = np.arange(dim)
y = 2 * np.exp(-0.3 * x) + np.random.normal(0.0, 0.3, dim)
yerr = [0.3] * dim
oy = []
for i, item in enumerate(x):
oy.append(pe.pseudo_Obs(y[i], yerr[i], 'test'))
for priors in [None, {0: "0(2)"}]:
fr = pe.least_squares(x, oy, func, silent=True, priors=priors)
dof.append(fr.dof)
cd.append(fr.chisquare_by_dof)
assert np.allclose(dof, [0, 1, 1, 2])
assert cd[0] != cd[0] # Check for nan
assert np.all(np.array(cd[1:]) > 0)
def fit_general(x, y, func, silent=False, **kwargs):
"""Performs a non-linear fit to y = func(x) and returns a list of Obs corresponding to the fit parameters.