Implement geometric accumulation mode for risk_analysis function (#964) #1895
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This change adds a new parameter
mode
to therisk_analysis
function which determines the accumulation method to be used when calculating returns. Default parameter value formode
is "sum", which maintains original behavior.If
mode
is "product", the following calculations are used instead of the current arithmetic methods to align with expectations for geometrically accumulated returns:mean = np.prod(1 + r) ** (1 / len(r)) - 1 # geometric mean
std = np.log(1 + r).std(ddof=1) # volatility of log returns
cumulative_return = np.prod(1 + r) - 1
annualized_return = (1 + cumulative_return) ** (N / len(r)) - 1
max_drawdown = ((1 + r).cumprod() / (1 + r).cumprod().cummax() - 1).min() # max percentage drawdown from peak cumulative product
information_ratio
uses the same equation in "product" mode as in "sum" mode, but with geometric mean and volatility of log returns instead of their arithmetic counterparts.These calculations are what I would intuitively expect for geometrically compounded returns (there weren't too many specifics in the issue thread), but I'm happy to change any of the underlying calculations if a different equation should be used for any of the metrics in "product" mode.
Motivation and Context
Resolves #964 as well as the TODO inside
risk_analysis
Adding a geometric/product accumulation method to the
risk_analysis
function is a new feature that aligns with what many users would expect in quant finance.How Has This Been Tested?
pytest qlib/tests/test_all_pipeline.py
under upper directory ofqlib
.Screenshots of Test Results (if appropriate):
Types of changes