nautilus_analysis/statistics/
sortino_ratio.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::fmt::Display;
17
18use nautilus_model::position::Position;
19
20use crate::{Returns, statistic::PortfolioStatistic};
21
22/// Calculates the Sortino ratio for portfolio returns.
23///
24/// The Sortino ratio is a variation of the Sharpe ratio that only penalizes downside
25/// volatility, making it more appropriate for strategies with asymmetric return distributions.
26///
27/// Formula: `Mean Return / Downside Deviation * sqrt(period)`
28///
29/// Where downside deviation is calculated as:
30/// `sqrt(sum(negative_returns^2) / total_observations)`
31///
32/// Note: Uses total observations count (not just negative returns) as per Sortino's methodology.
33///
34/// # References
35///
36/// - Sortino, F. A., & van der Meer, R. (1991). "Downside Risk". *Journal of Portfolio Management*, 17(4), 27-31.
37/// - Sortino, F. A., & Price, L. N. (1994). "Performance Measurement in a Downside Risk Framework".
38///   *Journal of Investing*, 3(3), 59-64.
39#[repr(C)]
40#[derive(Debug, Clone)]
41#[cfg_attr(
42    feature = "python",
43    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.analysis")
44)]
45pub struct SortinoRatio {
46    period: usize,
47}
48
49impl SortinoRatio {
50    /// Creates a new [`SortinoRatio`] instance.
51    #[must_use]
52    pub fn new(period: Option<usize>) -> Self {
53        Self {
54            period: period.unwrap_or(252),
55        }
56    }
57}
58
59impl Display for SortinoRatio {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "Sortino Ratio ({} days)", self.period)
62    }
63}
64
65impl PortfolioStatistic for SortinoRatio {
66    type Item = f64;
67
68    fn name(&self) -> String {
69        self.to_string()
70    }
71
72    fn calculate_from_returns(&self, raw_returns: &Returns) -> Option<Self::Item> {
73        if !self.check_valid_returns(raw_returns) {
74            return Some(f64::NAN);
75        }
76
77        let returns = self.downsample_to_daily_bins(raw_returns);
78        let total_n = returns.len() as f64;
79        let mean = returns.values().sum::<f64>() / total_n;
80
81        let downside = (returns
82            .values()
83            .filter(|&&x| x < 0.0)
84            .map(|x| x.powi(2))
85            .sum::<f64>()
86            / total_n)
87            .sqrt();
88
89        if downside < f64::EPSILON {
90            return Some(f64::NAN);
91        }
92
93        let annualized_ratio = (mean / downside) * (self.period as f64).sqrt();
94
95        Some(annualized_ratio)
96    }
97    fn calculate_from_realized_pnls(&self, _realized_pnls: &[f64]) -> Option<Self::Item> {
98        None
99    }
100
101    fn calculate_from_positions(&self, _positions: &[Position]) -> Option<Self::Item> {
102        None
103    }
104}
105
106#[cfg(test)]
107mod tests {
108    use std::collections::BTreeMap;
109
110    use nautilus_core::{UnixNanos, approx_eq};
111    use rstest::rstest;
112
113    use super::*;
114
115    fn create_returns(values: Vec<f64>) -> BTreeMap<UnixNanos, f64> {
116        let mut new_return = BTreeMap::new();
117        let one_day_in_nanos = 86_400_000_000_000;
118        let start_time = 1_600_000_000_000_000_000;
119
120        for (i, &value) in values.iter().enumerate() {
121            let timestamp = start_time + i as u64 * one_day_in_nanos;
122            new_return.insert(UnixNanos::from(timestamp), value);
123        }
124
125        new_return
126    }
127
128    #[rstest]
129    fn test_empty_returns() {
130        let ratio = SortinoRatio::new(None);
131        let returns = create_returns(vec![]);
132        let result = ratio.calculate_from_returns(&returns);
133        assert!(result.is_some());
134        assert!(result.unwrap().is_nan());
135    }
136
137    #[rstest]
138    fn test_zero_downside_deviation() {
139        let ratio = SortinoRatio::new(None);
140        let returns = create_returns(vec![0.02, 0.03, 0.01]);
141        let result = ratio.calculate_from_returns(&returns);
142        assert!(result.is_some());
143        assert!(result.unwrap().is_nan());
144    }
145
146    #[rstest]
147    fn test_valid_sortino_ratio() {
148        let ratio = SortinoRatio::new(Some(252));
149        let returns = create_returns(vec![-0.01, 0.02, -0.015, 0.005, -0.02]);
150        let result = ratio.calculate_from_returns(&returns);
151        assert!(result.is_some());
152        assert!(approx_eq!(
153            f64,
154            result.unwrap(),
155            -5.273224492824493,
156            epsilon = 1e-9
157        ));
158    }
159
160    #[rstest]
161    fn test_name() {
162        let ratio = SortinoRatio::new(None);
163        assert_eq!(ratio.name(), "Sortino Ratio (252 days)");
164    }
165}