Spearman Rank Correlation

Spearman Rank Correlation

All functions · nseries package

SpearmanRankCorrelation

func (s Series) SpearmanRankCorrelation(n int, s2 ...Series) Series

SpearmanRankCorrelation calculates the Spearman Rank Coefficient (correlation) over a rolling window of n bars. If a second series is passed in as a parameter, the receiver is ranked against it; only the first such series is used and any further ones are ignored. If no second series is provided, each window of the receiver is ranked against a sorted (low to high) copy of itself, which provides an indication of the strength of a trend (since the series is being ranked against itself in ascending order). A second series is right-aligned with the receiver at the most recent bar: with m = min(len(s), len(s2[0])), the last m bars of each are paired, the correlation of each pair of windows is written at the position of the window’s latest receiver bar, and values of either series before the overlap are never read. The result always has the receiver’s length. It is zero before the first complete window, and entirely zero when n < 2 or n exceeds the bars available (m with a second series, including an empty one, or len(s) without). A window in which any value of either series is NaN, +Inf or -Inf gives NaN at that position. Tied values receive the average of their ranks, and a window whose ranks are all tied gives 0. The rank scratch is allocated once per call, so the number of allocations does not grow with len(s) or n. Series are paired by position only; see “Time alignment” in the manual.