[Question] Custom Factor as Inputs for another Custom Factor in Pipeline?

Hi,

I have some questions about the custom factor.

How am I able to use the output of one custom factor as the input of another custom factor. For example, I have a few custom factors depending on a window of historic daily cross section residual return, here residual return will be another another custom factor I created separately.

1. I tried to create a custom factor derived from another one and use super(), but was warned that super() is not allowed.
2. What I can though of is to add the logic for calculating the residual return for each and every other factors that depend on it. But apparently it is redundant and low efficient.
3. Let's say I need the past K days residual return for M securities, that would be a K * M matrix. Can I cache the results so that I only need to calculate the last day's residual return and append it to the bottom of this matrix (drop the top line) every day?

Many Thanks, any comment is welcomed.
Tony

5 responses

Any comment please? I don't know if it is feasible in current Pipeline framework to redirect the output of one custom factor to the input of another custom factor? Can anyone answer my question? Thanks!

Here is what David Michalowicz said about using factors as input of other factor:

"[...] is now allowed for a select few factors deemed safe for use as inputs. This includes Returns and any factors created from rank or zscore. The main reason that these factors can be used as inputs is that they are comparable across splits. Returns, rank and zscore produce normalized values, meaning that they can be meaningfully compared in any context."

Anyway, if you are sure your factor is safe to be used as input to another factor define window_safe = True in the factor class and pipeline won't complain.

And this is an example:

from quantopian.pipeline.factors import CustomFactor, Returns

class LogReturns(CustomFactor):
inputs = [Returns(window_length=2)]
window_length = 1
window_safe = True

def compute(self, today, assets, out, returns):
out[:] = np.log( (returns+1) )

class MeanReturn(CustomFactor):
inputs = [Returns(window_length=2)]
def compute(self, today, assets, out, returns):
out[:] = np.nanmean(returns, axis=0)

returns = Returns(window_length=50)