Giter Club home page Giter Club logo

Comments (6)

prd1 avatar prd1 commented on June 4, 2024 1

Looks good Brad. Thank you. I really like the rolling functionality - didn't think it was possible to make it this fast!

from pyfinance.

prd1 avatar prd1 commented on June 4, 2024

example:

from pyfinance import ols
import pandas as pd
import numpy as np
X = pd.DataFrame(index=list(range(5)), data=list(range(5)), columns=['X'])
Y = pd.DataFrame(index=list(range(5)), data=list(np.arange(0,10,2)+1), columns=['Y'])
window = 2
reg_df = pd.concat([Y, X],axis=1)
rolling_reg = ols.PandasRollingOLS(y=reg_df.iloc[:,0],  # Series
                                   x=reg_df.iloc[:,1:],  # DataFrame
                                   window=window,
                                   has_const=False,
                                   use_const=False,
                                   names=['Const','X'])
print(rolling_reg.beta)

output:

   Const    X
1    1.0  2.0
2    1.0  2.0
3    1.0  2.0
4    1.0  2.0

if you try to run without specifying a name for the constant in "names" parameter:
ValueError: Shape of passed values is (2, 4), indices imply (1, 4)

from pyfinance.

bsolomon1124 avatar bsolomon1124 commented on June 4, 2024

Thank you for catching this @prd1 and I'm sorry it's taken me so long to have a look. Hopefully better late than never. It seems to be a legitimate bug. The signature for ols._clean_xy() is:

def _clean_xy(y, x=None, has_const=False, use_const=True):

However, in ols.RollingOLS.__init__(), it gets called just as:

    def __init__(self, y, x=None, has_const=False, use_const=True):
        self.x, self.y, self.k = _clean_xy(y, x)

Noticeably leaving the other two parameters unpassed and so ignoring them.

I'll make a correction, push the updated changes and post a new package version on PyPI.

from pyfinance.

bsolomon1124 avatar bsolomon1124 commented on June 4, 2024

Closed via 5d07d1e and 4d7d7b5 - please let me know if that doesn't clear everything up @prd1. These changes are reflected on PyPI as well.

from pyfinance.

prd1 avatar prd1 commented on June 4, 2024

Hi Brad. Sorry still having a couple issues with constants. Using 1.2.1.

Issue1:

from pyfinance import ols
import pandas as pd
import numpy as np
X = pd.DataFrame(index=list(range(5)), data=list(range(5)), columns=['X'])
X.loc[:,'Const'] = 1
Y = pd.DataFrame(index=list(range(5)), data=list(np.arange(0,10,2)+1), columns=['Y'])
reg_df = pd.concat([Y, X],axis=1)
rolling_reg = ols.OLS(y=reg_df.iloc[:,0],  # Series
                                   x=reg_df.iloc[:,1:],  # DataFrame
                                   has_const=False,
                                   use_const=True,
                     )
print(rolling_reg.beta)

returns: ValueError: data already contains a constant
As it should. But, executing the same code with has_const = True returns:
ValueError: Data does not contain a constant; specify has_const=False

Issue 2: (I think)

from pyfinance import ols
import pandas as pd
import numpy as np
X = pd.DataFrame(index=list(range(5)), data=list(range(5)), columns=['X'])
# X.loc[:,'Const'] = 1
Y = pd.DataFrame(index=list(range(5)), data=list(np.arange(0,10,2)+1), columns=['Y'])
reg_df = pd.concat([Y, X],axis=1)
rolling_reg = ols.OLS(y=reg_df.iloc[:,0],  # Series
                                   x=reg_df.iloc[:,1:],  # DataFrame
                                   has_const=False,
                                   use_const=True,
                     )
print(rolling_reg.beta)

Above does not add a constant. Is it supposed to?

Currently, adding the constant manually and specifying has_const and use_cost both as False will give the desired result with constant.

from pyfinance.

bsolomon1124 avatar bsolomon1124 commented on June 4, 2024

Hi @prd1, thanks for bearing with me here. I've made a few more commits with minor fixes and tests that should correct the issues you pointed out here, too. Current version is 1.2.2 and the commits are 6de1db1 and 5046dd9. Please let me know if this clears everything up, and I'll leave things open til then. Using pip install --upgrade pyfinance should give you the latest version.

from pyfinance.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.