Functional Python Programming

(Wang) #1
Chapter 16

Here are some test cases we can use to confirm that we're computing this properly:


• ()

γ 1, 2 1=−e−^2 ≈0.8646647

• ()

γ 1, 3 1=−e−^3 ≈0.9502129

• ()

(^1) ,2 erf 2 1.6918067
2
γπ=× ≈

The error function, erf(), is another interesting function. We won't look into it here
because it's available in the Python math library.
Our interest is narrowly focused on the chi-squared distribution. We're not generally
interested in the incomplete gamma function for other mathematical purposes.
Because of this, we can narrow our test cases to the kinds of values we expect to be
using. We can also limit the accuracy of the results. Most chi-squared tests involve
three digits of precision. We've shown seven digits in the test data, which is more
than we might properly need.


Computing the complete gamma value


The complete gamma function is a bit more difficult. There are a number of different
approximations. For more information, visit http://dlmf.nist.gov/5. There's
a version available in the Python math library. It represents a broadly useful
approximation that is designed for many situations.


We're not actually interested in a general implementation of the complete gamma
function. We're interested in just two special cases: integer values and halves. For
these two special cases, we can get exact answers, and don't need to rely on an
approximation.


For integer values, Γnn=−()1 !. The gamma function for integers can rely on the
factorial function we defined previously.


For halves, there's a special form:


1 ()2n!
2 4 n!

n
n

Γ += π

This includes an irrational value, so we can only represent this approximately using
float or Fraction objects.

Free download pdf