As we know, Returns can be passed as input in customfactor. I am curious too see if it gives the same result as pct_change. So I create a customfactor like this:
class test(CustomFactor):
inputs = [USEquityPricing.close,
Returns(inputs=[USEquityPricing.close], window_length= 3),
]
window_length=10
def compute(self, today, assets, out, close, ret):
prices = pd.DataFrame(close, columns=assets)
prices_ret = prices.pct_change(2)
out[:] = prices_ret.iloc[-1,:]
Here is the printout:
prices_ret
DataFrame:
24 700 1335 5061 20088
0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN
2 -0.022979 -0.001833 -0.014212 -0.020139 -0.019471
3 -0.058495 -0.041379 -0.024194 -0.052760 -0.010981
4 -0.024083 -0.022644 -0.017038 -0.048193 0.004000
5 0.038641 0.007848 0.038567 -0.028772 0.003824
6 0.011843 -0.028178 0.010667 -0.029412 0.014229
7 -0.010699 0.002596 0.026525 -0.050133 0.026590
8 0.033809 0.043170 0.060686 -0.004603 0.019943
9 0.052763 0.018123 0.023256 0.031587 0.033047pd.DataFrame(ret, columns=assets)
DataFrame:
24 700 1335 5061 20088
0 -0.015948 -0.029063 -0.052632 -0.010602 -0.012518
1 -0.006575 -0.025061 -0.067669 -0.006593 -0.041141
2 -0.022979 -0.001833 -0.014212 -0.020139 -0.019471
3 -0.058495 -0.041379 -0.024194 -0.052760 -0.010981
4 -0.024083 -0.022644 -0.017038 -0.048193 0.004000
5 0.038641 0.007848 0.038567 -0.028772 0.003824
6 0.011843 -0.028178 0.010667 -0.029412 0.014229
7 -0.010699 0.002596 0.026525 -0.050133 0.026590
8 0.033809 0.043170 0.060686 -0.004603 0.019943
9 0.052763 0.018123 0.023256 0.031587 0.033047
Notice that the first two rows are different. This might have an impact if I do column-wise ops (sum, mean etc).
So where are the numbers of the first two rows in Returns coming from? Junk? what is the best way to compute and use a percent change ?