• Tidak ada hasil yang ditemukan

PORTFOLIO VARIANCE FUNCTION WITH ARRAY INPUTS

APPENDIX 3B RECORDING KEYSTROKES IN ‘RELATIVE REFERENCES’ MODE

4.6 PORTFOLIO VARIANCE FUNCTION WITH ARRAY INPUTS

Functions to calculate the mean and variance of returns for portfolios are easy to develop. To illustrate the process, a simple spreadsheet formulation using Excel functions is explained, then the cell formulas are coded in VBA to produce the corresponding functions.

The spreadsheet in Figure 4.4 contains risk and return information for three assets, A, B and C, and shows the results of constructing a portfolio with equal amounts of each

of the three assets. The asset returns are in cells C5:C7, a column vector referred to with range name e in the spreadsheet formula and in the VBA code referred to as retsvec. The portfolio is made up of the three assets in the proportions set out in cells E5:E7, a second column vector with range namewin the spreadsheet formulas and referred to as wtsvecin the VBA code.

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

A B C D E F G

Portfolio Risk & Return

e w Portfolio

Asset Data Exp Ret StDevRet weights ExpRet

A 8% 4% 33.3% 9.3%

B 15% 5% 33.3% StDev

C 5% 1% 33.3% 2.57%

100%

Correlations 1 0.5 -0.2

0.5 1 -0.1

-0.2 -0.1 1

VCV Matrix V A B C

A 0.0016 0.0010 -0.0001 Variance B 0.0010 0.0025 -0.0001 =wTVw C -0.0001 -0.0001 0.0001 0.07%

Figure 4.4 Risk and return for portfolio of three assets in PFolio sheet

The expected portfolio return (in cell G5) is the weighted sum of the expected returns on each of A, B and C (in fact, 0.093 or 9.3%). The formula in G5 using the named ranges w and e is the Excel function SUMPRODUCT(e,w), which gives the weighted return, provided the number of elements in each of the column vectors is the same.

The variance–covariance matrix for the three assets can be calculated cell-by-cell from the correlations (C10:E12) and the individual asset return standard deviations (in cells D5:D7). The resulting matrix of cells (C15:E17) contains the individual variances of returns for assets A, B and C on the diagonal and also the covariances for all pairs of assets in the off-diagonal cells. Suppose this variance–covariance matrix is given the range name V. Together with the weights array, it forms the basis for calculating the portfolio variance, essentially a matrix multiplication of form wTVw where wT is the transpose of the weights vectorw. The formula in cell G17 for variance combines the Excel func- tions TRANSPOSE for transposing an array and MMULT for matrix multiplication. It is written as:

MMULT(TRANSPOSE(w), MMULT(V,w))

(Since this is an array function, in entering the formula into cell G17, remember to press the keystroke combination CtrlCShiftCEnter.)

For Excel’s MMULT function to work, the dimensions of any pair of adjacent matrices being multiplied must conform, i.e. the number of columns in the first matrix must equal the number of rows in the second matrix. Here TRANSPOSE(w) is a 1 by 3 row vector, and MULT(V,w) is a 3 by 1 column vector, so matrix multiplication is possible, giving the single cell value of 0.0007 (or 0.07%). So for this portfolio, the risk in return is p0.0007D0.0257 (or 2.57%).

Writing VBA User-defined Functions 83 It is simple to write the code for the portfolio return function, calledPFReturn(wtsvec, retsvec):

Option Base 1

Function PFReturn(wtsvec, retsvec) ’returns weighted return for PortFolio If Application.Count(wtsvec) <> Application.Count(retsvec) Then

PFReturn = “ Counts not equal”

Exit Function

ElseIf retsvec.Rows.Count <> wtsvec.Rows.Count Then wtsvec = Application.Transpose(wtsvec)

End If

PFReturn=Application.SumProduct(retsvec, wtsvec) End Function

The second function, PFVariance(wtsvec, vcvmat), has a more complex code:

Function PFVariance(wtsvec, vcvmat) ’calculates PF variance Dim nc As Integer, nr As Integer

Dim v1 As Variant

nc=wtsvec.Columns.Count nr=wtsvec.Rows.Count

If nc>nr Then wtsvec=Application.Transpose(wtsvec) v1=Application.MMult(vcvmat, wtsvec)

PFVariance=Application.SumProduct(v1, wtsvec) End Function

The function has vector and matrix inputs and requires VBA statements that satisfy the conformity requirements for matrix multiplication. We would prefer the function to work when the weights array is a row vector as well as when it is a column vector. To achieve this, the dimensions of the array are determined. Variables nr and nc (representing the number of rows and columns) for the weights arraywtsvecare declared as integers. For the matrix multiplication to work, the weights vector must be a column vector, hence the statement:

If nc > nr Then wtsvec = Application.Transpose(wtsvec)

A temporary vectorv1is used in the matrix multiplication, so it is declared up front as a variant. As mentioned in Section 3.4.1, this is a flexible data type that can be of any form, scalar or array or matrix. Here v1stores the 3 by 1 column vectorVw. One convenient way of evaluatingwTVwin VBA is to use the SUMPRODUCT function to multiply the elements of the two 3 by 1 column vectors wandVw. Hence the penultimate statement line of code:

PFVariance = Application.SumProduct(v1, wtsvec)

which returns a scalar value to PFVariance.

The code for the PFVariance function centres on ensuring that the Excel MMULT function evaluates properly. Because the user enters the function arguments, the code needs to adjust them where necessary to enforce matrix conformity. However, since the MMULT and SUMPRODUCT functions handle all the array computations, it is not necessary to declare dimensions for arrayv1in the code or to process individual elements.

This keeps the code somewhat simpler.

Although the spreadsheet illustrates results for a three-asset portfolio, the two functions have the advantage of working with arrays of any size.