diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index 3ab866fba..6ba0b8e7c 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -109,6 +109,29 @@ bool FreeSetPrefers(const std::vector &r_terms, double nll_free, const st const double se = n > 1 ? std::sqrt(std::max(sum2 - sum * sum / n, 0.0) * n / (n - 1)) : 0.0; return nll_free < nll_free_b && sum < se; } +// The upper alpha point of the F distribution with d1 and d2 degrees of freedom, by Paulson's normal approximation of +// F^(1/3) (z the normal deviate of alpha) - accurate to a few percent at the counts a displacement model has. +// Following Paulson (1942) Ann. Math. Statist. 13, 233-235 +double FQuantile(double d1, double d2, double z) { + const double u = 2.0 / (9.0 * d1), v = 2.0 / (9.0 * d2), a = 1.0 - v, c = 1.0 - u; + const double q = a * a - z * z * v; + const double x = (a * c + std::sqrt(std::max(a * a * c * c - q * (c * c - z * z * u), 0.0))) / q; + return x * x * x; +} + +// Of two displacement models, the one with more parameters is taken only where R-work falls by more than its extra +// parameters account for: R_small / R_large above Hamilton's ratio sqrt(b / (n - m) F(b, n - m, 0.05) + 1), n the +// working reflections, m the larger model's displacement parameters (the trials hold the positions) and b its extra +// ones - each counted unrestrained, the conservative count. True where `t` has no more parameters than `s`. +// Following Hamilton (1965) Acta Cryst. 18, 502-510, as Merritt (2012) Acta Cryst. D68, 468-477 and PDB-REDO decide +// the B model by it +bool HamiltonPasses(const RefinementWeightTrial &t, const RefinementWeightTrial &s, int n_work) { + const int b = t.adp_parameters - s.adp_parameters; + if (b <= 0) + return true; + const int dof = n_work - t.adp_parameters; + return dof > 0 && s.r_work / t.r_work > std::sqrt(b / double(dof) * FQuantile(b, dof, 1.6449) + 1.0); +} // Every other step is taken on the work-set target (WorkTarget) where it lowers it by more than this, relative. constexpr double TARGET_TOLERANCE = 1e-4; // The rounds of RefineOnce's weight and displacement-model choice (4b.): the first from the model as given, each @@ -2539,6 +2562,13 @@ TrialOutcome RunTrial(TrialSlot slot, XyzCalibrations &calibrations, const std:: t.ncs_rmsd = NcsRmsd(*ncs, ctx.XyzB()); if (hyp != nullptr) t.adp = hyp->name; + // the displacement parameters: a B per refinable atom, the TLS bodies' 20 each beside it, or a U per atom + int atoms_b = 0; + for (int i : refinable) + atoms_b += !ctx.atoms[i]->is_hydrogen(); + t.adp_parameters = kind == AdpHypothesis::Kind::Anisotropic ? 6 * static_cast(u_atoms.size()) + : kind == AdpHypothesis::Kind::Tls ? atoms_b + 20 * static_cast(hyp->bodies.size()) + : atoms_b; MeasureWorkTarget(ctx, topo, ncs, ncs_sigma, t); out.line = fmt::format("{}X-ray weight {:.3g}, {} {}{}: R-work {:.4f}, R-free {:.4f}{}, target {:.6g}, rmsd bonds " "{:.4f} A, angles {:.2f} deg{}{}", @@ -4377,7 +4407,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector(k); } // (the model where the weights ended keeps the displacement model a round before chose for it, its U held by diff --git a/image_analysis/structure_refinement/Refinement.h b/image_analysis/structure_refinement/Refinement.h index ecd8a6acc..c745f254c 100644 --- a/image_analysis/structure_refinement/Refinement.h +++ b/image_analysis/structure_refinement/Refinement.h @@ -163,6 +163,7 @@ struct RefinementWeightTrial { // The work-set target's terms where the trial ended (RefineOnce's WorkTarget): -ln L of the working reflections, // the restraints' sum of the geometry (with the NCS restraints at ncs_sigma) and that of the B similarity. double nll_work = NAN, e_geometry = NAN, e_b = NAN; + int adp_parameters = 0; // the displacement model's parameters (HamiltonPasses) std::vector free_r_terms; // R-free as one term per free twin group (FreeRTerms), for paired comparisons GeometryStatistics geometry; bool within_bounds = false; // the geometry bounds the choice is made inside