Implement geometric accumulation mode for risk_analysis function (#964)
Description
This change adds a new parameter mode to the risk_analysis function which determines the accumulation method to be used when calculating returns. Default parameter value for mode 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?
- [x] Pass the test by running:
pytest qlib/tests/test_all_pipeline.pyunder upper directory ofqlib. - [x] If you are adding a new feature, test on your own test scripts.
Screenshots of Test Results (if appropriate):
-
Pipeline test:
-
Your own tests:
Types of changes
- [ ] Fix bugs
- [x] Add new feature
- [ ] Update documentation
@microsoft-github-policy-service agree
Hi, @eabjab Please let the CI pass by pulling the main branch.
Hi, @eabjab It was nice to see your PR, and I'm glad to see it. But since you didn't reply. So I created a new PR 1938. I merged the main branch based on your commit. It retains your commit information and is now merged. Thank you for your contribution to qlib. This PR will be closed.