Back to Community
np.lib.stride_tricks.as_strided support

I'm trying to use np.lib.stride_tricks.as_strided for some rolling window calculations so that they are faster than for loops. I tested my code standalone in Python but in Quantopian it seems it is not supported or blocked?

This is the function call:

    lag_rol = np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

This is the error:

SecurityViolation: 0002 Security Violations(s): Insecure attribute access "numpy.lib.stride_tricks" on line 496 There was a runtime error on line 110.  

Thanks,
Carlos

6 responses

That submodule isn't currently supported on the platform, I'll add your request for the functionality.

cheers,
Alisa

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks Alisa, do you know how long adding a new module usually takes?

All the best,
Carlos

We prioritize adding modules with the rest of our queue: adding new features, improving the backtest and research API, expanding our data availability, security, and more. Modules are usually added in batches (instead of one-offs) and when these are available, we share in the community. The timeline varies, but it's not something we're focused on right now. We took a look at this specific function and unfortunately it won't be something we can add to the platform -- there are a few security concerns. If you'd like to use it locally, you can download Zipline (our open-sourced backtester) and work on your machine.

Elaborating a bit on the security issue here: as_strided in full generality allows access to raw (and therefore potentially unsafe) memory addresses.

Zipline does currently implement a few utility functions in terms of safe usages ofas_strided in zipline.utils.numpy_utils.

It looks like you're looking to implement rolling-window calculations in terms of as_strided, which isn't something that we currently have implemented, but which would likely be a welcome contribution.

I took a stab at implementing what looks like your particular use-case in this Zipline PR.

Sample output:

In [1]: import numpy as np

In [2]: from zipline.utils.numpy_utils import rolling_window

In [3]: data = np.arange(15).reshape(5, 3)

In [4]: data  
Out[4]:  
array([[ 0,  1,  2],  
       [ 3,  4,  5],  
       [ 6,  7,  8],  
       [ 9, 10, 11],  
       [12, 13, 14]])

In [5]: window = rolling_window(data, 3)

In [6]: window[0]  
Out[6]:  
array([[0, 1, 2],  
       [3, 4, 5],  
       [6, 7, 8]])

In [7]: window[1]  
Out[7]:  
array([[ 3,  4,  5],  
       [ 6,  7,  8],  
       [ 9, 10, 11]])

In [8]: window[2]  
Out[8]:  
array([[ 6,  7,  8],  
       [ 9, 10, 11],  
       [12, 13, 14]])  
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Scott,

Thanks for looking into this and providing an example with existing zipline utils. I'll give it a try.

Thanks,
Carlos

@Carlos, to be clear, I don't think zipline.utils.numpy_utils is currently whitelisted either, but it's more likely that it might be made available compared to the full freedom of as_strided because of the memory safety concern.