Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement geometric accumulation mode for risk_analysis function (#964) #1895

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

eabjab
Copy link

@eabjab eabjab commented Mar 13, 2025

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?

  • Pass the test by running: pytest qlib/tests/test_all_pipeline.py under upper directory of qlib.
  • If you are adding a new feature, test on your own test scripts.

Screenshots of Test Results (if appropriate):

  1. Pipeline test:
qlib_issue#964_pipeline_tests
  1. Your own tests:
qlib_issue#964_custom_tests

Types of changes

  • Fix bugs
  • Add new feature
  • Update documentation

@github-actions github-actions bot added the waiting for triage Cannot auto-triage, wait for triage. label Mar 13, 2025
@eabjab
Copy link
Author

eabjab commented Mar 13, 2025

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for triage Cannot auto-triage, wait for triage.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Concerns about the calculation of return
1 participant