Hi all,
I was looking for some help understanding how to create custom factors. I am confused by how the input variable works on the backend. Per the Q's API, it is an MxN matrix with m=securities in the universe and n = window_length. Thus I understand the following example code and performing calculations on axis = 0 to get calculations related to the particular security.
class MedianValue(CustomFactor):
"""
Computes the median value of an arbitrary single input over an
arbitrary window..
Does not declare any defaults, so values for `window_length` and
`inputs` must be passed explicitly on every construction.
"""
def compute(self, today, assets, out, data):
from numpy import nanmedian
out[:] = data.nanmedian(data, axis=0)
However, that logic does not apply to the next code example to mu understanding.
class Momentum(CustomFactor):
# Default inputs
inputs = [USEquityPricing.close]
# Compute momentum
def compute(self, today, assets, out, close):
out[:] = close[-1] / close[0]
In this example, input appears to be a list? If it were a MxN matrix, that calculation does not make any sense?
I am asking because I am currently working on the below piece of code and not sure how to complete it:
class momentum(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 90
def compute(self, today, assets, out, inp):
x = np.arange(len(inp))
slope, intercept, r_value, p_value, std_err = stats.linregress(x, inp)
out[:] = r_value
I've tried messing with this a bunch but keep getting an error that "ValueError: all the input array dimensions except for the concatenation axis must match exactly." I'm quite certain the code as written is trying to pass linregress a list and an array for a regression and breaking it.
But I'm not sure the proper way to format. Any guidance regarding explaining the input variable and this last piece of code is appreciated. All I am trying to achieve in the lat bit is get the r_value of the last x trading days.
Thanks,