pyfli.solver.shared_metrics#
Centralize tau ordering, lifetime summaries, FRET efficiency, and fit-quality metrics.
This module belongs to pyfli.solver and is part of PyFLI least-squares, maximum-
likelihood, CPU, GPU, binned, and global FLI fitting routines. Public API includes
functions enforce_tau_ordering(), poisson_deviance(),
expected_poisson_deviance(), reduced_poisson_deviance(),
pearson_chi_square(), compute_fli_stats(), compute_pearson_stats(),
compute_average_lifetime(), and compute_fret_efficiency().
Goodness of fit is reported as the Poisson deviance D (the likelihood-ratio
statistic for photon counts). At low counts the expected deviance of a gate is not 1
(about 0.47 at 0.1 expected counts, 1.15 at 1), so the reduced value divides D by
its expectation under the fitted model, sum_k E[D_k](mu_k) - p (Kaastra 2017,
A&A 605, A51), instead of n - p; it averages 1 for a correct model at any count
level. The former Pearson statistic, whose variance is floored at 1 count and which
therefore reads below 1 whenever many gates hold less than one expected count, is
still available as pearson_chi_square() / compute_pearson_stats().
Functions
|
|
|
Compute average lifetime. |
|
Compute FLI fit statistics. |
|
Compute FRET efficiency. |
|
|
|
Enforce tau ordering. |
|
Expected Poisson deviance |
|
Pearson chi-square |
|
Poisson deviance |
|
|
- enforce_tau_ordering(popt, perr=None, pcov=None, bounds=None)[source]#
Enforce tau ordering.
- Parameters:
popt (
np.ndarray) – Optimized model parameter vector.perr (
Any | None) – One-standard-deviation parameter uncertainty estimates.pcov (
np.ndarray | None) – Parameter covariance matrix.bounds (
tuple[np.ndarray,np.ndarray] | None) – Optional (low, high) bound vectors used for the fit. When either tau1 or tau2 was pinned by the caller (low == high), that parameter’s slot is left alone.
- Returns:
Tuple containing the reordered parameter vector and any reordered uncertainty or covariance data.
- Return type:
tuple[Any,]
- poisson_deviance(model, data, axis=-1)[source]#
Poisson deviance
2 * sum(mu - d + d * ln(d / mu))along axis (gates withd <= 0contribute2 * (mu - d)). For counts drawn from the model,deviance / (sum E[D_k] - p)averages 1 (seereduced_poisson_deviance()).
- expected_poisson_deviance(model)[source]#
Expected Poisson deviance
E[2 * (mu - d + d * ln(d / mu))]of a gate with expected count mu (element-wise), ford ~ Poisson(mu): about2 mu ln(1/mu)formu -> 0, peaks near 1.15 atmu ~ 1and tends to1 + 1/(6 mu)for largemu. Tabulated exactly belowmu = 10.
- reduced_poisson_deviance(model, data, n_params, axis=-1)[source]#
(D, D_reduced): the Poisson deviance along axis and the deviance divided by its expectation under the model minus the number of fitted parameters,D / max(sum E[D_k] - n_params, 1), which averages 1 for a correct model.
- pearson_chi_square(model, data, axis=-1)[source]#
Pearson chi-square
sum((d - mu)^2 / max(mu, 1))along axis – the fit statistic reported before the switch to the Poisson deviance. The variance floor of 1 count makes it read belown - pwhen many gates hold less than one expected count.
- compute_fli_stats(final_model, d_fit, n_params)[source]#
Compute FLI fit statistics.
- Parameters:
final_model (
np.ndarray) – Model decay evaluated at the fitted parameters.d_fit (
np.ndarray) – Measured decay samples over the fitted range.n_params (
int) – Number of fitted model parameters.
- Returns:
(ssr, chi_sq, red_chi_sq, r_sq, rmse): sum of squared residuals, the Poisson deviance (poisson_deviance()), the reduced deviance (reduced_poisson_deviance()), R-squared and RMSE.- Return type:
tuple[Any,]
- compute_pearson_stats(final_model, d_fit, n_params)[source]#
(pearson_chi2, pearson_reduced_chi2)with the former definition (variance floored at 1 count, dof = n - p), for comparison with earlier results.