Download arrays

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Modified Dietz method wikipedia , lookup

Stock valuation wikipedia , lookup

Stock trader wikipedia , lookup

Transcript
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,
a 11
a
21
A


a
 r1
a 12  a 1c 
b11
b
a 22  a 2 c 
 and B   21
 

 
b

a r 2  a rc 
 i1
Sumproduct(A, B)
b12  b1 j 
b22  b2 j 


 
bi 2  bij 
 a11b11  a12b12  a13b13    arc bij
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 A  B .
Example
7 4 6 
 9 3 8
and
A
B


10 12 1
 5 2 11


Sumproduct(A, B)  a11b11  a12b12  a13b13    arc bij
 7 9   4 3   6 8     111
 208
Excel Method
1
2
3
4
5
6
7
8
Applied Regression Analysis
A
7
5
B
4
2
C
6
11
9
10
3
12
8
1
208
D
=SUMPRODUCT(A1:C2,A4:C5)
2
Prof. Juran
Transpose
Definition
For matrix A,
a 11
a
21
A
 
a
 r1
 a 11
a
12
T
A 
 
a
 1c
a 12  a 1c 
a 22  a 2 c 


 
a r 2  a rc 
a 21  a r 1 
a 22  a r 2 


 
a 2 c  a rc 
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
7 4 6 
A

 5 2 11
7 5 
A  4 2 


 6 11
T
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
1
2
3
4
5
6
A
7
5
B
4
2
C
6
11
D
E
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)
Press Shift+Ctrl+Enter, and curly brackets will appear round the formula (you can’t
type them in).
1
2
3
4
5
6
Applied Regression Analysis
A
7
5
B
4
2
7
4
6
5
2
11
C
6
11
D
E
=TRANSPOSE(A1:C2)
4
Prof. Juran
Multiplication
Definition
For matrices A and B,
a 11
a
21
A


a
 r1
a 12  a 1c 
b11
b
a 22  a 2 c 
 and B   21
 

 
b
a r 2  a rc 
 i1
 a 11b11  a 12 b21    a 1c bi 1
a b  a b    a b
21 11
22 21
2c i1
AB  


a b  a b   a b
rc i 1
 r 1 11 r 2 21
b12  b1 j 
b22  b2 j 


 
bi 2  bij 
a 11b12  a 12 b22    a 1c bi 2
a 21b12  a 22 b22    a 2 c bi 2

a r 1b12  a r 2 b22    a rc bi 2
 a 11b1 j  a 12 b2 j    a 1c bij 
 a 21b1 j  a 22 b2 j    a 2 c bij 



 a r 1b1 j  a r 2 b2 j    a rc bij 
Notes:

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
9 10
7 4 6 


A
 and B   3 12
5
2
11


8 1 
AB
 7 9   4 3  68

59   2 3  118
7 10  4 12   61 
510  2 12   111
123 124


139 85 
Applied Regression Analysis
5
Prof. Juran
Excel Method
1
2
3
4
5
6
7
8
9
A
7
5
B
4
2
9
3
8
10
12
1
123
139
124
85
C
6
11
D
E
=MMULT(A1:C2,A4:B6)
Remember:

Select the entire range A8:B9 before typing the formula.

Press Shift+Ctrl+Enter.
You can also get the same results using SUMPRODUCT:
1
2
3
4
5
6
7
8
9
10
11
12
A
7
5
B
4
2
9
3
8
10
12
1
C
6
11
D
E
F
9
10
G
3
12
H
I
8
1
=TRANSPOSE(A4:B6)
=SUMPRODUCT(A1:C1,E1:G1
)
Using SUMPRODUCTs
=SUMPRODUCT(A1:C1,E2:G2)
123
124
139
85
=SUMPRODUCT(A2:C2,E2:G2)
=SUMPRODUCT(A2:C2,E1:G1)
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:
 1 0  0
0 1  0 

I

 
0 0  1


This 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 A 1 ) such that:
AA1
 Ij
Note:
Not all square matrices can be inverted, a fact that has implications for regression
analysis.
Example
123 124
If A  

139 85 
 0.01254 0.01829 
Then A 1  

 0.02050  0.01814
Because
AA1
123  0.01254   124 0.02050 

 139  0.01254   85 0.02050 
1230.01829   124  0.01814 
139 0.01829   85 0.01814  
 1 0


0 1
 I2
Applied Regression Analysis
7
Prof. Juran
Excel Method
1
2
3
4
5
6
A
B
123
139
124
85
-0.01254 0.01829
0.02050 -0.01814
C
D
check
1
0
E
0
1
F
G
H
=MMULT(A2:B3,A5:B6)
=MINVERSE(A2:B3)
Remember:

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:
A
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Mean return
Variance of return
StDev of return
B
Stock 1
0.140
0.200
0.447
C
Stock 2
0.110
0.080
0.283
D
Stock 3
0.100
0.180
0.424
E
F
Stock 1
1.00
0.80
0.70
Stock 2
0.80
1.00
0.90
Stock 3
0.70
0.90
1.00
Stock 1
0.333
Stock 2
0.667
Stock 3
0.000
Actual
0.120
Required
=SUMPRODUCT(B2:D2,B14:D14)
>=
0.120
Correlations
Stock 1
Stock 2
Stock 3
G
H
I
J
Stock 1
0.2000
0.1012
0.1328
Stock 2
0.1012
0.0800
0.1080
Stock 3
0.1328
0.1080
0.1800
Covariances
Stock 1
Stock 2
Stock 3
Investment decision
Fractions to invest
Total
1
=
Required
1
Expected portfolio return
Portfolio variance
Portfolio stdev
0.103
0.321
=MMULT(B14:D14,MMULT(H8:J10,TRANSPOSE(B14:D14)))
=SQRT(B20)
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
 1x1  2 x2  3 x3
=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:
A  x1
x2
x3   0.333 0.667 0.000
COV11 COV12 COV13  0.2000 0.1012 0.1328
B  COV21 COV22 COV23   0.1012 0.0800 0.1080

 

COV31 COV32 COV33  0.1328 0.1080 0.1800
Applied Regression Analysis
9
Prof. Juran
Recall that:

The covariance of a random variable with itself is its variance. For example,
COV11   12

The covariance of two random variables is independent of order. For example,
COV13  COV31
Now the explicit calculation:
MMULT(B14:D14,MMULT(H8:J10,TRANSPOSE(B14:D14)))
 ABAT 
 x1
 x1
(1)
(2)
x2
 COV11 COV12 COV13   x1  


x3  COV21 COV22 COV23   x2  

 
 COV

COV
COV
31
32
33

  x3  

(3)
x2
 COV11x1  COV12 x2  COV13 x3  


x3  COV21x1  COV22 x2  COV23 x3  


 COV x  COV x  COV x  
31 1
32 2
33 3  

 x1COV11x1  x1COV12 x2  x1COV13 x3 
(4)
 x2COV21x1  x2COV22 x2  x2COV23 x3 
 x3COV31x1  X 3COV32 x2  x3COV33 x3 
 x1COV11x1   x2COV22 x2   x3COV33 x3 
(5)
  12 x12   22 x22   32 x32  2 x1 x2COV12  2 x2 x3COV23  2 x1 x3COV13
(6)
 0.447 2 0.3332  0.28320.667 2  0.424 2 0.0002
(7)
 x1COV12 x2  x2COV21x1   x2COV23 x3  x3COV32 x2   x1COV13 x3  x3COV31x1 
 20.3330.667 0.1012  20.667 0.0000.1080  20.3330.0000.1328
 0.10275
(8)
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