Maximum Active Drawdown in python

前端 未结 3 805
孤街浪徒
孤街浪徒 2021-02-01 09:10

I recently asked a question about calculating maximum drawdown where Alexander gave a very succinct and efficient way of calculating it with DataFrame methods in pandas.

3条回答
  •  迷失自我
    2021-02-01 09:52

    You may have noticed that your individual components do not equal the whole, either in an additive or geometric manner:

    >>> cum.tail(1)
         Portfolio  Benchmark    Active
    199   1.342179   1.280958  1.025144
    

    This is always a troubling situation, as it indicates that some sort of leakage may be occurring in your model.

    Mixing single period and multi-period attribution is always always a challenge. Part of the issue lies in the goal of the analysis, i.e. what are you trying to explain.

    If you are looking at cumulative returns as is the case above, then one way you perform your analysis is as follows:

    1. Ensure the portfolio returns and the benchmark returns are both excess returns, i.e. subtract the appropriate cash return for the respective period (e.g. daily, monthly, etc.).

    2. Assume you have a rich uncle who lends you $100m to start your fund. Now you can think of your portfolio as three transactions, one cash and two derivative transactions: a) Invest your $100m in a cash account, conveniently earning the offer rate. b) Enter into an equity swap for $100m notional c) Enter into a swap transaction with a zero beta hedge fund, again for $100m notional.

    We will conveniently assume that both swap transactions are collateralized by the cash account, and that there are no transaction costs (if only...!).

    On day one, the stock index is up just over 1% (an excess return of exactly 1.00% after deducting the cash expense for the day). The uncorrelated hedge fund, however, delivered an excess return of -5%. Our fund is now at $96m.

    Day two, how do we rebalance? Your calculations imply that we never do. Each is a separate portfolio that drifts on forever... For the purpose of attribution, however, I believe it makes total sense to rebalance daily, i.e. 100% to each of the two strategies.

    As these are just notional exposures with ample cash collateral, we can just adjust the amounts. So instead of having $101m exposure to the equity index on day two and $95m of exposure to the hedge fund, we will instead rebalance (at zero cost) so that we have $96m of exposure to each.

    How does this work in Pandas, you might ask? You've already calculated cum['Portfolio'], which is the cumulative excess growth factor for the portfolio (i.e. after deducting cash returns). If we apply the current day's excess benchmark and active returns to the prior day's portfolio growth factor, we calculate the daily rebalanced returns.

    import numpy as np
    import pandas as pd
    
    np.random.seed(314)
    df_returns = pd.DataFrame({
        'Portfolio': np.random.randn(200) / 100 + 0.001,
        'Benchmark': np.random.randn(200) / 100 + 0.001})
    df_returns['Active'] = df.Portfolio - df.Benchmark
    
    # Copy return dataframe shape and fill with NaNs.
    df_cum = pd.DataFrame()  
    
    # Calculate cumulative portfolio growth
    df_cum['Portfolio'] = (1 + df_returns.Portfolio).cumprod()
    
    # Calculate shifted portfolio growth factors.
    portfolio_return_factors = pd.Series([1] + df_cum['Portfolio'].shift()[1:].tolist(), name='Portfolio_return_factor')
    
    # Use portfolio return factors to calculate daily rebalanced returns.
    df_cum['Benchmark'] = (df_returns.Benchmark * portfolio_return_factors).cumsum()
    df_cum['Active'] = (df_returns.Active * portfolio_return_factors).cumsum()
    

    Now we see that the active return plus the benchmark return plus the initial cash equals the current value of the portfolio.

       >>> df_cum.tail(3)[['Benchmark', 'Active', 'Portfolio']]
             Benchmark    Active  Portfolio
        197   0.303995  0.024725   1.328720
        198   0.287709  0.051606   1.339315
        199   0.292082  0.050098   1.342179
    

    By construction, df_cum['Portfolio'] = 1 + df_cum['Benchmark'] + df_cum['Active']. Because this method is difficult to calculate (without Pandas!) and understand (most people won't get the notional exposures), industry practice generally defines the active return as the cumulative difference in returns over a period of time. For example, if a fund was up 5.0% in a month and the market was down 1.0%, then the excess return for that month is generally defined as +6.0%. The problem with this simplistic approach, however, is that your results will drift apart over time due to compounding and rebalancing issues that aren't properly factored into the calculations.

    So given our df_cum.Active column, we could define the drawdown as:

    drawdown = pd.Series(1 - (1 + df_cum.Active)/(1 + df_cum.Active.cummax()), name='Active Drawdown')
    
    >>> df_cum.Active.plot(legend=True);drawdown.plot(legend=True)
    

    You can then determine the start and end points of the drawdown as you have previously done.

    Comparing my cumulative Active return contribution with the amounts you calculated, you will find them to be similar at first, and then drift apart over time (my return calcs are in green):

提交回复
热议问题