I'm running a function called calculate_hedgeratio inside pandas.rolling_apply. The function works when called for itself, but inside rolling_apply it throws the following error:
regression = pandas.ols(x=df[xsymbol], y=df[ysymbol])
IndexError: only integers, slices (
:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices
Here is the function calculate_hedgeratio:
def calculate_hedgeratio(df, xsymbol, ysymbol): import pandas from scipy import odr import numpy regression = pandas.ols(x=df[xsymbol], y=df[ysymbol]) m = regression.beta[0] n = regression.beta[1] model = odr.Model(lambda B,x: (B[0]*x + B[1])) data = odr.RealData(df[xsymbol].values,df[ysymbol].values, sx=numpy.std(df[xsymbol].values), sy=numpy.std(df[ysymbol].values)) myodr = odr.ODR(data, model, beta0=[m, n]) myoutput = myodr.run() results = [myoutput.beta[0], myoutput.beta[1], myoutput.res_var] return results This is the function from where I call it:
def simple_Spreadtest(symbol1, symbol2, startdate, enddate, lookbackperiod): import pandas import numpy df=pandas.DataFrame() df[symbol1]=numpy.random.rand(1000) df[symbol2]=numpy.random.rand(1000) df['m','n','Chired'] = pandas.rolling_apply(df,lookbackperiod, lambda x: calculate_hedgeratio(x, symbol1, symbol2)) return df 'Main Code:' symbol1 = 'A' symbol2 = 'B' lookbackperiod = 250 import datetime startdate = datetime.datetime(1990, 1, 1) enddate = datetime.datetime(2015, 7, 31) df = simple_Spreadtest(symbol1, symbol2, startdate, enddate, lookbackperiod) What's the reason for it?