From 84b7280a8b067c0ed5e8cb27fe1e8cbf71d4deda Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Mon, 20 Feb 2023 17:37:38 +0100 Subject: [PATCH] ZH: figlib: report chi**2 when fitting --- .../airshower_beacon_simulation/lib/figlib.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/simulations/airshower_beacon_simulation/lib/figlib.py b/simulations/airshower_beacon_simulation/lib/figlib.py index 2c53b8c..9bfc25e 100644 --- a/simulations/airshower_beacon_simulation/lib/figlib.py +++ b/simulations/airshower_beacon_simulation/lib/figlib.py @@ -98,6 +98,7 @@ def phase_comparison_figure( def fitted_histogram_figure( amplitudes, fit_distr = None, + calc_chisq = True, text_kwargs={}, hist_kwargs={}, mean_snr = None, @@ -169,6 +170,14 @@ def fitted_histogram_figure( ax.plot(xs, fit_ys*scale, label=label) + chisq_strs = [] + if calc_chisq: + ct = np.diff(distr_func.cdf(bins, *fit_params))*np.sum(counts) + c2t = stats.chisquare(counts, ct, ddof=len(fit_params)) + chisq_strs = [ + f"$\\chi^2$/dof = {c2t[0]: .2g}/{len(fit_params)}" + ] + # change parameters if needed text_fit_params = fit_params2text_params(fit_params) @@ -176,6 +185,8 @@ def fitted_histogram_figure( [label] + [ f"{param} = {value: .2e}" for param, value in zip_longest(param_names, text_fit_params, fillvalue='?') ] + + + chisq_strs ) this_info = { @@ -185,6 +196,10 @@ def fitted_histogram_figure( 'text_str': text_str, } + if chisq_strs: + this_info['chisq'] = c2t[0] + this_info['dof'] = len(fit_params) + fit_info.append(this_info) loc = (0.02, 0.95)