-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 exponential mode for annualized_return calculation in risk_… #1433
base: main
Are you sure you want to change the base?
Conversation
DimitarSivrev, I tried the exponential mode, but it seems still not correct. pls see here #1512 |
@@ -24,16 +24,14 @@ | |||
logger = get_module_logger("Evaluate") | |||
|
|||
|
|||
def risk_analysis(r, N: int = None, freq: str = "day"): | |||
def risk_analysis(r, N: int = None, freq: str = "day", accumulation_mode = "summation"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Literal
https://github.com/microsoft/qlib/blob/main/qlib/typehint.py#L13
@@ -61,10 +61,16 @@ def cal_risk_analysis_scaler(freq): | |||
warnings.warn("risk_analysis freq will be ignored") | |||
if N is None: | |||
N = cal_risk_analysis_scaler(freq) | |||
|
|||
if accumulation_mode not in ["summation", "exponential"]: | |||
raise ValueError("Invalid value for `accumulation_mode`. Only 'summation' and 'exponential' are supported") | |||
|
|||
mean = r.mean() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be geometric mean if production is considered
if accumulation_mode == "summation": | ||
annualized_return = mean * N | ||
else: | ||
annualized_return = (np.prod(1 + r) - 1) | ||
information_ratio = mean / std * np.sqrt(N) | ||
max_drawdown = (r.cumsum() - r.cumsum().cummax()).min() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The maxdrawdown should also be changed if production is considered
if accumulation_mode == "summation": | ||
annualized_return = mean * N | ||
else: | ||
annualized_return = (np.prod(1 + r) - 1) | ||
information_ratio = mean / std * np.sqrt(N) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if the annualized volatility should be chagned if production is considered.
But intuitively, the accumulated volatility and production vollatility is different.
My first contribution to Qlib,
Resolves #964
Description
This change allows the user to choose between accumulation mode and exponential mode when calculating the annualized_return in the risk_analysis function. The exponential mode uses the formula product of (1 + r_i) minus 1. A new parameter 'accumulation_mode' has been added to the risk_analysis function to control the mode of calculation. An error will be thrown if the parameter is not set to 'accumulation' or 'exponential'. I can resolve any issues with this change.
Types of changes