diff --git a/image_analysis/scale_merge/SearchSpaceGroup.cpp b/image_analysis/scale_merge/SearchSpaceGroup.cpp index d4d4f4df8..de1790b91 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.cpp +++ b/image_analysis/scale_merge/SearchSpaceGroup.cpp @@ -731,6 +731,10 @@ SearchSpaceGroupResult SearchSpaceGroup( // floor a-dependent, on a quantity that has no a. Leave it alone. auto merge_systematic_b = [&](const Orbits& orb) -> double { std::vector> obs; // I, sigma, deviation-from-orbit-mean + long n_orbits = 0; // orbits contributing, for the degrees of freedom + for (const Acc& g : orb.acc) + if (g.n >= 2) + ++n_orbits; for (size_t i = 0; i < n; ++i) { if (orb.orbit[i] < 0) continue; @@ -739,13 +743,19 @@ SearchSpaceGroupResult SearchSpaceGroup( continue; obs.push_back({I[i], Sigma[i], I[i] - g.swI / g.sw}); } - if (obs.size() < 20) + // Each deviation is taken from a mean fitted on its own orbit, so one degree of freedom per + // orbit is spent: dof = sum(n-1) = N - G, the same denominator chi2_under uses above. Dividing + // by N instead understates the reduced chi^2 and so overstates the b that brings it to 1, and + // it does so unevenly - a low-multiplicity parent loses a larger fraction of its dof than the + // higher-multiplicity candidate, which inflates the b RATIO the gates read. + const double dof = static_cast(obs.size()) - static_cast(n_orbits); + if (obs.size() < 20 || dof <= 0.0) return 0.0; auto reduced_chi2 = [&](double b) { double s = 0.0; for (const auto& o : obs) s += o[2] * o[2] / (o[1] * o[1] + (b * o[0]) * (b * o[0])); - return s / static_cast(obs.size()); + return s / dof; }; if (reduced_chi2(0.0) <= 1.0) return 0.0;