Skip to main content

wickra_core/indicators/
tail_ratio.rs

1//! Tail Ratio — the right tail (95th percentile) over the absolute left tail (5th percentile).
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::traits::Indicator;
7
8/// Tail Ratio over a trailing window of `period` returns.
9///
10/// ```text
11/// TailRatio = P95(returns) / |P5(returns)|
12/// ```
13///
14/// The Tail Ratio contrasts the magnitude of the best outcomes against the worst:
15/// the 95th percentile of the return distribution divided by the absolute value of
16/// the 5th percentile. A value above `1.0` means the right tail (upside surprises)
17/// is fatter than the left tail (downside surprises); below `1.0` means crashes are
18/// larger than rallies. It is a distribution-shape statistic, distinct from the
19/// average-based [`SharpeRatio`](crate::SharpeRatio): two series with the same mean
20/// and variance can have very different tail ratios.
21///
22/// Percentiles are computed by linear interpolation over the sorted window
23/// (the same rule `NumPy` uses by default). A window whose 5th percentile is exactly
24/// zero has no measurable left tail and the indicator reports `0.0` rather than
25/// dividing by zero.
26///
27/// The first value lands after `period` returns; each `update` re-sorts the window
28/// (O(period log period)), which is O(1) in the length of the overall series.
29///
30/// # Example
31///
32/// ```
33/// use wickra_core::{Indicator, TailRatio};
34///
35/// let mut indicator = TailRatio::new(20).unwrap();
36/// let mut last = None;
37/// for i in 0..40 {
38///     last = indicator.update((f64::from(i) * 0.3).sin() * 0.02);
39/// }
40/// assert!(last.is_some());
41/// ```
42#[derive(Debug, Clone)]
43pub struct TailRatio {
44    period: usize,
45    window: VecDeque<f64>,
46}
47
48impl TailRatio {
49    /// Construct a Tail Ratio over `period` returns.
50    ///
51    /// # Errors
52    ///
53    /// Returns [`Error::InvalidPeriod`] if `period < 2` (percentiles need at least
54    /// two observations to interpolate).
55    pub fn new(period: usize) -> Result<Self> {
56        if period < 2 {
57            return Err(Error::InvalidPeriod {
58                message: "tail ratio needs period >= 2",
59            });
60        }
61        Ok(Self {
62            period,
63            window: VecDeque::with_capacity(period),
64        })
65    }
66
67    /// Configured window of returns.
68    pub const fn period(&self) -> usize {
69        self.period
70    }
71
72    fn compute(&self) -> f64 {
73        let mut sorted: Vec<f64> = self.window.iter().copied().collect();
74        sorted.sort_unstable_by(f64::total_cmp);
75        let upper = percentile(&sorted, 95.0);
76        let lower = percentile(&sorted, 5.0).abs();
77        if lower > 0.0 {
78            upper / lower
79        } else {
80            0.0
81        }
82    }
83}
84
85/// Linear-interpolation percentile of an ascending, non-empty slice.
86fn percentile(sorted: &[f64], pct: f64) -> f64 {
87    let last_index = sorted.len() - 1;
88    #[allow(clippy::cast_precision_loss)]
89    let rank = pct / 100.0 * last_index as f64;
90    let floor = rank.floor();
91    // `rank` lies in `[0, last_index]`, so its floor is a valid in-bounds index.
92    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
93    let lower = floor as usize;
94    if lower >= last_index {
95        return sorted[last_index];
96    }
97    let frac = rank - floor;
98    sorted[lower] + frac * (sorted[lower + 1] - sorted[lower])
99}
100
101impl Indicator for TailRatio {
102    type Input = f64;
103    type Output = f64;
104
105    fn update(&mut self, ret: f64) -> Option<f64> {
106        if !ret.is_finite() {
107            return None;
108        }
109        if self.window.len() == self.period {
110            self.window.pop_front();
111        }
112        self.window.push_back(ret);
113        if self.window.len() < self.period {
114            return None;
115        }
116        Some(self.compute())
117    }
118
119    fn reset(&mut self) {
120        self.window.clear();
121    }
122
123    fn warmup_period(&self) -> usize {
124        self.period
125    }
126
127    fn is_ready(&self) -> bool {
128        self.window.len() == self.period
129    }
130
131    fn name(&self) -> &'static str {
132        "TailRatio"
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use crate::traits::BatchExt;
140    use approx::assert_relative_eq;
141
142    #[test]
143    fn rejects_period_less_than_two() {
144        assert!(matches!(
145            TailRatio::new(1),
146            Err(Error::InvalidPeriod { .. })
147        ));
148        assert!(matches!(
149            TailRatio::new(0),
150            Err(Error::InvalidPeriod { .. })
151        ));
152    }
153
154    #[test]
155    fn accessors_and_metadata() {
156        let tr = TailRatio::new(20).unwrap();
157        assert_eq!(tr.period(), 20);
158        assert_eq!(tr.warmup_period(), 20);
159        assert_eq!(tr.name(), "TailRatio");
160        assert!(!tr.is_ready());
161    }
162
163    #[test]
164    fn reference_value() {
165        // sorted window [-0.04, -0.02, 0.0, 0.02, 0.04], last_index = 4.
166        // P95: rank 3.8 -> 0.02 + 0.8*(0.04-0.02) = 0.036.
167        // P5:  rank 0.2 -> -0.04 + 0.2*(0.02)     = -0.036, abs 0.036.
168        // ratio = 0.036 / 0.036 = 1.0.
169        let mut tr = TailRatio::new(5).unwrap();
170        let out = tr.batch(&[-0.04, -0.02, 0.0, 0.02, 0.04]);
171        assert_relative_eq!(out[4].unwrap(), 1.0, epsilon = 1e-9);
172    }
173
174    #[test]
175    fn fatter_right_tail_exceeds_one() {
176        let mut tr = TailRatio::new(5).unwrap();
177        let out = tr.batch(&[-0.01, 0.0, 0.01, 0.02, 0.10]);
178        assert!(out[4].unwrap() > 1.0);
179    }
180
181    #[test]
182    fn flat_window_is_zero() {
183        let mut tr = TailRatio::new(4).unwrap();
184        let last = tr.batch(&[0.0; 4]).into_iter().flatten().last().unwrap();
185        assert_relative_eq!(last, 0.0, epsilon = 1e-12);
186    }
187
188    #[test]
189    fn ignores_non_finite_input() {
190        let mut tr = TailRatio::new(3).unwrap();
191        assert_eq!(tr.update(0.01), None);
192        assert_eq!(tr.update(f64::NAN), None);
193        assert_eq!(tr.update(0.02), None);
194        assert!(tr.update(0.03).is_some());
195    }
196
197    #[test]
198    fn reset_clears_state() {
199        let mut tr = TailRatio::new(3).unwrap();
200        tr.batch(&[-0.01, 0.0, 0.02]);
201        assert!(tr.is_ready());
202        tr.reset();
203        assert!(!tr.is_ready());
204        assert_eq!(tr.update(0.01), None);
205    }
206
207    #[test]
208    fn batch_equals_streaming() {
209        let rets: Vec<f64> = (0..60)
210            .map(|i| (f64::from(i) * 0.25).sin() * 0.02)
211            .collect();
212        let batch = TailRatio::new(15).unwrap().batch(&rets);
213        let mut streamer = TailRatio::new(15).unwrap();
214        let streamed: Vec<_> = rets.iter().map(|r| streamer.update(*r)).collect();
215        assert_eq!(batch, streamed);
216    }
217
218    #[test]
219    fn percentile_at_top_returns_last() {
220        // When the rank floor reaches the final index (the 100th percentile), the
221        // helper returns the largest element without interpolating past the end.
222        assert_relative_eq!(percentile(&[1.0, 2.0, 3.0], 100.0), 3.0, epsilon = 1e-12);
223    }
224}