roll_annual_volatility¶
Defined in fynance.features.metrics
- roll_annual_volatility(X, period=252, log=True, w=None, axis=0, dtype=None, ddof=0)[source]
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:
- Xnp.ndarray[dtype, ndim=1 or 2]
Time-series of price, performance or index.
- periodint, optional
Number of period per year, default is 252 (trading days per year).
- logbool, optional
If True then logarithmic returns are computed.
Else then returns in percentage are computed.
- wint, optional
Size of the lagged window of the rolling function, must be positive. If
w is Noneorw=0, thenw=X.shape[axis]. Default is None.- axis{0, 1}, optional
Axis along wich the computation is done. Default is 0.
- dtypenp.dtype, optional
The type of the output array. If dtype is not given, infer the data type from X input.
- ddofint, optional
Means Delta Degrees of Freedom, the divisor used in calculations is
t - ddof, wheretrepresents 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
mdd,drawdown,sharpe,annual_return
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
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]])