Statistics
All functions · nseries package
RMS
func (s Series) RMS(n int) Series
RMS returns the root-mean-square of n values. Values of n less than two, or greater than the series length, return a zero-filled series of the same length.
The warm-up reads 0, not NaN: bars 0 to n-2 are 0.
Standardise
func (s Series) Standardise(n int) Series
Standardise returns the z-score of each value against the mean and population standard deviation of its own trailing n-bar window.
For i >= n-1 the result at i is (s[i] - mean)/sigma, where mean and sigma are the mean and the population standard deviation of the window s[i-n+1..i], and the result is 0 when sigma is 0. The computation is ZScore’s rolling-variance sequence with val = s[i] at each bar, so s.Standardise(n)[i] equals s.ZScore(n, s[i])[i] bit for bit, where ZScore scores one constant against every window. Standardise does that work in O(1) amortised time per bar rather than O(len(s)^2) in total.
The first valid index is n-1 and the minimum length is n; earlier outputs are zero. Any n < 2 or n > len(s) gives aligned zeros, a zero-filled series of the same length.
The valid-data class is window-local. A NaN or ±Inf at index p gives NaN for exactly the outputs whose windows contain it, indices max(p, n-1) to min(p+n-1, len(s)-1), and later windows recover. A flat window, in which every value is equal, gives exactly 0. The result is not bitwise start-invariant, because the rolling variance depends on the history before the window. Windows whose sum of squared deviations overflows (deviations above about 1e154/sqrt(n)) give NaN or a signed zero while the extreme values are in the window, and later windows recover.
For the n-1 convention of Ernest Chan’s MATLAB and R listings (his Python listing divides by n), multiply the result by math.Sqrt(float64(n-1)/float64(n)).
Uses include Chan’s pair rules on a spread or ratio (for example long at z <= -2, short at z >= 2 and exit at |z| <= 1) and Markos Katsanos’s Z = (C - MA)/SD. Later relative recipes build on it: the z-score of a log ratio, the %b of a relationship line, rolling co-movement, and the divergence of two z-scores.
Reverre’s ratio mispricing, under “Compositions” in the “Relative: Channels” category, uses it. The later recipes that use it will be listed under “Compositions” in the “Relative: Channels”, “Relative: Ratio and Relative Strength”, “Relative: Regression and Spread” and “Relative: Correlation” categories as they are added.
Verification: a Python reference (exact arithmetic) and an R reference (two-pass population variance) agree, and s.Standardise(n)[i] equals the existing s.ZScore(n, s[i])[i] bit for bit for every i and every n >= 2.
StdDev
func (s Series) StdDev(n int) Series
StdDev returns the population standard deviation of each n values. Values of n less than two, or greater than the series length, return a zero-filled series of the same length.
The warm-up reads 0, not NaN: bars 0 to n-2 are 0, which reads as no volatility.
ZScore
func (s Series) ZScore(avgLen int, val float64) Series
ZScore returns the z-score of the constant val against the mean and population standard deviation of each trailing avgLen-bar window.
The z-score is a statistical measure of how many standard deviations a value is from the mean. Windows with zero standard deviation, and avgLen values outside the bounds of the series, yield zero. For the z-score of each value against its own window, use Standardise.
The output is in standard deviations. The warm-up reads 0, not NaN: bars 0 to avgLen-2 are 0, which reads as at the mean.