ENH: add warm start for root finder in find_degrees_of_freedom#1403
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1403 +/- ##
=========================================
Coverage 95.61% 95.61%
=========================================
Files 825 825
Lines 68487 68596 +109
=========================================
+ Hits 65482 65589 +107
- Misses 3005 3007 +2
... and 2 files with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
|
It seems like all the testing errors aren't related to this PR. The issue is in If I understand the derivation, this approximation only seems valid for |
Find degrees of freedom for `non_central_f` distribution
The degree of freedom finders can call the quantile with arguments which result in an infinite result, as far as our root finders are concerned, any large value as a result will do, but we need to not allow an exception to escape or the root finder terminates.
|
@dschmitz89 : what seems to have happened here is that the new initial guess has exposed an old bug. Basically we're starting from a different location which results in a call to the quantile with df = 0.0012658573267757545 and p = 0.125. The true result of that is -INF at double precision, hence the overflow_error. Interestingly when evaluated without long double support, numeric instability results in a finite double result "by chance" and everything proceeds as normal since any large value will do as far as the root finder is concerned. Updated to catch the exceptions and return an appropriately large value. |
As promised in #1385 I added a similar warm start for the other degrees of freedom finder. It is based on Hill's approximation for the t distribution quantile$t_{p, v}$ with probability $p$ and degrees of freedom $v$ :
where$z_p$ denotes the normal distribution quantile. This was again done with help from an LLM and iterated on manually for more robustness (step 5 in details). I did a few spot checks where I printed out the number of iterations of the root finder and found that it reduced by ca. 30%. I guess the root finder anyway spends most of its time fine tuning the solution to machine precision, so even a good guess does not give a large speedup.
Detailed derivation
Derivation of the Hill-Corrected Initial Guess for Student's$t$ Degrees of Freedom
To improve the numerical efficiency of finding degrees of freedom$\nu$ for a specified statistical power, we derive an analytical "warm start" using Hill's approximation.
1. The Power Equation
The required sample size$n = \nu + 1$ is defined by the relationship between the effect size $\delta$ , standard deviation $\sigma$ , and the quantiles of the $t$ -distribution at significance level $\alpha$ and power $1-\beta$ :
Squaring both sides and rearranging to solve for$\nu$ :
2. Hill's Approximation
We substitute the Student's$t$ quantiles with Hill’s approximation, which corrects the normal quantile $z_p$ for the heavy tails of the $t$ -distribution:
Let$z_a = \Phi^{-1}(1-\alpha)$ and $z_b = \Phi^{-1}(1-\beta)$ . Substituting the approximation into the power equation:
3. First-Order Expansion
Let$S_1 = z_a + z_b$ and $S_2 = \frac{z_a(z_a^2+1) + z_b(z_b^2+1)}{4}$ . The equation becomes:
Expanding the square and truncating terms of order$O(1/\nu^2)$ and higher:
Defining the zero-order normal approximation as$\nu_{norm} + 1 = \left( \frac{\sigma S_1}{\delta} \right)^2$ :
4. Solving for the Correction Term
Assuming$\frac{\nu+1}{\nu} \approx 1$ for the correction term, we isolate the shift from the normal approximation:
Substituting the definitions of$S_1$ and $S_2$ :
5. Algebraic Trick to Avoid Division
Using the sum of cubes identity$a^3 + b^3 = (a+b)(a^2 - ab + b^2)$ , we can factor the numerator:
The$(z_a + z_b)$ terms cancel out, leaving a robust, division-free correction:
Final Guess Formula
The implemented "warm start" is: