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

/statsmodels/sandbox/examples/ex_random_panel.py

http://github.com/statsmodels/statsmodels
Python | 151 lines | 93 code | 19 blank | 39 comment | 2 complexity | 37555fcfb34788e7eaa084da3ac3346b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri May 18 13:05:47 2012
  4. Author: Josef Perktold
  5. moved example from main of random_panel
  6. """
  7. import numpy as np
  8. from statsmodels.sandbox.panel.panel_short import ShortPanelGLS, ShortPanelGLS2
  9. from statsmodels.sandbox.panel.random_panel import PanelSample
  10. import statsmodels.sandbox.panel.correlation_structures as cs
  11. import statsmodels.stats.sandwich_covariance as sw
  12. #from statsmodels.stats.sandwich_covariance import (
  13. # S_hac_groupsum, weights_bartlett, _HCCM2)
  14. from statsmodels.stats.moment_helpers import se_cov
  15. cov_nw_panel2 = sw.cov_nw_groupsum
  16. examples = ['ex1']
  17. if 'ex1' in examples:
  18. nobs = 100
  19. nobs_i = 5
  20. n_groups = nobs // nobs_i
  21. k_vars = 3
  22. # dgp = PanelSample(nobs, k_vars, n_groups, corr_structure=cs.corr_equi,
  23. # corr_args=(0.6,))
  24. # dgp = PanelSample(nobs, k_vars, n_groups, corr_structure=cs.corr_ar,
  25. # corr_args=([1, -0.95],))
  26. dgp = PanelSample(nobs, k_vars, n_groups, corr_structure=cs.corr_arma,
  27. corr_args=([1], [1., -0.9],), seed=377769)
  28. print('seed', dgp.seed)
  29. y = dgp.generate_panel()
  30. noise = y - dgp.y_true
  31. print(np.corrcoef(y.reshape(-1,n_groups, order='F')))
  32. print(np.corrcoef(noise.reshape(-1,n_groups, order='F')))
  33. mod = ShortPanelGLS2(y, dgp.exog, dgp.groups)
  34. res = mod.fit()
  35. print(res.params)
  36. print(res.bse)
  37. #Now what?
  38. #res.resid is of transformed model
  39. #np.corrcoef(res.resid.reshape(-1,n_groups, order='F'))
  40. y_pred = np.dot(mod.exog, res.params)
  41. resid = y - y_pred
  42. print(np.corrcoef(resid.reshape(-1,n_groups, order='F')))
  43. print(resid.std())
  44. err = y_pred - dgp.y_true
  45. print(err.std())
  46. #OLS standard errors are too small
  47. mod.res_pooled.params
  48. mod.res_pooled.bse
  49. #heteroscedasticity robust does not help
  50. mod.res_pooled.HC1_se
  51. #compare with cluster robust se
  52. print(sw.se_cov(sw.cov_cluster(mod.res_pooled, dgp.groups.astype(int))))
  53. #not bad, pretty close to panel estimator
  54. #and with Newey-West Hac
  55. print(sw.se_cov(sw.cov_nw_panel(mod.res_pooled, 4, mod.group.groupidx)))
  56. #too small, assuming no bugs,
  57. #see Peterson assuming it refers to same kind of model
  58. print(dgp.cov)
  59. mod2 = ShortPanelGLS(y, dgp.exog, dgp.groups)
  60. res2 = mod2.fit_iterative(2)
  61. print(res2.params)
  62. print(res2.bse)
  63. #both implementations produce the same results:
  64. from numpy.testing import assert_almost_equal
  65. assert_almost_equal(res.params, res2.params, decimal=12)
  66. assert_almost_equal(res.bse, res2.bse, decimal=13)
  67. mod5 = ShortPanelGLS(y, dgp.exog, dgp.groups)
  68. res5 = mod5.fit_iterative(5)
  69. print(res5.params)
  70. print(res5.bse)
  71. #fitting once is the same as OLS
  72. #note: I need to create new instance, otherwise it continuous fitting
  73. mod1 = ShortPanelGLS(y, dgp.exog, dgp.groups)
  74. res1 = mod1.fit_iterative(1)
  75. res_ols = mod1._fit_ols()
  76. assert_almost_equal(res1.params, res_ols.params, decimal=12)
  77. assert_almost_equal(res1.bse, res_ols.bse, decimal=13)
  78. #cov_hac_panel with uniform_kernel is the same as cov_cluster for balanced
  79. #panel with full length kernel
  80. #I fixe default correction to be equal
  81. mod2._fit_ols()
  82. cov_clu = sw.cov_cluster(mod2.res_pooled, dgp.groups.astype(int))
  83. clubse = se_cov(cov_clu)
  84. cov_uni = sw.cov_nw_panel(mod2.res_pooled, 4, mod2.group.groupidx,
  85. weights_func=sw.weights_uniform,
  86. use_correction='cluster')
  87. assert_almost_equal(cov_uni, cov_clu, decimal=7)
  88. #without correction
  89. cov_clu2 = sw.cov_cluster(mod2.res_pooled, dgp.groups.astype(int),
  90. use_correction=False)
  91. cov_uni2 = sw.cov_nw_panel(mod2.res_pooled, 4, mod2.group.groupidx,
  92. weights_func=sw.weights_uniform,
  93. use_correction=False)
  94. assert_almost_equal(cov_uni2, cov_clu2, decimal=8)
  95. cov_white = sw.cov_white_simple(mod2.res_pooled)
  96. cov_pnw0 = sw.cov_nw_panel(mod2.res_pooled, 0, mod2.group.groupidx,
  97. use_correction='hac')
  98. assert_almost_equal(cov_pnw0, cov_white, decimal=13)
  99. time = np.tile(np.arange(nobs_i), n_groups)
  100. #time = mod2.group.group_int
  101. cov_pnw1 = sw.cov_nw_panel(mod2.res_pooled, 4, mod2.group.groupidx)
  102. cov_pnw2 = cov_nw_panel2(mod2.res_pooled, 4, time)
  103. #s = sw.group_sums(x, time)
  104. c2, ct, cg = sw.cov_cluster_2groups(mod2.res_pooled, time, dgp.groups.astype(int), use_correction=False)
  105. ct_nw0 = cov_nw_panel2(mod2.res_pooled, 0, time, weights_func=sw.weights_uniform, use_correction=False)
  106. cg_nw0 = cov_nw_panel2(mod2.res_pooled, 0, dgp.groups.astype(int), weights_func=sw.weights_uniform, use_correction=False)
  107. assert_almost_equal(ct_nw0, ct, decimal=13)
  108. assert_almost_equal(cg_nw0, cg, decimal=13) #pnw2 0 lags
  109. assert_almost_equal(cov_clu2, cg, decimal=13)
  110. assert_almost_equal(cov_uni2, cg, decimal=8) #pnw all lags
  111. import pandas as pa
  112. #pandas.DataFrame does not do inplace append
  113. se = pa.DataFrame(res_ols.bse[None,:], index=['OLS'])
  114. se = se.append(pa.DataFrame(res5.bse[None,:], index=['PGLSit5']))
  115. clbse = sw.se_cov(sw.cov_cluster(mod.res_pooled, dgp.groups.astype(int)))
  116. se = se.append(pa.DataFrame(clbse[None,:], index=['OLSclu']))
  117. pnwse = sw.se_cov(sw.cov_nw_panel(mod.res_pooled, 4, mod.group.groupidx))
  118. se = se.append(pa.DataFrame(pnwse[None,:], index=['OLSpnw']))
  119. print(se)
  120. #list(se.index)
  121. from statsmodels.iolib.table import SimpleTable
  122. headers = [str(i) for i in se.columns]
  123. stubs=list(se.index)
  124. # print SimpleTable(np.round(np.asarray(se), 4),
  125. # headers=headers,
  126. # stubs=stubs)
  127. print(SimpleTable(np.asarray(se), headers=headers, stubs=stubs,
  128. txt_fmt=dict(data_fmts=['%10.4f']),
  129. title='Standard Errors'))