Repository navigation
Expand file tree
/
Copy pathch16_ex3.py
More file actions
166 lines (146 loc) · 4.48 KB
/
Copy pathch16_ex3.py
File metadata and controls
166 lines (146 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
"""Functional Python Programming
Chapter 16, Example Set 3
"""
from functools import lru_cache, reduce
import operator
from fractions import Fraction
import math
import warnings
@lru_cache(128)
def fact(k):
"""Simple factorial of a Fraction or an int.
>>> fact(1)
1
>>> fact(2)
2
>>> fact(3)
6
>>> fact(4)
24
"""
if k < 2: return 1
return reduce( operator.mul, range(2, int(k)+1) )
def gamma(s, z):
"""Incomplete gamma function.
>>> import math
>>> round(float(gamma(1, 2)),7)
0.8646647
>>> round(1-math.exp(-2),7)
0.8646647
>>> round(float(gamma(1, 3)),7)
0.9502129
>>> round(1-math.exp(-3),7)
0.9502129
>>> round(float(gamma(Fraction(1,2), Fraction(2))),7)
1.6918067
>>> round(math.sqrt(math.pi)*math.erf(math.sqrt(2)),7)
1.6918067
>>> g= gamma(Fraction(1,2), Fraction(2)).limit_denominator(1000000)
>>> g
Fraction(144438, 85375)
>>> round(float(g),7)
1.6918067
"""
def terms(s, z):
for k in range(100):
t2= Fraction(z**(s+k))/(s+k)
term= Fraction((-1)**k,fact(k))*t2
yield term
warnings.warn( "More than 100 terms" )
def take_until(function, iterable):
for v in iterable:
if function(v): return
yield v
ε= 1E-8
return sum( take_until(lambda t:abs(t) < ε, terms(s, z) ) )
pi = Fraction(5419351, 1725033)
# Fraction(817696623, 260280919)
sqrt_pi = Fraction(677622787, 382307718)
# Fraction(582540, 328663) # Good for almost all test cases but one.
def Gamma_Half(k):
"""Gamma(k) with special case for k = n+1/2; k-1/2=n.
>>> import math
>>> Gamma_Half(2)
1
>>> Gamma_Half(3)
2
>>> Gamma_Half(4)
6
>>> Gamma_Half(5)
24
>>> g= Gamma_Half(Fraction(1,2)) # Varies with sqrt_pi setting
>>> g.limit_denominator(2000000)
Fraction(582540, 328663)
>>> round(float(g), 7)
1.7724539
>>> round(math.sqrt(math.pi), 7)
1.7724539
>>> g= Gamma_Half(Fraction(3,2)) # Varies with sqrt_pi setting
>>> g.limit_denominator(2000000)
Fraction(291270, 328663)
>>> round(float(g), 7)
0.8862269
>>> round(math.sqrt(math.pi)/2, 7)
0.8862269
"""
if isinstance(k,int):
return fact(k-1)
elif isinstance(k,Fraction):
if k.denominator == 1:
return fact(k-1)
elif k.denominator == 2:
n = k-Fraction(1,2)
return fact(2*n)/(Fraction(4**n)*fact(n))*sqrt_pi
raise ValueError( "Can't compute Γ({0})".format(k) )
def cdf( x, k ):
"""χ² cumulative distribution function.
:param 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
From http://en.wikipedia.org/wiki/Chi-squared_distribution
>>> round(float(cdf(0.004, 1)), 2)
0.95
>>> cdf(0.004, 1).limit_denominator(100)
Fraction(94, 99)
>>> round(float(cdf(10.83, 1)), 3)
0.001
>>> cdf(10.83, 1).limit_denominator(1000)
Fraction(1, 1000)
>>> round(float(cdf(3.94, 10)), 2)
0.95
>>> cdf(3.94, 10).limit_denominator(100)
Fraction(19, 20)
>>> round(float(cdf(29.59, 10)), 3)
0.001
>>> cdf(29.59, 10).limit_denominator(10000)
Fraction(8, 8005)
>>> expected=[0.95, 0.90, 0.80, 0.70, 0.50, 0.30, 0.20, 0.10, 0.05, 0.01, 0.001]
>>> chi2= [0.004, 0.02, 0.06, 0.15, 0.46, 1.07, 1.64, 2.71, 3.84, 6.64, 10.83]
>>> act= [round(float(x), 3) for x in map(cdf, chi2, [1]*len(chi2))]
>>> act
[0.95, 0.888, 0.806, 0.699, 0.498, 0.301, 0.2, 0.1, 0.05, 0.01, 0.001]
From http://www.itl.nist.gov/div898/handbook/prc/section4/prc45.htm
>>> round(float(cdf(19.18, 6)), 5)
0.00387
>>> round(float(cdf(12.5916, 6)), 2)
0.05
>>> cdf(19.18, 6).limit_denominator(1000)
Fraction(3, 775)
From http://www.itl.nist.gov/div898/handbook/prc/section4/prc46.htm
>>> round(float(cdf(12.131, 4)), 5) # 0.01639 shown in reference
0.0164
>>> cdf(12.131, 4).limit_denominator(1000)
Fraction(16, 975)
>>> round(float(cdf(9.488, 4)), 2)
0.05
>>> cdf(9.488, 4).limit_denominator(1000)
Fraction(1, 20)
"""
return 1-gamma(Fraction(k,2), Fraction(x/2))/Gamma_Half(Fraction(k,2))
#return 1-gamma(Fraction(k,2), Fraction(x/2).limit_denominator(1000))/Gamma_Half(Fraction(k,2))
def test():
import doctest
doctest.testmod(verbose=1)
if __name__ == "__main__":
test()