Lindo Systems

! Portfolio with a DrawDown Risk Measure.    (PortDrawDn.lng)
     In a sequence of periods, the drawdown of a portfolio in period t
  is the difference: (highest previous portfolio value) - (current value).
     One measure of portfolio risk is the sum of the k largest drawdowns,
  over a period of time, the K Median drawdown. 
  The objective in this model is:
  Maximize (Ending value) - alpha*KMedianDrawDown, where alpha is
 a parameter that indicates one's risk tolerance.;
! Keywords: Draw down, Portfolio, Risk measurement;
   SETS:
    SCENE: R, VP, VPMX, DD, XSDD;
    ASSET: X, USE;
    SXA( SCENE, ASSET): VE, CG;
   ENDSETS
  DATA:
!Here are several cases;
! Case original Markowitz example, 1943 to 1954;
!CHarry   VP0 = 1;  ! Starting portfolio value;
!CHarry   KMED = 2; ! Consider KMED highest drawdowns(User choice);
!CHarry   ALPHA = 0.01; ! Weight to apply to k-median risk(User choice);
!CHarry   ASSET = 
     SP500   ATT   GMT   USX;
!  Which assets to use/consider;
!CHarry  USE =
     0         1     1       1;
!CHarry    VE =
   1.258997    1.3  1.225  1.149
   1.197526  1.103  1.29   1.26
   1.364361  1.216  1.216  1.419
   0.919287  0.954  0.728  0.922
    1.05708  0.929  1.144  1.169
   1.055012  1.056  1.107  0.965
   1.187925  1.038  1.321  1.133
   1.31713   1.089  1.305  1.732
   1.240164  1.09   1.195  1.021
   1.183675  1.083  1.39   1.131
   0.990108  1.035  0.928  1.006
   1.526236  1.176  1.715  1.908;


! Case Start75: Data from 1975 to 2021;
!Start75;  VP0 = 1;  ! Starting portfolio value;
!Start75;  KMED = 3; ! Consider KMED highest drawdowns;
!Start75;  ALPHA = 0.1; ! Weight to apply to k-median risk;
! 1      2        3        4       5      6     7          8    9      10      11      12      13      14;
!CStart75; ASSET = 
  SP500  Chevron  CocaCola DupontDow PandG IBM  Weyerhauser Ford  GE  ExxonMobil Deere  Boeing  Merck    MMM;  
!  Which assets to use/consider;
!CStart75; USE =
    0        1     1       1         1     1       1       1     1       1       1       1       1      1;
  ! Data from 1STOCK1, 1975 to 2021;
