macrosynergy.panel.historic_vol#

Function for calculating historic volatility of quantamental data.

expo_weights(lback_periods=21, half_life=11)[source]#

Calculates exponential series weights for finite horizon, normalized to 1.

Parameters:
  • lback_periods – Number of lookback periods over which volatility is calculated. Default is 21.

  • half_life (int) – Refers to the half-time for “xma” and full lookback period for “ma”. Default is 11.

Return <np.ndarray>:

An Array of weights determined by the length of the lookback period.

Note: 50% of the weight allocation will be applied to the number of days delimited by

the half_life.

expo_std(x, w, remove_zeros=True)[source]#

Estimate standard deviation of returns based on exponentially weighted absolute values.

Parameters:
  • x (ndarray) – array of returns

  • w (ndarray) – array of exponential weights (same length as x); will be normalized to 1.

  • remove_zeros (bool) – removes zeroes as invalid entries and shortens the effective window.

Return <float>:

exponentially weighted mean absolute value (as proxy of return standard deviation).

flat_std(x, remove_zeros=True)[source]#

Estimate standard deviation of returns based on exponentially weighted absolute values.

Parameters:
  • x (ndarray) – array of returns

  • remove_zeros (bool) – removes zeroes as invalid entries and shortens the effective window.

Return <float>:

flat weighted mean absolute value (as proxy of return standard deviation).

historic_vol(df, xcat=None, cids=None, lback_periods=21, lback_meth='ma', half_life=11, start=None, end=None, est_freq='D', blacklist=None, remove_zeros=True, postfix='ASD', nan_tolerance=0.25)[source]#

Estimate historic annualized standard deviations of asset returns. User Function. Controls the functionality.

Parameters:
  • df (DataFrame) – standardized DataFrame with the following necessary columns: ‘cid’, ‘xcat’, ‘real_date’ and ‘value’. Will contain all of the data across all macroeconomic fields.

  • xcat (str) – extended category denoting the return series for which volatility should be calculated. Note: in JPMaQS returns are represented in %, i.e. 5 means 5%.

  • cids (List[str]) – cross sections for which volatility is calculated; default is all available for the category.

  • lback_periods – Number of lookback periods over which volatility is calculated. Default is 21.

  • lback_meth (str) – Lookback method to calculate the volatility, Default is “ma”. Alternative is “xma”, Exponential Moving Average. Expects to receive either the aforementioned strings.

  • half_life – Refers to the half-time for “xma”. Default is 11.

  • start (str) – earliest date in ISO format. Default is None and earliest date in df is used.

  • end (str) – latest date in ISO format. Default is None and latest date in df is used.

  • est_freq (str) – Frequency of (re-)estimation of volatility. Options are ‘D’ for end of each day (default), ‘W’ for end of each work week, ‘M’ for end of each month, and ‘Q’ for end of each week.

  • blacklist (dict) – cross sections with date ranges that should be excluded from the data frame. If one cross section has several blacklist periods append numbers to the cross section code.

  • half_life – Refers to the half-time for “xma” and full lookback period for “ma”.

  • remove_zeros (bool) – if True (default) any returns that are exact zeros will not be included in the lookback window and prior non-zero values are added to the window instead.

  • postfix – string appended to category name for output; default is “ASD”.

  • nan_tolerance (float) – maximum ratio of NaNs to non-NaNs in a lookback window, if exceeded the resulting volatility is set to NaN. Default is 0.25.

Return <pd.DataFrame>:

standardized DataFrame with the estimated annualized standard deviations of the chosen xcat. If the input ‘value’ is in % (as is the standard in JPMaQS) then the output will also be in %. ‘cid’, ‘xcat’, ‘real_date’ and ‘value’.