Repository navigation
Gamma.hf's derivatives keep their digits from y = 30 (#777, Gamma.hf derivatives) - #786
Merged
Merged
Conversation
…derivatives) Below y = beta x = 1000 the Gamma hazard was f / S, exp(log f - log S), whose derivatives in x and alpha are differences of terms of size 1 that cancel to a size 1 / y: alpha's was 2e-7 off mpmath's at y = 100 and 4e-5 at 1000, x's 8e-8 at 1000, and the second derivative in alpha off by more than itself. The continued fraction of the upper incomplete gamma now takes over from y = 30 (and a + 2 sqrt(a) + 1), as an autograd primitive rather than a traced loop (traced from y = 10 it made a Gamma PH fit 1.7 times slower). Its derivative in y is r (a - 1) g / y, with no cancelling terms; in alpha the fraction's own derivative, a complex step through the same Lentz loop (about the cost of the value); the second derivatives are carried along the loop (forward mode), so a Hessian is exact. Values, first and second derivatives are within 1e-14 of mpmath from y = 30 up. Six GammaPH/AFT fits take 1.01 times as long in all (1.08 at most, on data far into the tail; best of runs alternated with develop's). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TRhKL2fJBiAAfNo9rihwts
This was referenced Oct 9, 2026
derrynknife
pushed a commit
that referenced
this pull request
Oct 10, 2026
The derivatives of log P and log Q in the shape were five-point differences of scipy's values: 1e-10 off mpmath's typically, up to 4e-9 (at a = 0.5, x = 1), wherever the Gamma's hazard is taken from them (a standardised time below 30; above it the hazard has its own fraction, #786). They are now carried along the series of P (below x = a + 1) and Legendre's continued fraction of Q (at or above it), in real forward mode, each to rounding: within 1e-14 of mpmath, median 3e-16. Where neither settles (a shape of millions with x near it), and at x of 0 or inf, they are differenced as before; the shape curvature a Hessian takes is the difference of the exact first derivative. Decided with the user: the cost is accepted. A GammaPH fit on 400 rows takes 0.32 s where it took 0.21 s (GammaAFT 0.17 to 0.24 s, a censored Gamma 0.04 to 0.05 s); the estimates are the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TRhKL2fJBiAAfNo9rihwts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #777.
Below y = beta x = 1000 the Gamma hazard was f / S, exp(log f - log S),
whose derivatives in x and alpha are differences of terms of size 1
that cancel to a size 1 / y: alpha's was 2e-7 off mpmath's at y = 100
and 4e-5 at 1000, x's 8e-8 at 1000, and the second derivative in alpha
off by more than itself.
The continued fraction of the upper incomplete gamma now takes over
from y = 30 (and a + 2 sqrt(a) + 1), as an autograd primitive rather
than a traced loop (traced from y = 10 it made a Gamma PH fit 1.7 times
slower). Its derivative in y is r (a - 1) g / y, with no cancelling
terms; in alpha the fraction's own derivative, a complex step through
the same Lentz loop (about the cost of the value); the second
derivatives are carried along the loop (forward mode), so a Hessian is
exact. Values, first and second derivatives are within 1e-14 of mpmath
from y = 30 up. Six GammaPH/AFT fits take 1.01 times as long in all
(1.08 at most, on data far into the tail; best of runs alternated with
develop's).
🤖 Generated with Claude Code
https://claude.ai/code/session_01TRhKL2fJBiAAfNo9rihwts
Generated by Claude Code