/usr/share/pyshared/statsmodels/nonparametric/bandwidths.py is in python-statsmodels 0.4.2-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import numpy as np
from scipy.stats import scoreatpercentile as sap
#from scipy.stats import norm
def _select_sigma(X):
"""
Returns the smaller of std(X, ddof=1) or normalized IQR(X) over axis 0.
References
----------
Silverman (1986) p.47
"""
# normalize = norm.ppf(.75) - norm.ppf(.25)
normalize = 1.349
# IQR = np.subtract.reduce(percentile(X, [75,25],
# axis=axis), axis=axis)/normalize
IQR = (sap(X, 75) - sap(X, 25))/normalize
return np.minimum(np.std(X, axis=0, ddof=1), IQR)
## Univariate Rule of Thumb Bandwidths ##
def bw_scott(x):
"""
Scott's Rule of Thumb
Parameters
----------
x : array-like
Array for which to get the bandwidth
Returns
-------
bw : float
The estimate of the bandwidth
Notes
-----
Returns 1.059 * A * n ** (-1/5.) where ::
A = min(std(x, ddof=1), IQR/1.349)
IQR = np.subtract.reduce(np.percentile(x, [75,25]))
References
----------
Scott, D.W. (1992) Multivariate Density Estimation: Theory, Practice, and
Visualization.
"""
A = _select_sigma(x)
n = len(x)
return 1.059 * A * n ** -.2
def bw_silverman(x):
"""
Silverman's Rule of Thumb
Parameters
----------
x : array-like
Array for which to get the bandwidth
Returns
-------
bw : float
The estimate of the bandwidth
Notes
-----
Returns .9 * A * n ** (-1/5.) where ::
A = min(std(x, ddof=1), IQR/1.349)
IQR = np.subtract.reduce(np.percentile(x, [75,25]))
References
----------
Silverman, B.W. (1986) `Density Estimation.`
"""
A = _select_sigma(x)
n = len(x)
return .9 * A * n ** -.2
## Plug-In Methods ##
## Least Squares Cross-Validation ##
## Helper Functions ##
bandwidth_funcs = dict(scott=bw_scott,silverman=bw_silverman)
def select_bandwidth(x, bw, kernel):
"""
Selects bandwidth for a selection rule bw
this is a wrapper around existing bandwidth selection rules
Parameters
----------
x : array-like
Array for which to get the bandwidth
bw : string
name of bandwidth selection rule, currently "scott" and "silverman"
are supported
kernel : not used yet
Returns
-------
bw : float
The estimate of the bandwidth
"""
bw = bw.lower()
if bw not in ["scott","silverman"]:
raise ValueError("Bandwidth %s not understood" % bw)
#TODO: uncomment checks when we have non-rule of thumb bandwidths for diff. kernels
# if kernel == "gauss":
return bandwidth_funcs[bw](x)
# else:
# raise ValueError("Only Gaussian Kernels are currently supported")
|