Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with Linear Regression

I'm trying to do a linear regression calculation in before_trading_start, but I'm not getting the results I was expecting. As a test I ran a linear regression in pipeline, and as you can see the results sometimes move in sympathy, but mostly not.

What am I doing wrong?

4 responses

Try this way:

# Help with Linear Regression

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline.filters import StaticAssets  
from quantopian.pipeline.factors import Returns  
from quantopian.pipeline import Pipeline  
import numpy as np  
# ---------------------------------------------------------  
stocks, ret_wnd, wnd = symbols('SHAK','TRIP','TSLA'), 1, 10  
# ---------------------------------------------------------  
def initialize(context):  
    schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(minutes = 65))  
    m = StaticAssets(stocks)  
    stocks_returns = Returns(window_length = ret_wnd + 1, mask = m)  
    market_returns = Returns(window_length = ret_wnd + 1, mask = StaticAssets(symbols('SPY')))[symbol('SPY')]  
    ret_regr = stocks_returns.linear_regression(target = market_returns, regression_length = wnd, mask = m)  
    alpha = ret_regr.alpha  
    beta = ret_regr.beta  
    attach_pipeline(Pipeline(columns = {'alpha': alpha, 'beta': beta}, screen = m), 'pipeline')

def before_trading_start(context, data):  
    bars = ret_wnd + wnd

    context.output = pipeline_output('pipeline')  
    record(b1 = context.output.beta[symbol('SHAK')])  
    record(a1 = context.output.alpha[symbol('SHAK')])  
    stock = symbol('SHAK')  
    bench = symbol('SPY')  
    bench_history = data.history(bench, 'close', bars, '1d')  
    asset_history = data.history(stock, 'close', bars, '1d')

    y = asset_history.pct_change(ret_wnd)[ret_wnd:]  
    x = bench_history.pct_change(ret_wnd)[ret_wnd:]

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

def rebalance(context, data):  
    order_target_percent(symbol('SHAK'), 1.0)  

Wow, thanks! I understand my mistake now. I misunderstood the how window_length works on the Returns factor, and I thought it was DailyReturns, which I may as well use instead:

    stocks_returns = DailyReturns(mask = m)  
    market_returns = DailyReturns(mask = StaticAssets(symbols('SPY')))[symbol('SPY')]  

Gary(Blue Seahawk),

My code is slightly differ from yours in lines 71, 72

there should be

    ret_wnd = 1

    y = asset_history.pct_change(ret_wnd)[ret_wnd:]  
    x = bench_history.pct_change(ret_wnd)[ret_wnd:]  

or

    y = asset_history.pct_change(1)[1:]  
    x = bench_history.pct_change(1)[1:]  

please correct.

Those two are speaking a language that some of us only understand a little bit.
Clarifying differences merging the two different styles.