Repository navigation
Expand file tree
/
Copy pathchi_sq.py
More file actions
588 lines (504 loc) · 16.2 KB
/
Copy pathchi_sq.py
File metadata and controls
588 lines (504 loc) · 16.2 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env python3
# χ² Calculation
# ==============
# .. contents::
# The χ² table in a statistics book enumerates
# values of the χ² Cumulative Distribution Function,
# CDF.
#
# .. math::
#
# F(x;k) = \dfrac{\gamma \left( \frac{k}{2}, \frac{x}{2}\right)}{\Gamma\left(\frac{k}{2}\right)}
#
# See http://en.wikipedia.org/wiki/Chi-squared_distribution
#
# Given a sum-of-squares value, :math:`\chi^2`, and degrees of freedom, :math:`f`.
# We can compute :math:`p=1-F(\chi^2;k)` which is the probability of the
# :math:`\chi^2` being random.
# A value of :math:`p \leq 0.05` means the data is unlikely to be random. A value
# of :math:`p > 0.05` means that the null hypothesis is probably true:
# the data is random. The higher the :math:`p` value, the more likely that
# the null hypothesis is true.
#
# This requires the complete and incomplete gamma functions,
# :math:`\gamma(s,z)` and :math:`\Gamma(t)`. These can be fairly complex
# calculations.
#
# See http://en.wikipedia.org/wiki/Incomplete_gamma_function#Regularized_Gamma_functions_and_Poisson_random_variables
#
# See http://en.wikipedia.org/wiki/Stirling%27s_approximation
#
# See http://dlmf.nist.gov/5 and http://dlmf.nist.gov/8
#
# See http://netlib.org/ Specifically module 542
# Imports
# -------
# Modules required by this module.
import operator
from functools import reduce, lru_cache
from fractions import Fraction
import math
# Factorial
# -----------
# Simple integer factorial is used in a variety of places for computing
# the incomplete and complete gamma function values.
# We'll use this definition of factorial:
#
# .. math::
#
# n! = \prod_{i=1}^{n} i
@lru_cache(128)
def fact(k):
"""Simple factorial.
>>> fact(1)
1
>>> fact(2)
2
>>> fact(3)
6
>>> fact(4)
24
"""
if k < 2: return 1
return reduce( operator.mul, range(2,k+1) )
# The implementation uses ``reduce( operator.mul, ... )`` to compute
# the product of a sequence of integer values.
# We've included the ``@lru_cache`` because this is used often,
# and the small domain of possible values leads to some benefit
# from the cache.
# We could also use ``math.factorial()``. In order to make use of
# the cache, we would need to do something like this.
#
# .. parsed-literal::
#
# fact = lru_cache(128)(math.factorial)
#
# This would create a similarly cached factorial function.
# Incomplete Gamma
# -----------------
# The incomplete (lower) gamma function is this:
#
# .. math::
#
# \gamma(s,z) = \sum_{k=0}^{\infty} \dfrac {(-1)^k} {k!} \; \dfrac {z^{s+k}} {s+k}
#
def gamma(s, z):
"""Incomplete gamma function.
>>> import math
>>> round(gamma(1, 2),7)
0.8646647
>>> round(1-math.exp(-2),7)
0.8646647
>>> round(gamma(1, 3),7)
0.9502129
>>> round(1-math.exp(-3),7)
0.9502129
>>> round(gamma(0.5, 2),7)
1.6918067
>>> round(math.sqrt(math.pi)*math.erf(math.sqrt(2)),7)
1.6918067
"""
def terms(s, z):
for k in range(1000):
term= ((-1)**k/fact(k))*(z**(s+k)/(s+k))
yield term
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) ) )
# The idea here is to compute an infinite sequence of
# values for ``((-1)**k/fact(k))*(z**(s+k)/(s+k))``.
# Or, more readably, :math:`\dfrac {(-1)^k} {k!} \; \dfrac {z^{s+k}} {s+k}`.
# We enumerate these values while they're greater than :math:`\epsilon`.
#
# We wrote our own filter function ``take_until( end_condition, function )``.
# This stops the infinite generation of terms when the end condition
# is met.
#
# One alternative to this is a single tail-recursion, which we can optimize
# to create a simple **for** loop which emits terms until the values are too small
# to be relevant.
#
# .. parsed-literal::
#
# sigma= 0
# for k in range(1000):
# term= ((-1)**k/fact(k))*(z**(s+k)/(s+k))
# if abs(term) < ε: break
# sigma += term
# return sigma
#
# We could futher optimize this function to use stateful
# internal variables in the **for** loop. We could slightly
# improve performance with two changes.
#
# The ``(-1)**k`` expression flips the sign on each term.
# We could use, for example, ``1 if k%2 == 0 else -1``,
# which would somewhat less expensive to evaluate.
#
# Similarly, we don't really need to compute :math:`k!` for
# increasing values of *k*. We could, instead, keep a stateful running
# product calculation that we simply multiply by the next value
# of *k*.
# Gamma Function 1
# -----------------
# The various gamma function approxiations are all slightly
# wrong in various places. We've done three implementations
# in an attempt to locate a "best" function.
#
# The ``math.gamma()`` function is available in Python 3.2.
# We can compare this with our requirements, also.
#
# The complete gamma function has this definition:
#
# .. math::
#
# \Gamma(t) = \dfrac{1}{t} \prod_{k=1}^{\infty} \dfrac{\left(1+\frac{1}{k}\right)^t}{1+\frac{t}{k}}
#
def Gamma1( t ):
"""Gamma Function.
Gamma(n) == fact(n-1)
>>> import math
>>> round(Gamma1(2),0)
1.0
>>> round(Gamma1(3),0)
2.0
>>> round(Gamma1(4),0)
6.0
>>> round(Gamma1(5),0)
24.0
>>> round(Gamma1(.5), 7) # Not quite right
1.7726754
>>> round(math.sqrt(math.pi), 7)
1.7724539
"""
def num_den(t):
for k in range(1,1000):
yield (1+1/k)**t, (1+t/k)
def take_until_star( function, iterable ):
for v in iterable:
if function(*v): return
yield v
prod= lambda x: reduce(operator.mul, x)
fst= lambda x: x[0]
snd= lambda x: x[1]
ε= 1E-8
terms= tuple(take_until_star( lambda n,d: abs(n/d-1)<ε, num_den(t) ))
return prod(map(fst, terms))/(t*prod(map(snd,terms)))
# This involves two products: the numerator product and the denominator
# product.
# The ``num_den()`` function emits an sequence of pairs, ``(n, d)``.
#
# We could consider this as a sequence of pairs like this:
#
# .. math::
#
# \left\lbrace \left\langle \left(1+\frac{1}{k}\right)^t, 1+\frac{t}{k} \right\rangle,
# \ldots \bigl\vert 1 \leq k < \infty \right\rbrace
#
# We wrote our own filter function ``take_until_star( end_condition, function )``.
# This stops the infinite generation of tuples when the end condition for the
# tuple is met. We can't use the simple ``take_util()`` defined above because
# it makes working with tuples a little bit clunky.
# This version works more elegantly with multi-valued tuples.
#
# If the :math:`\frac{n}{d}` fraction is close to 1, we can stop taking values
# from the infinite iterator. We'll save this sequence in a materialized object,
# ``terms`` because we need to do two reductions on the sequence.
#
# We then split the two values in the ``terms`` sequence
# using ``fst()`` and ``snd()`` functions.
# This allows us to compute the numerator and denominator products separately.
# We defer doing the final division to the very end to
# preserve as many bits of accuracy as possible.
#
# Here's an alternative loop construction.
#
# .. parsed-literal::
#
# p_num = p_den = 1
# for n in range(1,1000):
# num, den = (1+1/n)\*\*t, (1+t/n)
# if abs(num/den-1) < ε: break
# p_num \*= num
# p_den \*= den
# return p_num/(t*p_den)
#
# Note that the value of :math:`\Gamma\left(\frac{1}{2}\right)` is very
# close to the defined value of :math:`\sqrt{\pi}`.
# The integer values, however, require rounding to zero places
# to show the expected values.
# Here's a second version using ``Fraction`` objects instead of floats.
# The idea here is to compute the **exact** value to see if -- perhaps --
# the slight discrepancies between actual and expected are due to
# float issues.
# This only works for ``int`` and ``Fraction`` values. It doesn't work
# for arbitrary ``float`` values. That's not a big limitation for this
# application.
def Gamma1f( t ):
"""Gamma Function.
Gamma(n) == fact(n-1)
>>> import math
>>> round(Gamma1f(2),0)
Fraction(1, 1)
>>> round(Gamma1f(3),0)
Fraction(2, 1)
>>> round(Gamma1f(4),0)
Fraction(6, 1)
>>> round(Gamma1f(5),0)
Fraction(24, 1)
>>> round(Gamma1f(Fraction(1,2)), 7) # Not quite right
1.7726754
>>> round(math.sqrt(math.pi), 7)
1.7724539
"""
def num_den(t):
for k in range(1,1000):
yield (1+Fraction(1,k))**t, (1+t/k)
def take_until_star( function, iterable ):
for v in iterable:
if function(*v): return
yield v
prod= lambda x: reduce(operator.mul, x)
fst= lambda x: x[0]
snd= lambda x: x[1]
ε= 1E-8
t_f= Fraction(t)
terms= tuple(take_until_star( lambda n,d: abs(n/d-1)<ε, num_den(t_f) ))
return prod(map(fst, terms))/(t_f*prod(map(snd,terms)))
# We've replaced the division operation in the ``num_den()`` function
# with ``Fraction()``. We've also replaced the argument value, ``t``,
# with a ``Fraction``, ``t_f``.
#
# Two other divisions were left in place because the arguments would be
# ``Fraction`` instances:
#
# - In the ``num_den()`` function, we left a division because the argument,
# ``t`` will be a ``Fraction``.
#
# - The final division (betweem two ``Fraction`` objects) is left in place.
#
# Interestingly, this produces essentially the same results as the previous
# version. This, too, is not very accurate for values near :math:``\frac{1}{2}``.
# Gamma Function 2
# ------------------
# It's not clear that we can do better than ``gamma1()``, but it's worth looking
# at other alternatives. The Nemes approximation has the
# advantage of brevity.
#
# .. math::
#
# \Gamma(z) \sim \sqrt{ \frac{2\pi}{z} } \left( \frac{1}{e} \left(z+\frac{1}{12z-\frac{1}{10z}} \right) \right)^z
#
def Gamma2( z ):
"""Gamma Function. Gergő Nemes version.
Gamma(n) == fact(n-1)
>>> import math
>>> round(Gamma2(2),1)
1.0
>>> round(Gamma2(3),1)
2.0
>>> round(Gamma2(4),1)
6.0
>>> round(Gamma2(5),1)
24.0
>>> round(Gamma2(.5), 7) # Not quite right
1.7630962
>>> round(math.sqrt(math.pi), 7)
1.7724539
"""
t_1= math.sqrt(2*math.pi/z)
t_2= (z+(1/(12*z-(1/(10*z)))))/math.e
return t_1*t_2**z
# We broke the expression into two parts just to shorten up the
# expression.
#
# .. math::
#
# t_1 &= \sqrt{ \frac{2\pi}{z} } \\
# t_2 &= \frac{\left(z+\frac{1}{12z-\frac{1}{10z}} \right)}{e} \\
# & {t_1} {t_2}^z
#
# This is a simple, closed-form evaluation of a function.
# It's remarkably inaccurate for :math:`\Gamma \left( \dfrac{1}{2} \right)`.
# For integer values, however, it's very good.
# Gamma Function 3
# -------------------
# Here's the Sterling version. This sums a sequence of values.
#
# .. math::
#
# \Gamma(z) \sim e^{-z}z^z \sqrt{ \frac{2\pi}{z} } \left( \sum_{k=0}^\infty \dfrac{g_k} {z^k} \right)
#
# Here is the beginning of the sequence of :math:`g_k` values.
#
# .. math::
#
# g_0 &=1, \\
# g_1 &=1/12, \\
# g_2 &=1/288, \\
# g_3 &=−139/51840, \\
# g_4 &=−571/24\,88320, \\
# g_5 &=1\,63879/2090\,18880, \\
# g_6 &=52\,46819/7\,52467\,96800.
#
# There's a defined rule for calculating these obscure-looking values.
# However, we note that the last of these is near ``10E-5``, this is enough
# to give us a usefully accurate answer.
# This is not a convergent series: using more terms used may actually *reduce*
# the accuracy of the approximation. For the values we're working with,
# we don't seem to need all six terms shown above.
def Gamma3( z ):
"""Gamma Function. Sterling version.
http://dlmf.nist.gov/5.11#E3
Gamma(n) == fact(n-1)
>>> import math
>>> round(Gamma3(2),1)
1.0
>>> round(Gamma3(3),1)
2.0
>>> round(Gamma3(4),1)
6.0
>>> round(Gamma3(5),1)
24.0
>>> round(Gamma3(.5), 7)
1.7737381
>>> round(math.sqrt(math.pi), 7)
1.7724539
"""
t_1= math.exp(-z)*z**z
t_2= math.sqrt(2*math.pi/z)
g = [1, 1/12, 1/288, -139/51840, -571/2488320, 163879/209018880, 5246819/75246796800]
t_3= sum( g[k]/(z**k) for k in range(2) )
return t_1*t_2*t_3
# We've broken this into three parts, to reduce the size of the overall
# expression.
#
# .. math::
#
# t_1 &= e^{-z}z^z \\
# t_2 &= \sqrt{ \frac{2\pi}{z} } \\
# t_3 &= \sum_{k=0}^\infty \dfrac{g_k} {z^k}
#
# This is acceptable for :math:`\Gamma \left( \dfrac{1}{2} \right)`.
# Note that we're only using the first two terms. Compare these results with the
# ``Gamma2()`` function, shown above. The number of terms required seems
# to vary with the range of the vaue of :math:`z`.
# Gamma Hybrid
# -------------
# Most of the approximations aren't very accurate
# for the :math:`\Gamma\left(\frac{k}{2}\right)` values.
# We can use this exact closed-form expression instead:
#
# .. math::
#
# \Gamma\left(\frac{1}{2}+n\right) = \frac{(2n)!}{4^n n!}\sqrt{\pi}
#
# This provides precise values for the special cases we're using.
# Here's a hybrid ``Gamma()`` function. For certain values, we'll use the exact
# :math:`\Gamma\left(\frac{1}{2}+n\right)` value. For other values,
# we'll use the ``Gamma2()`` approximation, above.
def Gamma_Half(k):
"""Gamma(k) with special case for k = n+1/2; k-1/2=n.
>>> import math
>>> round(Gamma_Half(2),1)
1.0
>>> round(Gamma_Half(3),1)
2.0
>>> round(Gamma_Half(4),1)
6.0
>>> round(Gamma_Half(5),1)
24.0
>>> round(Gamma_Half(.5), 7)
1.7724539
>>> round(math.sqrt(math.pi), 7)
1.7724539
>>> round(Gamma_Half(1.5), 7)
0.8862269
>>> round(math.sqrt(math.pi)/2, 7)
0.8862269
"""
ε= 1E-6
if abs(k-int(k)-.5) < ε:
n = int(k-.5)
return fact(2*n)/(4**n*fact(n))*math.sqrt(math.pi)
else:
return float(Gamma2(k))
# If the value is an :math:`n+\dfrac{1}{2} \pm \epsilon`, we'll use the special
# close-form expression. If the value is not close to :math:`n+\dfrac{1}{2}`,
# we'll use a more general approximation.
# The math.gamma() Version
# ---------------------------
# Here's a test case for the ``math.gamma()`` function.
test_math_gamma = """
>>> import math
>>> round(math.gamma(2),2)
1.0
>>> round(math.gamma(3),2)
2.0
>>> round(math.gamma(4),2)
6.0
>>> round(math.gamma(5),2)
24.0
>>> round(math.gamma(.5), 7)
1.7724539
>>> round(math.sqrt(math.pi), 7)
1.7724539
>>> round(math.gamma(1.5), 7)
0.8862269
>>> round(math.sqrt(math.pi)/2, 7)
0.8862269
"""
# This looks good, also. For the given test cases, it's as accurate
# as our hybrid shown above.
# Cumulative Distribution Function
# ==================================
# The real calculation of the CDF from a :math:`\chi^2` value, ``x``, and
# the degrees of freedom, ``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(cdf(0.004, 1), 2)
0.95
>>> round(cdf(10.83, 1), 3)
0.001
>>> round(cdf(3.94, 10), 2)
0.95
>>> round(cdf(29.59, 10), 3)
0.001
>>> 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(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(cdf(19.18, 6), 5)
0.00387
>>> round(cdf(12.5916, 6), 2)
0.05
From http://www.itl.nist.gov/div898/handbook/prc/section4/prc46.htm
>>> round(cdf(12.131, 4), 5)
0.01639
>>> round(cdf(9.488, 4), 2)
0.05
"""
return 1-gamma(k/2, x/2)/Gamma_Half(k/2)
# The calcuation is 1 minus the ratio the partial
# gamma to the full gamma.
# Unit Test Cases
# ===============
# We'll use the doctest comments in each function defined above.
# Additionally, we'll use some strings with doctest test cases.
__test__ = {
"test_math_gamma": test_math_gamma,
}
def test(*args, **kw):
import doctest
doctest.testmod(*args, **kw)
if __name__ == "__main__":
test()