macrosynergy.pnl.historic_portfolio_volatility#
Estimation of Historic Portfolio Volatility.
- flat_weights_arr(lback_periods, *args, **kwargs)[source]#
Flat weights for the look-back period.
- Return type:
- expo_weights_arr(lback_periods, half_life, *args, **kwargs)[source]#
Exponential weights for the lookback period.
- Return type:
- estimate_variance_covariance(piv_ret, remove_zeros, weights_func, lback_periods, half_life, lback_min_obs=1)[source]#
Estimation of the variance-covariance matrix needs to have the following configuration options
Absolutely vs squared deviations,
Flat weights (equal) vs. exponential weights,
Frequency of estimation (daily, weekly, monthly, quarterly) and their weights.
- Return type:
DataFrame
- get_max_lookback(lb, nt)[source]#
Calculate the maximum lookback period for a given lookback period and nan tolerance.
- stack_covariances(vcv_df, real_date)[source]#
Stack the covariance matrix DataFrame.
- Return type:
DataFrame
- historic_portfolio_vol(df, sname, fids, rstring='XR', rebal_freq='m', lback_meth='ma', est_freqs=['D', 'W', 'M'], est_weights=[1, 1, 1], lback_periods=[-1, -1, -1], half_life=[11, 5, 6], lback_min_obs=1, start=None, end=None, blacklist=None, nan_tolerance=0.25, remove_zeros=True, return_variance_covariance=True)[source]#
Historical portfolio volatility. Estimates annualized standard deviations of a portfolio, based on historic variances and co-variances.
- Parameters:
df (QuantamentalDataFrame) – JPMaQS standard DataFrame containing contract-specific signals and return series.
sname (str) – the name of the strategy. It must correspond to contract signals in the dataframe, which have the format “<cid>_<ctype>_CSIG_<sname>”, and which are typically calculated by the function contract_signals().
fids (List[str]) – list of financial contract identifiers in the format “<cid>_<ctype>”. It must correspond to contract signals in the dataframe.
rstring (str) – a general string of the return category. This identifies the contract returns that are required for the volatility-targeting method, based on the category identifier format <cid>_<ctype><rstring> in accordance with JPMaQS conventions. Default is ‘XR’.
rebal_freq (str) – the frequency of rebalancing and volatility estimation. Default is ‘M’ for monthly. Alternatives are ‘W’ for business weekly, ‘D’ for daily, and ‘Q’ for quarterly. Estimations are conducted for the end of the period.
est_freqs (List[str]) – the list of frequencies for which the volatility is estimated. Volatility for a given period is the weighted sum of the volatilities estimated for each frequency. Default is [“D”, “W”, “M”].
est_weights (List[float]) – the list of weights for each frequency in est_freqs. Weights are normalized before applying. In cases where there may be missing data or NaNs in the result, the remaining weights are normalized. Default is None, which means that the weights are equal.
lback_meth (str) – the method to use for the lookback period of the volatility-targeting method. Default is “ma” for moving average. Alternative is “xma”, for exponential moving average.
lback_periods (List[int]) – the number of periods to use for the lookback period of the volatility-targeting method. Each element corresponds to the the same index in est_freqs. Passing a single element will apply the same value to all frequencies. Default is [-1], which means that the lookback period is the full available data for all specified frequencies.
half_life (List[int]) – number of periods in the half-life of the exponential moving average. Each element corresponds to the same index in est_freqs.
start (str) – the start date of the data. Default is None, which means that the start date is taken from the dataframe.
end (str) – the end date of the data. Default is None, which means that the end date is taken from the dataframe.
blacklist (dict) – a dictionary of contract identifiers to exclude from the calculation. Default is None, which means that no contracts are excluded.
nan_tolerance (float) – maximum ratio of number of NaN values to the total number of values in a lookback window. If exceeded the resulting volatility is set to NaN, else prior non- zero values are added to the window instead. Default is 0.25.
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.
- Returns:
JPMaQS dataframe of annualized standard deviation of estimated strategy PnL, with category name <sname>_PNL_USD1S_ASD. TODO: check if this is correct. The values are in % annualized. Values between estimation points are forward filled.
- Return type:
pd.DataFrame
Notes
If returns in the lookback window are not available the function will replace them with the average of the available returns of the same contract type. If no returns are available for a contract type the function will reduce the lookback window up to a minimum of 11 days. If no returns are available for a contract type for at least 11 days the function returns an NaN for that date and sends a warning of all the dates for which this happened.