ZH: figlib: report chi**2 when fitting

This commit is contained in:
Eric Teunis de Boone 2023-02-20 17:37:38 +01:00
parent 06e4cd9f00
commit 84b7280a8b

View file

@ -98,6 +98,7 @@ def phase_comparison_figure(
def fitted_histogram_figure( def fitted_histogram_figure(
amplitudes, amplitudes,
fit_distr = None, fit_distr = None,
calc_chisq = True,
text_kwargs={}, text_kwargs={},
hist_kwargs={}, hist_kwargs={},
mean_snr = None, mean_snr = None,
@ -169,6 +170,14 @@ def fitted_histogram_figure(
ax.plot(xs, fit_ys*scale, label=label) 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 # change parameters if needed
text_fit_params = fit_params2text_params(fit_params) text_fit_params = fit_params2text_params(fit_params)
@ -176,6 +185,8 @@ def fitted_histogram_figure(
[label] [label]
+ +
[ f"{param} = {value: .2e}" for param, value in zip_longest(param_names, text_fit_params, fillvalue='?') ] [ f"{param} = {value: .2e}" for param, value in zip_longest(param_names, text_fit_params, fillvalue='?') ]
+
chisq_strs
) )
this_info = { this_info = {
@ -185,6 +196,10 @@ def fitted_histogram_figure(
'text_str': text_str, 'text_str': text_str,
} }
if chisq_strs:
this_info['chisq'] = c2t[0]
this_info['dof'] = len(fit_params)
fit_info.append(this_info) fit_info.append(this_info)
loc = (0.02, 0.95) loc = (0.02, 0.95)