Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
ValueError: NaN or Inf values provided to FactorExposure for argument 'loadings'.

I'm encountering the following error when running a full backtest between the dates 2007-04-01 and 2010-05-01:

ValueError: NaN or Inf values provided to FactorExposure for argument 'loadings'.
Rows/Columns with NaNs:
row=Equity(32620 [PFF]) col='momentum'
row=Equity(32620 [PFF]) col='short_term_reversal'
row=Equity(32620 [PFF]) col='size'
row=Equity(32620 [PFF]) col='value'
row=Equity(32620 [PFF]) col='volatility'
Rows/Columns with Infs:
None
There was a runtime error on line 194.

I've copied the algorithm in the getting started tutorial and dropped in my custom alpha factor and a factor for filtering out securities with nans and infs as a mask for it. I've verified that my pipeline output is nan free over the test period via the research interface. In any case the error seems to suggest that it is the Factor values that are causing problems. The 2010-04-01 to 2013-05-01 window works fine without throwing any errors.

Line 194 is : version=opt.Newest
This is within:
factor_risk_constraints = opt.experimental.RiskModelExposure(
context.risk_factor_betas,
version=opt.Newest
)

As a side note I thought Q1500US and QTradableStocksUS excluded etfs (I've reproduced this with both universes as base)?

4 responses

I am hitting the same issue after introducing risk_loading_pipeline/RiskModelExposure into my algorithm, and have the same questions as Josh (why am I getting this error, and why are ETFs being considered when using Q1500US)...

To get rid of the error try adding dropna() to risk_loading_pipeline by changing the code from:

constrain_sector_style_risk = opt.experimental.RiskModelExposure(
context.risk_loading_pipeline,
version=0,
)

By changing to this:

constrain_sector_style_risk = opt.experimental.RiskModelExposure(
context.risk_loading_pipeline.dropna(),
version=0,
)

Thanks Indigo,

This is what the current tutorial code should look like to avoid throwing the error in the event anyone with even less programming experience then myself searches for this:

factor_risk_constraints = opt.experimental.RiskModelExposure(
context.risk_factor_betas.dropna(),
version=opt.Newest
)

Might be worth adding that to the tutorial until the risk model is a little more stable. I'm still curious why etfs are showing up in QTradableStocksUS and Q1500US?

Thanks Indigo Monkey very helpful. Again, it is curious ETFs are showing up in the QTradableStocksUS universe. In my case HYG. My argument has always been from the asset allocation side and the inclusion of Bonds makes perfect sense from that point of view. Nonetheless, what other asset class ETFs are hidden away in that universe? Gold? Crude?