PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/pandas/stats/tests/test_math.py

https://github.com/thouis/pandas
Python | 67 lines | 52 code | 15 blank | 0 comment | 6 complexity | 6162429798ed649c7ecfbcb0206eac11 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import unittest
  2. import nose
  3. from datetime import datetime
  4. from numpy.random import randn
  5. import numpy as np
  6. from pandas.core.api import Series, DataFrame, date_range
  7. from pandas.util.testing import assert_almost_equal
  8. import pandas.core.datetools as datetools
  9. import pandas.stats.moments as mom
  10. import pandas.util.testing as tm
  11. import pandas.stats.math as pmath
  12. import pandas.tests.test_series as ts
  13. from pandas import ols
  14. N, K = 100, 10
  15. _have_statsmodels = True
  16. try:
  17. import statsmodels.api as sm
  18. except ImportError:
  19. try:
  20. import scikits.statsmodels.api as sm
  21. except ImportError:
  22. _have_statsmodels = False
  23. class TestMath(unittest.TestCase):
  24. _nan_locs = np.arange(20, 40)
  25. _inf_locs = np.array([])
  26. def setUp(self):
  27. arr = randn(N)
  28. arr[self._nan_locs] = np.NaN
  29. self.arr = arr
  30. self.rng = date_range(datetime(2009, 1, 1), periods=N)
  31. self.series = Series(arr.copy(), index=self.rng)
  32. self.frame = DataFrame(randn(N, K), index=self.rng,
  33. columns=np.arange(K))
  34. def test_rank_1d(self):
  35. self.assertEqual(1, pmath.rank(self.series))
  36. self.assertEqual(0, pmath.rank(Series(0, self.series.index)))
  37. def test_solve_rect(self):
  38. if not _have_statsmodels:
  39. raise nose.SkipTest
  40. b = Series(np.random.randn(N), self.frame.index)
  41. result = pmath.solve(self.frame, b)
  42. expected = ols(y=b, x=self.frame, intercept=False).beta
  43. self.assert_(np.allclose(result, expected))
  44. def test_inv_illformed(self):
  45. singular = DataFrame(np.array([[1, 1], [2, 2]]))
  46. rs = pmath.inv(singular)
  47. expected = np.array([[0.1, 0.2], [0.1, 0.2]])
  48. self.assert_(np.allclose(rs, expected))
  49. if __name__ == '__main__':
  50. import nose
  51. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  52. exit=False)