From 19bf558c78161a3b0f97c760b13428220749cd65 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 6 Dec 2022 17:17:03 +0000 Subject: [PATCH 1/2] feat: Hotelling t-squared p-value for correlated fits added. --- pyerrors/fits.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pyerrors/fits.py b/pyerrors/fits.py index fdc11feb..c7dac075 100644 --- a/pyerrors/fits.py +++ b/pyerrors/fits.py @@ -25,6 +25,12 @@ class Fit_result(Sequence): fit_parameters : list results for the individual fit parameters, also accessible via indices. + chisquare_by_dof : float + reduced chisquare. + p_value : float + p-value of the fit + t2_p_value : float + Hotelling t-squared p-value for correlated fits. """ def __init__(self): @@ -50,6 +56,8 @@ class Fit_result(Sequence): my_str += '\u03C7\u00b2/\u03C7\u00b2exp = ' + f'{self.chisquare_by_expected_chisquare:2.6f}' + '\n' if hasattr(self, 'p_value'): my_str += 'p-value = ' + f'{self.p_value:2.4f}' + '\n' + if hasattr(self, 't2_p_value'): + my_str += 't\u00B2p-value = ' + f'{self.t2_p_value:2.4f}' + '\n' my_str += 'Fit parameters:\n' for i_par, par in enumerate(self.fit_parameters): my_str += str(i_par) + '\t' + ' ' * int(par >= 0) + str(par).rjust(int(par < 0.0)) + '\n' @@ -633,6 +641,11 @@ def _standard_fit(x, y, func, silent=False, **kwargs): output.chisquare = chisquare output.dof = x.shape[-1] - n_parms output.p_value = 1 - scipy.stats.chi2.cdf(output.chisquare, output.dof) + # Hotelling t-squared p-value for correlated fits. + if kwargs.get('correlated_fit') is True: + n_cov = np.min(np.vectorize(lambda x: x.N)(y)) + output.t2_p_value = 1 - scipy.stats.f.cdf((n_cov - output.dof) / (output.dof * (n_cov - 1)) * output.chisquare, + output.dof, n_cov - output.dof) if kwargs.get('resplot') is True: residual_plot(x, y, func, result) From 850be29d20be07f5afab5613ab0154c9cc027a98 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 6 Dec 2022 17:18:16 +0000 Subject: [PATCH 2/2] tests: test for Hotelling t-squared p-value added. --- tests/fits_test.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/fits_test.py b/tests/fits_test.py index 41d876c7..828c0cbe 100644 --- a/tests/fits_test.py +++ b/tests/fits_test.py @@ -214,6 +214,15 @@ def test_correlated_fit(): assert(diff.is_zero_within_error(sigma=5)) +def test_hotelling_t(): + tt1 = pe.Obs([np.random.rand(50)], ["ens"]) + tt1.gamma_method() + tt2 = pe.Obs([np.random.rand(50)], ["ens"]) + tt2.gamma_method() + ft = pe.fits.least_squares([1, 2], [tt1, tt2], lambda a, x: a[0], correlated_fit=True) + assert ft.t2_p_value >= ft.p_value + + def test_fit_corr_independent(): dim = 30 x = np.arange(dim)