PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/index_GFF_id/pymodules/python2.7/lib/python/statsmodels-0.5.0-py2.7-linux-x86_64.egg/statsmodels/tsa/filters/bk_filter.py

https://gitlab.com/pooja043/Globus_Docker_4
Python | 79 lines | 52 code | 2 blank | 25 comment | 1 complexity | 9078e88ff14d307f95b0bc6425084afc MD5 | raw file
  1. from __future__ import absolute_import
  2. import numpy as np
  3. from scipy.signal import fftconvolve
  4. from .utils import _maybe_get_pandas_wrapper
  5. def bkfilter(X, low=6, high=32, K=12):
  6. """
  7. Baxter-King bandpass filter
  8. Parameters
  9. ----------
  10. X : array-like
  11. A 1 or 2d ndarray. If 2d, variables are assumed to be in columns.
  12. low : float
  13. Minimum period for oscillations, ie., Baxter and King suggest that
  14. the Burns-Mitchell U.S. business cycle has 6 for quarterly data and
  15. 1.5 for annual data.
  16. high : float
  17. Maximum period for oscillations BK suggest that the U.S.
  18. business cycle has 32 for quarterly data and 8 for annual data.
  19. K : int
  20. Lead-lag length of the filter. Baxter and King propose a truncation
  21. length of 12 for quarterly data and 3 for annual data.
  22. Returns
  23. -------
  24. Y : array
  25. Cyclical component of X
  26. References
  27. ---------- ::
  28. Baxter, M. and R. G. King. "Measuring Business Cycles: Approximate
  29. Band-Pass Filters for Economic Time Series." *Review of Economics and
  30. Statistics*, 1999, 81(4), 575-593.
  31. Notes
  32. -----
  33. Returns a centered weighted moving average of the original series. Where
  34. the weights a[j] are computed ::
  35. a[j] = b[j] + theta, for j = 0, +/-1, +/-2, ... +/- K
  36. b[0] = (omega_2 - omega_1)/pi
  37. b[j] = 1/(pi*j)(sin(omega_2*j)-sin(omega_1*j), for j = +/-1, +/-2,...
  38. and theta is a normalizing constant ::
  39. theta = -sum(b)/(2K+1)
  40. Examples
  41. --------
  42. >>> import statsmodels.api as sm
  43. >>> dta = sm.datasets.macrodata.load()
  44. >>> X = dta.data['realinv']
  45. >>> Y = sm.tsa.filters.bkfilter(X, 6, 24, 12)
  46. """
  47. #TODO: change the docstring to ..math::?
  48. #TODO: allow windowing functions to correct for Gibb's Phenomenon?
  49. # adjust bweights (symmetrically) by below before demeaning
  50. # Lancosz Sigma Factors np.sinc(2*j/(2.*K+1))
  51. _pandas_wrapper = _maybe_get_pandas_wrapper(X, K)
  52. X = np.asarray(X)
  53. omega_1 = 2.*np.pi/high # convert from freq. to periodicity
  54. omega_2 = 2.*np.pi/low
  55. bweights = np.zeros(2*K+1)
  56. bweights[K] = (omega_2 - omega_1)/np.pi # weight at zero freq.
  57. j = np.arange(1,int(K)+1)
  58. weights = 1/(np.pi*j)*(np.sin(omega_2*j)-np.sin(omega_1*j))
  59. bweights[K+j] = weights # j is an idx
  60. bweights[:K] = weights[::-1] # make symmetric weights
  61. bweights -= bweights.mean() # make sure weights sum to zero
  62. if X.ndim == 2:
  63. bweights = bweights[:,None]
  64. X = fftconvolve(X, bweights, mode='valid') # get a centered moving avg/
  65. # convolution
  66. if _pandas_wrapper is not None:
  67. return _pandas_wrapper(X)
  68. return X