-
Notifications
You must be signed in to change notification settings - Fork 0
/
djstats.py
60 lines (46 loc) · 1.68 KB
/
djstats.py
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
# djdistributions.py
#
# To avoid importing 45M of scipy, I provide the option of using the
# library that scipy uses directly.
#
# This library is cephes, see www.netlib.org/cephes
# see http://pylab.sourceforge.net/packages for an overview of functions
# it provides.
# As far as I can reconstruct, cephes has been packaged for python
# by the numpy or numerical python team, thanks!!
# The library lives in _cephes.so (about 0.9M) or _cephes.pyd (1.9M)
#
# $Revision$
#
# Being lazy, I only wrap those function that I need
# which is currently ONE
import sys, os
try:
import _cephes as cephes
except ImportError:
"""when unpacking an extension bundle, spss leaves non-python files in a directory with the
name of the bundle. so we temporarily add that name and then reload. sigh """
newdirs = [os.path.join(x, 'djmixed') for x in sys.path if 'SPSSInc' in x ]
# TODO this only works when code is placed under SPSS, obviously
sys.path.extend(newdirs)
try:
import _cephes as cephes
sys.path[-(len(newdirs)):] = []
except ImportError:
""" if all fails, we see if scipy is present """
sys.path[-(len(newdirs)):] = []
try:
import scipy.special._cephes as cephes
except ImportError:
raise ImportError("Could not find the _cephes.pyd library (or equivalent)")
##### pchisq(value, df) -> probability
# compare to scipy.stats.chi2.cdf(value, df) -> probability
# that routine returns the lower tail, ie the area between 0..value
# which is identical to R: pchisq(value, df)
def pchisq(value, df, lowertail=True):
if lowertail:
return cephes.chdtr(df, value)
else:
return cephes.chdtrc(df, value)
if __name__ == '__main__':
pass