I am new to Q and see Returns and pct_change being used to compute returns. Can anyone comment on their use case or are they basically the same?
I am new to Q and see Returns and pct_change being used to compute returns. Can anyone comment on their use case or are they basically the same?
Good question! (and welcome)
What is the difference between Returns
and pct_change
?
First off 'Returns' is a factor (https://www.quantopian.com/help#built-in-factors) while 'pct_change' is a pandas dataframe method (https://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.pct_change.html). Let's see what that means.
Factors are used to define the columns which are returned from a pipeline. Note the term 'define'. Returns itself doesn't directly manipulate any data - just set's up the mechanism to fetch data and perform any calcs. The code below will create a pipeline with a single column for returns.
pipe = Pipeline(
columns = {'returns': Returns(inputs=[USEquityPricing.close], window_length=2) }
)
Pandas methods, on the other hand, directly manipulate data in a dataframe. The output of a pipeline is a pandas dataframe so any of the pandas methods can manipulate the output of a pipeline but only after it has been run. The code below will create a new column 'percent_change' in the dataframe returned by a pipeline.
result['percent_change'] = result.groupby(level='stock').close_price.pct_change()
In the end one will get the exact same number in a column of a dataframe.
There are several advantages to getting returns using the Returns
factor however. First, the pct_change
approach will leave a NaN value in the first row while Returns
will not. Second there is a bit cleaner way to get log returns by using the Returns
factor. Finally, one can create filters and other factors going the factor approach which can't be done if calculating the pct_change once pipeline is run. See the attached notebook for a bit of explanation of each of these.
Hope that clears it up a bit.
(Not to confuse things more but there is also a data fetch method called returns
(notice the small r) https://www.quantopian.com/help#quantopian_research_returns. Assuming that's not what the question was asking? )
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.