From 522a1f152ac030ef9a4c5ed932ceb1f824358f92 Mon Sep 17 00:00:00 2001 From: Simon Kuberski Date: Mon, 15 Nov 2021 17:54:04 +0100 Subject: [PATCH] Bugfix in tests, changed output in case of correlated fits --- pyerrors/fits.py | 10 ++++------ tests/fits_test.py | 12 +++++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pyerrors/fits.py b/pyerrors/fits.py index 5c93506b..b663f6f0 100644 --- a/pyerrors/fits.py +++ b/pyerrors/fits.py @@ -584,18 +584,16 @@ def _standard_fit(x, y, func, silent=False, **kwargs): print('chisquare/d.o.f.:', output.chisquare_by_dof) if kwargs.get('expected_chisquare') is True: - if kwargs.get('correlated_fit') is True: - output.chisquare_by_expected_chisquare = output.chisquare_by_dof - else: + if kwargs.get('correlated_fit') is not True: W = np.diag(1 / np.asarray(dy_f)) cov = covariance_matrix(y) A = W @ jacobian(func)(fit_result.x, x) P_phi = A @ np.linalg.inv(A.T @ A) @ A.T expected_chisquare = np.trace((np.identity(x.shape[-1]) - P_phi) @ W @ cov @ W) output.chisquare_by_expected_chisquare = chisquare / expected_chisquare - if not silent: - print('chisquare/expected_chisquare:', - output.chisquare_by_expected_chisquare) + if not silent: + print('chisquare/expected_chisquare:', + output.chisquare_by_expected_chisquare) fitp = np.concatenate((fit_result.x, [o.value for o in const_par])) hess_inv = np.linalg.pinv(jacobian(jacobian(chisqfunc_aug))(fitp)) diff --git a/tests/fits_test.py b/tests/fits_test.py index 978cc18a..cdd22ee6 100644 --- a/tests/fits_test.py +++ b/tests/fits_test.py @@ -44,8 +44,8 @@ def test_least_squares(): assert math.isclose(chi2_pyerrors, chi2_scipy, abs_tol=1e-10) out = pe.least_squares(x, oy, func, const_par=[beta[1]]) - assert((out.fit_parameters[0] - beta[0]).is_zero) - assert((out.fit_parameters[1] - beta[1]).is_zero) + assert((out.fit_parameters[0] - beta[0]).is_zero()) + assert((out.fit_parameters[1] - beta[1]).is_zero()) num_samples = 400 N = 10 @@ -88,7 +88,9 @@ def test_least_squares(): fitpc = pe.least_squares(x, data, fitf, correlated_fit=True) for i in range(2): - assert((fitp[i] - fitpc[i]).is_zero_within_error) + diff = fitp[i] - fitpc[i] + diff.gamma_method() + assert(diff.is_zero_within_error(sigma=1.5)) def test_total_least_squares(): @@ -130,8 +132,8 @@ def test_total_least_squares(): assert math.isclose(pe.covariance(beta[0], beta[1]), output.cov_beta[0, 1], rel_tol=2.5e-1) out = pe.total_least_squares(ox, oy, func, const_par=[beta[1]]) - assert((out.fit_parameters[0] - beta[0]).is_zero) - assert((out.fit_parameters[1] - beta[1]).is_zero) + assert((out.fit_parameters[0] - beta[0]).is_zero()) + assert((out.fit_parameters[1] - beta[1]).is_zero()) pe.Obs.e_tag_global = 0