Black–Litterman portfolio optimization

Một phần của tài liệu Financial risk modelling and portfolio optimization with r second edition (Trang 318 - 324)

This first example shows how the BL approach can be applied to the one-step-ahead forecasts for European equity markets derived from a VECM model. The estimated parameters of the posterior distributions are then used to obtain a weight vector that maximizes the portfolio’s Sharpe ratio—that is, the solution to the empirical tangency portfolio is sought. The example involves a back-test where, in addition to the BL approach, portfolio weights based upon the prior distribution and a naive allocation are computed and the resulting portfolios’ equities are contrasted with each other.

The first step of this analysis is shown in Listing 13.1. The packagesurcaandvars are brought into memory and theEuStockMarketsdata set (see Section 9.5.1) is loaded. To summarize, this data set consists of the daily levels for the DAX, FTSE, CAC, and SMI indexes and has class attributemts. In line 5 the data set is converted to azooobject and in line 7 the month’s-end values are extracted. The sample starts in June 1991 and ends in August 1998, comprising a total of 87 observations. The first 61 entries will be used as a subsample for unit root and co-integration testing, as is achieved by thewindow()function in line 10. The remaining data is kept back for executing the back-test. In lines 12–18 the degree of integration is determined by applying the ADF and ERS tests to the level and first differences of the series. The outcome of these tests is shown in Table 13.2.

Given critical values for the tests at the 5% level of −2.89 (with constant) and

−1.95 (without constant), it can be concluded that all series are difference stationary.

k k Rcode 13.1Integration and co-integration analysis of equity indexes.

l i b r a r y ( u r c a ) 1

l i b r a r y ( v a r s ) 2

# # L o a d i n g d a t a s e t and c o n v e r t i n g t o z o o 3

d a t a ( E u S t o c k M a r k e t s ) 4

A s s e t s <− a s . zoo ( E u S t o c k M a r k e t s ) 5

# # A g g r e g a t i n g a s month ’ send s e r i e s 6

AssetsM <− a g g r e g a t e ( A s s e t s , a s . yearmon , t a i l , 1 ) 7

h e a d ( AssetsM ) 8

# # A p p l y i n g u n i t r o o t t e s t s f o r subs a m p l e 9

A s s e t s M s u b <− window ( AssetsM , s t a r t = s t a r t ( AssetsM ) , 10

end = " J u n 1996 " ) 11

# # L e v e l s 12

ADF <− l a p p l y ( A s s e t s M s u b , u r . df , t y p e = " d r i f t " , 13

s e l e c t l a g s = " AIC " ) 14

ERS <− l a p p l y ( A s s e t s M s u b , u r . e r s ) 15

# # D i f f e r e n c e s 16

DADF <− l a p p l y ( d i f f ( A s s e t s M s u b ) , u r . df , s e l e c t l a g s = " AIC " ) 17

DERS <− l a p p l y ( d i f f ( A s s e t s M s u b ) , u r . e r s ) 18

# # VECM 19

VEC <− c a . j o ( A s s e t s M s u b , e c d e t = " n o n e " , s p e c = " t r a n s i t o r y " ) 20

summary (VEC) 21

Table 13.2 Test statistics for unit root tests.

Unit roots ADF ERS

Level Diff Level Diff

DAX −0.33 −5.75 −0.05 −2.82

SMI 0.33 −4.74 0.36 −2.76

CAC −2.56 −6.62 −1.64 −3.09

FTSE −0.66 −6.10 0.26 −2.49

Next, the subsample is fitted to a VECM model by means of the expression in line 20.

The default lag order of 2 (expressed in level form) and the transitory specification of the VECM have been used. The results of the maximum eigenvalue test are shown in Table 13.3. The null hypothesis of no long-run relationship is rejected, hence the four equity markets are co-integrated with rank 1. This model specification will be maintained throughout the back-test.

Having established the structure of the working model, the one-step-ahead fore- casts and the return expectations deduced from them are computed as shown in List- ing 13.2. First, the date stamps for the extending back-test windows are assigned to the objectidxand the names and sizes of the equity indexes are stored in the ob- jectsANamesandNAssets, respectively. In the body of functionf1()a simple

k k Table 13.3 Results of maximum eigenvalue test for

VECM.

Co-integration Max. eigenvalue Critical values

Statistic 10% 5% 1%

r≤3 0.10 6.50 8.18 11.65

r≤2 5.68 12.91 14.90 19.19

r≤1 16.62 18.90 21.07 25.75

r=0 33.33 24.78 27.14 32.14

Rcode 13.2Generating views derived from VAR model of assets.

# # I n d e x o f t i m e s t a m p s i n backt e s t ( e x t e n d i n g window ) 1

