Skip to content

ENH: Compute the first partial moment of Student's t for large nu - #970

Merged
bashtage merged 1 commit into
bashtage:mainfrom
mkzung:fix/studentst-partial-moment-large-nu
Oct 20, 2026
Merged

bashtage merged 1 commit into
bashtage:mainfrom
mkzung:fix/studentst-partial-moment-large-nu

Conversation

@mkzung

@mkzung mkzung commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Closes #969

The n=1 term is now -(nu + z**2) / (nu - 1) * stats.t.pdf(z, nu), so it no longer divides two gamma functions that overflow. The n >= 2 branch already takes its constant from scipy the same way.

Checked against an mpmath closed form for nu from 2.06 to 500, orders 0 to 4 and z from -30 to 8: largest relative error 2e-13. The new test covers nu = 342, 400 and 500.

The first lower partial moment used gamma((nu + 1) / 2) / gamma(nu / 2), which
overflows for nu above about 342, inside the bounds of the distribution, so
partial_moment returned 0.0 or NaN for every odd order.
@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.95%. Comparing base (88c7162) to head (827a551).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #970   +/-   ##
=======================================
  Coverage   98.95%   98.95%           
=======================================
  Files         136      136           
  Lines       30045    30052    +7     
  Branches     2331     2333    +2     
=======================================
+ Hits        29731    29738    +7     
  Misses        256      256           
  Partials       58       58           
Flag Coverage Δ
adder 98.89% <100.00%> (+<0.01%) ⬆️
subtractor 98.89% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bashtage

Copy link
Copy Markdown
Owner

Thanks. Makes sense to use SciPy's t, which probably has better accuracy in some cases. The gamma is clearly a problem, would have been better to use gammaln given the ratio of 2 gamma functions. This is still probably better.

@bashtage
bashtage merged commit c9759fd into bashtage:main Oct 20, 2026
18 checks passed
@bashtage bashtage changed the title BUG: Compute the first partial moment of Student's t for large nu ENH: Compute the first partial moment of Student's t for large nu Oct 20, 2026
jngod2011 pushed a commit to jngod2011/arch that referenced this pull request Oct 31, 2026
…ounds

partial_moment accepts -inf and inf, but the terms z**(n-1) * pdf(z) of the
recursions are inf * 0 there, which is nan. Normal and StudentsT returned
nan for n >= 2, and, since bashtage#970, StudentsT also for n = 1. The terms are 0
at -inf and inf, so the partial moment is 0 at -inf and the moment at inf.
jngod2011 pushed a commit to jngod2011/arch that referenced this pull request Oct 31, 2026
- Highlights and new features: NGARCH (bashtage#966), the VaR backtests (bashtage#965), the
  local block bootstrap (bashtage#967) and bandwidth_method (bashtage#964, and
  phillips_ouliaris here).
- Changes that alter results: the automatic bandwidth entry of bashtage#983 now
  also describes the bandwidth of the kernels with bounded support, the
  fractional bandwidths and the Tukey-Parzen constant of bashtage#964, its S&P 500
  bandwidths are those of the bandwidth attribute (30.96 and 34.50, where it
  said the 31.96 and 35.50 of opt_bandwidth), and it links phillips_ouliaris
  in arch.unitroot.cointegration. The ADF-Z tables of bashtage#972, bashtage#973 and bashtage#980.
- Backward-incompatible changes: strings in object data.
- Bug fixes: bashtage#954, bashtage#970, bashtage#974, bashtage#975, bashtage#977, bashtage#978, bashtage#982, and the constant
  data of the Newey-West bandwidth and mackinnonp at -inf and inf.
- Maintenance: the CI changes and the version file.
- Contributors: galafis, jackiectl2, mkzung and wavebyrd.
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.

StudentsT.partial_moment returns 0 or nan for odd orders when nu is above about 342

2 participants