fynance.features.metrics.roll_annual_volatility¶
-
fynance.features.metrics.
roll_annual_volatility
(X, period=252, log=True, w=None, axis=0, dtype=None, ddof=0)¶ Compute the annualized volatility of each X’ series.
In finance, volatility is the degree of variation of a trading price series over time as measured by the standard deviation of logarithmic returns [2].
Parameters: - X : np.ndarray[dtype, ndim=1 or 2]
Time-series of price, performance or index.
- period : int, optional
Number of period per year, default is 252 (trading days per year).
- log : bool, optional
- If True then logarithmic returns are computed.
- Else then returns in percentage are computed.
- w : int, optional
Size of the lagged window of the rolling function, must be positive. If
w is None
orw=0
, thenw=X.shape[axis]
. Default is None.- 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.
- ddof : int, optional
Means Delta Degrees of Freedom, the divisor used in calculations is
t - ddof
, wheret
represents the number of elements in time axis. Default is 0.
Returns: - dtype or np.ndarray([dtype, ndim=1 or 2])
Rolling annualized volatility for each series.
See also
Notes
The rolling annualized volatility of returns is computed such that \(\forall t \in [1, T]\):
\[\begin{split}annualVolatility_t = \sqrt{period \times Var(R_{1:t})} \\ \\\end{split}\]Where, \(R_1 = 0\) and \(R_{2:t} = \begin{cases}ln(\frac{X_{2:t}} {X_{1:t-1}}) \text{, if log=True}\\ \frac{X_{2:t}}{X_{1:t-1}} - 1 \text{, otherwise} \\ \end{cases}\).
References
[2] https://en.wikipedia.org/wiki/Volatility_(finance) Examples
Assume series of monthly prices:
>>> X = np.array([100, 110, 105, 110, 120, 108]).astype(np.float64) >>> roll_annual_volatility(X, period=12, log=False, ddof=1) array([0. , 0.24494897, 0.25777176, 0.21655755, 0.21313847, 0.27344193]) >>> roll_annual_volatility(X.reshape([6, 1]), period=12, log=False) array([[0. ], [0.17320508], [0.21046976], [0.18754434], [0.19063685], [0.24961719]])