i d x <− i n d e x ( AssetsM ) [−c ( 1 : 6 0 ) ] 2

ANames <− c o l n a m e s ( AssetsM ) 3

N A s s e t s <− n c o l ( AssetsM ) 4

# # F u n c t i o n f o r r e t u r n e x p e c t a t i o n s 5

f 1 <− f u n c t i o n ( x , c i , p e r c e n t = TRUE) { 6

d a t a <− window ( AssetsM , s t a r t = s t a r t ( AssetsM ) , end = x ) 7

Lobs <− t ( t a i l ( d a t a , 1 ) ) 8

v e c <− c a . j o ( d a t a , e c d e t = " none " , s p e c = " t r a n s i t o r y " ) 9

m <− v e c 2 v a r ( vec , r = 1 ) 10

f c s t <− p r e d i c t (m, n . a h e a d = 1 , c i = c i ) 11

LU <− m a t r i x ( u n l i s t ( f c s t $ f c s t ) , 12

n c o l = 4 , byrow = TRUE) [ , c ( 2 , 3 ) ] 13

RE <− r e p ( 0 , N A s s e t s ) 14

PView <− LU [ , 1 ] > Lobs 15

NView <− LU [ , 2 ] < Lobs 16

RE [ PView ] <− (LU[ PView , 1 ] / Lobs [ PView , 1 ] − 1 ) 17

RE [ NView ] <− (LU[ NView , 1 ] / Lobs [ NView , 1 ] − 1 ) 18

names ( RE ) <− ANames 19

i f ( p e r c e n t ) RE <− RE 100 20

r e t u r n ( RE ) 21

} 22

R e t u r n E s t <− l a p p l y ( i d x , f 1 , c i = 0 . 5 ) 23

qv <− zoo ( m a t r i x ( u n l i s t ( R e t u r n E s t ) , 24

n c o l = N A s s e t s , byrow = TRUE) , i d x ) 25

c o l n a m e s ( qv ) <− ANames 26

t a i l ( qv ) 27

k k forecasting rule is implemented—only those forecasts deemed sufficiently large in

absolute terms will enter into a view for a certain equity market. This function is used in the call tolapply()in line 23, so that the views for the entire back-test window are computed by one line of code. Within the function, first the subsample for recursive estimation of the VECM is extracted from the objectAssetsMand the last row of this observation is assigned to the objectLobs. This object will be used later to determine whether a directional view warrants a certain return for a given confidence level. In lines 9–10 the VECM is estimated and converted into its level representation by utilizing the functionvec2var(), where a co-integration rank of 1 is assumed. This step is necessary because the one-step-ahead forecasts together with the lower and upper bounds for a given confidence levelcican then be swiftly computed by means of the availablepredict()method for this kind of object. Be- cause thepredict()method returns alistobject, the lower and upper bounds for the one-step-ahead forecasts are extracted and put in the matrixLU. The vector of return expectations is initialized to zero for all four markets. A positive (negative) view on a market is given when the lower (upper) bound is above (below) the latest observed price. Whether this is the case is determined by the logical vectorsPView andNView, respectively. The return expectations are then replaced at the relevant entries inREaccordingly. Hence, the absolute return should be at least as high as in REwith1 - ci/2confidence. Thelistobject of the views, where each element contains the view for the subsequent period, is assigned to the objectReturnEst. In lines 24–27 thislistobject is converted to azooobject (qv) where the rows, with their date information, carry the views formed at that period for the subsequent period.

Having determined the views for each period, the estimates for the parameters of the posterior distributions have to be computed for each period in the back-test sample, and these can then be employed in portfolio optimizations. These tasks are accomplished in Listing 13.3.

First, the necessary packagesBLCOP, fCopulae andfPortfoliofor conducting these computations are loaded into the workspace. In line 5 the discrete one-period percentage returns are calculated and assigned to the objectR. In the following lines the pick matrixPis created; because only directional views for each asset will be implemented, this pick matrix is diagonal. In lines 9–21 an auxiliary functionBL() is created which returns an object of classBLResult. ThisS4object contains the parameters of the prior and posterior distributions. Similar to the previous function for generating views,BL()includes an argumentxthat tells it when the extending sample ends. Within the function the view of a certain period is recovered fromqv and the subsample of the returns is assigned to the objectRw. Next, the parameters of the prior distribution are estimated. The confidences in the views can take any value, because in the estimation of the posterior distribution they will be replaced by the sample variances of the returns, enforced by callingBL()with the argument kappa=1. In the final lines, an object view of classBLViewsis created, which is then employed in the estimation of the posterior parameters. This function is then called in line 23 by applyinglapply()to the time indexidx. The result will be a listobject of length 27 and each element contains the objects of classBLResult.

