nautilus_analysis/python/statistics/treynor_ratio.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 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::collections::BTreeMap;
17
18use pyo3::prelude::*;
19
20use super::transform_returns;
21use crate::{statistic::PortfolioStatistic, statistics::treynor_ratio::TreynorRatio};
22
23#[pymethods]
24#[pyo3_stub_gen::derive::gen_stub_pymethods]
25impl TreynorRatio {
26 /// Calculates the Treynor ratio of portfolio returns relative to a benchmark.
27 ///
28 /// The Treynor ratio measures excess return per unit of systematic risk (beta):
29 ///
30 /// `Treynor = (annualized_return - rf_annual) / beta`
31 ///
32 /// The portfolio's annualized return is computed geometrically (CAGR-style) from the
33 /// aligned returns: `annualized_return = (prod(1 + r_i))^(period / n) - 1`. The
34 /// per-period risk-free rate is annualized geometrically as
35 /// `rf_annual = (1 + rf)^period - 1`. Beta is the sample (`ddof = 1`) beta of the
36 /// portfolio against the benchmark. The period defaults to 252 trading days and `rf`
37 /// defaults to 0.0.
38 ///
39 /// # References
40 ///
41 /// - Treynor, J. L. (1965). "How to Rate Management of Investment Funds".
42 /// *Harvard Business Review*, 43(1), 63-75.
43 /// - CFA Institute Investment Foundations, 3rd Edition
44 #[new]
45 #[pyo3(signature = (period=None, risk_free_rate=None))]
46 fn py_new(period: Option<usize>, risk_free_rate: Option<f64>) -> Self {
47 Self::new(period, risk_free_rate)
48 }
49
50 fn __repr__(&self) -> String {
51 self.to_string()
52 }
53
54 #[getter]
55 #[pyo3(name = "name")]
56 fn py_name(&self) -> String {
57 self.name()
58 }
59
60 #[pyo3(name = "calculate_from_returns")]
61 fn py_calculate_from_returns(&self, _returns: BTreeMap<u64, f64>) -> Option<f64> {
62 None
63 }
64
65 #[pyo3(name = "calculate_from_realized_pnls")]
66 fn py_calculate_from_realized_pnls(&self, _realized_pnls: Vec<f64>) -> Option<f64> {
67 None
68 }
69
70 #[pyo3(name = "calculate_from_positions")]
71 fn py_calculate_from_positions(&self, _positions: Vec<Py<PyAny>>) -> Option<f64> {
72 None
73 }
74
75 #[pyo3(name = "calculate_from_returns_with_benchmark")]
76 #[expect(clippy::needless_pass_by_value)]
77 fn py_calculate_from_returns_with_benchmark(
78 &self,
79 returns: BTreeMap<u64, f64>,
80 benchmark: BTreeMap<u64, f64>,
81 ) -> Option<f64> {
82 self.calculate_from_returns_with_benchmark(
83 &transform_returns(&returns),
84 &transform_returns(&benchmark),
85 )
86 }
87}