Skip to main content

CorrelationMatrix

Struct CorrelationMatrix 

Source
pub struct CorrelationMatrix { /* private fields */ }
Expand description

A streaming Pearson correlation matrix for a fixed set of indicators.

Feed one sample per bar via CorrelationMatrix::update. Once window samples have been accumulated the full n × n correlation matrix is available.

§Example

use fin_primitives::correlation::CorrelationMatrix;

let mut cm = CorrelationMatrix::new(3, 5, 0.95).unwrap();
for i in 0..5 {
    let vals = vec![i as f64, (i * 2) as f64, (10 - i) as f64];
    cm.update(&vals).unwrap();
}
// indicators 0 and 1 are perfectly correlated
let r = cm.get(0, 1).unwrap();
assert!((r - 1.0).abs() < 1e-9);

Implementations§

Source§

impl CorrelationMatrix

Source

pub fn new( n_indicators: usize, window: usize, redundancy_threshold: f64, ) -> Result<Self, FinError>

Constructs a new CorrelationMatrix.

§Parameters
  • n_indicators: number of indicators (must be >= 2)
  • window: rolling window in bars (must be >= 2)
  • redundancy_threshold: absolute correlation above which a pair is flagged
§Errors

Returns FinError::InvalidPeriod if window < 2. Returns FinError::InvalidInput if n_indicators < 2 or threshold is not in (0, 1].

Source

pub fn with_defaults( n_indicators: usize, window: usize, ) -> Result<Self, FinError>

Constructs a CorrelationMatrix with the default redundancy threshold (0.95).

§Errors

See CorrelationMatrix::new.

Source

pub fn update(&mut self, values: &[f64]) -> Result<(), FinError>

Feeds one bar’s worth of indicator values.

values.len() must equal the n_indicators supplied at construction.

§Errors

Returns FinError::InvalidInput if values.len() != n_indicators.

Source

pub fn is_ready(&self) -> bool

Returns true when enough samples have been accumulated to compute correlations.

Source

pub fn get(&self, i: usize, j: usize) -> Option<f64>

Returns the Pearson correlation between indicators i and j.

Returns None when fewer than window samples have been seen, or when either indicator has zero variance (correlation is undefined).

Source

pub fn matrix(&self) -> Option<Vec<f64>>

Returns the full n × n correlation matrix as a flat Vec<f64> (row-major).

Element at row i, column j is at index i * n + j. Returns None until ready.

Source

pub fn most_correlated_with(&self, indicator_id: usize) -> Vec<(usize, f64)>

Returns all indicators whose absolute correlation with indicator_id exceeds threshold, sorted descending by absolute correlation value.

Returns an empty Vec if the matrix is not yet ready.

Source

pub fn redundant_pairs(&self) -> Vec<(usize, usize, f64)>

Returns all pairs (i, j) where i < j and |r| >= threshold.

These pairs are considered redundant signals. Returns an empty Vec if the matrix is not ready.

Source

pub fn n_indicators(&self) -> usize

Returns the number of indicators tracked.

Source

pub fn window(&self) -> usize

Returns the configured window size.

Source

pub fn sample_count(&self) -> usize

Returns the number of samples currently buffered.

Trait Implementations§

Source§

impl Debug for CorrelationMatrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.