bsolomon1124 / pyfinance

Python package designed for general financial and security returns analysis.
https://pypi.python.org/pypi/pyfinance
MIT License
325 stars 66 forks source link

PandasRollingOLS reports ValueError #11

Open zhangda425 opened 5 years ago

zhangda425 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago
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)))