pub struct ShannonEntropy { /* private fields */ }Expand description
Shannon Entropy — the Shannon information entropy (in bits) of the distribution of values in a rolling window, after binning them into a fixed number of equal-width buckets.
bucket each of the last `period` values into `bins` equal-width bins over
[min, max] of the window
p_i = count_i / period
H = − Σ p_i · log2(p_i) (over non-empty bins)Entropy measures how spread out and unpredictable the recent values are. A
window concentrated in one bin (a flat or tightly-ranging market) has low
entropy near 0; a window whose values are spread evenly across all bins (a
noisy, directionless market) approaches the maximum log2(bins). Traders use
it as a regime filter: low entropy favours trend/breakout strategies, high
entropy favours mean-reversion or standing aside.
The output lies in [0, log2(bins)]. A degenerate window where every value is
identical (max == min) returns 0. The first value lands after period
inputs; each update rebins the window in O(period).
§Example
use wickra_core::{Indicator, ShannonEntropy};
let mut indicator = ShannonEntropy::new(32, 8).unwrap();
let mut last = None;
for i in 0..64 {
last = indicator.update((f64::from(i) * 0.7).sin() * 10.0);
}
assert!(last.is_some());Implementations§
Source§impl ShannonEntropy
impl ShannonEntropy
Sourcepub fn new(period: usize, bins: usize) -> Result<Self>
pub fn new(period: usize, bins: usize) -> Result<Self>
Construct a Shannon entropy over period values binned into bins
buckets.
§Errors
Returns Error::PeriodZero if either argument is 0, or
Error::InvalidPeriod if bins < 2 (entropy needs at least two bins).
Trait Implementations§
Source§impl Clone for ShannonEntropy
impl Clone for ShannonEntropy
Source§fn clone(&self) -> ShannonEntropy
fn clone(&self) -> ShannonEntropy
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 ShannonEntropy
impl Debug for ShannonEntropy
Source§impl Indicator for ShannonEntropy
impl Indicator for ShannonEntropy
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 ShannonEntropy
impl RefUnwindSafe for ShannonEntropy
impl Send for ShannonEntropy
impl Sync for ShannonEntropy
impl Unpin for ShannonEntropy
impl UnsafeUnpin for ShannonEntropy
impl UnwindSafe for ShannonEntropy
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> BatchNanExt for T
impl<T> BatchNanExt for T
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