Course objective Modélisation Financière et Applications UE 111 Application series #2 Diversification and Efficient Frontier Juan Raposo and Fabrice Riva Université Paris Dauphine The previous session dealt with individual stock returns This session deals with the properties of stock portfolios through three different applications 1 The effect of naïve diversification for the variance of portfolio returns 2 The computation of the feasible set of portfolios 3 The computation of minimum variance portolios and the construction of the efficient frontier 2015/16 Snapshot of the Returns worksheet Objective How does risk (measured as the variance of returns) evolve when the number of stocks in a portfolio increases? We will start with a universe of 38 stocks belonging to the CAC 40 index We will construct portfolios of increasing size, from 1 to 15 stocks For each portfolio of size i, we will build 20 different portfolios by randomly selecting (with replacement) i stocks among the initial sample of 38 stocks
Snapshot of the Diversification worksheet The problem will be solved using two different methods: 1 : We will first compute the return time series of the various built portfolios and compute their variance from the variance of their time series returns 2 : We will directly compute the variance of portfolio returns using matrix algebra Step 1: Random selection of stocks in portfolio x The return on portfolio x on date t is computed as: where: R x,t = n x i R i,t (1) i=1 R x,t is the portfolio (discrete) return on date t R i,t is asset i s (discrete) return on date t x i is the weight on asset i in portfolio x, where x i = Amount invested in asset i Amount invested in portfolio x Naïve diversification means that i, x i = 1/n (2) This involves selecting randomly n stocks among the 38 available stocks This amounts to drawing n integer numbers in the interval [1; 38] The drawing will be performed using the RandBetween(<min>, <max>) worksheetfunction, which generates a uniformly distributed integer random number in the interval [< min >; < max >] The n selected stocks (their number) will be stored in a 1-dimension array variable PF stocks containing n rows and whose declaration will thus be made using Redim PF stocks(n) Stock selection 1. Function Stock Selection(n) 2. ReDim PF stocks(n) 3. For i = 1 to n 4. PF stocks(i) = WorksheetFunction.RandBetween(1, 38) 5. Next i 6. Stock Selection = PF Stocks 7. End Function
Step 2: Time series of portfolio returns Step 3: Storing and displaying results We first need to determine (once only same number of returns for every stock) the number of sample returns Nb returns = Range(Cells(2, 1), Cells(2, 1).End(xlDown)).Cells.Count We will use the variable Current date as the counter variable in a For...Next loop to go through all sample dates For a given value of that variable, we will compute portfolio returns by adding the individual returns of the stocks pertaining to portfolio x. The sum of the individual returns will then be divided by n (naïve diversification) and stored in array variable PF x Retrieving the individual returns will be obtained using a second loop that goes through all elements of PF stocks For stock number Stock the value of its return for a given value of the Current date variable is obtained using Cells(Current date, PF stocks(stock)).offset(1, 1).Value Once the array variable PF x is filled, the variance of portfolio returns is obtained by applying the Var worksheetfunction to that variable Steps 1 and 2 will iterated 15 20 times At each iteration, the value of the variance will be stored in the (15, 20) Results array variable The content of this variable will then be reported in the Diversification worksheet starting from cell B2 Evolution of portfolio variance with size The variance σ 2 x of portfolio returns can be computed directly using matrix algebra where: σ 2 x = x Vx (3) x = (x 1, x 2,..., x n) is the column vector of portfolio weights V is the (n, n) variance-covariance matrix of returns, with σ 1,1 σ 1,2 σ 1,i σ 1,n σ 2,1 σ 2,2 σ 2,i σ 2,n V =.............. σ i,1 σ i,2 σ i,i σ i,n.............. σ n,1 σ n,2 σ n,i σ n,n (4)
Example: variance computation using matrix algebra with 2 stocks ( ) ( ) ( ) σx 2 σ1,1 σ = (x 1 x 2 ) 1,2 x1 x1 σ = (x σ 2,1 σ 2,2 x 1 x 2 ) 1,1 + x 2 σ 1,2 2 x 1 σ 2,1 + x 2 σ 2,2 = x 2 1 σ 1,1 + x 1 x 2 σ 1,2 + x 2 x 1 σ 2,1 + x 2 2 σ 2,2 = x 2 1 σ2 1 + x 2 2 σ2 2 + 2x 1x 2 σ 1,2 Matrix operations in Excel: Interactive/worksheet mode Product: =PRODUITMAT MMULT(<Mat1>;<Mat2>) Transpose: =TRANSPOSE(<Mat>) Inverse: =INVERSEMAT MINVERSE(<Mat>) Determinant: =DETERMAT MDETERM(<Mat>) Scalar product: =SOMMEPROD SUMPRODUCT(<Vec1>;<Vec2>) WARNING: Matrix operations must be validated by pressing simultaneously CTRL + SHIFT + ENTER Matrix operations in Excel: VBA functions Notes: Product: WorksheetFunction.MMult(<Mat1>;<Mat2>) Transpose: WorksheetFunction.Transpose(<Mat>) Inverse: WorksheetFunction.MInverse(<Mat>) Determinant: WorksheetFunction.MDeterm(<Mat>) Scalar product: WorksheetFunction.SumProd(<Vec1>;<Vec2>) Option Base 1 is the sole possible option for matrix operations to work It is safer to declare a row [column] vector as a (n, 1) [respectively (1, n)] matrix rather than a 1-dimension array (in which case it is a row vector) Step 1: Variance-Covariance matrix Since the sample contains 38 stocks, the VCV matrix is a (38, 38) matrix Dim VCV(38, 38) as Double The covariance between the returns on two stocks is computed thanks to the Covar worksheetfunction. Its syntax is the following WorksheetFunction.Covar(<Vector 1>, <Vector 2>) Cell(i, j) in the VCV matrix is computed by applying the Covar worksheetfunction and using as its first argument the range that contains stock i returns and as its second argument the range that contains stock j returns Reference to the range that contains stock i returns can be achieved as follows: Define object variable Range start of type Range which corresponds to the range that contains the sample dates Set Range start = Range(Cells(2, 1), Cells(2, 1).End(xlDown)) Then Range start.offset(0, i) will correspond to the range that contains stock i returns Note that the VCV matrix is symmetric and that we only need to compute the lower triangle and report the results in the upper triangle
Step 2: Building the portfolio weights Step 3: Portfolio variance computation Suppose we randomly selected stocks 12 and 28 to build a 2-stock portfolio. The vector of weights will contain the values 1 and 1 on rows 12 and 28 and 0 2 2 everywhere else Warning: Suppose we select twice a given stock in a 3-stock portfolio. Then this stock must be given a weight of 2 to ensure that the portfolio is feasible 3 We will use the following approach: We declare a 38 rows and 1 column array variable x Each of the 38 elements of x is initially set to 0 using a For...Next loop We randomly select n stocks (where n is the portfolio size) by drawing n random numbers in the interval [1; 38] using worksheetfunction RandBetween Assigning the number drawn to variable Position, the corresponding element in variable x is adjusted using the instruction x(position, 1) = x(position, 1) + 1 / n Once the VCV matrix and the vector of weights are available, the variance is computed as x Vx. Its VBA translation is WorksheetFunction.MMult(WorksheetFunction. Transpose(x), WorksheetFunction.MMult(VCV, x)) This VBA instruction works fine but it returns a (1, 1) matrix, not a scalar. Converting this matrix into a scalar number involves the unpleasant use of the Index worksheetfunction This can be avoided by using the SumProduct worksheetfunction as follows WorksheetFunction.SumProduct(x, WorksheetFunction.MMult(VCV, x)) The previous application allowed us to analyze the evolution of portfolio risk as we increase the number of stocks it contains Yet, how do expected or average returns evolve when risk increases? Are some portfolios better than others? Can we make the risk of a portfolio arbitrarily small? Our objective in this application will be to compute the set (more modestly a part of the set) of feasible portfolios A portfolio is said to be feasible if its weights sum to 1 (100%) Some stocks may be given negative weights, which corresponds to short sales The various portfolios we compute will be plotted in an expected (average) return / variance (or standard deviation) diagram
Average return 0.3% 0.2% 0.2% 0.1% 0.1% 0.0% -0.1% -0.1% -0.2% Individual (38) sample stocks plotted in terms of average return against standard deviation -0.2% 0.00% 0.50% 1.00% 1.50% 2.00% 2.50% 3.00% 3.50% 4.00% 4.50% 5.00% Standard deviation Step 1: Average return of a portfolio : Same as what we did in method #1 with portfolio variance, i.e. compute the time series of portfolio returns and then compute the time series average return using the Average worksheetfunction. However, we know that is not optimal in terms of computation time : Relies on matrix algebra. Denoting R x the expected (average) return of portfolio x: R x = x R (5) where R = ( R 1, R 2,..., R n) is the column vector of average individual stock returns We need to compute first the vector of average individual returns. These returns will be stored in (38, 1) array variable R of type Double The cells of this variable will be filled using the same approach as the one we used to compute covariances and applying the Average worksheetfunction Note that it is possible (and recommended) to compute the R vector within one of the loops that computes the VCV matrix Step 2: Generating a feasible portfolio Generating a feasible portfolio amounts to generate 38 random numbers and store them in array variable x These random numbers will be generated using the Rnd VBA functions that draws independent random numbers, uniformly distributed over the interval [0, 1) Since Rnd generates a uniformly distributed random number on [0, 1), Rnd * 2-1 will generate a uniformly distributed random number on [ 1, 1). Generating negative weights allows to account for short positions There is no reason why these 38 random number should add up to 1 (on average the sum will be equal du 0) The initial portfolio will be normalized, i.e. transformed into a feasible portfolio by dividing each of the initial weights by their initial sum Note that the initial weights will add up to 0 on average. Thus, although the initial weights lie in the [ 1, 1) interval, the normalized weights can lie potentially in the (, + ) interval Step 3: computation of portfolio average return and variance Computation of portfolio average return and variance using matrix algebra Average return: R x = x R Standard deviation: σ x = x Vx Computation of portfolio average return and variance in VBA Average return: WorksheetFunction.SumProduct(x, R) Standard deviation: Sqr(WorksheetFunction.SumProduct(x, WorksheetFunction.MMult(VCV, x)))
Step 4: Iteration and display of results Feasible set We will simulate 1,000 random feasible portfolios These portfolios will be simulated using a For... Next loop The standard deviation and average return of the various portfolios will be stored in a (1000, 2) array variable named Results The content of the Results variable will then be reported in a new worksheet named Feasible set A scatter plot will then be produced Based on previous graph analysis, some portfolios are dominated in the sense that we can find portfolios having the same average (expected) return but whose risk is lower The objective in the current application is to build portfolios which, for a given level of risk, maximize the average return This objective is achieved by solving the following constrained optimization program: max {x} Rx s.t. x Vx = σx 2 x 1 = 1 This programm can be solved in closed form using the Lagrangian technique (see UE 106) Here we will solve the program using numerical optimization thanks to Excel solver tool (6) The solver interface
Laying the ground for the solver The Efficient frontier worksheet We need first to prepare a worksheet ( Efficient frontier ) that contains all intermediate computations for the solver to be able to handle the problem The worksheet must contain the following elements 1 The initial vector of weights (starting values) 2 The vector of individual average returns and the VCV matrix 3 The formulas that compute the portfolio average return and variance as well as the sum of its weights The following piece of code aims at performing steps 1 and 2: PF average return and variance 1. Sub Prepare Worksheet 2. Matrices = Compute Matrices 3. Worksheets("Efficient frontier").activate 4. Range("I2").Resize(38, 1).Value = Matrices(2) 5. Range("J2").Resize(38, 38).Value = Matrices(1) 6. End Sub Example #1: Finding the coordinates of the Global Minimum Variance Portfolio (GMVP) Example #2: Finding the coordinates of the MVP whose return variance is equal to.01
Step 0: adding the Solver add-in in the References The Solver add-in must be installed in the VBA project references library Solver programming is made in 5 steps 1 SolverReset resets the solver, i.e. deletes all elements coming from a previous parameterization 2 SolverOk aims at specifying the target cell, the optimization type and variable cells SolverOK SolverOk SetCell:= <target cell>, MaxMinVal: = <optimization type>, [ValueOf:= <target value>, ] ByChange:= <variable cell(s)> The optimization type is coded in the following way : 1 is for maximization, 2 is for minimization and 3 is to equate the target cell with the value given at the ValueOf stage 3 SolverAdd allows to specify the constraint(s). In case of multiple constraints, one SolverAdd block must be created for each constraint SolverAdd SolverAdd CellRef:=<constraint left hand side>, Relation: = <constraint type>, FormulaText:="=<constraint right hand side>" Or FormulaText:="=" & <variable> 4 SolverOption allows to check (True) /uncheck (False) the option on whether control variables can take on negative values or not SolverOptions AssumeNonNeg:=True False 5 SolverSolve serves to indicate whether the solver musk ask the user to validate (False) or not (True) in which case the user accepts by default the solution found by the solver SolverSolver userfinish:=true False The constraint type is coded in the following way: 1 is, 2 is =, 3 is
Finding the coordinates of the GMVP Finding the coordinates of the MVP with.01 variance VBA code for GMVP 1. Worksheets("Efficient frontier").activate 2. SolverReset 3. SolverOk SetCell:=Range("F1"), 4. MaxMinVal:=2, 5. ByChange:=Range("x vec") 6. SolverAdd CellRef:=Range("F3"), 7. Relation:=2, 8. FormulaText:="=1" 9. SolverOptions AssumeNonNeg:=False 10. SolverSolve userfinish:=true VBA code for MVP with.01 variance 1. Worksheets("Efficient frontier").activate 2. SolverReset 3. SolverOk SetCell:=Range("F2"), 4. MaxMinVal:=1, 5. ByChange:=Range("x vec") 6. SolverAdd CellRef:=Range("F3"), 7. Relation:=2, 8. FormulaText:="=1" 9. SolverAdd CellRef:=Range("F1"), 10. Relation:=2, 11. FormulaText:="=.01" 12. SolverOptions AssumeNonNeg:=False 13. SolverSolve userfinish:=true Efficient frontier Our starting point will be the coordinates of the GMVP The coordinates of the GMVP cells F1 and F2 will be copied in cells A2 and B2, repsectively We will also store in a variable the variance of the GMVP We will build 100 MVP of increasing variance. For MVP i, the target variance will be set to GMVP variance + i.005 Each time, the solver will be called to find the coordinates of the new MVP The coordinates of MVP #1 will be copied in cells A3 and B3, those of MVP #2 in cells A4 and B4, etc. We will then draw a scatter plot from the cells contained in range A2:B101