nautilus_analysis/python/statistics/expected_shortfall.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_core::python::to_pyvalue_err;
19use nautilus_model::position::Position;
20use pyo3::prelude::*;
21
22use super::transform_returns;
23use crate::{statistic::PortfolioStatistic, statistics::expected_shortfall::ExpectedShortfall};
24
25#[pymethods]
26#[pyo3_stub_gen::derive::gen_stub_pymethods]
27impl ExpectedShortfall {
28 /// Calculates the historical Expected Shortfall (Conditional Value at Risk) of
29 /// portfolio returns.
30 ///
31 /// Expected Shortfall is the average of the losses that occur beyond the
32 /// `ValueAtRisk` threshold at a
33 /// given confidence level - the mean of the worst
34 /// `1 - confidence` tail of the return distribution. It is a coherent risk
35 /// measure and captures tail severity that `VaR` alone does not.
36 ///
37 /// `ES(c) = mean( r | r <= VaR(c) )`
38 ///
39 /// `confidence` defaults to `0.95`. The result is expressed as a return (e.g.
40 /// `-0.05` is a 5% expected tail loss); it is always less than or equal to the
41 /// corresponding `VaR`. Returns `NaN` for an empty series.
42 ///
43 /// # References
44 ///
45 /// - Acerbi, C., & Tasche, D. (2002). "Expected Shortfall: A Natural Coherent Alternative
46 /// to Value at Risk". *Economic Notes*, 31(2), 379-388.
47 /// - Rockafellar, R. T., & Uryasev, S. (2000). "Optimization of Conditional Value-at-Risk".
48 /// *Journal of Risk*, 2(3), 21-41.
49 #[new]
50 #[pyo3(signature = (confidence=None))]
51 fn py_new(confidence: Option<f64>) -> PyResult<Self> {
52 Self::new_checked(confidence).map_err(to_pyvalue_err)
53 }
54
55 fn __repr__(&self) -> String {
56 self.to_string()
57 }
58
59 #[getter]
60 #[pyo3(name = "name")]
61 fn py_name(&self) -> String {
62 self.name()
63 }
64
65 #[pyo3(name = "calculate_from_returns")]
66 #[expect(clippy::needless_pass_by_value)]
67 fn py_calculate_from_returns(&mut self, raw_returns: BTreeMap<u64, f64>) -> Option<f64> {
68 self.calculate_from_returns(&transform_returns(&raw_returns))
69 }
70
71 #[pyo3(name = "calculate_from_realized_pnls")]
72 fn py_calculate_from_realized_pnls(&mut self, _realized_pnls: Vec<f64>) -> Option<f64> {
73 None
74 }
75
76 #[pyo3(name = "calculate_from_positions")]
77 fn py_calculate_from_positions(&mut self, _positions: Vec<Position>) -> Option<f64> {
78 None
79 }
80}