Returns the incomplete gamma function for LOWER or UPPER tail.
(a, x, tail=UPPER)
| 828 | return exp(gammaln(x)) |
| 829 | |
| 830 | def gammai(a, x, tail=UPPER): |
| 831 | """ Returns the incomplete gamma function for LOWER or UPPER tail. |
| 832 | """ |
| 833 | |
| 834 | # Series approximation to the incomplete gamma function. |
| 835 | def gs(a, x, epsilon=3.e-7, iterations=700): |
| 836 | ln = gammaln(a) |
| 837 | s = 1.0 / a |
| 838 | d = 1.0 / a |
| 839 | for i in range(1, iterations): |
| 840 | d = d * x / (a + i) |
| 841 | s = s + d |
| 842 | if abs(d) < abs(s) * epsilon: |
| 843 | return (s * exp(-x + a * log(x) - ln), ln) |
| 844 | raise StopIteration, (abs(d), abs(s) * epsilon) |
| 845 | |
| 846 | # Continued fraction approximation of the incomplete gamma function. |
| 847 | def gf(a, x, epsilon=3.e-7, iterations=200): |
| 848 | ln = gammaln(a) |
| 849 | g0 = 0.0 |
| 850 | a0 = 1.0 |
| 851 | b0 = 0.0 |
| 852 | a1 = x |
| 853 | b1 = 1.0 |
| 854 | f = 1.0 |
| 855 | for i in range(1, iterations): |
| 856 | a0 = (a1 + a0 * (i - a)) * f |
| 857 | b0 = (b1 + b0 * (i - a)) * f |
| 858 | a1 = x * a0 + a1 * i * f |
| 859 | b1 = x * b0 + b1 * i * f |
| 860 | if a1 != 0.0: |
| 861 | f = 1.0 / a1 |
| 862 | g = b1 * f |
| 863 | if abs((g - g0) / g) < epsilon: |
| 864 | return (g * exp(-x + a * log(x) - ln), ln) |
| 865 | g0 = g |
| 866 | raise StopIteration, (abs((g-g0) / g)) |
| 867 | |
| 868 | if a <= 0.0: |
| 869 | return 1.0 |
| 870 | if x <= 0.0: |
| 871 | return 1.0 |
| 872 | if x < a + 1: |
| 873 | if tail == LOWER: |
| 874 | return gs(a, x)[0] |
| 875 | return 1 - gs(a, x)[0] |
| 876 | else: |
| 877 | if tail == UPPER: |
| 878 | return gf(a, x)[0] |
| 879 | return 1 - gf(a, x)[0] |
| 880 | |
| 881 | #--- COMPLEMENTARY ERROR FUNCTION ------------------------------------------------------------------ |
| 882 | # Based on: http://www.johnkerl.org/python/sp_funcs_m.py.txt, Tom Loredo |
no test coverage detected
searching dependent graphs…