for each row é until all elements -
Gans Gans © + Barn, Are amy + + Fan =O and Gn =i.
APPENDIX 3 - 653
For example, beginning with
2 4 8 346 5 2 223 6 5 30
Divide row 1 by the value 2 (position a;,):
124 176 5 2 223 6 5 30
Multiply row 1 by the value 6 to get (6 12 24 102) and subtract the new calculated rowfrom row 2:
1 2 4 170 -7 -22 -803 6 5 30,
Multiply row 1 by the number 3 to get (3 6 12 51), then subtract the calculated rowfrom row 3:
1 2 4 170 -7 -22 -800 0 -7 -21
Column 1 is now completed. Divide row 2 by ~7 and get:12 4 70 1 422/7 +80/7o 0 7 -21
Multiply row 2 by 2 and subtract the result (0 2 44/7 _160/7) from row 1 to eliminateposition 2 in the first row:
1.0 -16/7 -41/7O 1 +22/7 +80/7o 0 7 21
Because the element in row 3 and column 2 is already 0, move to row 3. Divide row 3 by~7 and get -
1 0 -16/7 ~41/7 _ -O 1 +22/7 +80/7 7o 0 1 3,
Multiply row 3 by -16/7 and subtract the result (0 0 -16/7 —48/7) from row 1:
10 ° 1 .O 1 +22/7 +80/7 . ct0 0 1 3, 
654 APPENDIX 3
Multiply row 3 by +22/7 and subtract (0 0 +22/7 +66/7) from row 2:
1001010 2O00 1 3
The results show that x, = 1, x, = 2, x;=3.There are other ways to reduce the matrix to the final representation, but this tech-nique is well defined and lends itself to being programmed on a computer.
Solution to Weather Probabilities Expressed as a Markov Chain
Leta = P(clear),,, = P(clear),B = P(cloudy),, ; = P(cloudy),C=P(rainy),, 1 = P(rainy),
because when they converge all ith elements will equal (¢ + 1)th elements. The equationsare
A= 7A+.2B+.2C (2) :B=.25A+.6B+4C (2)C=.05A+.2B+.4C (3)
In additionA+Bt+e
 
1To solve this system of equations using matrices, convert and add equation (3) to (4),-3A4.2B+.2C=0 (1).25A-.4B+.4C=0 (2)1.0544+12B+.4C=1 (3)which becomes the matrix-3 2 20) @)25 -4 40) @)105 12 4 If @)
 