In the following block of statements, two functions,EstPrior()andEstBL(),

k k Rcode 13.3Maximum Sharpe ratio portfolio specifications.

l i b r a r y ( BLCOP ) 1

l i b r a r y ( f C o p u l a e ) 2

l i b r a r y ( f P o r t f o l i o ) 3

# # C o m p u t i n g r e t u r n s 4

R <− ( AssetsM / l a g ( AssetsM , k = −1) −1 . 0 ) 100 5

# # P i c k m a t r i x f o r d i r e c t i o n a l v i e w s 6

P <− d i a g ( N A s s e t s ) 7

c o l n a m e s ( P ) <− ANames 8

# # F u n c t i o n f o r BL p o s t e r i o r d i s t r i b u t i o n 9

BL <− f u n c t i o n ( x , t a u , k a p p a ) { 10

q <− qv [ t i m e ( qv ) == x , ] 11

q <− c ( c o r e d a t a ( q ) ) 12

Rw <− window ( R , s t a r t = s t a r t ( R ) , end = x ) 13

mu <− c o l M e a n s (Rw) 14

cov <− cov (Rw) 15

c l e v e l <− r e p ( 1 , N A s s e t s ) 16

v i e w s <− BLViews ( P = P , q = q , c o n f i d e n c e s = c l e v e l , 17

a s s e t N a m e s = ANames ) 18

p o s t <− p o s t e r i o r E s t ( v i e w s , mu = mu , t a u = t a u , 19

s i g m a = cov , k a p p a = k a p p a ) 20

r e t u r n ( p o s t ) 21

} 22

P o s t D i s t <− l a p p l y ( i d x , BL , t a u = 1 , k a p p a = 1 ) 23

# # D e f i n i n g p o r t f o l i o s p e c i f i c a t i o n s 24

E s t P r i o r <− f u n c t i o n ( x , s p e c = NULL, . . . ) { 25

l i s t ( mu = BLR@priorMean , Sigma = BLR@priorCovar ) 26

} 27

EstBL <− f u n c t i o n ( x , s p e c = NULL, . . . ) { 28

l i s t ( mu = BLR@posteriorMean , Sigma = B L R @ p o s t e r i o r C o v a r ) 29

} 30

# # P r i o r s p e c i f i c i c a t i o n 31

M S P r i o r <− p o r t f o l i o S p e c ( ) 32

s e t E s t i m a t o r ( M S P r i o r ) <− " E s t P r i o r " 33

# # BL s p e c i f i c a t i o n 34

MSBl <− p o r t f o l i o S p e c ( ) 35

s e t E s t i m a t o r ( MSBl ) <− " EstBL " 36

# # C o n s t r a i n t s 37

BoxC <− c ( "minW [ 1 : N A s s e t s ] = −0.8 " , "maxW [ 1 : N A s s e t s ] = 0 . 8 " ) 38

are defined which will be used to extract the estimated parameters of the prior and posterior distributions, respectively. These functions are then assigned as estimators in the portfolio specifications employed for determining the solutions of the max- imum Sharpe ratio portfolios derived from the estimates for the parameters of the prior and posterior distributions, respectively. A box constraint where the weights are allowed to vary in the interval[−0.8,0.8]completes the specification.

k k Rcode 13.4Maximum Sharpe ratio portfolio back-test.

# # Backt e s t f u n c t i o n 1

BT <− f u n c t i o n ( DataSub , BLR ) { 2

D a t a S u b <− a s . t i m e S e r i e s ( D a t a S u b ) 3

P P r i o r <− t a n g e n c y P o r t f o l i o ( d a t a = DataSub , s p e c = MSPrior , 4

c o n s t r a i n t s = BoxC ) 5

PBl <− t a n g e n c y P o r t f o l i o ( d a t a = DataSub , s p e c = MSBl , 6

c o n s t r a i n t s = BoxC ) 7

W e i g h t s <− r b i n d ( g e t W e i g h t s ( P P r i o r ) , g e t W e i g h t s ( PBl ) ) 8

c o l n a m e s ( W e i g h t s ) <− ANames 9

rownames ( W e i g h t s ) <− c ( " P r i o r " , "BL" ) 10

r e t u r n ( W e i g h t s ) 11

} 12

# # C o n d u c t i n g backt e s t 13

B a c k t e s t <− l i s t ( ) 14

f o r ( i i n 1 : l e n g t h ( i d x ) ) { 15

D a t a S u b <− window ( R , s t a r t = s t a r t ( AssetsM ) , end = i d x [ i ] ) 16

BLR <− P o s t D i s t [ [ i ] ] 17

B a c k t e s t [ [ i ] ] <− BT ( DataSub , BLR ) 18

} 19

