pub struct ValueAtRisk { /* private fields */ }Expand description
Rolling historical Value-at-Risk.
Input is treated as a period return. Over the trailing window of period
returns the indicator reports the empirical lower-tail quantile at the
given confidence level (e.g. 0.95 = the 95 %-confident worst-case
loss). The output is the magnitude of that loss, sign-flipped to be a
non-negative number (so a 5 % VaR is reported as 0.05, not -0.05):
q = (1 − confidence)
VaR_t = − percentile(returns over window, q · 100) if it is negative
VaR_t = 0 otherwisepercentile uses linear interpolation between the two closest order
statistics (“type 7” in R / NumPy default). If the q-quantile of the
window is itself non-negative (a window where every return was at or above
zero) the indicator returns 0.0 — there is no loss to report.
Each update is O(period · log period) due to the window-sort. Good
enough for the typical period ≤ 252 rolling-VaR workflow.
§Example
use wickra_core::{Indicator, ValueAtRisk};
let mut var = ValueAtRisk::new(100, 0.95).unwrap();
let mut last = None;
for i in 0..120 {
last = var.update((f64::from(i) * 0.1).sin() * 0.02);
}
assert!(last.is_some());Implementations§
Source§impl ValueAtRisk
impl ValueAtRisk
Sourcepub fn new(period: usize, confidence: f64) -> Result<Self>
pub fn new(period: usize, confidence: f64) -> Result<Self>
Construct a new rolling historical VaR.
§Errors
Returns Error::InvalidPeriod if period < 2, or if
confidence is outside the open interval (0, 1).
Sourcepub const fn confidence(&self) -> f64
pub const fn confidence(&self) -> f64
Configured confidence level.
Trait Implementations§
Source§impl Clone for ValueAtRisk
impl Clone for ValueAtRisk
Source§fn clone(&self) -> ValueAtRisk
fn clone(&self) -> ValueAtRisk
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ValueAtRisk
impl Debug for ValueAtRisk
Source§impl Indicator for ValueAtRisk
impl Indicator for ValueAtRisk
Source§fn update(&mut self, input: f64) -> Option<f64>
fn update(&mut self, input: f64) -> Option<f64>
None if the indicator is still warming up.Source§fn reset(&mut self)
fn reset(&mut self)
Source§fn warmup_period(&self) -> usize
fn warmup_period(&self) -> usize
None output can be produced.Auto Trait Implementations§
impl Freeze for ValueAtRisk
impl RefUnwindSafe for ValueAtRisk
impl Send for ValueAtRisk
impl Sync for ValueAtRisk
impl Unpin for ValueAtRisk
impl UnsafeUnpin for ValueAtRisk
impl UnwindSafe for ValueAtRisk
Blanket Implementations§
Source§impl<T> BatchExt for Twhere
T: Indicator,
impl<T> BatchExt for Twhere
T: Indicator,
Source§fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
None during warmup) per input.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more