Skip to main content

nautilus_analysis/python/statistics/
returns_kurtosis.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::returns_kurtosis::ReturnsKurtosis};
23
24#[pymethods]
25#[pyo3_stub_gen::derive::gen_stub_pymethods]
26impl ReturnsKurtosis {
27    /// Calculates the excess kurtosis of portfolio returns.
28    ///
29    /// Kurtosis measures the heaviness of the tails of the return distribution
30    /// relative to a normal distribution. A positive value indicates fatter tails
31    /// (more outliers); a negative value indicates thinner tails.
32    ///
33    /// Uses the bias-corrected sample excess kurtosis (adjusted Fisher-Pearson),
34    /// matching `pandas.Series.kurt` and Excel `KURT`. A normal distribution yields 0:
35    ///
36    /// `G2 = n(n + 1) / ((n - 1)(n - 2)(n - 3)) * sum(((x - mean) / s)^4)
37    ///       - 3(n - 1)^2 / ((n - 2)(n - 3))`
38    ///
39    /// where `s` is the sample standard deviation (Bessel's correction, ddof=1).
40    /// Returns `NaN` for fewer than four returns or zero dispersion.
41    ///
42    /// # References
43    ///
44    /// - Joanes, D. N., & Gill, C. A. (1998). Comparing measures of sample skewness
45    ///   and kurtosis. *Journal of the Royal Statistical Society: Series D*, 47(1), 183-189.
46    #[new]
47    fn py_new() -> Self {
48        Self::new()
49    }
50
51    fn __repr__(&self) -> String {
52        self.name()
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    #[expect(clippy::needless_pass_by_value)]
63    fn py_calculate_from_returns(&mut self, raw_returns: BTreeMap<u64, f64>) -> Option<f64> {
64        self.calculate_from_returns(&transform_returns(&raw_returns))
65    }
66
67    #[pyo3(name = "calculate_from_realized_pnls")]
68    fn py_calculate_from_realized_pnls(&mut self, _realized_pnls: Vec<f64>) -> Option<f64> {
69        None
70    }
71
72    #[pyo3(name = "calculate_from_positions")]
73    fn py_calculate_from_positions(&mut self, _positions: Vec<Position>) -> Option<f64> {
74        None
75    }
76}