In Listing 13.4 an auxiliary functionBT()is created with the purpose of returning a matrix containing the optimal weights for the prior- and posterior-based maximum Sharpe ratio portfolios. This function is then employed in a forloop where these matrices are stored as elements in thelistobjectBacktest. Note that in this loop the object BLRis created which will be used by means of lexical scoping in the call totangencyPortfolio(). To that end, the optimal allocations in each period of the back-test sample are determined and can then be used for calculating the portfolio wealth trajectories.

As a final step, the weights have to be recovered from thelistobject and the portfolio equities calculated as shown in Listing 13.5. In the first two blocks of state- ments the first and second rows are extracted from each list element and arranged as zooobjects for the prior and posterior weights. The weights are lagged by one pe- riod such that the allocations can then be directly multiplied with the corresponding returns. These calculations are accomplished in lines 12–18. First, the subsample of returns pertinent to the back-test period is extracted from the objectRwhich holds the returns for the entire sample period. These returns are then multiplied by the optimal allocations, the row sums are computed, and 1 is added, to arrive at the return factors.

The seed wealth is set to 100 monetary units and the cumulated product of initial wealth and the return factors equals the portfolio wealth trajectories for the maxi- mum Sharpe ratio allocations according to the prior and posterior distributions. For better comparison, the wealth trajectory of a naive (i.e., equal-weighted) allocation is computed to be used as a benchmark.

The paths of the portfolio equities and the relative wealth shares between the BL-based portfolio allocations vis-à-vis those determined from the prior distribution

k k Rcode 13.5Comparison of portfolio strategies.

# # E x t r a c t i n g w e i g h t s b a s e d on p r i o r 1

W P r i o r <− m a t r i x ( u n l i s t ( l a p p l y ( B a c k t e s t , f u n c t i o n ( x ) x [ 1 , ] ) ) , 2

n c o l = N A s s e t s , byrow = TRUE) 3

W P r i o r <− zoo ( WPrior , o r d e r . by = i d x ) 4

WPriorL1 <− l a g ( WPrior , k = −1 , na . pad = TRUE) 5

# # E x t r a c t i n g w e i g h t s b a s e d on BL 6

WBl <− m a t r i x ( u n l i s t ( l a p p l y ( B a c k t e s t , f u n c t i o n ( x ) x [ 2 , ] ) ) , 7

n c o l = N A s s e t s , byrow = TRUE) 8

WBl <− zoo ( WBl , o r d e r . by = i d x ) 9

WBlL1 <− l a g ( WBl , k = −1 , na . pad = TRUE) 10

# # Compute p o r t f o l i o e q u i t i e s 11

Rsub <− R [ t i m e ( WBlL1 ) , ] / 100 12

R e t F a c P r i o r <− rowSums ( Rsub WPriorL1 ) + 1 13

R e t F a c P r i o r [ 1 ] <− 100 14

R e t F a c B l <− rowSums ( Rsub WBlL1 ) + 1 15

R e t F a c B l [ 1 ] <− 100 16

E q u i t y P r i o r <− zoo ( cumprod ( R e t F a c P r i o r ) , i n d e x ( Rsub ) ) 17

E q u i t y B L <− zoo ( cumprod ( R e t F a c B l ) , i n d e x ( Rsub ) ) 18

# # Equalw e i g h t s t r a t e g y 19

EW <− m a t r i x ( 1 / N A s s e t s , n c o l = N A s s e t s , nrow = nrow ( WBlL1 ) ) 20

RetFacEw <− rowSums ( Rsub EW) + 1 21

RetFacEw [ 1 ] <− 100 22

EquityEw <− zoo ( cumprod ( RetFacEw ) , i n d e x ( Rsub ) ) 23

and the equal-weighted allocations are displayed in Figure 13.3; the associatedR code is shown in Listing 13.6.

The MSR portfolio allocation well outperforms the equal-weighted approach, and the BL approach adds value compared to the allocation based on the prior distribu- tion. Finally, the distributions of the allocations derived from the prior and posterior parameter estimates are shown as box-plots in Figure 13.4. The weights applied to the equity indexes according to the prior distribution are much more concentrated compared to the portfolio solution derived from the BL posterior distribution. Qual- itatively, only short positions would have been taken in the former approach with respect to the French stock market, and only long positions for the remaining three markets. In contrast to this, the allocations according to the BL posterior span a wider interval, and long positions for the French stock market as well as short positions for the UK stock market would have been entered during the back-test, reflecting the impact of the diverging return expectations between the prior and posterior estimates.

Một phần của tài liệu Financial risk modelling and portfolio optimization with r second edition (Trang 318 - 324)

Tải bản đầy đủ (PDF)

(436 trang)