Sortino 比率计算
Sortino Ratio Calc
题目详情
Sortino Ratio 在 Sharpe Ratio 的基础上仅惩罚下行波动,对非正态分布的收益率提供更准确的风险调整绩效衡量。通过专注于低于特定目标的收益率,帮助投资者区分有害波动和一般价格波动。
任务:实现函数 solution(returns, target),计算 Sortino Ratio:下行偏差 = sqrt(mean(min(r - target, 0)^2)),Sortino Ratio = (mean(returns) - target) / 下行偏差。
英文原题
The Sortino Ratio improves upon the Sharpe Ratio by penalizing only downside volatility, offering a more accurate risk-adjusted performance measure for return distributions that are not normally distributed. By focusing exclusively on returns falling below a specific target, it helps investors distinguish between harmful volatility and general price fluctuations.
Task
Implement a function solution(returns, target_return) that calculates the Sortino Ratio given a list of historical returns and a
解析
问题分析
Sortino 比率是夏普比率的改进版,仅使用下行标准差(低于最低可接受收益的波动)。这更符合投资者对风险的直觉——上行波动是好事,只有下行波动需要惩罚。
实现
double sortinoRatio(const std::vector<double>& returns, double mar = 0.0) {
double mean = std::accumulate(returns.begin(), returns.end(), 0.0) / returns.size();
double sum_sq = 0; int count = 0;
for (double r : returns) {
if (r < mar) { double diff = r - mar; sum_sq += diff * diff; count++; }
}
double downside_dev = count > 0 ? std::sqrt(sum_sq / count) : 0.0;
return downside_dev > 0 ? (mean - mar) / downside_dev : 0.0;
}复杂度与边界
- 时间复杂度:O(N) 单次遍历
- 空间复杂度:O(1)
- 边界条件:(1) 无下行数据时返回 0 (2) mar 通常取 0 或无风险利率 (3) 样本量小(<30)时统计不稳定
英文解析
Analysis
The Sortino ratio is an improvement over the Sharpe ratio that uses only downside standard deviation (volatility below the minimum acceptable return). This better aligns with investor intuition about risk - upside volatility is desirable, only downside volatility should be penalized.
Solution
double sortinoRatio(const std::vector<double>& returns, double mar = 0.0) {
double mean = std::accumulate(returns.begin(), returns.end(), 0.0) / returns.size();
double sum_sq = 0; int count = 0;
for (double r : returns) {
if (r < mar) { double diff = r - mar; sum_sq += diff * diff; count++; }
}
double downside_dev = count > 0 ? std::sqrt(sum_sq / count) : 0.0;
return downside_dev > 0 ? (mean - mar) / downside_dev : 0.0;
}Complexity & Edge Cases
- Time complexity: O(N) single pass
- Space complexity: O(1)
- Edge cases: (1) Returns 0 when no downside data exists (2) mar is typically set to 0 or the risk-free rate (3) Statistically unstable with small sample sizes (<30)
Verification
Calculate Sortino ratio for a strategy with asymmetric returns. Verify downside deviation only counts negative returns. Benchmark against Sharpe ratio to confirm downside-only penalty effect.
Key Considerations
The Sortino ratio is preferred over Sharpe for strategies with asymmetric return distributions (e.g., trend-following CTA strategies with large upside but limited downside). Using downside deviation avoids penalizing profitable volatility, giving a more accurate risk-adjusted performance measure for long-only equity and option strategies.