pub struct RSquared { /* private fields */ }Expand description
R² (coefficient of determination) of the rolling least-squares fit.
Over the trailing window indexed x = 0, 1, …, period − 1 the OLS line
y = a + b·x is fitted and the ratio of variance explained by the line
to total variance is reported:
slope = (n·Σxy − Σx·Σy) / (n·Σxx − (Σx)²)
SS_total = Σy² − n·ȳ²
SS_explained = slope² · ( denom / n )
R² = SS_explained / SS_total if SS_total > 0
= 1 otherwise (flat window)A reading of 1.0 means the window lies on a straight line — perfect
linear fit. 0.0 means the slope is irrelevant; the trend explains none
of the variance. Mid-range values quantify how trending the recent price
action is, independent of the slope’s sign or magnitude. Use it as a
trend-quality filter: a strategy that needs a clear trend can require
R² > 0.7, while a mean-reversion strategy can prefer R² < 0.3.
A flat window has SS_total = 0; the line is also flat and the fit is
trivially perfect, so the indicator returns 1.0 rather than dividing
by zero.
Each update is O(1) via the same rolling sums as
crate::LinearRegression, plus a running Σy². The output is
clamped to [0, 1] to absorb tiny floating-point cancellation.
§Example
use wickra_core::{Indicator, RSquared};
let mut indicator = RSquared::new(14).unwrap();
let mut last = None;
for i in 0..40 {
last = indicator.update(f64::from(i));
}
assert!(last.is_some());Implementations§
Trait Implementations§
Source§impl Indicator for RSquared
impl Indicator for RSquared
Source§fn update(&mut self, value: f64) -> Option<f64>
fn update(&mut self, value: 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 RSquared
impl RefUnwindSafe for RSquared
impl Send for RSquared
impl Sync for RSquared
impl Unpin for RSquared
impl UnsafeUnpin for RSquared
impl UnwindSafe for RSquared
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