PageRenderTime 56ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/pandas/stats/interface.py

https://github.com/smc77/pandas
Python | 134 lines | 106 code | 1 blank | 27 comment | 0 complexity | 1702a4d77fa83691d28ac54f18c19791 MD5 | raw file
  1. from pandas.core.api import Series, DataFrame, Panel, MultiIndex
  2. from pandas.stats.ols import OLS, MovingOLS
  3. from pandas.stats.plm import PanelOLS, MovingPanelOLS, NonPooledPanelOLS
  4. import pandas.stats.common as common
  5. def ols(**kwargs):
  6. """Returns the appropriate OLS object depending on whether you need
  7. simple or panel OLS, and a full-sample or rolling/expanding OLS.
  8. Will be a normal linear regression or a (pooled) panel regression depending
  9. on the type of the inputs:
  10. y : Series, x : DataFrame -> OLS
  11. y : Series, x : dict of DataFrame -> OLS
  12. y : DataFrame, x : DataFrame -> PanelOLS
  13. y : DataFrame, x : dict of DataFrame/Panel -> PanelOLS
  14. y : Series with MultiIndex, x : Panel/DataFrame + MultiIndex -> PanelOLS
  15. Parameters
  16. ----------
  17. y: Series or DataFrame
  18. See above for types
  19. x: Series, DataFrame, dict of Series, dict of DataFrame, Panel
  20. weights : Series or ndarray
  21. The weights are presumed to be (proportional to) the inverse of the
  22. variance of the observations. That is, if the variables are to be
  23. transformed by 1/sqrt(W) you must supply weights = 1/W
  24. intercept: bool
  25. True if you want an intercept. Defaults to True.
  26. nw_lags: None or int
  27. Number of Newey-West lags. Defaults to None.
  28. nw_overlap: bool
  29. Whether there are overlaps in the NW lags. Defaults to False.
  30. window_type: {'full sample', 'rolling', 'expanding'}
  31. 'full sample' by default
  32. window: int
  33. size of window (for rolling/expanding OLS). If window passed and no
  34. explicit window_type, 'rolling" will be used as the window_type
  35. Panel OLS options:
  36. pool: bool
  37. Whether to run pooled panel regression. Defaults to true.
  38. entity_effects: bool
  39. Whether to account for entity fixed effects. Defaults to false.
  40. time_effects: bool
  41. Whether to account for time fixed effects. Defaults to false.
  42. x_effects: list
  43. List of x's to account for fixed effects. Defaults to none.
  44. dropped_dummies: dict
  45. Key is the name of the variable for the fixed effect.
  46. Value is the value of that variable for which we drop the dummy.
  47. For entity fixed effects, key equals 'entity'.
  48. By default, the first dummy is dropped if no dummy is specified.
  49. cluster: {'time', 'entity'}
  50. cluster variances
  51. Examples
  52. --------
  53. # Run simple OLS.
  54. result = ols(y=y, x=x)
  55. # Run rolling simple OLS with window of size 10.
  56. result = ols(y=y, x=x, window_type='rolling', window=10)
  57. print result.beta
  58. result = ols(y=y, x=x, nw_lags=1)
  59. # Set up LHS and RHS for data across all items
  60. y = A
  61. x = {'B' : B, 'C' : C}
  62. # Run panel OLS.
  63. result = ols(y=y, x=x)
  64. # Run expanding panel OLS with window 10 and entity clustering.
  65. result = ols(y=y, x=x, cluster='entity', window_type='expanding', window=10)
  66. Returns
  67. -------
  68. The appropriate OLS object, which allows you to obtain betas and various
  69. statistics, such as std err, t-stat, etc.
  70. """
  71. pool = kwargs.get('pool')
  72. if 'pool' in kwargs:
  73. del kwargs['pool']
  74. window_type = kwargs.get('window_type')
  75. window = kwargs.get('window')
  76. if window_type is None:
  77. if window is None:
  78. window_type = 'full_sample'
  79. else:
  80. window_type = 'rolling'
  81. else:
  82. window_type = common._get_window_type(window_type)
  83. if window_type != 'full_sample':
  84. kwargs['window_type'] = common._get_window_type(window_type)
  85. y = kwargs.get('y')
  86. x = kwargs.get('x')
  87. panel = False
  88. if isinstance(y, DataFrame) or (isinstance(y, Series) and
  89. isinstance(y.index, MultiIndex)):
  90. panel = True
  91. if isinstance(x, Panel):
  92. panel = True
  93. if window_type == 'full_sample':
  94. for rolling_field in ('window_type', 'window', 'min_periods'):
  95. if rolling_field in kwargs:
  96. del kwargs[rolling_field]
  97. if panel:
  98. if pool == False:
  99. klass = NonPooledPanelOLS
  100. else:
  101. klass = PanelOLS
  102. else:
  103. klass = OLS
  104. else:
  105. if panel:
  106. if pool == False:
  107. klass = NonPooledPanelOLS
  108. else:
  109. klass = MovingPanelOLS
  110. else:
  111. klass = MovingOLS
  112. return klass(**kwargs)