import numberdb.sage
from math import factorial
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField
def moment(n):
"""Raw moment of the unit-duration standard motion positive-part area."""
if n < 1 or int(n) != n:
raise ValueError('n must be a positive integer')
n = int(n)
alpha = [QQ(factorial(6*j))/(QQ(576)**j*factorial(3*j)*factorial(2*j))
for j in range(n+1)]
beta, ell = [QQ(1)], [QQ(1)]
for j in range(1,n+1):
beta.append(alpha[j]+QQ(3*(2*j-1))/4*beta[j-1])
ell.append((alpha[j]+beta[j])/QQ(2)
+ sum(alpha[k]*ell[j-k]/QQ(6*k-1) for k in range(1,j+1)))
if n%2 == 0:
return QQ(factorial(n))*ell[n]/(QQ(2)**(n//2)*factorial(3*n//2))
field = RealBallField(513)
return (field(factorial(n))*field(ell[n])
/ (field(2)**(QQ(n)/2)*field(QQ(3*n+2)/2).gamma()))
# For example, moment(1) gives the mean.
The recurrences for $K_n$, $Q_n$ and $P_n$, the convolution for $L_n^+$, and the rational prefactors were evaluated exactly over $\mathbb Q$. Even moments are stored as exact rational numbers. Odd moments use an Arb enclosure of $\sqrt{2/\pi}$ at 513 bits, with a measured minimum relative accuracy of 509 bits, and are written to 100 significant digits. Each decimal denotes the interval within one unit of its last place.
All twelve orders were compared with an independent polynomial solution of the Brownian Feynman–Kac resolvent equation and an exact quotient of Airy asymptotic series, using [1], Appendix C.1 and equations (71), (184), and (268). Direct Arb Gamma evaluation at 768 bits checks the half-integer simplification and containment within each written decimal interval. The zeroth moment and the first resolvent polynomial serve as controls. The first two moments also follow from Gaussian marginal and joint-product integrals. Orders one through four agree with Table 8 of [1], and orders one through ten agree with the printed Table 1 of [2]. The attached checks test strict moment log-convexity with $M_0=1$, the identities on database readback, and the one-value program. See checks.py, derivation.md and numerical-evidence.json for methods, controls and measurements.