The following are key steps in the solution:
1, Reduce the first row, -
1 -.6667 -.6667 025 -4 4 0} > “1.05 1.2 ALand make the leading entries of rows 2 and 3 zero:
{1 ~6667 -.6567 0 -0 ~.2333 5667 00 1.9000 1.1000 1,
APPENDIX 3 - 655
2. Reduce the second row,
1 -.6667 -.6667 00 1 -2.4291 00 1.9000 1.1000 1and make the second entries of rows 1 and 3 zero1.0 2.2857 0)O 1 -2.4291 00 0 5.7143 1.
3. Reduce the third row,
1 0 2.2862 00 1 -2.4291 0Oa) 1.1750,
and make the third entry of rows 1 and 2 zero:
1 0 0 .40000 1 0 | .4250Oo 0 1.1750,
Then A =".4000, B = .4250, and C= .1750.
Computer Program Direct Solution
‘The following FORTRAN program accepts a system of 10 equations in 10 variables(unknowns) and applies the matrix method of solution. This is done using the method ofGaussian elimination, discussed in the previous section.
PROGRAM MATRIX
Matrix solution to simultaneous linear equations
Copyright 1986 PJ Kaufman
DIMENSION A(10,10), C(10)
OPEN(6,FILE=" PRN’)
WRITE(*,7000)
7000 FORMAT(’ Enter matrix size (n)>‘\)READ(*,5000)N
5000 FORMAT(BN,14)IF(N.GT.10)STOP ‘Matrix limited to 10 x 10’WRITE(*,7001) (1,1=1,N)
7001 FORMAT(' Enter matrix elements row by row under headings
 
~ + * do not iaclude constant to right of+ TX, 1O(12, "= xxxxx"))00 201 = 1,N
- WRITE(*,7003)17003 FORMAT(' Row',12,°:‘S)READC*,5003) (ACI,J), J=1,N)5003 FORMAT (BN, 10F8.0)20 CONTINUEWRITEC*,7004) (o,d=2,N) -WRITE(6,7004) (J,J=1,N)7004 FORMAT(/* input matrix is:'//9X,10(12,"-col ‘))|
 
656 APPENDIX 3
DO 30 1 - 1,N -WRITE(6,7005)1,¢AC1,J),J™1,N)30 WRITE(™,7005)1, (ACT, J) ,J=2,N) 17005  FORMAT(/ Row’, i2,":", 10F8.3)WRITEC*,7008)(1,[=1,N)7008 FORMAT(/’ Enter constant vector under headings.+ IX, LOCTL, "xxxxxx 3)READ(* 5008) (C(I), P=1,N)5008 FORMAT(BN,10F8.0)WRITEC*,7009) (CCL), T=1,N)WRITE(6,7009) (CCL), T=1,N)7009 FORMAT(/’ Constant vector is:'/1OF8.3--- Process row by row (Gaussian Elimination)00 100 1 = 1,NDIV = ACI, fT)DO 40 J = 1,N40 ACT,0) = ACT, 2) /01¥CCL) = CCE) /OIV~ Zero out column f for each row00 60 J = 1,NIF(J.E0.1)G0TO 60FACTOR = ACJ,1)00 50 K = I,N
 
c
 
50 ACQ,K) = ACJ, K) = ACL, K)*FACTORC(d) = Clg) ~ CCE) *FACTOR60 CONTINUE
100 CONTINUE
WRITE(*,7007)(CCI), 1 = 1,N)
WRITE(6,7007)(CCI),L = 1,N) :7007 FORMAT(/* Solution vector is: ‘/10F8.3)
CALL EXIT
END
Sample Computer Printout
MATRIXEnter matrix size (N)>3 |Enter matrix elements row by row under headings.do not include constant to right of =
LeXKXKK 2° XXKKX 3 = XXX.
Row l: 2 4 aRow 2: 6 5 2Row 3: 3 6 5Input matrix is:"= Arcot 2-col 3-colRow 1: 2.000 4.000 8.000 _Row 2: 6.000 $.000 2.000 _ _—Row 3: 3.000 6.000 5.000 “7
Enter constant vector under headings. -LXXXXK 2XXAXKX BKK34 22 30
Constant vector is34.000 22.000 30.000 ~
Solution vector is:1.000 2.000 3.000
APPENDIX 3 657
CONVERGENCE METHOD
This method performs a series of matrix multiplications until the difference between thenew and previous matrix is very small.’ Following the procedure in Chapter 2, “Basic Con.cepts,” we can create a 3 x 3 frequency matrix by counting the number of up, down, andneutral days that follow other up, down, and neutral days. We use the term neutral toallow very small price changes to be considered in this group, rather than limit it to onlythose days with zero changes. For this example we will look at the number of up, down,and neutral days that follow a 5-day trend that was considered up, down, or neutral on theprevious day. Suppose the results were those shown in the frequency matrix F:
CL
 
Next Day Price ChangeUp Neutral Down ‘Total———$____________ Pr _Newtrel Down TotalPrevious day trend Up 70 40 30 140Neutral 50 30 45 125Down 35 45 65 145
Divide each item in F by the total given at the end of that cow, and get the probability ofeach occurrence in a transition matrix T.
—Next Day Price Change
Up Neutral DownNeutra Dow
 
Previous day trend = Up 500 285 214Neutral 400 240 360Down 241 310 8
 
SEE SS
Now it is necessary to perform matrix multiplication. To multiply matrix A by matrix B, wemultiply the corresponding items in row i of A-by the corresponding item in column j of B,add those products together to get the item in row 4, column j of the new matrix C. If wehave two 3 x 3 matrices A and B, and we wanted to find the element in row 2, column 1 ofthe new product matrix G, we would multiply and add
C2 = G21 X Oy, + Gz X bn + Gry X B31
The general formula for this is
cy= > GuX by)
m
 
For spreadsheet users, there is no general formula to copy from one cell to another, Formatrix A of 3 columns and 3 rows located in columns A, B, C, rows 1, 2, 3; matrix B incolumns D, £, & rows 1, 2, 3; and matrix C in rows G, H, J, columns 1, 2, 3, we enter theformuia for cy, in cell G1 as
cell G1 = A1*D1 + B1*D2 + C1*D3
While this is clearly tedious, at least the arithmetic will be correct.
“George R. Arrington, “Markov Chains," Technical Analysis of Stocks & Commodities (December 1993). 
658 | APPENDIX 3oeIterative Matrix .
We can now find the solution to the Markov chain, the long-term probabilities, by per-forming a series of matrix multiplications beginning with the transition matrix.
1. Multiply the transition matrix T by itself to get the first iterative matrix J,,
=TxT2. Multiply the iterative matrix by the transition matrix to get the next iterative matrix,h=xT
the Jast iterative matrix by the transition matrix until the new
. Continue to multiply u L5 a o the previous iterative matrix,
iterative matrix is unchanged (or very close) tIn= Iq XT
‘When each element satisfies the condition@abs(Iy(m) ~ Iy( ~ 1) < 001
then the iterative matrix holds the final long-term. probabilities of the Markov chain.
- APPENDIX 4|
Trigonometric Regressionfor Finding Cycles
The following computer programs and examples solve the cycle problems in Chapter 8
SINGLE-FREQUENCY TRIGONOMETRIC REGRESSION
The FORTRAN program TRIG1 and the subroutine LiNREG are used to find the single-frequency representation of the copper cycle. The program output is clearly separated intothe following information:1, Input data, where each time period is the average cash price for a calendar quarter.2. The solution to the linear regression, giving the detrending line. With b = .267,there is an inflationary bias of +.267¢ per quarter.3. The detrended data resulting from subtracting the line values (2) from the originaldata (1).4, Intermediate values for a, @, and T5, The constant values a and b for the normal equations solving the single-frequencyproblem.6. The cycle resulting from the detrended data.7. The final cycle with the trend added back.The results show a copper cycle of approximately 8.4 quarters, or slightly more than 2%years.‘An additional test was run on monthly cash corn prices from 1964 through 1983 to seeif the seasonal cycle dominated the detrended pattern. The linear regression equationused for detrending was calculated as:Y= .939+ Oleshowing only a 1¢ per bushel per month rate of inflation, despite the bull markets in 1973and 1980 through 1981. The cycle showed a period of 21.4 months, with the last highs inthe cycle in August 1983 and the last lows in September 1982. Because this is clearly not aseasonal pattern, it must be either:. Dominated by other supply-demand characteristics, such as stocks, or,. Distorted by the nonseasonal rallies of 1973 and 1980, which each took three yearsto return to the traditional seasonal pattern.PROGRAM TRIGL -
Single Frequency Trigonometric RegressionCopyright 1986 PU Kaufman
DIMENSION X(250), ¥(259), D(250), R(250)QOUBLE PRECISION SC2,SCO
DATA MAX/250/
ne
 
OPEN(6, FILE=' PRN")
°
Read data, set X to incremental timeTe2WRITE(*,7000) 
 
 
660 - APPENDIX 4BOO
7000 FORMAT(' SINGLE FREQUENCY TRIGONOMETRIC REGRESSION’+ ‘ Enter data 1 per line'/' Extra <return> ends input'//
+ * Enter below:")
10 WRITE(*,7001)17001 FORMAT(14,'>"\)READ(*,5000)¥(1)5000 FORMAT(BN,F8.2)x(1) = 1TF(Y(1) .NE.O) THENIF(I.GT.MAX)STOP ‘Maximum data exceeded[= T4LGOTO 10ENOTFN= 1-1WRITE(6, 6000)6000 FORMAT('1Single Frequency Trigonometric Regression’ ,20X,+ ‘Data Input'//' Time',16X,‘Prices")DO 40 J = 1,N,440 WRITE(6,6001)0,(Y(1), 1=0, 043)6001 — FORMAT(14,4F8.2)
Linear regression analysis for detrendingCALL LINREG(N,Y,A,B, SD)WRITE (6, 6002)A,B6002 FORMAT(/ ‘Linear regression resuTts: A =",F8.3,', B =',F8.3)Detrend data into 0 and computer sums for equation (4)00 60 1 = 1,N ~OCT) = YCE) = (AtB*1)+ Print detrended dataWRITE(6, 6003) .6003 FORMAT(/* Detrended data'/)00 65 1 = 1,N,465 WRITE(6,6001)1,(D(J},deI, 1+3)sc2 = 0sco = 0(---- Solve equation (4) using detrended dataDO 70 1 = 2,N-1 -DI = D(T) ‘Sc2 = SC2 + DI*DI70 SCD = SCD + DI*(D(T-1)+D¢1+1))
 
 
 
ALPHA = SCO/SC2WRITE( 6, 6004)SC2,SCD, ALPHA
6004 FORMAT(/*Sum C-squared ~’,F8.1,', Sum C x D =',F8.1,", Alpha =",+ F8.3) -
C---- Solve for omegaW = ACOSCALPHA/2)T + 360/W - -WRITE(6,6014)W,T
6014 FORMAT(/*Omega (W) =',F5.1,' degrees, Period (T) =",F6.2
+ ‘time units’)
C---+ Sums for normal equationscos2 = 0COSSIN = 0YCOS = 0~ SINCOS =SIN2 = 0YSIN = 0
0 - . -
APPENDIX 4 661
 
Do 80 1 = 1,N
C = cOS(w*I)S = SINCW*I)cos2 = cos2 + c*cCOSSIN = COSSIN + C*SYCOS = YCOS + Y(I)*CSINCOS = SINCOS + S*CSIN2 = SIN2 + S*S
80 -YSIN = YSIN + Y(T)*S
C-++- Solve normal equationsTB = (YSIN*COS2 - YCOS)/(SIN2*COS2 - COSSIN)TA = (YCOS - B*COSSIN)/COS2WRITE(6,6005)TA, TS6005 FORMAT(/” Solution to normal equations: A =',F8.3,', B =",F8.3
C---- Values of fitted curve using detrended dataDO 90 I = 1,N90 R(T) = TA*COS(W*I) + TB*SIN(W*I)WRITE(6, 6006)6006 FORMAT(/' Trigonometric regression results using detrended data’,+ 1)00 100 1 = 1,N,4100 WRITE(6,6001)1,(R(J),J=1, 1+3) .
 
Add trend back to result, 00.110 T= 1,810 RCL) = R(T) + A + BST
WRITEC6 , 6007)
6007 FORMAT(/* Final regression results with trend added back'/)00 120 1 = 1,N,4
120° WRITE(6,6001)I1, (R(J),J=1,1+3)
CALL EXITENO
SUBROUTINE LINREG(N,OATA,A,8,S0)- Generalized simple linear. regression
DIMENSION DATA(2)
 
 
Initialize sumsSX=0.sY=0.
 
S0=0.IFCN.LE.2)RETURN
00 100 I=2,N ~X=1 -Y = DATACI)SX=SK+K -SY=SY+YSKY=SXY+¥*XSK2=SX24K*X,100 CONTINUEMeN=1 .Be(M*™SXY-SX*SY) / (M*SX2-SK*SX) ~A=(SY-B*SX) / MAPPENDIX 4 APPENDIX 4 - 663
 
   
 
. Trigonometric regression results using detrended data- Restauals 1 81 1.78 1.80 86Seo to ee. 5 1.66 1.88 = -1-10200 rach) 9 1.50 1.92 1.32Hah versen) 13 “1.31 -1.92 0 -2.51| Bor ee Vv 1.09 «1.88 1.66200 CONTINUE a 83 13 Le: : 68 1.87SD=SQRT(SSR/M) 29 -.320 0 -1.53 2.92RETURN 33 1041.34 3.92END 37 2300-113 -1.894L 2.51 189 «1.82Trigonometric Regression 45 v7 7664-1671single Frequency Trigo 49 Loe one]time prices 53 1.24.09 -1.38! 1 22.12 22.46 22.17 22.00 57 1.4400 -.1900 «1.175 23.18 24.56 25.57 30.89 61 1.61 +469 28.23 33.77 35-90 40.08 65 71.83) -1.75 0.7213 set 35.30 30.36 Sez Final regression results with trend added back7 . . : : 1 9. : : :2 39.75 30.07 . 29.08 32.13 5 re 38 3 33 3 3 3525 38.94 42.95 43.38 46.23 9 31.57 33.06 33.75 33.4129 47.70 46.98 35-78 27.35 : : 13. 32.37 31.32 30.98 31.6633 25.40 29.45. 27.15 28.48 17 33,15 34.79 35.85 38.9037 32.74 33.53 30.01 29.28 21 38.06 33.92 33.23 33.5241 36.82 45.07 $5.13 65.51 i 25 34.75 36.43 37.79 38.24— 4 66.56 70.06 27.30 35.62 29° «(37-70 -«36.58 35.64 35.5249 32.06 31.46 35.78 36.86 3336.43 «38.02 39.59 40.4353 38.22 43.24 45.46 38.9 37° 40.25 «39.28 «= 38.18 37.6957 37.08 38.72 34.01 33.00 41 38.21 39.60 41.27 42.4661 35.07 40.23 41.63 44.95 45 42.68 41.95 40.81 40.0165 1.12 63.71 59-56 63.38 43 40.12 41.23. «42.89 44.3453 44.96 44.56 43.49 42.47Linear regression results: A ~ 28-889, 8 = .267 57 42.19 42.95 44,47 46.09‘ 61 47.09 47.07 «46.18 45.05Detrended data 65 44.42 44.78 46.07 47.74L 7.08 8-96 es an . TWO-FREQUENCY TRIGONOMETRIC REGRESSION9 -3.06 2.21 4.07 7.95 ‘The FORTRAN program TRIG2 and its subroutines LINREG (found in Appendix 4) and MTX13 13.86 18.85 Ae 2.19 are used to find the two-frequency representation of the copper cycle. The program out-th 398 =4.70 -5.96 “3.17 put is clearly separated into the following steps.25 3.37 7A 7.28 9.86 _ 1. Input data, where each time period is the average cash price for a calendar quarter.29 11.06 10.07 8 8 a 2. The solution to the linear regression, giving the detrending line.38 13 Bet “79130 =10.33 3. The detrended data resulting from subtracting the line values (2) from the originalaL =3.02 4.96 14.78 24.86 - data (1).45 25.65 28.88 714.15 =6.10 4, Intermediate values for 04, O2, ©), and @2.49 =9_92 -10.79 -6.77 -6.32 5. Resulting values ay, 61, 42, and b;, which arederived from the matrix solution using53 -4,83 = 08 1.87 18 oc : Gaussian elimination. ~87 -7.04 5.67 10.68 ga 6. The cycle resulting from the detrended data.8 "10-32 3 Boy 16.32 7. The final cycle with the trend added back.
PROGRAM TRIG2- 2-Frequency Trigonometric Regression- Copyright 1986 Pd Kaufman
Omega (W) = .7 degrees, Period (T) “480.50 time units DIMENSION X(250), ¥(250); 0(250), R(250), A(4,4), B(4) aa
Sotution to normal equations: A= -.603, B = 1.831 EQUIVALENCE (A(1,1),C2W1),(AC1,2),CW1SW1), (ACL,3) ,CWLCW2),
sum C-squared = 6236.4, Sum C x 0 = 9282.2, Alpha = 1.464
   
 
664+ (ACL, 4) sCWISW2),(A(Z,13,DUPL), (AC2,2) ,S2WL)+ (AC2,3)  SWICH2) , (ACZ,, 4), SHTSW2}, CAC3, LD, DUP2),+ {AC3,2) ,DUP3) , (AC3,3) ,C2WZ) , (AC3, 4), CH2SH2?+ (ACA, 1), DUPS), (A(4, 2), DUPS), CAC, 3), DUP6),+ (ACH, 4) )S2W2), (BCL}, YC), (BC2) ,YSWL) , (B¢3) , YCN2) ,+ (BCA), YSW2), (B(1),A1), (B(2),B1), (B(3),A2), (BC 4) ,B2)DATA MAX/250/ ,NDIM/4/OPENC6, FILE='PRN')C---- Read data, set X to incremental timeI=1WRITE(*, 7000)7000 FORMAT(’ 2-FREQUENCY TRIGONOMETRIC REGRESSION’ /+ ' Enter data 1 per line'/*Extra <return> end input'//+ * Enter below: ')
10 WRITE(*,7001)17001 FORMAT(14,">'\)READ(*,5000)¥(1)5000 FORMAT(BN,F8.2)xX) =TFCY(L) NEO) THENIF(I.GT.MAX)STOP ‘Maximum data exceeded"T= d+GOTO 10ENOTFN= 1-1WRITE(6 6000)6000 FORMAT('12-Frequency Trigonometric Regression’/+ ‘Time’ ,16X, ‘Prices*)00 40 J = 1,N,440 WRITE(S, 600190, (Y¢1), Ind, 043)6001 — FORMAT(14,4F8.2)
C---- Linear regression analysis for detrendingCALL LINREG(N,Y,ALIN,8LIN, SD)WRITE(6,6002)ALIN,BLIN
6002 FORMAT(/* Linear regression results: A =",F8.3,', B
 
»F8.3)
(---- Detrend data into 0 and computer sums for equation (4)00 60 T= 1,N60 BCE) = ¥(I) - CALINBLIN®T)C--+- Print detrended dataWRITE(6,6003)6003 FORMAT(/' Detrended data'/)00 65 1 = 1,N,465 WRITE(6, 600191, (D(J) JT, 143) -scz = 0Sco = 0scp = 0soz = 0 ~soe = 0C--++ Solve for alphal and alpha2 using detrended data