!CStart75;  VE =
1.3846  1.3202  1.5519  1.6659  1.092  1.3348  1.3562  1.3184  1.382  1.3733  1.217   1.5476  1.0433  1.2033  
1.2420  1.3957  0.9605  0.9468  1.052  1.2447  1.2525  1.3977  1.206  1.2085  1.2193  1.8359  0.9838  1.0203  
0.9222  0.9482  0.9430  0.6167  0.9172 0.9798  0.5887  0.9299  0.8966 0.8974  0.7826  1.257   0.8147  0.8565  
1.0641  1.2058  1.1779  0.9299  1.0349 1.0914  0.8995  0.9208  0.9449 1.0208  1.399   2.5378  1.2185  1.3015  
1.1869  1.2027  0.7863  1.2915  0.8354 0.8626  1.2893  0.7596  1.0743 1.1221  1.13    1.0639  1.0684  0.796  
1.3276  1.765   0.9674  1       0.9276 1.0544  1.0748  0.625   1.2099 1.4626  1.2268  1.3074  1.173   1.1741  
0.9467  0.8618  1.0412  0.8171  1.167  0.8379  0.8498  0.8375  0.9367 0.7752  0.7396  0.5099  1       0.9237  
1.2122  0.7464  1.4964  0.9857  1.4712 1.6923  1.2414  2.3209  1.6536 0.952   0.831   1.5056  0.9985  1.3761  
1.2313  1.082   1.0288  1.2899  0.9619 1.2675  0.9375  1.635  1.2358  1.1218  1.3051  1.2915  1.0679  1.1  
1.0596  0.9025  1.1659  0.824   1.0022 1.0092  0.863  1.0767  0.9659  1.3483  0.7727  1.2943  1.0401  0.953  
1.3224  1.22    1.3547  1.4909  1.2237 1.2629  1.0558  1.2712  1.2848  1.225  0.9664  1.3841  1.4574  1.1415  
1.1906  1.1902  1.3402  1.4268  1.095  0.7717  1.2276  1.4547  1.1821  1.2721  0.7957  0.9785  1.8084  1.2953  
1.0569  0.8733  1.0099  1.5385  1.1178 0.9625  1.0265  1.34  1.0262  1.0873  1.5191  0.7237  1.2795  1.1075  
1.1664  1.1546  1.1705  0.975   1.019  1.0552  0.9726  1.34  1.0142  1.1541  1.3813  1.6385  1.0931  0.9631  
1.32    1.4809  1.7311  1.2201  1.6149 0.7723  1.0995  0.8639  1.4413  1.1364  1.2813  1.4691  1.342  1.2843  
0.9658  1.072   1.2039  0.6655  1.2331 1.2005  0.7919  0.6103  0.8895  1.035  0.7622  1.1463  1.1597  1.0769  
1.3095  0.9501  1.7258  1.1316  1.0837 0.7876  1.2571  1.0563  1.3333  1.1763  1.024  1.0523  1.8526  1.1108  
1.076   1.0072  1.0436  1.0651  1.1425 0.566  1.3409  1.5244  1.1176  1.0041  0.9115  0.8403  0.7815  1.0564  
1.1017  1.2536  1.0675  0.9913  1.0629 1.1216  1.2102  1.5044  1.2266  1.0327  1.6914  1.0779  0.7925  1.0807  
1.0119  1.0244  1.1541  1.185   1.0877 1.3009  0.8403  0.8643  0.9726  0.9723  0.8953  1.0867  1.1091  0.9816  
1.3802  1.1737  1.4417  1.0446  1.3387 1.2432  1.1533  1.0359  1.4118  1.3116  1.5962  1.6676  1.7213  1.2436  
1.2306  1.2411  1.4175  1.1157  1.2967 1.658  1.0954  1.1169  1.3733  1.2174  1.1489  1.3589  1.2133  1.2505  
1.3367  1.1846  1.2672  1.2951  1.4832  1.3812  1.0356  1.5058  1.4842  1.2487  1.4383  0.919  1.3312  0.9887  
1.2873  1.0771  1.0047  0.8959  1.1441  1.7622  1.0357  1.2085  1.3901  1.1951  0.5644  0.6667  1.3915  0.8667  
1.2111  1.0445  0.8694  1.4694  1.1999  1.1702  1.4133  0.9084  1.5172  1.1017  1.3194  1.2701  0.911  1.3761  
0.9089  0.9747  1.0461  0.8223  0.7159  0.7879  0.7067  0.4396  0.9293  1.0791  1.0562  1.5928  1.3935  1.2312  
0.8802  1.0613  0.7737  0.9223  1.0088  1.4231  1.0656  0.6707  0.8361  0.9041  0.953  0.5876  0.628  0.981  
0.7773  0.7419  0.9298  0.8792  1.0861  0.6407  0.9099  0.5916  0.6075  0.8891  1.0502  0.8507  0.9628  1.0431  
1.2872  1.2995  1.1576  1.3997  1.1622  1.1959  1.3005  1.7204  1.2723  1.1734  1.4188  1.2774  0.8161  1.3792  
1.1082  1.2156  0.8205  1.191   1.1029  1.0637  1.0503  0.915  1.1782  1.2502  1.1437  1.2285  0.6957  0.9652  
1.0479  1.0811  0.9681  0.8851  1.0508  0.8338  0.9869  0.5273  0.9603  1.0958  0.9155  1.3568  0.9897  0.9443  
1.1574  1.2952  1.197   0.9105  1.1104  1.1819  1.065  0.9728  1.0616  1.3643  1.3958  1.2648  1.3706  1.0055  
1.0546  1.2693  1.2719  0.988   1.1424  1.1127  1.0437  0.8961  0.9962  1.2226  1.959  0.9845  1.3328  1.082  
0.6278  0.7926  0.7377  0.3828  0.842  0.7785  0.4151  0.3403  0.437  0.8521  0.4115  0.4879  0.5231  0.6824  
1.2711  1.0408  1.2591  1.831   0.9808 1.5554  1.4093  4.3668  0.934  0.8542  1.4115  1.2686  1.202  1.4367  
1.1487  1.1852  1.1539  1.2356  1.061  1.1212  1.0512  1.679  1.2089  1.0723  1.5354  1.2056  0.9863  1.0439  
1.0207  1.166   1.0639  0.8424  1.037  1.2529  0.9863  0.6409  0.9792  1.1592  0.9314  1.124  1.0461  0.947  
1.1588  1.0164  1.0362  1.1241  1.0177  1.0417  1.4901  1.2035  1.172  1.0211  1.1173  1.0274  1.0859  1.1361  
1.3243  1.1551  1.1396  1.3733  1.1991  0.9792  1.1348  1.1915  1.3354  1.1693  1.0568  1.8112  1.2225  1.5105  
1.1381  0.8981  1.022   1.0273  1.1189  0.8554  1.1368  1.0045  0.9015  0.9135  0.9687  0.9523  1.1347  1.1716  
1.0131  0.8019  1.0175  1.1287  0.8718  0.8578  0.8353  0.909  1.2327  0.8432  0.8621  1.1124  0.9301  0.9167  
1.1193  1.3084  0.9651  1.1115  1.0588  1.2061  1.0037  0.8609  1.0144  1.1579  1.351  1.0767  1.1145  1.1854  
1.2194  1.0636  1.1066  1.2447  1.0928  0.9243  1.1718  1.0297  0.5522  0.9267  1.5189  1.8943  0.9558  1.3181  
0.9559  0.869   1.032   0.7509  1.0004  0.7409  0.62  0.6125  0.4338  0.8153  0.9531  1.0936  1.3579  0.8095  
1.311   1.1077  1.169   2.7766  1.3588  1.1792  1.3815  1.2157  1.5298  1.0233  1.1615  1.0101  1.1903  0.9259  
1.1576  0.7008  0.9908  1.1076  1.114  0.9391  1.1103  0.9452  0.9677  0.5907  1.5529  0.6571  0.8994  0.9908  
1.2689  1.3896  1.0797  1.1376  1.1757  1.0906  1.2282  2.3629  1.0934  1.4845  1.2744  0.9405  1.3092  1.0162 ;

