Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trying to build a modified regression

Hi, I'm trying to build a modified regression, but am having issues due to quantopian's security setup. Basically I thought I can lift the code from http://www.zipline.io/_modules/zipline/pipeline/factors/statistical.html#RollingLinearRegression, paste it into my file (using the proper imports), and then perform the desired modifications on the function

def regress(y, x):  
            regr_results = linregress(y=y, x=x)  
            # `linregress` returns its results in the following order:  
            # slope, intercept, r-value, p-value, stderr  
            alpha[i] = regr_results[1]  
            beta[i] = regr_results[0]  
            r_value[i] = regr_results[2]  
            p_value[i] = regr_results[3]  
            stderr[i] = regr_results[4]  

However I cannot import the modules used, nor can I call the method super. How should I go about this?
Basically what I am trying to do is design a CostumFactor which will also give me the price data for sid(8554) (SPY) for the desired window length... how can I do this?

7 responses

Use numpy. The linear regression code looks something like this:

        A = np.vstack([x, np.ones(len(x))]).T  
        m, c = np.linalg.lstsq(A, y)[0]  

You'll need to import numpy as np.

Here's the documentation: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linalg.lstsq.html

Here's how you'd get the x and y in your custom factor:

class linRegres(CustomFactor):  
    inputs = [USEquityPricing.close]  
    def compute(self, today, assets, out, close):  
        x = returns[sid(8554)] #SPY  
        y = pd.DataFrame(close, columns=assets).pct_change()[1:]  
        # ...  

Hope that gets you started. Sorry I can't be of more help. I barely know what I'm doing in python.

did you test this code before posting it as an answer? because it doesn't work

What do you mean it doesn't work? Of course it works. The linalg.lstsq code is straight from the documentation link I posted. Yes, I've tested it. I've been using it.

I'm guessing I have to define "returns" somewhere like

returns = Returns(window_length=2)  

right?

yeah with a customfactor you set something like this:

out[:] = whatever_value_you_want_to_return

not sure how you would return alpha (intercept), beta (slope), r value, p value, and std from one customfactor. I don't know enough python/quantopian to answer that. Somebody else will have to answer.

class linRegres(CustomFactor):  
    outputs = ['alpha', 'beta', 'r_value']  
    inputs = [USEquityPricing.close]  
    def compute(self, today, assets, out, close):  
        x = returns[sid(8554)] #SPY  
        y = pd.DataFrame(close, columns=assets).pct_change()[1:]  
        # ...  

      for i in range(close.shape[1]):  
        out[i].alpha = ...  
        out[i].beta = ...  
        out[i].r_value = ...  

There's also RollingLinearRegressionOfReturns, which might be simpler.