Spearman Correlation
lookback = 30 | smoothing_length = 3 Overview
Spearman Correlation compares two synchronized input series by ranking their rolling returns and then measuring how closely those ranks move together. The indicator emits both the raw rank-correlation series and a smoothed follow-up series built from a short moving average over the raw values.
In practice this is useful when you want a monotonic-correlation view that is less sensitive to outliers than a pure Pearson calculation. The stream path maintains rolling return windows for both series and begins emitting finite values once both the lookback and smoothing windows are satisfied.
Defaults: `lookback = 30` and `smoothing_length = 3`.
Implementation Examples
Run the indicator on two slices or on two candle sources.
use vector_ta::indicators::spearman_correlation::{
spearman_correlation, SpearmanCorrelationInput, SpearmanCorrelationParams,
};
let out = spearman_correlation(&SpearmanCorrelationInput::from_slices(
&main,
&compare,
SpearmanCorrelationParams { lookback: Some(30), smoothing_length: Some(3) },
))?;
println!("{:?}", out.raw.last());
println!("{:?}", out.smoothed.last());API Reference
Input Methods▼
SpearmanCorrelationInput::from_candles(&Candles, "close", "hl2", SpearmanCorrelationParams)
-> SpearmanCorrelationInput
SpearmanCorrelationInput::from_slices(&[f64], &[f64], SpearmanCorrelationParams)
-> SpearmanCorrelationInput
SpearmanCorrelationInput::with_default_candles(&Candles)
-> SpearmanCorrelationInputParameters Structure▼
pub struct SpearmanCorrelationParams {
pub lookback: Option<usize>, // default 30
pub smoothing_length: Option<usize>, // default 3
}Output Structure▼
pub struct SpearmanCorrelationOutput {
pub raw: Vec<f64>,
pub smoothed: Vec<f64>,
}Validation, Warmup & NaNs▼
- The main and comparison series must be non-empty, finite enough to produce rolling returns, and equal in length.
lookbackandsmoothing_lengthmust both be greater than0.- The direct path requires at least
lookback + 1valid paired samples after the first finite values because it works on returns. - The stream path returns
NaNvalues until both the return window and smoothing window are fully populated. - Batch mode validates both sweep ranges and rejects non-batch kernels.
Builder, Streaming & Batch APIs▼
SpearmanCorrelationBuilder::new()
.source("close")
.comparison_source("close")
.lookback(usize)
.smoothing_length(usize)
.kernel(Kernel)
.apply(&Candles)
.apply_slices(&[f64], &[f64])
.into_stream()
SpearmanCorrelationStream::try_new(params)
stream.update(main, compare) -> (f64, f64)
SpearmanCorrelationBatchBuilder::new()
.source("close")
.comparison_source("close")
.lookback_range((start, end, step))
.smoothing_length_range((start, end, step))
.kernel(Kernel)
.apply(&Candles)
.apply_slices(&[f64], &[f64])Python Bindings
Python exposes a scalar function returning a dictionary with raw and smoothed arrays, a stream class whose update returns a two-field dictionary, and a batch helper that returns reshaped raw and smoothed matrices plus the expanded sweep axes.
from vector_ta import spearman_correlation, spearman_correlation_batch, SpearmanCorrelationStream
out = spearman_correlation(main, compare, lookback=30, smoothing_length=3)
stream = SpearmanCorrelationStream()
point = stream.update(main[-1], compare[-1])
batch = spearman_correlation_batch(
main,
compare,
lookback_range=(20, 40, 10),
smoothing_length_range=(3, 7, 2),
)JavaScript/WASM Bindings
The WASM layer exposes scalar and batch helpers plus low-level allocation and into-buffer APIs for the raw and smoothed outputs.
import init, {
spearman_correlation_js,
spearman_correlation_batch_js,
spearman_correlation_alloc,
spearman_correlation_free,
spearman_correlation_into,
spearman_correlation_batch_into,
} from "vector-ta-wasm";
await init();
const out = spearman_correlation_js(main, compare, 30, 3);
const batch = spearman_correlation_batch_js(main, compare, {
lookback_range: [20, 40, 10],
smoothing_length_range: [3, 7, 2],
});CUDA Bindings (Rust)
Additional details for the CUDA bindings can be found inside the VectorTA repository.
Performance Analysis
Across sizes, Rust CPU runs about 1.14× faster than Tulip C in this benchmark.
AMD Ryzen 9 9950X (CPU) | NVIDIA RTX 4090 (GPU)