ENDDATA
! Constants:
    VP0 = portfolio value at start = initial investment,
    VE( t, j) = growth factor over period t of asset j,
    CG( t, j) = compounded growth of asset j, = CG( t-1,j)* VE( t, j)
    KMED = k median index = how many of the the largest drawdowns to consider,
! Variables:
    X( J) = amount invested in ASSET J at 1st period, following buy-and-hold strategy,
    VP( t) = portfolio value at end of period t,
    VPMX( t) = maximum portfolio value seen up to period t,
    DD( t) = drawdown measured at end of period t, (decrease from earlier max),
    THETAKM = k median of drawdowns,
    XSDD( t) = Excess drawdown beyond THETAKM in period t,
    SUMKHI = sum of K highest drawdowns.
   ;

! Compute some constants;
  NSCENE = @SIZE( SCENE); ! Number periods;
! Compute compounded growth by asset;
   @FOR( ASSET( J):
     CG( 1, J) = VE( 1, J); ! Asset values, end of period 1;
     @FOR( SCENE( t) | t #GT# 1:  ! Remaining periods;
       CG( t, J) = CG( t-1, J)* VE( t, J);
         );
       );

! Maximize end of horizon portfolio value - ALPHA* RiskMeasure;
  [OBJ] MAX = VP( NSCENE) - ALPHA* SUMKHI;

! Budget constraint at beginning;
   [BUDGET] @SUM( ASSET( J) | USE( J) : X( J)) = VP0;

! Portfolio value at end of each period;
   @FOR( SCENE( t) :
     VP( t) = @SUM( ASSET( J) | USE( J) : CG( t, J) * X( J));
       ); 

! Enforce max portfolio value seen up to period t;
   VPMX( 1) = VP( 1);
   DD( 1) = 0; ! Compute drawdown;
   @FOR( SCENE( t) | t #GT# 1:
     VPMX( t) >= VPMX( t-1); ! Max value of portfolio up to t;
     VPMX( t) >= VP( t);
     DD( t) >= VPMX( t) - VP( t); ! Max loss of value up to t;
       );

! Sum K-Median calculations, where 
 THETAKM = value of kth highest drawdown.
Excess drawdown beyond the k median ;
  @FOR( SCENE( t):
        XSDD( t) >= DD( t) - THETAKM;
      );

! Compute sum of k largest;
   SUMKHI >= KMED * THETAKM + @SUM( SCENE( t): XSDD( t));

 ! Argument:    
     Assume objective (maximize) is a decreasing function of SUMKHI.
I.e., high SUMKHI is bad.
    Claim: At optimum, THETAKM = kth highest value of DD( t).
    Proof: 
      a) Suppose THETAKM is too small, so number DD( t) > THETAKM is r > KMED.
       Increasing THETAKM by epsilon will 
        decrease SUMKHI by r*epsilon - KMED * epsilon, so solution could not be optimal.,
      b) Suppose THETAKM is too large, so number DD( t) > THETAKM is r < KMED.
        Decreasing THETAKM by epsilon will 
        decrease SUMKHI by KMED * epsilon - r*epsilon, so solution could not be optimal.,
! Ref. Uryasev et al. discussion of CVaR;
  END