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
impl CorrelationMatrix
Sourcepub fn new(
n_indicators: usize,
window: usize,
redundancy_threshold: f64,
) -> Result<Self, FinError>
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].
Sourcepub fn with_defaults(
n_indicators: usize,
window: usize,
) -> Result<Self, FinError>
pub fn with_defaults( n_indicators: usize, window: usize, ) -> Result<Self, FinError>
Constructs a CorrelationMatrix with the default redundancy threshold (0.95).
§Errors
Sourcepub fn update(&mut self, values: &[f64]) -> Result<(), FinError>
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.
Sourcepub fn is_ready(&self) -> bool
pub fn is_ready(&self) -> bool
Returns true when enough samples have been accumulated to compute correlations.
Sourcepub fn get(&self, i: usize, j: usize) -> Option<f64>
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).
Sourcepub fn matrix(&self) -> Option<Vec<f64>>
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.
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.
Sourcepub fn redundant_pairs(&self) -> Vec<(usize, usize, f64)>
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.
Sourcepub fn n_indicators(&self) -> usize
pub fn n_indicators(&self) -> usize
Returns the number of indicators tracked.
Sourcepub fn sample_count(&self) -> usize
pub fn sample_count(&self) -> usize
Returns the number of samples currently buffered.