Introduction to Excel Array Functions
This document is intended to introduce the use of Excel to perform basic matrix algebra operations.
Sumproduct
Definition For matrices A and B,
a11a21Aar1a12a22ar2a1ca2carcb11b21 and Bbi1b12b22bi2b1jb2j bijSumproduct(A, B) a11b11a12b12a13b13arcbij Notes: r = i and c = j. In other words, the two matrices must have the same number of rows as each other and the same number of columns as each other. They do not need to be square matrices (where r = c and i = j).
Algebra geeks sometimes call this operation the “dot product” (to distinguish it from matrix multiplication — described later in this document), and symbolize it as AB. Example 7A5426119 and B103128 1Sumproduct(A, B) a11b11a12b12a13b13arcbij
794368111 208
Excel Method 12345691031281A75B42C611D=SUMPRODUCT(A1:C2,A4:C5)72088 Applied Regression Analysis 2 Prof. Juran
Transpose
Definition For matrix A,
a11a21Aar1a12a22ar2a1ca2carc
a11a12a1ca21a22a2car1ar2arcAT
Notes: If A is an r x c matrix, then and AT must be a c x r matrix. A does not need to be a square matrix. Example 7A5T426115211
A746
Applied Regression Analysis 3 Prof. Juran
Excel Method There is an Excel function for this purpose, called TRANSPOSE. This function is one of a special class of functions called array functions. In contrast with most other Excel functions, array functions have two important differences: They are entered into ranges of cells, not single cells You enter them by pressing Shift+Ctrl+Enter, not just Enter
12345A75B42C611DE Using the spreadsheet above as an example, we start by selecting the entire range A4:C6. Then type into the formula bar =TRANSPOSE(A1:C2)
6Press Shift+Ctrl+Enter, and curly brackets will appear round the formula (you can’t type them in).
1234567465211A75B42C611=TRANSPOSE(A1:C2)DE
Applied Regression Analysis 4 Prof. Juran
Multiplication
Definition For matrices A and B,
a11a21Aar1a12a22ar2a1ca2carcb11b21 and Bbi1b12b22bi2b1jb2j bija11b1ja12b2ja1cbija21b1ja22b2ja2cbij
ar1b1jar2b2jarcbija11b11a12b21a1cbi1ababab211122212ci1ABabar2b21arcbi1r111a11b12a12b22a1cbi2a21b12a22b22a2cbi2ar1b12ar2b22arcbi2Notes: It is conventional to describe the shape of a matrix by listing the number of rows first, and the number of columns second. Matrix A above is an r x c matrix, and matrix B is an i x j matrix. In this operation, it is necessary for c = i. However it is not necessary for r = j. In other words, B must have the same number of rows as A has columns, but it is not necessary for B to have the same number of columns as A has rows. The product AB will always be an r x j matrix. Example 7A54261193 and B81012 1
AB
79436859231181231391248571041261510212111
Applied Regression Analysis 5 Prof. Juran
Excel Method 1234567891231391248593810121=MMULT(A1:C2,A4:B6)A75B42C611DE Remember:
Select the entire range A8:B9 before typing the formula. Press Shift+Ctrl+Enter.
You can also get the same results using SUMPRODUCT:
123456789101112=SUMPRODUCT(A2:C2,E1:G1)A75938B4210121C611DE910F312G81HI=TRANSPOSE(A4:B6)=SUMPRODUCT(A1:C1,E1:G1)Using SUMPRODUCTs12313912485=SUMPRODUCT(A1:C1,E2:G2)=SUMPRODUCT(A2:C2,E2:G2) Applied Regression Analysis 6 Prof. Juran
Inverse
Definition First, define a square matrix Ij as a matrix with j rows and j columns, completely filled with zeroes, except for ones on the diagonal:
10I001000 1This special matrix is called the identity matrix.
Now, for a square matrix A with j rows and j columns, there may exist a matrix called A-inverse (symbolized A1) such that:
AA1 Ij
Note: Not all square matrices can be inverted, a fact that has implications for regression analysis. Example If A139Then A1123124 850.01829
0.018140.012540.02050Because
AA1
1230.012541240.020501390.01254850.020501230.018291240.01814
1390.01829850.01814100 1 I2
Applied Regression Analysis 7 Prof. Juran
Excel Method A1BCDcheckEFGH=MMULT(A2:B3,A5:B6)2123124103139850145-0.012540.01829=MINVERSE(A2:B3)60.02050-0.01814Remember:
Select the entire range A5:B6 before typing the formula. Press Shift+Ctrl+Enter.
Applied Regression Analysis 8 Prof. Juran
Appendix: Application to Financial Portfolios
Here is a spreadsheet model of a three-stock portfolio optimization problem:
A123456789101112131415161718192021Portfolio variancePortfolio stdevExpected portfolio returnActual0.1200.1030.321Required=SUMPRODUCT(B2:D2,B14:D14)>=0.120=MMULT(B14:D14,MMULT(H8:J10,TRANSPOSE(B14:D14)))=SQRT(B20)Investment decisionStock 1Fractions to invest0.333Stock 20.667Stock 30.000Total1=Required1Stock 1Stock 2Stock 3CorrelationsStock 11.000.800.70Stock 20.801.000.90Stock 30.700.901.00Stock 1Stock 2Stock 3CovariancesStock 10.20000.10120.1328Stock 20.10120.08000.1080Stock 30.13280.10800.1800Mean returnVariance of returnStDev of returnBStock 10.1400.2000.447CStock 20.1100.0800.283DStock 30.1000.1800.424EFGHIJ In cell B18 the SUMPRODUCT function is used to calculate the expected return on the
portfolio. The expected return is a function of (a) the expected returns on the three stocks and (b) the portfolio weights (fractions to invest). To be explicit:
SUMPRODUCT(B2:D2,B14:D14) =B2*B14+C2*C14+D2*D14
1x12x23x3
=0.14*0.333+0.11*0.667+0.10*0.000 =0.120
The portfolio weights are decision variables in this problem; if these change, then of course the expected return on the portfolio would also change.
In cell B20, the MMULT and TRANSPOSE functions are combined to calculate the variance of the portfolio.
For notational purposes, let’s define two matrices:
Ax1COVBCOVCOVx2112131x30.333COVCOVCOV1222320.667130.000
0.10120.08000.10800.13280.1080
0.18000.2000COV230.1012COV330.1328COVApplied Regression Analysis 9 Prof. Juran
Recall that:
The covariance of a random variable with itself is its variance. For example,
2COV111 The covariance of two random variables is independent of order. For example,
COV13COV31 Now the explicit calculation:
MMULT(B14:D14,MMULT(H8:J10,TRANSPOSE(B14:D14)))
ABAT (1) (2)
COVx3COVCOVCOVx3COVCOV112131112131COVCOVCOV122232x1x2x1COV23x2COV33x31312COV
(3)
(4)
x1x2x3xCOV22x2COV23x3211xCOV32x2COV33x331111131222x1COVx2COVx1COVx2COVx3COVx1x1COVx1x2COVx1X3COVx2x1COVx2x2COVx2x3COV1323x3x3x333
(5) (6)
(7) (8)
3233x1COVx1COV221112x1x2COVx2x2COV2122x2x3COV23x332x1x2COV12x3x3COV23x2x1COV1313x3x3COV31x1
1x12x23x32x1x2COV22222x2x3COV222x1x3COV
0.4470.33320.333220.2830.667220.4240.0000.6670.101220.6670.0000.108020.3330.0000.13280.10275
This calculation is usually presented in the form of step (6) in introductory statistics
classes, so as to avoid frightening people with the more general matrix notation.
Applied Regression Analysis 10 Prof. Juran
因篇幅问题不能全部显示,请点此查看更多更全内容