/HLI_RNAseqQC_optimized/pymodules/python2.7/lib/python/statsmodels-0.5.0-py2.7-linux-x86_64.egg/statsmodels/sandbox/nonparametric/tests/test_smoothers.py

https://gitlab.com/pooja043/Globus_Docker_4 · Python · 108 lines · 63 code · 31 blank · 14 comment · 1 complexity · 530b8a3febe7ccae7f1da7663648a065 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Nov 04 10:51:39 2011
  4. Author: Josef Perktold
  5. License: BSD-3
  6. """
  7. import numpy as np
  8. from numpy.testing import assert_almost_equal, assert_equal
  9. from statsmodels.sandbox.nonparametric import smoothers, kernels
  10. from statsmodels.regression.linear_model import OLS, WLS
  11. class CheckSmoother(object):
  12. def test_predict(self):
  13. assert_almost_equal(self.res_ps.predict(self.x),
  14. self.res2.fittedvalues, decimal=13)
  15. assert_almost_equal(self.res_ps.predict(self.x[:10]),
  16. self.res2.fittedvalues[:10], decimal=13)
  17. def test_coef(self):
  18. #TODO: check dim of coef
  19. assert_almost_equal(self.res_ps.coef.ravel(),
  20. self.res2.params, decimal=14)
  21. def test_df(self):
  22. #TODO: make into attributes
  23. assert_equal(self.res_ps.df_model(), self.res2.df_model+1) #with const
  24. assert_equal(self.res_ps.df_fit(), self.res2.df_model+1) #alias
  25. assert_equal(self.res_ps.df_resid(), self.res2.df_resid)
  26. class BasePolySmoother(object):
  27. def __init__(self):
  28. #DGP: simple polynomial
  29. order = 3
  30. sigma_noise = 0.5
  31. nobs = 100
  32. lb, ub = -1, 2
  33. self.x = x = np.linspace(lb, ub, nobs)
  34. self.exog = exog = x[:,None]**np.arange(order+1)
  35. y_true = exog.sum(1)
  36. np.random.seed(987567)
  37. self.y = y = y_true + sigma_noise * np.random.randn(nobs)
  38. class TestPolySmoother1(BasePolySmoother, CheckSmoother):
  39. def __init__(self):
  40. super(self.__class__, self).__init__() #initialize DGP
  41. y, x, exog = self.y, self.x, self.exog
  42. #use order = 2 in regression
  43. pmod = smoothers.PolySmoother(2, x)
  44. pmod.fit(y) #no return
  45. self.res_ps = pmod
  46. self.res2 = OLS(y, exog[:,:2+1]).fit()
  47. class TestPolySmoother2(BasePolySmoother, CheckSmoother):
  48. def __init__(self):
  49. super(self.__class__, self).__init__() #initialize DGP
  50. y, x, exog = self.y, self.x, self.exog
  51. #use order = 3 in regression
  52. pmod = smoothers.PolySmoother(3, x)
  53. #pmod.fit(y) #no return
  54. pmod.smooth(y) #no return, use alias for fit
  55. self.res_ps = pmod
  56. self.res2 = OLS(y, exog[:,:3+1]).fit()
  57. class TestPolySmoother3(BasePolySmoother, CheckSmoother):
  58. def __init__(self):
  59. super(self.__class__, self).__init__() #initialize DGP
  60. y, x, exog = self.y, self.x, self.exog
  61. nobs = y.shape[0]
  62. weights = np.ones(nobs)
  63. weights[:nobs//3] = 0.1
  64. weights[-nobs//5:] = 2
  65. #use order = 2 in regression
  66. pmod = smoothers.PolySmoother(2, x)
  67. pmod.fit(y, weights=weights) #no return
  68. self.res_ps = pmod
  69. self.res2 = WLS(y, exog[:,:2+1], weights=weights).fit()
  70. if __name__ == '__main__':
  71. t1 = TestPolySmoother1()
  72. t1.test_predict()
  73. t1.test_coef()
  74. t1.test_df
  75. t3 = TestPolySmoother3()
  76. t3.test_predict()