Functional Python Programming

(Wang) #1
Chapter 16

These can also be shown as proper Fraction values. The irrational values lead to
large, hard-to-read fractions. We can use something like this:





g= Gamma_Half(Fraction(3,2))








g.limit_denominator(2000000)





Fraction(291270, 328663)


This provides a value where the denominator has been limited to be in the range of 1
to 2 million; this provides pleasant-looking six-digit numbers that we can use for unit
test purposes.


Computing the odds of a distribution being random


Now that we have the incomplete gamma function, gamma, and complete gamma
function, Gamma_Half, we can compute the X^2 CDF values. The CDF value shows
us the odds of a given X^2 value being random or having some possible correlation.


The function itself is quite small:


def cdf(x, k):


"""X² cumulative distribution function.


:param x: X² value -- generally sum (obs[i]-exp[i])**2/exp[i]


for parallel sequences of observed and expected values.:
param k: degrees of freedom >= 1; generally len(data)-1


"""


return 1-gamma(Fraction(k,2),
Fraction(x/2))/Gamma_Half(Fraction(k,2))


We included some docstring comments to clarify the parameters. We created
proper Fraction objects from the degrees of freedom and the chi-squared value,
x. When converting a float value to a Fraction object, we'll create a very large
fractional result with a large number of entirely irrelevant digits.


We can use Fraction(x/2).limit_denominator(1000) to limit the size of the
x/2 Fraction method to a respectably small number of digits. This will compute
a correct CDF value, but won't lead to gargantuan fractions with dozens of digits.


Here are some sample data called from a table of X^2. Visit http://en.wikipedia.
org/wiki/Chi-squared_distribution for more information.

Free download pdf