Sharpe ratio variance and probabilistic sharpe ratio#232
Sharpe ratio variance and probabilistic sharpe ratio#232tschm merged 6 commits intoJebel-Quant:mainfrom
Conversation
WalkthroughTwo new per-column statistical methods were added to Stats: Changes
Sequence Diagram(s)sequenceDiagram
participant Test as Tests
participant Stats as Stats Class
participant Series as pl.Series
participant Dist as Normal CDF
rect rgb(230, 247, 255)
Test->>Stats: call sharpe_variance(series, periods?)
Note right of Stats: compute mean, sd, skew, kurtosis\nderive asymptotic variance, annualize if periods
Stats-->>Test: variance (float or NaN)
end
rect rgb(245, 255, 230)
Test->>Stats: call prob_sharpe_ratio(series, benchmark_sr)
Note right of Stats: compute observed SR\nuse sharpe_variance to get variance\nif variance>0 -> z=(obs-bench)/sqrt(var)
Stats->>Dist: norm.cdf(z)
Dist-->>Stats: probability
Stats-->>Test: probability (float or NaN)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used🧬 Code graph analysis (1)src/jquantstats/_stats.py (1)
🪛 Ruff (0.14.3)src/jquantstats/_stats.py11-11: Unused Remove unused (RUF100) 🔇 Additional comments (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/jquantstats/_stats.py (2)
433-472: Consider clarifying the annualization behavior in the docstring.The docstring states "annualized if requested" (line 451), but the method doesn't have a boolean flag to control annualization. It always multiplies the base variance by the
periodsparameter (defaulting to_periods_per_year). Consider updating the docstring to be more precise about this behavior.Suggested clarification:
Returns: - float: The asymptotic variance of the Sharpe ratio (annualized if requested). + float: The asymptotic variance of the Sharpe ratio, scaled by the periods parameter.
474-510: Clarify whetherbenchmark_srshould be annualized or unannualized.The docstring doesn't specify whether the
benchmark_srparameter should be annualized or unannualized. Based on the implementation (line 506 uses it directly in the variance formula with unannualizedobserved_sr), it appearsbenchmark_srshould be unannualized. Making this explicit would prevent user confusion.Suggested enhancement:
Args: series (pl.Series): The series to calculate probabilistic Sharpe ratio for. - benchmark_sr (float): The target Sharpe ratio to compare against. + benchmark_sr (float): The target Sharpe ratio to compare against (unannualized).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/jquantstats/_stats.py(2 hunks)tests/test_stats.py(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
tests/test_stats.py (3)
tests/test_quantstats.py (1)
stats(9-19)src/jquantstats/_stats.py (5)
sharpe_variance(434-472)skew(88-98)kurtosis(101-113)sharpe(414-431)prob_sharpe_ratio(475-510)src/jquantstats/_data.py (1)
_periods_per_year(196-216)
src/jquantstats/_stats.py (1)
src/jquantstats/_data.py (1)
_periods_per_year(196-216)
🪛 Ruff (0.14.3)
src/jquantstats/_stats.py
11-11: Unused noqa directive (non-enabled: PLR0904)
Remove unused noqa directive
(RUF100)
🔇 Additional comments (3)
tests/test_stats.py (3)
6-6: LGTM!The import is correctly added to support the new
test_prob_sharpe_ratiotest.
318-339: LGTM!The test correctly validates the
sharpe_varianceimplementation by manually computing the expected variance using the same formula components (skew, kurtosis, unannualized Sharpe ratio, sample size, and periods per year).
342-353: LGTM!The parametrized test validates the probabilistic Sharpe ratio implementation across multiple benchmark values (0.0, 0.5, 1.0), correctly replicating the variance calculation and CDF computation.
|
@mjvakili please adjust the docstrings as suggested by coderabbitai |
|
I modified the docstring of I also made it clear in the docstring of |
|
Many thanks. Great job @mjvakili |
Adds the following functions:
sharpe_varianceprob_sharpe_ratioReference:
Summary by CodeRabbit
New Features
Tests