Chapter 16
Both of these functions will require a factorial calculation, n!. We've seen several
variations on the fractions theme. We'll use the following one:
@lru_cache(128)
def fact(k):
if k < 2: return 1
return reduce(operator.mul, range(2, int(k)+1))
This is k!=Π 2 ≤≤i k i: a product of numbers from 2 to k (inclusive). We've omitted the
unit test cases.
Computing the partial gamma value
The partial gamma function has a simple series expansion. This means that we're
going to compute a sequence of values and then do a sum on those values. For more
information, visit http://dlmf.nist.gov/8.
()
()
0
1
,
!
k sk
k
z
sz
ks k
γ
+
≤≤∞
−
=
+
∑
This series will have a sequence of terms that—eventually—become too small to be
relevant. The calculation ()− 1 k will yield alternating signs:
−=1 1^0 , 1−=^1 −−1, 1 1^2 =−, 1^3 =− 1
The sequence of terms looks like this with s=1 and z=2:
2/1, -2/1, 4/3, -2/3, 4/15, -4/45, ..., -2/638512875
At some point, each additional term won't have any significant impact on the result.
When we look back at the cumulative distribution function, Fx();k, we can consider
working with fractions.Fraction values. The degrees of freedom, k, will be an
integer divided by 2. The X^2 value, x, may be either a Fraction or a float value; it
will rarely be a simple integer value.
When evaluating the terms of γ, the value of
()^1
!
k
k
−
will involve integers and can be
represented as a proper Fraction value. The value of
zsk+
could be a Fraction or
float value; it will lead to irrational values when sk+ is not an integer value. The
value of sk+ will be a proper Fraction value, sometimes it will have the integer
values, and sometimes it will have values that involve 1/2.