Essays/Stirling's Approximation

From J Wiki
Jump to: navigation, search

Stirling's formulae provide approximations to the gamma or factorial functions. They are given in Abramowitz and Stegun Section 6.1.

sbase =: %:@(2p1&%) * %&1x1 ^ ]
scorr =: 1 1r12 1r288 _139r51840 _571r2488320&p.@%
stirlg=: sbase * scorr                         NB. 6.1.37

stirlf=: ^@(1r12&%) * %:@(2p1&*) * %&1x1 ^ ]   NB. 6.1.38

stirlf approximates the factorial function while the more accurate stirlg approximates the gamma function.

g =: stirlg |@-.@% !@<:  NB. relative difference between stirlg and !@<:
f =: stirlf |@-.@% !     NB. relative difference between stirlf and !

   (stirlf , stirlg@>: , !) 10
3.62881e6 3.6288e6 3.6288e6
   f 10
2.7699e_6
   g 11
4.84055e_9

   (stirlf , stirlg@>: , !) 5j4
20.7062j23.4239 20.7061j23.4242 20.7061j23.4242
   f 5j4
1.05637e_5
   g 6j4
4.00025e_8



See also

MathWorld



Contributed by Roger Hui.