[PATCH] Fix overflow and underflow in regr_r2()
[PATCH] Fix overflow and underflow in regr_r2()
Hi,
While looking at the corr() overflow/underflow discussion [1], I noticed
that regr_r2() still computes
(Sxy * Sxy) / (Sxx * Syy)
directly. At very small or very large scales, those products can round
to zero or infinity even when the ratio itself is finite.
For example,
SELECT regr_r2(1e-100 + g * 1e-105,
1e-100 + g * 1e-105)
FROM generate_series(1, 3) g;
returns NaN without the patch, although the inputs are perfectly
correlated and the result should be 1.
corr() already has a stabilized calculation for the same Sxx * Syy
denominator scale. This patch factors that into a helper and lets
regr_r2() use it as a fallback when one of its direct products has
rounded to zero or infinity. Otherwise, regr_r2() keeps the existing
direct formula.
This preserves regr_r2()'s existing SQL-level special cases. The added
tests cover the fallback path and nearby NaN behavior.
Thoughts?
References:
[1] https://www.postgresql.org/message-id/flat/19340-6fb9f6637f562092%40postgresql.org
While looking at the corr() overflow/underflow discussion [1], I noticed
that regr_r2() still computes
(Sxy * Sxy) / (Sxx * Syy)
directly. At very small or very large scales, those products can round
to zero or infinity even when the ratio itself is finite.
For example,
SELECT regr_r2(1e-100 + g * 1e-105,
1e-100 + g * 1e-105)
FROM generate_series(1, 3) g;
returns NaN without the patch, although the inputs are perfectly
correlated and the result should be 1.
corr() already has a stabilized calculation for the same Sxx * Syy
denominator scale. This patch factors that into a helper and lets
regr_r2() use it as a fallback when one of its direct products has
rounded to zero or infinity. Otherwise, regr_r2() keeps the existing
direct formula.
This preserves regr_r2()'s existing SQL-level special cases. The added
tests cover the fallback path and nearby NaN behavior.
Thoughts?
References:
[1] https://www.postgresql.org/message-id/flat/19340-6fb9f6637f562092%40postgresql.org
--
Best regards,
Chengpeng Yan
Re: [PATCH] Fix overflow and underflow in regr_r2()
От:
Chengpeng Yan <chengpeng_yan@outlook.com>
Дата:
Hi, > On May 16, 2026, at 17:39, Dean Rasheed wrote: > >> corr() already has a stabilized calculation for the same Sxx * Syy >> denominator scale. This patch factors that into a helper and lets >> regr_r2() use it as a fallback when one of its direct products has >> rounded to zero or infinity. Otherwise, regr_r2() keeps the existing >> direct formula. > > The comments need work -- in particular float8_regr_r2() needs a > comment explaining the new overflow/underflow checks, similar to the > comment in float8_corr(). In fact, doing that, I think it's preferable > to just keep this change local to float8_regr_r2(), rather than > refactoring into a helper function for just a few lines of code. > > This new check in float8_regr_r2(): > > + if (Sxy == 0 && !isnan(Sxx) && !isnan(Syy)) > + PG_RETURN_FLOAT8(0.0); > > seems pointless. It's optimising for a special case that will very > rarely occur in practice, and which is handled fine by the general > code. We don't want to slow down the common code path for such rare > special cases. > > I noticed that this new overflow test case: > > +SELECT regr_r2(1e154::float8 * g, 1e154::float8 * g) > + FROM generate_series(1, 2) g; > + regr_r2 > +--------- > + 1 > +(1 row) > > only produces 1 because it's run with a reduced extra_float_digits > value. I think it's better to use the test values "1e100 + g * 1e95", > which still trigger the overflow on HEAD, but more reliably produce 1, > regardless of the extra_float_digits setting, making it less likely > that there will be variations between platforms. That's also more > consistent with the other nearby test cases. Thanks for the thorough review and feedback — I learned a lot from it! > Attached is a v2 patch with those changes, plus a little more tidying > up of the regression tests. v2 LGTM. Thanks for the updates and test cleanup. -- Best regards, Chengpeng Yan
Re: [PATCH] Fix overflow and underflow in regr_r2()
On Sat, 23 May 2026 at 03:42, Chengpeng Yan wrote: > > Thanks for the regr_intercept.patch. The approach looks good to me. Thanks for reviewing, and sorry for the delay getting back to you. > 2. `dy` seems a bit hard to understand. Perhaps `offset`, as used in the > earlier sketch, would be clearer? [Shrug] I think dy is common enough to denote a difference in y-values, and it seems clear enough, given the large comment above it. > 3. Do we need to add tests for the underflow path, and perhaps for the > Inf/NaN guard? Yeah, I think it makes sense to include a test with underflow, since that really can lead to a large relative error. I don't think it's worth testing the Inf/NaN guard, since that's more about avoiding operating on technically uninitialised variables, and I don't believe that it actually affects the results. I've add this test case: SELECT regr_intercept(y, x) FROM (VALUES (-1e-131, 0), (2e-131, 3e-131)) v(x, y); Here, directly computing Sx * Sxy / Sxx causes an underflow to zero, while the correct result should be 1e-131. Since Sy is 3e-131, this makes a noticeable difference to the final result (without the patch, it returns an intercept of 1.5e-131, whereas with the patch, it correctly returns 1e-131). If there are no objections from the RMT, I'll push both of these (to HEAD only) in a couple of days or so. Regards, Dean
Re: [PATCH] Fix overflow and underflow in regr_r2()
От:
Chengpeng Yan <chengpeng_yan@outlook.com>
Дата:
Hi, > On May 28, 2026, at 20:37, Dean Rasheed wrote: > > [Shrug] I think dy is common enough to denote a difference in > y-values, and it seems clear enough, given the large comment above it. Makes sense, thanks for the clarification. >> 3. Do we need to add tests for the underflow path, and perhaps for the >> Inf/NaN guard? > > Yeah, I think it makes sense to include a test with underflow, since > that really can lead to a large relative error. I don't think it's > worth testing the Inf/NaN guard, since that's more about avoiding > operating on technically uninitialised variables, and I don't believe > that it actually affects the results. > > I've add this test case: > > SELECT regr_intercept(y, x) FROM (VALUES (-1e-131, 0), (2e-131, > 3e-131)) v(x, y); > > Here, directly computing Sx * Sxy / Sxx causes an underflow to zero, > while the correct result should be 1e-131. Since Sy is 3e-131, this > makes a noticeable difference to the final result (without the patch, > it returns an intercept of 1.5e-131, whereas with the patch, it > correctly returns 1e-131). > > If there are no objections from the RMT, I'll push both of these (to > HEAD only) in a couple of days or so. > > Regards, > Dean > v2 LGTM. I ran the regression tests locally on Apple Silicon as well, and all tests passed. -- Best regards, Chengpeng Yan
Re: [PATCH] Fix overflow and underflow in regr_r2()
От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Fri, 29 May 2026 at 05:23, Chengpeng Yan wrote: > > > On May 28, 2026, at 20:37, Dean Rasheed wrote: > > > > If there are no objections from the RMT, I'll push both of these (to > > HEAD only) in a couple of days or so. > > v2 LGTM. > > I ran the regression tests locally on Apple Silicon as well, and all > tests passed. OK, I have pushed both patches. Thanks for reviewing and testing. Regards, Dean