Previous topic


Next topic



fynance.features.metrics.roll_z_score(X, w=None, kind='s', axis=0, dtype=None)

Compute vector of rolling/moving Z-score function.

X : np.ndarray[dtype, ndim=1 or 2]

Series of index, prices or returns.

w : int, optional

Size of the lagged window of the moving averages, must be positive. If w is None or w=0, then w=X.shape[axis]. Default is None.

kind : {‘e’, ‘s’, ‘w’}
  • If ‘e’ then use exponential moving average, see ema for details.
  • If ‘s’ (default) then use simple moving average, see sma for details.
  • If ‘w’ then use weighted moving average, see wma for details.
axis : {0, 1}, optional

Axis along wich the computation is done. Default is 0.

dtype : np.dtype, optional

The type of the output array. If dtype is not given, infer the data type from X input.

np.ndarray[dtype, ndim=1 or 2]

Vector of Z-score at each period.


Compute for each observation the z-score function for a specific moving average function such that \(\forall t \in [1:T]\):

\[z_t = \frac{X_t - \mu_t}{\sigma_t}\]

Where \(\mu_t\) is the moving average and \(\sigma_t\) is the moving standard deviation.


>>> X = np.array([70, 100, 80, 120, 160, 80]).astype(np.float64)
>>> roll_z_score(X, w=3, kind='e')
array([ 0.        ,  1.41421356, -0.32444284,  1.30806216,  1.27096675,
>>> roll_z_score(X, w=3)
array([ 0.        ,  1.        , -0.26726124,  1.22474487,  1.22474487,