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