pyfinance icon indicating copy to clipboard operation
pyfinance copied to clipboard

PandasRollingOLS reports ValueError

Open zhangda425 opened this issue 7 years ago • 2 comments

the following code cannot work and report ValueError. hope to see a better version soon, thx.

from pyfinance import ols data = {'A':[2,3,4,5,6],'B':[10,11,12,13,14]} df = pd.DataFrame(data) rolling = ols.RollingOLS(y=df['B'], x=df['A'], window=3,has_const=False,use_const=False)

File "C:\ProgramData\Anaconda3\lib\site-packages\pyfinance\utils.py", line 705, in rolling_windows ' a, {1}.'.format(window, a.shape[0]))

ValueError: Specified window length of 3 exceeds length of a, 1.

zhangda425 avatar Mar 10 '19 03:03 zhangda425

I fix the bug above by adding
if x.shape[0]==1: x=x.T into the function _clean_xy. But when I call the property rsq of rolling, there bumps another bug.

from pyfinance import ols data = {'A':range(0,500),'B':range(500,1000)} df = pd.DataFrame(data) y=df['B'] x=df['A'] rolling = ols.PandasRollingOLS(y=y, x=x, window=3,has_const=False,use_const=False) rolling.rsq Traceback (most recent call last):

File "", line 1, in rolling.rsq

File "C:\ProgramData\Anaconda3\lib\site-packages\pyfinance\ols.py", line 822, in rsq return self._wrap_series(stat='_rsq')

File "C:\ProgramData\Anaconda3\lib\site-packages\pyfinance\ols.py", line 749, in _wrap_series return Series(getattr(self, stat), index=self.ridx, name=name)

File "C:\ProgramData\Anaconda3\lib\site-packages\pyfinance\ols.py", line 471, in _rsq return self._ss_reg / self._ss_tot

File "C:\ProgramData\Anaconda3\lib\site-packages\pyfinance\ols.py", line 459, in _ss_reg return np.sum(np.square(self._predicted

File "C:\ProgramData\Anaconda3\lib\site-packages\pyfinance\ols.py", line 426, in _predicted axis=-1)))

ValueError: shapes (498,3) and (498,1) not aligned: 3 (dim 1) != 498 (dim 0)

zhangda425 avatar Mar 10 '19 08:03 zhangda425

it seems that you use matrix multiplication.  should it be dot multiplication? I don't know. it takes so much efforts to fix them........

def _predicted(self): """The predicted values of y ('yhat').""" return np.squeeze(np.matmul(self.xwins, np.expand_dims(self.solution, axis=-1)))

zhangda425 avatar Mar 10 '19 08:03 zhangda425