Sensitivity of Portfolio to Shocks

perfana.monte_carlo.sensitivity.sensitivity_m(data, weights, freq, shock=0.05, geometric=True, rebalance=True, cov=None, cvar_cutoff=3, cvar_data=None, alpha=0.95, invert=True, names=None, leveraged=False, distribute=True)[source]

Calculates the sensitivity of adding and removing from the asset class on the portfolio.

This is a wrapper function for the 3 sensitivity calculations. For more granular usages, use the base functions instead.

Notes

When given a positive shock and a “proportionate” distribution strategy, each asset class is given an additional amount by removing from the other asset classes proportionately. For example, given a portfolio with weights [0.1, 0.2, 0.3, 0.4], a shock of 5% to the first asset in the portfolio will result in weights [0.15, 0.19, 0.28, 0.38]. A negative shock works by removing from the asset class and adding to the other asset classes proportionately.

If the distribution strategy is set to False, the asset class’ weight is increased without removing from the other asset classes. Thus the sum of the portfolio weights will not equal 1.

By default, the portfolio is not leveraged. This means that the asset class be shorted (negative shock) to go below 0 and levered (positive shock) to go above 1. The asset class weight is thus capped between 0 and 1 by default. If the leverage option is set to True, then this value is no longer capped.

Parameters
  • data (ndarray) – Monte carlo simulation data. This must be 3 dimensional with the axis representing time, trial and asset respectively.

  • weights (array_like) – Weights of the portfolio. This must be 1 dimensional and must match the dimension of the data’s last axis.

  • freq (Frequency) – Frequency of the data. Can either be a string (‘week’, ‘month’, ‘quarter’, ‘semi-annual’, ‘year’) or an integer specifying the number of units per year. Week: 52, Month: 12, Quarter: 4, Semi-annual: 6, Year: 1.

  • shock (float) – The amount to shock each asset class by. A positive number represents adding to the asset class by proportionately removing from the other asset class. A negative number represents removing from the asset class and adding to the other asset class proportionately.

  • geometric (bool) – If True, calculates the geometric mean, otherwise, calculates the arithmetic mean.

  • cov (ndarray) – Asset covariance matrix

  • cvar_cutoff (int) – Number of years to trim the data cube by for cvar calculation.

  • cvar_data (np.ndarray) – If specified, will use this data cube instead of the main data cube for cvar calculations.

  • alpha (float) – Confidence level for calculation.

  • invert (bool) – Whether to invert the confidence interval level

  • rebalance (bool) – If True, portfolio is assumed to be rebalanced at every step.

  • names (list of str) – Asset class names

  • leveraged (bool) – If True, asset weights are allowed to go below 0 and above 1. This represents that the asset class can be shorted or levered.

  • distribute (bool) – If True, asset value changes are distributed proportionately to all other asset classes. See Notes for more information.

Returns

A dataframe with the asset names as the indices and with columns (ret, vol, cvar) representing returns, volatility and CVaR respectively.

Return type

DataFrame

Examples

>>> from perfana.datasets import load_cube
>>> from perfana.monte_carlo import sensitivity_m
>>> data = load_cube()[..., :7]
>>> weights = [0.25, 0.18, 0.13, 0.11, 0.24, 0.05, 0.04]
>>> freq = 'quarterly'
>>> shock = 0.05  # 5% absolute shock
>>> sensitivity_m(data, weights, freq, shock)
              ret       vol      cvar
Asset_1  0.022403  0.113284 -0.485220
Asset_2  0.020484  0.121786 -0.542988
Asset_3  0.022046  0.113964 -0.492411
Asset_4  0.020854  0.109301 -0.478581
Asset_5  0.020190  0.104626 -0.459786
Asset_6  0.020335  0.106652 -0.467798
Asset_7  0.020220  0.106140 -0.468692