Skip to content

Gamma.hf's derivatives keep their digits from y = 30 (#777, Gamma.hf derivatives) - #786

Merged
derrynknife merged 1 commit into
developfrom
claude/surpyval-next-steps-7uatqe
Oct 9, 2026
Merged

derrynknife merged 1 commit into
developfrom
claude/surpyval-next-steps-7uatqe

Conversation

@derrynknife

Copy link
Copy Markdown
Owner

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

…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
@derrynknife
derrynknife merged commit 57f156d into develop Oct 9, 2026
5 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants