Trding Systems nd Methods, Fifth Edition By Perry J. Kufmn Copyright 2005, 2013 by Perry J. Kufmn APPENDIX 2 Mtrix Solution to Liner Equtions nd Mrkov Chins DIRECT SOLUTION AND CONVERGENCE METHOD Before computer progrms offered redy solutions, problems such s Mrkov chins were solved in direct mnner, by lgebriclly mnipulting the equtions. This direct solution requires n understnding of simple mtrix rithmetic nd very creful ttention to clculting the numbers correctly. The convergence method is now esier, lthough it requires mny more clcultions. Without the computer, we would never even consider using this pproch with computer, it is the best choice. GENERAL MATRIX FORM A mtrix is rectngulr rrngement of elements into rows nd columns. Mtrix A is sid to be m n (pronounced m by n ) if there re m rows nd n columns in A. m n = 12 1n 21 22 2n m1 m2 mn Certin properties of mtrix mke it vluble tool for solving simultneous liner equtions. These elementry mtrix opertions, clled trnsformtions, llow you to lter the rows (which will represent equtions) without chnging the solution. There re three bsic row opertions: 1. Multipliction or division of ll elements of the row by ny number. 2. Interchnging of ny two rows (nd consequently of ll rows). 3. The ddition or subtrction of the elements of one row with the corresponding elements of nother. 51
52 APPENDIX 2 To relte the mtrix to simultneous liner equtions, consider three-eqution exmple, where only the coefficients, x, remin s unknowns: x 1 + 12 x 2 + 13 x 3 = 14 (1) 21 x 1 + 22 x 2 + 23 x 3 = 24 (2) 31 x 1 + 32 x 2 + 33 x 3 = 34 (3) The three elementry row opertions cn now be interpreted in terms of simple opertions on n eqution: 1. When both sides of n eqution re multiplied or divided by the sme vlue, the results re equl. 2. Any two equtions in system of equtions cn be interchnged with no effect. 3. When two equls re dded or subtrcted, the results re equl. DIRECT SOLUTION Putting these rules into use, write the coefficients of the three simultneous liner equtions s 3 4 coefficient mtrix, 12 13 14 21 22 23 24 31 32 33 34 with the objective of reducing the mtrix to the form 1 0 0 0 1 0 0 0 1 A A A 1 2 3 which would men tht x 1 = A 1, x 2 = A 2, nd x 3 = A 3 since 1 x 1 + 0 x 2 + 0 x 3 = A 1 0 x 1 + 1 x 2 + 0 x 3 = A 2 0 x 1 + 0 x 2 + 1 x 3 = A 3 To chieve the results, divide the first eqution by the first element, leving 1 12 13 14 (2-1)
Appendix 2 53 nd then multiply by 21 to get the first elements in rows one nd two the sme: Now subtrct (1-2) from (2) nd get 0 22 21 12 21 13 21 12 21 23 13 21 14 21 24 14 21 (2-2) (2-3) Tht successfully elimintes the first element 21 from the second eqution. By going bck nd multiplying (2-1) by 31 nd subtrcting the resulting eqution from (3), 31 cn be eliminted from eqution (3). Now column 1 looks like 1 0 0 Column two or ny column cn be operted upon in the sme mnner s the first column: 1. Divide row n by the element in position n (for exmple, nn ), thereby setting nn = 1. 2. Multiply row n by the corresponding element in row i n, so tht nn in = in. 3. Subtrct row n from row i, resulting in in = 0, nd ll other elements reduced by the corresponding element in row n. Continue this procedure for ech row i until ll elements For exmple, beginning with 1n, 2n,..., i 1n, 1+1n,..., mn = 0 nd in = 1 2 4 8 34 6 5 2 22 3 6 5 30 Divide row 1 by the vlue 2 (position ): 1 2 4 17 6 5 2 22 3 6 5 30 Multiply row 1 by the vlue 6 to get (6 12 24 102) nd subtrct the new clculted row from row 2: 1 2 4 17 0 7 22 80 3 6 5 30
54 APPENDIX 2 Multiply row 1 by the number 3 to get (3 6 12 51), then subtrct the clculted row from row 3: 1 2 4 17 0 7 22 80 0 0 7 21 Column 1 is now completed. Divide row 2 by 7 nd get: 1 2 4 17 0 1 + 22 / 7 + 80 / 7 0 0 7 21 Multiply row 2 by 2 nd subtrct the result (0 2 44/7 160/7) from row 1 to eliminte position 2 in the first row: 1 0 16 / 7 41 / 7 0 1 + 22 / 7 + 80 / 7 0 0 7 21 Becuse the element in row 3 nd column 2 is lredy 0, move to row 3. Divide row 3 by 7 nd get 1 0 16 / 7 41 / 7 0 1 + 22 / 7 + 80 / 7 0 0 1 3 Multiply row 3 by 16/7 nd subtrct the result (0 0 16/7 48/7) from row 1: 1 0 0 1 0 1 + 22 / 7 + 80 / 7 0 0 1 3 Multiply row 3 by +22/7 nd subtrct (0 0+22/7 +66/7) from row 2: 1 0 0 1 0 1 0 2 0 0 1 3 The results show tht x 1 = 1, x 2 = 2, x 3 = 3. There re other wys to reduce the mtrix to the finl representtion, but this technique is well defined nd lends itself to being progrmmed on computer.
Appendix 2 55 Solution to Wether Probbilities Expressed s Mrkov Chin Let A = P(cler) i+1 = P(cler) i B = P(cloudy) i+1 = P(cloudy) i C = P(riny) i+1 = P(riny) i becuse when they converge ll ith elements will equl (i + 1)th elements. The equtions re In ddition, A = 0.7A + 0.2B + 0.2C (1) B = 0.25A + 0.6B + 0.4C (2) C = 0.05A + 0.2B + 0.4C (3) A + B + C = 1 (4) To solve this system of equtions using mtrices, convert nd dd eqution (3) to (4), which becomes the mtrix 0.3A + 0.2B + 0.2C = 0 (1 ) 0.25A 0.4B + 0.4C = 0 (2 ) 1.05A + 1.2B + 0.4C = 1 (3 ) 0.3 0.2 0.2 0 0.25 0.4 0.4 0 1.05 1.2 0.4 1 (1 ) (2 ) (3 ) The following re key steps in the solution: 1. Reduce the first row, 1 0.6667 0.6667 0 0.25 0.4 0.4 0 1.05 1.2 0.4 1 nd mke the leding entries of rows 2 nd 3 zero: 1 0.6667 0.6667 0 0 0.2333 0.5667 0 0 1.9000 1.1000 1
56 APPENDIX 2 2. Reduce the second row, 1 0.6667 0.6667 0 0 1 2.4291 0 0 1.9000 1.1000 1 nd mke the second entries of rows 1 nd 3 zero: 1 0 2.2857 0 0 1 2.4291 0 0 0 5.7143 1 3. Reduce the third row, 1 0 2.2862 0 0 1 2.4291 0 0 0 1 0.1750 nd mke the third entry of rows 1 nd 2 zero: 1 0 0 0.4000 0 1 0 0.4250 0 0 1 0.1750 Then A = 0.4000, B = 0.4250, nd C = 0.1750. Computer Progrm Direct Solution The following FORTRAN progrm ccepts system of 10 equtions in 10 vribles ( unknowns) nd pplies the mtrix method of solution. This is done using the method of Gussin elimintion. PROGRAM MATRIX C---- Mtrix solution to simultneous liner equtions C---- Copyright 1986 PJ Kufmn DIMENSION A(10,10), C(10) OPEN(6,FILE= PRN ) WRITE(*,7000) 7000 FORMAT( Enter mtrix size (n)> \) READ(*,5000)N 5000 FORMAT(BN,14) IF(N.GT.1O)STOP Mtrix limited to 10 x 10 WRITE(*,7001) (1,1=1,N)
Appendix 2 57 7001 FORMAT( Enter mtrix elements row by row under hedings + do not include constnt to right of + 7X,10(12, -xxxxx )) DO 20 I = 1,N WRITE(*,7003)1 7003 FORMAT( Row,12, : \) READ(*,5003) (A(IMP), J=1,N) 5003 FORMAT(BN,10F8.0) 20 CONTINUE WRITE(*,7004) (J,J=1,N) WRITE(6,7004) (J,J=1,N) 7004 F0RMAT(/ Input mtrix is: //9X,10(I2, -col )) DO 30 I = 1,N WRITE(6,7005)I,(A(I,J),J=1,N) 30 WRITE(*,7005)I,(A(I,J),J=1,N) 7005 FORMAT(/ Row,I2, :,10F8.3) WRITE(*,7008)(I,I=1,N) 7008 FORMAT(/ Enter constnt vector under hedings... / + 1X,10(I1, xxxxxx )) READ(*,5008)(C(I),I=1,N) 5008 FORMAT(BN,10F8.0) WRITE(*,7009)(C(I),I=1,N) WRITE(6,7009)(C(I),I=1,N) 7009 FORMAT(/ Constnt vector is: /10F8.3) C----- Process row by row (Gussin Elimintion) DO 100 I = 1,N DIV = A(I,I) DO 40 J = 1,N 40 A(I,J) = A(I,J)/DIV C(I) = C(I)/DIV C----- Zero out column I for ech row DO 60 J = 1,N IF(J.EQ.I)GOTO 60 FACT0R = A(J,I) DO 50 K = 1,N 50 A(J,K) = A(J,K) - A(I,K)*FACT0R C(J) = C(J) - C(I)*FACT0R 60 CONTINUE 100 CONTINUE WRITE(*,7007)(C(I),I = 1,N) WRITE(6,7007)(C(I),I = 1,N) 7007 FORMAT(/ Solution vector is: /10F8.3) CALL EXIT END
58 APPENDIX 2 Smple Computer Printout MATRIX Enter mtrix size (N)> 3 Enter mtrix elements row by row under hedings... do not include constnt to right of = 1-xxxxx 2-xxxxx 3-xxxxx Row 1: 2 4 8 Row 2: 6 5 2 Row 3: 3 6 5 Input mtrix is: 1-col 2-col 3-col Row 1: 2.000 4.000 8.000 Row 2: 6.000 5.000 2.000 Row 3: 3.000 6.000 5.000 Enter constnt vector under hedings., lxxxxxx 2xxxxxx 3xxxxxx 34 22 30 Constnt vector is: 34.000 22.000 30.000 Solution vector is: 1.000 2.000 3.000 CONVERGENCE METHOD This method performs series of mtrix multiplictions until the difference between the new nd previous mtrix is very smll. 1 Following the procedure in Chpter 2, Bsic Concepts nd Clcultions, we cn crete 3 3 frequency mtrix by counting the number of up, down, nd neutrl dys tht follow other up, down, nd neutrl dys. We use the term neutrl to llow very smll price chnges to be considered in this group, rther thn limit it to only those dys with zero chnges. For this exmple we will look t the number of up, down, nd neutrl dys tht follow 5-dy trend tht ws considered up, down, or neutrl on the previous dy. Suppose the results were those shown in the frequency mtrix F: Next Dy Price Chnge Up Neutrl Down Totl Previous dy trend Up 70 40 30 140 Neutrl 50 30 45 125 Down 35 45 65 145 1 George R. Arrington, Mrkov Chins, Technicl Anlysis of Stocks & Commodities (December 1993).
Appendix 2 59 Divide ech item in F by the totl given t the end of tht row, nd get the probbility of ech occurrence in trnsition mtrix T. Next Dy Price Chnge Up Neutrl Down Previous dy trend Up 0.500 0.285 0.214 Neutrl 0.400 0.240 0.360 Down 0.241 0.310 0.448 Now it is necessry to perform mtrix multipliction. To multiply mtrix A by mtrix B, we multiply the corresponding items in row i of A by the corresponding item in column j of B, dd those products together to get the item in row i, column j of the new mtrix C. If we hve two 3 3 mtrices A nd B, nd we wnted to find the element in row 2, column 1 of the new product mtrix C, we would multiply nd dd The generl formul for this is c 21 = 21 b + 22 b 21 + 23 b 31 N c = ( b ) ij ik kj k= 1 For spredsheet users, there is no generl formul to copy from one cell to nother. For mtrix A of 3 columns nd 3 rows locted in columns A, B, C, rows 1, 2, 3; mtrix B in columns D, E, F, rows 1, 2, 3; nd mtrix C in rows G, H, I, columns 1, 2, 3, we enter the formul for c in cell G1 s cell G1 = A1* D1 + B1* D2 +C1* D3 While this is clerly tedious, t lest the rithmetic will be correct. Itertive Mtrix We cn now find the solution to the Mrkov chin, the long-term probbilities, by performing series of mtrix multiplictions beginning with the trnsition mtrix. 1. Multiply the trnsition mtrix T by itself to get the first itertive mtrix I 1, I 1 = T T 2. Multiply the itertive mtrix by the trnsition mtrix to get the next itertive mtrix, I 2 = I 1 T
60 APPENDIX 2 3. Continue to multiply the lst itertive mtrix by the trnsition mtrix until the new itertive mtrix is unchnged (or very close) to the previous itertive mtrix, I n = I n 1 T When ech element stisfies the condition bs(i ij (n) I ij (n 1)) < 0.001 then the itertive mtrix holds the finl long-term probbilities of the Mrkov chin.