Back to Community
Regressing all Stocks and printing Alpha

Hi:

How do I regress all stocks over SPY and print the alpha of it? Right now my code is

regression_factor = RollingLinearRegressionOfReturns(  
        target=symbols(8554),  
        returns_length=2,  
        regression_length=50  
    )  
alpha = regression_factor.alpha  

and alpha is a RecArrayField object. What is that? According to the documentations it should be something like a data.frame. There's got to be a easier way to do get a data frame showing just the alphas, right? And BTW can we add the code to all the cells in the documentation? I haven't seen many documentations where the code that shows the output is not available. What is the point of the example outputs if we don't have the code to the output?

2 responses

You are half the way there. RollingLinearRegressionOfReturns is a factor and not the actual data. It's just the definition for how to retrieve the data. You need to use it in conjunction with pipeline. Running the pipeline will return the actual dataframe which has the data you are looking for. (see the documentation on how pipelines work https://www.quantopian.com/help#pipeline-title)

regression_factor = RollingLinearRegressionOfReturns(  
        target=symbols(8554),  
        returns_length=2,  
        regression_length=50,  
        mask = aapl_ibm,  
    ) 

# The regression factor has two outputs. We just care about alpha for now  
alpha = regression_factor.alpha 

# Now create our pipeline object which does the heavy lifting to get the data we specify  
# Add a screen to just return specific assets. Otherwise all assets in the Q database are returned  
# Defining the pipeline is really just defining the columns we want in the returned dataframe and  
# limiting the rows (if desired) to a particular subset of assets.  
my_pipe = Pipeline(  
        screen = aapl_ibm,  
        columns = {  
            'alpha': alpha,  
        }  
    )

results = run_pipeline(my_pipe, '1-5-2017', '2-5-2017')

Here 'results' is the returned dataframe. You can slice and dice and look at this dataframe any way you wish. See the attached notebook.

Loading notebook preview...
Notebook previews are currently unavailable.

Thanks a lot Dan! I'm pretty new to Quantopian and your response really helped! I looked more carefully into the pipeline tutorials and it said the same.