PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/pandas/stats/tests/test_moments.py

https://github.com/thouis/pandas
Python | 732 lines | 533 code | 175 blank | 24 comment | 50 complexity | 9607590493e2a513c8dfd91ed19f37d3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import unittest
  2. import nose
  3. import sys
  4. import functools
  5. from datetime import datetime
  6. from numpy.random import randn
  7. import numpy as np
  8. from pandas import Series, DataFrame, bdate_range, isnull, notnull
  9. from pandas.util.testing import (
  10. assert_almost_equal, assert_series_equal, assert_frame_equal
  11. )
  12. from pandas.util.py3compat import PY3
  13. import pandas.core.datetools as datetools
  14. import pandas.stats.moments as mom
  15. import pandas.util.testing as tm
  16. N, K = 100, 10
  17. class TestMoments(unittest.TestCase):
  18. _multiprocess_can_split_ = True
  19. _nan_locs = np.arange(20, 40)
  20. _inf_locs = np.array([])
  21. def setUp(self):
  22. arr = randn(N)
  23. arr[self._nan_locs] = np.NaN
  24. self.arr = arr
  25. self.rng = bdate_range(datetime(2009, 1, 1), periods=N)
  26. self.series = Series(arr.copy(), index=self.rng)
  27. self.frame = DataFrame(randn(N, K), index=self.rng,
  28. columns=np.arange(K))
  29. def test_rolling_sum(self):
  30. self._check_moment_func(mom.rolling_sum, np.sum)
  31. def test_rolling_count(self):
  32. counter = lambda x: np.isfinite(x).astype(float).sum()
  33. self._check_moment_func(mom.rolling_count, counter,
  34. has_min_periods=False,
  35. preserve_nan=False,
  36. fill_value=0)
  37. def test_rolling_mean(self):
  38. self._check_moment_func(mom.rolling_mean, np.mean)
  39. def test_cmov_mean(self):
  40. try:
  41. from scikits.timeseries.lib import cmov_mean
  42. except ImportError:
  43. raise nose.SkipTest
  44. vals = np.random.randn(10)
  45. xp = cmov_mean(vals, 5)
  46. rs = mom.rolling_mean(vals, 5, center=True)
  47. assert_almost_equal(xp.compressed(), rs[2:-2])
  48. assert_almost_equal(xp.mask, np.isnan(rs))
  49. xp = Series(rs)
  50. rs = mom.rolling_mean(Series(vals), 5, center=True)
  51. assert_series_equal(xp, rs)
  52. def test_cmov_window(self):
  53. try:
  54. from scikits.timeseries.lib import cmov_window
  55. except ImportError:
  56. raise nose.SkipTest
  57. vals = np.random.randn(10)
  58. xp = cmov_window(vals, 5, 'boxcar')
  59. rs = mom.rolling_window(vals, 5, 'boxcar', center=True)
  60. assert_almost_equal(xp.compressed(), rs[2:-2])
  61. assert_almost_equal(xp.mask, np.isnan(rs))
  62. xp = Series(rs)
  63. rs = mom.rolling_window(Series(vals), 5, 'boxcar', center=True)
  64. assert_series_equal(xp, rs)
  65. def test_cmov_window_corner(self):
  66. try:
  67. from scikits.timeseries.lib import cmov_window
  68. except ImportError:
  69. raise nose.SkipTest
  70. # all nan
  71. vals = np.empty(10, dtype=float)
  72. vals.fill(np.nan)
  73. rs = mom.rolling_window(vals, 5, 'boxcar', center=True)
  74. self.assert_(np.isnan(rs).all())
  75. # empty
  76. vals = np.array([])
  77. rs = mom.rolling_window(vals, 5, 'boxcar', center=True)
  78. self.assert_(len(rs) == 0)
  79. # shorter than window
  80. vals = np.random.randn(5)
  81. rs = mom.rolling_window(vals, 10, 'boxcar')
  82. self.assert_(np.isnan(rs).all())
  83. self.assert_(len(rs) == 5)
  84. def test_cmov_window_frame(self):
  85. try:
  86. from scikits.timeseries.lib import cmov_window
  87. except ImportError:
  88. raise nose.SkipTest
  89. # DataFrame
  90. vals = np.random.randn(10, 2)
  91. xp = cmov_window(vals, 5, 'boxcar')
  92. rs = mom.rolling_window(DataFrame(vals), 5, 'boxcar', center=True)
  93. assert_frame_equal(DataFrame(xp), rs)
  94. def test_cmov_window_na_min_periods(self):
  95. try:
  96. from scikits.timeseries.lib import cmov_window
  97. except ImportError:
  98. raise nose.SkipTest
  99. # min_periods
  100. vals = Series(np.random.randn(10))
  101. vals[4] = np.nan
  102. vals[8] = np.nan
  103. xp = mom.rolling_mean(vals, 5, min_periods=4, center=True)
  104. rs = mom.rolling_window(vals, 5, 'boxcar', min_periods=4, center=True)
  105. assert_series_equal(xp, rs)
  106. def test_cmov_window_regular(self):
  107. try:
  108. from scikits.timeseries.lib import cmov_window
  109. except ImportError:
  110. raise nose.SkipTest
  111. win_types = ['triang', 'blackman', 'hamming', 'bartlett', 'bohman',
  112. 'blackmanharris', 'nuttall', 'barthann']
  113. for wt in win_types:
  114. vals = np.random.randn(10)
  115. xp = cmov_window(vals, 5, wt)
  116. rs = mom.rolling_window(Series(vals), 5, wt, center=True)
  117. assert_series_equal(Series(xp), rs)
  118. def test_cmov_window_special(self):
  119. try:
  120. from scikits.timeseries.lib import cmov_window
  121. except ImportError:
  122. raise nose.SkipTest
  123. win_types = ['kaiser', 'gaussian', 'general_gaussian', 'slepian']
  124. kwds = [{'beta' : 1.}, {'std' : 1.}, {'power' : 2., 'width' : 2.},
  125. {'width' : 0.5}]
  126. for wt, k in zip(win_types, kwds):
  127. vals = np.random.randn(10)
  128. xp = cmov_window(vals, 5, (wt,) + tuple(k.values()))
  129. rs = mom.rolling_window(Series(vals), 5, wt, center=True,
  130. **k)
  131. assert_series_equal(Series(xp), rs)
  132. def test_rolling_median(self):
  133. self._check_moment_func(mom.rolling_median, np.median)
  134. def test_rolling_min(self):
  135. self._check_moment_func(mom.rolling_min, np.min)
  136. a = np.array([1,2,3,4,5])
  137. b = mom.rolling_min(a, window=100, min_periods=1)
  138. assert_almost_equal(b, np.ones(len(a)))
  139. self.assertRaises(ValueError, mom.rolling_min, np.array([1,2,3]), window=3, min_periods=5)
  140. def test_rolling_max(self):
  141. self._check_moment_func(mom.rolling_max, np.max)
  142. a = np.array([1,2,3,4,5])
  143. b = mom.rolling_max(a, window=100, min_periods=1)
  144. assert_almost_equal(a, b)
  145. self.assertRaises(ValueError, mom.rolling_max, np.array([1,2,3]), window=3, min_periods=5)
  146. def test_rolling_quantile(self):
  147. qs = [.1, .5, .9]
  148. def scoreatpercentile(a, per):
  149. values = np.sort(a,axis=0)
  150. idx = per /1. * (values.shape[0] - 1)
  151. return values[int(idx)]
  152. for q in qs:
  153. def f(x, window, min_periods=None, freq=None, center=False):
  154. return mom.rolling_quantile(x, window, q,
  155. min_periods=min_periods,
  156. freq=freq,
  157. center=center)
  158. def alt(x):
  159. return scoreatpercentile(x, q)
  160. self._check_moment_func(f, alt)
  161. def test_rolling_apply(self):
  162. ser = Series([])
  163. assert_series_equal(ser, mom.rolling_apply(ser, 10, lambda x:x.mean()))
  164. def roll_mean(x, window, min_periods=None, freq=None, center=False):
  165. return mom.rolling_apply(x, window,
  166. lambda x: x[np.isfinite(x)].mean(),
  167. min_periods=min_periods,
  168. freq=freq,
  169. center=center)
  170. self._check_moment_func(roll_mean, np.mean)
  171. def test_rolling_apply_out_of_bounds(self):
  172. # #1850
  173. arr = np.arange(4)
  174. # it works!
  175. result = mom.rolling_apply(arr, 10, np.sum)
  176. self.assert_(isnull(result).all())
  177. result = mom.rolling_apply(arr, 10, np.sum, min_periods=1)
  178. assert_almost_equal(result, result)
  179. def test_rolling_std(self):
  180. self._check_moment_func(mom.rolling_std,
  181. lambda x: np.std(x, ddof=1))
  182. self._check_moment_func(functools.partial(mom.rolling_std, ddof=0),
  183. lambda x: np.std(x, ddof=0))
  184. def test_rolling_std_1obs(self):
  185. result = mom.rolling_std(np.array([1.,2.,3.,4.,5.]),
  186. 1, min_periods=1)
  187. expected = np.zeros(5)
  188. assert_almost_equal(result, expected)
  189. result = mom.rolling_std(np.array([np.nan,np.nan,3.,4.,5.]),
  190. 3, min_periods=2)
  191. self.assert_(np.isnan(result[2]))
  192. def test_rolling_std_neg_sqrt(self):
  193. # unit test from Bottleneck
  194. # Test move_nanstd for neg sqrt.
  195. a = np.array([0.0011448196318903589,
  196. 0.00028718669878572767,
  197. 0.00028718669878572767,
  198. 0.00028718669878572767,
  199. 0.00028718669878572767])
  200. b = mom.rolling_std(a, window=3)
  201. self.assert_(np.isfinite(b[2:]).all())
  202. b = mom.ewmstd(a, span=3)
  203. self.assert_(np.isfinite(b[2:]).all())
  204. def test_rolling_var(self):
  205. self._check_moment_func(mom.rolling_var,
  206. lambda x: np.var(x, ddof=1))
  207. self._check_moment_func(functools.partial(mom.rolling_var, ddof=0),
  208. lambda x: np.var(x, ddof=0))
  209. def test_rolling_skew(self):
  210. try:
  211. from scipy.stats import skew
  212. except ImportError:
  213. raise nose.SkipTest('no scipy')
  214. self._check_moment_func(mom.rolling_skew,
  215. lambda x: skew(x, bias=False))
  216. def test_rolling_kurt(self):
  217. try:
  218. from scipy.stats import kurtosis
  219. except ImportError:
  220. raise nose.SkipTest('no scipy')
  221. self._check_moment_func(mom.rolling_kurt,
  222. lambda x: kurtosis(x, bias=False))
  223. def test_fperr_robustness(self):
  224. # TODO: remove this once python 2.5 out of picture
  225. if PY3:
  226. raise nose.SkipTest
  227. # #2114
  228. data = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a@\xaa\xaa\xaa\xaa\xaa\xaa\x02@8\x8e\xe38\x8e\xe3\xe8?z\t\xed%\xb4\x97\xd0?\xa2\x0c<\xdd\x9a\x1f\xb6?\x82\xbb\xfa&y\x7f\x9d?\xac\'\xa7\xc4P\xaa\x83?\x90\xdf\xde\xb0k8j?`\xea\xe9u\xf2zQ?*\xe37\x9d\x98N7?\xe2.\xf5&v\x13\x1f?\xec\xc9\xf8\x19\xa4\xb7\x04?\x90b\xf6w\x85\x9f\xeb>\xb5A\xa4\xfaXj\xd2>F\x02\xdb\xf8\xcb\x8d\xb8>.\xac<\xfb\x87^\xa0>\xe8:\xa6\xf9_\xd3\x85>\xfb?\xe2cUU\xfd?\xfc\x7fA\xed8\x8e\xe3?\xa5\xaa\xac\x91\xf6\x12\xca?n\x1cs\xb6\xf9a\xb1?\xe8%D\xf3L-\x97?5\xddZD\x11\xe7~?#>\xe7\x82\x0b\x9ad?\xd9R4Y\x0fxK?;7x;\nP2?N\xf4JO\xb8j\x18?4\xf81\x8a%G\x00?\x9a\xf5\x97\r2\xb4\xe5>\xcd\x9c\xca\xbcB\xf0\xcc>3\x13\x87(\xd7J\xb3>\x99\x19\xb4\xe0\x1e\xb9\x99>ff\xcd\x95\x14&\x81>\x88\x88\xbc\xc7p\xddf>`\x0b\xa6_\x96|N>@\xb2n\xea\x0eS4>U\x98\x938i\x19\x1b>\x8eeb\xd0\xf0\x10\x02>\xbd\xdc-k\x96\x16\xe8=(\x93\x1e\xf2\x0e\x0f\xd0=\xe0n\xd3Bii\xb5=*\xe9\x19Y\x8c\x8c\x9c=\xc6\xf0\xbb\x90]\x08\x83=]\x96\xfa\xc0|`i=>d\xfc\xd5\xfd\xeaP=R0\xfb\xc7\xa7\x8e6=\xc2\x95\xf9_\x8a\x13\x1e=\xd6c\xa6\xea\x06\r\x04=r\xda\xdd8\t\xbc\xea<\xf6\xe6\x93\xd0\xb0\xd2\xd1<\x9d\xdeok\x96\xc3\xb7<&~\xea9s\xaf\x9f<UUUUUU\x13@q\x1c\xc7q\x1c\xc7\xf9?\xf6\x12\xdaKh/\xe1?\xf2\xc3"e\xe0\xe9\xc6?\xed\xaf\x831+\x8d\xae?\xf3\x1f\xad\xcb\x1c^\x94?\x15\x1e\xdd\xbd>\xb8\x02@\xc6\xd2&\xfd\xa8\xf5\xe8?\xd9\xe1\x19\xfe\xc5\xa3\xd0?v\x82"\xa8\xb2/\xb6?\x9dX\x835\xee\x94\x9d?h\x90W\xce\x9e\xb8\x83?\x8a\xc0th~Kj?\\\x80\xf8\x9a\xa9\x87Q?%\xab\xa0\xce\x8c_7?1\xe4\x80\x13\x11*\x1f? \x98\x00\r\xb6\xc6\x04?\x80u\xabf\x9d\xb3\xeb>UNrD\xbew\xd2>\x1c\x13C[\xa8\x9f\xb8>\x12b\xd7<pj\xa0>m-\x1fQ@\xe3\x85>\xe6\x91)l\x00/m>Da\xc6\xf2\xaatS>\x05\xd7]\xee\xe3\xf09>'
  229. arr = np.frombuffer(data, dtype='<f8')
  230. if sys.byteorder != "little":
  231. arr = arr.byteswap().newbyteorder()
  232. result = mom.rolling_sum(arr, 2)
  233. self.assertTrue((result[1:] >= 0).all())
  234. result = mom.rolling_mean(arr, 2)
  235. self.assertTrue((result[1:] >= 0).all())
  236. result = mom.rolling_var(arr, 2)
  237. self.assertTrue((result[1:] >= 0).all())
  238. # #2527, ugh
  239. arr = np.array([0.00012456, 0.0003, 0])
  240. result = mom.rolling_mean(arr, 1)
  241. self.assertTrue(result[-1] >= 0)
  242. result = mom.rolling_mean(-arr, 1)
  243. self.assertTrue(result[-1] <= 0)
  244. def _check_moment_func(self, func, static_comp, window=50,
  245. has_min_periods=True,
  246. has_center=True,
  247. has_time_rule=True,
  248. preserve_nan=True,
  249. fill_value=None):
  250. self._check_ndarray(func, static_comp, window=window,
  251. has_min_periods=has_min_periods,
  252. preserve_nan=preserve_nan,
  253. has_center=has_center,
  254. fill_value=fill_value)
  255. self._check_structures(func, static_comp,
  256. has_min_periods=has_min_periods,
  257. has_time_rule=has_time_rule,
  258. fill_value=fill_value,
  259. has_center=has_center)
  260. def _check_ndarray(self, func, static_comp, window=50,
  261. has_min_periods=True,
  262. preserve_nan=True,
  263. has_center=True,
  264. fill_value=None):
  265. result = func(self.arr, window)
  266. assert_almost_equal(result[-1],
  267. static_comp(self.arr[-50:]))
  268. if preserve_nan:
  269. assert(np.isnan(result[self._nan_locs]).all())
  270. # excluding NaNs correctly
  271. arr = randn(50)
  272. arr[:10] = np.NaN
  273. arr[-10:] = np.NaN
  274. if has_min_periods:
  275. result = func(arr, 50, min_periods=30)
  276. assert_almost_equal(result[-1], static_comp(arr[10:-10]))
  277. # min_periods is working correctly
  278. result = func(arr, 20, min_periods=15)
  279. self.assert_(np.isnan(result[23]))
  280. self.assert_(not np.isnan(result[24]))
  281. self.assert_(not np.isnan(result[-6]))
  282. self.assert_(np.isnan(result[-5]))
  283. arr2 = randn(20)
  284. result = func(arr2, 10, min_periods=5)
  285. self.assert_(isnull(result[3]))
  286. self.assert_(notnull(result[4]))
  287. # min_periods=0
  288. result0 = func(arr, 20, min_periods=0)
  289. result1 = func(arr, 20, min_periods=1)
  290. assert_almost_equal(result0, result1)
  291. else:
  292. result = func(arr, 50)
  293. assert_almost_equal(result[-1], static_comp(arr[10:-10]))
  294. if has_center:
  295. if has_min_periods:
  296. result = func(arr, 20, min_periods=15, center=True)
  297. expected = func(arr, 20, min_periods=15)
  298. else:
  299. result = func(arr, 20, center=True)
  300. expected = func(arr, 20)
  301. assert_almost_equal(result[1], expected[10])
  302. if fill_value is None:
  303. self.assert_(np.isnan(result[-9:]).all())
  304. else:
  305. self.assert_((result[-9:] == 0).all())
  306. if has_min_periods:
  307. self.assert_(np.isnan(expected[23]))
  308. self.assert_(np.isnan(result[14]))
  309. self.assert_(np.isnan(expected[-5]))
  310. self.assert_(np.isnan(result[-14]))
  311. def _check_structures(self, func, static_comp,
  312. has_min_periods=True, has_time_rule=True,
  313. has_center=True,
  314. fill_value=None):
  315. series_result = func(self.series, 50)
  316. self.assert_(isinstance(series_result, Series))
  317. frame_result = func(self.frame, 50)
  318. self.assertEquals(type(frame_result), DataFrame)
  319. # check time_rule works
  320. if has_time_rule:
  321. win = 25
  322. minp = 10
  323. if has_min_periods:
  324. series_result = func(self.series[::2], win, min_periods=minp,
  325. freq='B')
  326. frame_result = func(self.frame[::2], win, min_periods=minp,
  327. freq='B')
  328. else:
  329. series_result = func(self.series[::2], win, freq='B')
  330. frame_result = func(self.frame[::2], win, freq='B')
  331. last_date = series_result.index[-1]
  332. prev_date = last_date - 24 * datetools.bday
  333. trunc_series = self.series[::2].truncate(prev_date, last_date)
  334. trunc_frame = self.frame[::2].truncate(prev_date, last_date)
  335. assert_almost_equal(series_result[-1], static_comp(trunc_series))
  336. assert_almost_equal(frame_result.xs(last_date),
  337. trunc_frame.apply(static_comp))
  338. if has_center:
  339. if has_min_periods:
  340. minp = 10
  341. series_xp = func(self.series, 25, min_periods=minp).shift(-12)
  342. frame_xp = func(self.frame, 25, min_periods=minp).shift(-12)
  343. series_rs = func(self.series, 25, min_periods=minp,
  344. center=True)
  345. frame_rs = func(self.frame, 25, min_periods=minp,
  346. center=True)
  347. else:
  348. series_xp = func(self.series, 25).shift(-12)
  349. frame_xp = func(self.frame, 25).shift(-12)
  350. series_rs = func(self.series, 25, center=True)
  351. frame_rs = func(self.frame, 25, center=True)
  352. if fill_value is not None:
  353. series_xp = series_xp.fillna(fill_value)
  354. frame_xp = frame_xp.fillna(fill_value)
  355. assert_series_equal(series_xp, series_rs)
  356. assert_frame_equal(frame_xp, frame_rs)
  357. def test_legacy_time_rule_arg(self):
  358. from StringIO import StringIO
  359. # suppress deprecation warnings
  360. sys.stderr = StringIO()
  361. rng = bdate_range('1/1/2000', periods=20)
  362. ts = Series(np.random.randn(20), index=rng)
  363. ts = ts.take(np.random.permutation(len(ts))[:12]).sort_index()
  364. try:
  365. result = mom.rolling_mean(ts, 1, min_periods=1, freq='B')
  366. expected = mom.rolling_mean(ts, 1, min_periods=1,
  367. time_rule='WEEKDAY')
  368. tm.assert_series_equal(result, expected)
  369. result = mom.ewma(ts, span=5, freq='B')
  370. expected = mom.ewma(ts, span=5, time_rule='WEEKDAY')
  371. tm.assert_series_equal(result, expected)
  372. finally:
  373. sys.stderr = sys.__stderr__
  374. def test_ewma(self):
  375. self._check_ew(mom.ewma)
  376. arr = np.zeros(1000)
  377. arr[5] = 1
  378. result = mom.ewma(arr, span=100, adjust=False).sum()
  379. self.assert_(np.abs(result - 1) < 1e-2)
  380. def test_ewma_nan_handling(self):
  381. s = Series([1.] + [np.nan] * 5 + [1.])
  382. result = mom.ewma(s, com=5)
  383. assert_almost_equal(result, [1] * len(s))
  384. def test_ewmvar(self):
  385. self._check_ew(mom.ewmvar)
  386. def test_ewmvol(self):
  387. self._check_ew(mom.ewmvol)
  388. def test_ewma_span_com_args(self):
  389. A = mom.ewma(self.arr, com=9.5)
  390. B = mom.ewma(self.arr, span=20)
  391. assert_almost_equal(A, B)
  392. self.assertRaises(Exception, mom.ewma, self.arr, com=9.5, span=20)
  393. self.assertRaises(Exception, mom.ewma, self.arr)
  394. def test_ew_empty_arrays(self):
  395. arr = np.array([], dtype=np.float64)
  396. funcs = [mom.ewma, mom.ewmvol, mom.ewmvar]
  397. for f in funcs:
  398. result = f(arr, 3)
  399. assert_almost_equal(result, arr)
  400. def _check_ew(self, func):
  401. self._check_ew_ndarray(func)
  402. self._check_ew_structures(func)
  403. def _check_ew_ndarray(self, func, preserve_nan=False):
  404. result = func(self.arr, com=10)
  405. if preserve_nan:
  406. assert(np.isnan(result[self._nan_locs]).all())
  407. # excluding NaNs correctly
  408. arr = randn(50)
  409. arr[:10] = np.NaN
  410. arr[-10:] = np.NaN
  411. # ??? check something
  412. # pass in ints
  413. result2 = func(np.arange(50), span=10)
  414. self.assert_(result2.dtype == np.float_)
  415. def _check_ew_structures(self, func):
  416. series_result = func(self.series, com=10)
  417. self.assert_(isinstance(series_result, Series))
  418. frame_result = func(self.frame, com=10)
  419. self.assertEquals(type(frame_result), DataFrame)
  420. # binary moments
  421. def test_rolling_cov(self):
  422. A = self.series
  423. B = A + randn(len(A))
  424. result = mom.rolling_cov(A, B, 50, min_periods=25)
  425. assert_almost_equal(result[-1], np.cov(A[-50:], B[-50:])[0, 1])
  426. def test_rolling_corr(self):
  427. A = self.series
  428. B = A + randn(len(A))
  429. result = mom.rolling_corr(A, B, 50, min_periods=25)
  430. assert_almost_equal(result[-1], np.corrcoef(A[-50:], B[-50:])[0, 1])
  431. # test for correct bias correction
  432. a = tm.makeTimeSeries()
  433. b = tm.makeTimeSeries()
  434. a[:5] = np.nan
  435. b[:10] = np.nan
  436. result = mom.rolling_corr(a, b, len(a), min_periods=1)
  437. assert_almost_equal(result[-1], a.corr(b))
  438. def test_rolling_corr_pairwise(self):
  439. panel = mom.rolling_corr_pairwise(self.frame, 10, min_periods=5)
  440. correl = panel.ix[:, 1, 5]
  441. exp = mom.rolling_corr(self.frame[1], self.frame[5],
  442. 10, min_periods=5)
  443. tm.assert_series_equal(correl, exp)
  444. def test_flex_binary_frame(self):
  445. def _check(method):
  446. series = self.frame[1]
  447. res = method(series, self.frame, 10)
  448. res2 = method(self.frame, series, 10)
  449. exp = self.frame.apply(lambda x: method(series, x, 10))
  450. tm.assert_frame_equal(res, exp)
  451. tm.assert_frame_equal(res2, exp)
  452. frame2 = self.frame.copy()
  453. frame2.values[:] = np.random.randn(*frame2.shape)
  454. res3 = method(self.frame, frame2, 10)
  455. exp = DataFrame(dict((k, method(self.frame[k], frame2[k], 10))
  456. for k in self.frame))
  457. tm.assert_frame_equal(res3, exp)
  458. methods = [mom.rolling_corr, mom.rolling_cov]
  459. for meth in methods:
  460. _check(meth)
  461. def test_ewmcov(self):
  462. self._check_binary_ew(mom.ewmcov)
  463. def test_ewmcorr(self):
  464. self._check_binary_ew(mom.ewmcorr)
  465. def _check_binary_ew(self, func):
  466. A = Series(randn(50), index=np.arange(50))
  467. B = A[2:] + randn(48)
  468. A[:10] = np.NaN
  469. B[-10:] = np.NaN
  470. result = func(A, B, 20, min_periods=5)
  471. self.assert_(np.isnan(result.values[:15]).all())
  472. self.assert_(not np.isnan(result.values[15:]).any())
  473. self.assertRaises(Exception, func, A, randn(50), 20, min_periods=5)
  474. def test_expanding_apply(self):
  475. ser = Series([])
  476. assert_series_equal(ser, mom.expanding_apply(ser, lambda x: x.mean()))
  477. def expanding_mean(x, min_periods=1, freq=None):
  478. return mom.expanding_apply(x,
  479. lambda x: x.mean(),
  480. min_periods=min_periods,
  481. freq=freq)
  482. self._check_expanding(expanding_mean, np.mean)
  483. def test_expanding_corr(self):
  484. A = self.series.dropna()
  485. B = (A + randn(len(A)))[:-5]
  486. result = mom.expanding_corr(A, B)
  487. rolling_result = mom.rolling_corr(A, B, len(A), min_periods=1)
  488. assert_almost_equal(rolling_result, result)
  489. def test_expanding_count(self):
  490. result = mom.expanding_count(self.series)
  491. assert_almost_equal(result, mom.rolling_count(self.series,
  492. len(self.series)))
  493. def test_expanding_quantile(self):
  494. result = mom.expanding_quantile(self.series, 0.5)
  495. rolling_result = mom.rolling_quantile(self.series,
  496. len(self.series),
  497. 0.5, min_periods=1)
  498. assert_almost_equal(result, rolling_result)
  499. def test_expanding_cov(self):
  500. A = self.series
  501. B = (A + randn(len(A)))[:-5]
  502. result = mom.expanding_cov(A, B)
  503. rolling_result = mom.rolling_cov(A, B, len(A), min_periods=1)
  504. assert_almost_equal(rolling_result, result)
  505. def test_expanding_max(self):
  506. self._check_expanding(mom.expanding_max, np.max, preserve_nan=False)
  507. def test_expanding_corr_pairwise(self):
  508. result = mom.expanding_corr_pairwise(self.frame)
  509. rolling_result = mom.rolling_corr_pairwise(self.frame,
  510. len(self.frame),
  511. min_periods=1)
  512. for i in result.items:
  513. assert_almost_equal(result[i], rolling_result[i])
  514. def _check_expanding_ndarray(self, func, static_comp, has_min_periods=True,
  515. has_time_rule=True, preserve_nan=True):
  516. result = func(self.arr)
  517. assert_almost_equal(result[10],
  518. static_comp(self.arr[:11]))
  519. if preserve_nan:
  520. assert(np.isnan(result[self._nan_locs]).all())
  521. arr = randn(50)
  522. if has_min_periods:
  523. result = func(arr, min_periods=30)
  524. assert(np.isnan(result[:29]).all())
  525. assert_almost_equal(result[-1], static_comp(arr[:50]))
  526. # min_periods is working correctly
  527. result = func(arr, min_periods=15)
  528. self.assert_(np.isnan(result[13]))
  529. self.assert_(not np.isnan(result[14]))
  530. arr2 = randn(20)
  531. result = func(arr2, min_periods=5)
  532. self.assert_(isnull(result[3]))
  533. self.assert_(notnull(result[4]))
  534. # min_periods=0
  535. result0 = func(arr, min_periods=0)
  536. result1 = func(arr, min_periods=1)
  537. assert_almost_equal(result0, result1)
  538. else:
  539. result = func(arr)
  540. assert_almost_equal(result[-1], static_comp(arr[:50]))
  541. def _check_expanding_structures(self, func):
  542. series_result = func(self.series)
  543. self.assert_(isinstance(series_result, Series))
  544. frame_result = func(self.frame)
  545. self.assertEquals(type(frame_result), DataFrame)
  546. def _check_expanding(self, func, static_comp, has_min_periods=True,
  547. has_time_rule=True,
  548. preserve_nan=True):
  549. self._check_expanding_ndarray(func, static_comp,
  550. has_min_periods=has_min_periods,
  551. has_time_rule=has_time_rule,
  552. preserve_nan=preserve_nan)
  553. self._check_expanding_structures(func)
  554. if __name__ == '__main__':
  555. import nose
  556. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  557. exit=False)