nautilus_analysis/python/statistics/alpha.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 nautilus_model::position::Position;
19use pyo3::prelude::*;
20
21use super::transform_returns;
22use crate::{statistic::PortfolioStatistic, statistics::alpha::Alpha};
23
24#[pymethods]
25#[pyo3_stub_gen::derive::gen_stub_pymethods]
26impl Alpha {
27 /// Calculates Jensen's alpha of portfolio returns relative to a benchmark.
28 ///
29 /// Alpha measures the excess return of a portfolio over the return predicted by its
30 /// beta exposure to the benchmark (CAPM). The per-period alpha is:
31 ///
32 /// `alpha = (mean_portfolio - rf) - beta * (mean_benchmark - rf)`
33 ///
34 /// where `beta` is the sample (`ddof = 1`) beta of the portfolio against the benchmark.
35 /// The per-period alpha is then annualized geometrically over `period` (default 252):
36 ///
37 /// `alpha_annual = (1 + alpha)^period - 1`
38 ///
39 /// The risk-free rate `rf` is specified per period (default 0.0).
40 ///
41 /// # References
42 ///
43 /// - Jensen, M. C. (1968). "The Performance of Mutual Funds in the Period 1945-1964".
44 /// *Journal of Finance*, 23(2), 389-416.
45 /// - CFA Institute Investment Foundations, 3rd Edition
46 #[new]
47 #[pyo3(signature = (period=None, risk_free_rate=None))]
48 fn py_new(period: Option<usize>, risk_free_rate: Option<f64>) -> Self {
49 Self::new(period, risk_free_rate)
50 }
51
52 fn __repr__(&self) -> String {
53 self.to_string()
54 }
55
56 #[getter]
57 #[pyo3(name = "name")]
58 fn py_name(&self) -> String {
59 self.name()
60 }
61
62 #[pyo3(name = "calculate_from_returns")]
63 fn py_calculate_from_returns(&self, _returns: BTreeMap<u64, f64>) -> Option<f64> {
64 None
65 }
66
67 #[pyo3(name = "calculate_from_realized_pnls")]
68 fn py_calculate_from_realized_pnls(&self, _realized_pnls: Vec<f64>) -> Option<f64> {
69 None
70 }
71
72 #[pyo3(name = "calculate_from_positions")]
73 fn py_calculate_from_positions(&self, _positions: Vec<Position>) -> Option<f64> {
74 None
75 }
76
77 #[pyo3(name = "calculate_from_returns_with_benchmark")]
78 #[expect(clippy::needless_pass_by_value)]
79 fn py_calculate_from_returns_with_benchmark(
80 &self,
81 returns: BTreeMap<u64, f64>,
82 benchmark: BTreeMap<u64, f64>,
83 ) -> Option<f64> {
84 self.calculate_from_returns_with_benchmark(
85 &transform_returns(&returns),
86 &transform_returns(&benchmark),
87 )
88 }
89}