75 1.882.05 2.07 1,992.19 2.34 2.092.33 2,56 2.212.48 2,77 2.332.64- 2,95 2.452,80 3.12 2.592.98 3.27 2,733.16 3,42 2.873.34 3,56 —3,033.53 3.69 3.193.74 3.82 3.373.94 3,93 3.55-4.16 4,05 3.744.38 4,16 3.944.61 4.27 4.164.84 4,37 4,385.09 4,44 4.625.34 4,56 ‘4.875.59 4,66 5.14
r2
78,3
58.083.1
Data Input
St dev
8301,212806646
000000000000-00-0000-000000-00-00000000-00-00
WOLDMBMHMIYYIIINAHDHA DAU
DAMMAM AANMHANON REE
WLwMery4isaaaunn
Al702+346905-43
83
7186810.10.li.i.-6013.14,
20753495
2800
APPENDIX 2APPENDIX 2 - 647SSS
$s
Flot of Keoression Anatvsis
 
SOYBEANS7.327.407,507.207.09a.9F6.896.786.686.586.47 i6.376.2716
4
                
”
 
'1'ot'
 
*
meh npn tn npn nn nnn tnt
15.49 19,20 22.92 26.6
LINEAR A = 987. B= +211, C= +000APPENDIX 2
648
Plot of Kearession Analysis
 
 
 
 
xBere ne hn nner ten neeenteeenee:
 
AOFOreToTOVORNHROSON NN ET EYHMNANGHIIRISSRIBIHRSSSBLIFTLANS LGA g
SNNSS 66550058 55nH HHH KHE ESS
 
 
AyQQor ee apornrvsonnnesenBEART SR SASHA LS PBL SHSTSSdee
voeedaadad tenet tite
»23FENDINNAIMAANAA NNN
SOYBEANS +-~
11.77 15.49 19,20 22.91 26.6
8.06
YEARS
+00u
ce
sale.
Be
27h.
as
Los649
APPENDIX 2
on Analvsis
 
Plot of Regre
 
 
:ie5:x +::ty5i:.i P| «OB RELE MESSER SS #ORIATARUBNUN CES RORELGRONNN SSSR GRERE IESHPARGRBLIBTHRLSSBLGHSHAABEGN TH RRL SLIBAHR SSSR GBTHA AZGENSKA GS S666 66 SEKHKHSIAHAK EEE SEES EAH HONG358
26.6
22.91
8.06 11,77 15.49 19,20
4.34
YEARS
+000
ce
1055s
Be
+527,
ae
EXPON
 650 APPENDIX 2ENIX 2
e——
Flot of Rearession Analysis
iit
SOYBEANS
   
7.51 x47.40 ’7.30 :i
wy
4
3
*o4
* 4
* t
* t
i
x i
'
xt
t
$
:
t
1
'
'
t
'
t
   
fo pent nent ne tnt
4.54 8.06 1.77 15.49 19,20 22.91“YEARS:cURVI = 1.684, 8 =. JLI2.U = 1004APPENDIX 3|
Matrix Solution to LinearEquations and Markov Chains
DIRECT SOLUTION AND CONVERGENCE METHOD
Before computer programs offered ready solutions, problems such as Markov chains weresolved in a direct manner, by algebraically manipulating the equations. This direct solutionrequires an understanding of simple matrix arithmetic, and very careful attention to calcu-lating the numbers correctly. The convergence method is now easier, although it requiresmany more calculations. Without the computer, we would never even consider using thisapproach—with a computer, it is the best choice.
GENERAL MATRIX FORM
A matrix is a rectangular arrangement of elements into rows and columns. A matrix A issaid to be m x n (pronounced “m by n”) if there are m rows and n columns in A.
41 42 '" ay
4x Gn “' ayAmxn= |; : :
am Fm. "Amn
Certain properties of a matrix make it a valuable tool for solving simultaneous linear equa-tions, These elementary matrix operations, called transformations, allow you to alter therows (which will represent equations) without changing the solution. There are threebasic row operations:1. Multiplication or division of all elements of the row by any number.2. Interchanging of any two rows (and consequently of all rows).3. .The addition or subtraction of the elements of one row with the correspondingelements of another.To relate the matrix to simultaneous linear equations, consider a three-equation exam-ple, where only the coefficients, a, remain as unknowns:Ay, + Ay2Xq + 413%3 = Ay @ —~ aX: + AX, + 4x3%3= 42 (2)31X, + A32X2 + A33%3 = 34 (3) ~The three elementary row operations can now be interpreted in terms of simple oper-ations on an equation:1. When both sides of an equation are multiplied or divided by the same value, the re-sults are equal.2. Any two equations in a system of equations can be interchanged with no effect.3. When two equals are added or subtracted, the results are equal.
DIRECT SOLUTION
Putzing these rules into use, write the coefficients of the three simultaneous linear equa-tions as a 3 x 4 coefficient matrix,652 APPENDIX 3
 
Qn 42 G3 ay .Qn An Gry arsG31 Gyn G33 Ag
with the objective of reducing the matrix to the form1004,010 A0014;
which would mean that x, = Ay, %2, =A, and x3 = A; since
1xx,;+0x,+0x;=A;Oxx,+1X224+0X x3 = Az
Oxx,+0Xx.+1X x; =A;To achieve the results, divide the first equation by @,, the first element, leaving
( G2 a3 au4. 41 ay
} a-yand then multiply by a2; to get the first elements in rows one and two the same:
(a. BiygXGn Gi3XGn Ay ez) (1-2)an ay ayNow subtract (1-2) from (2) and get
23 24
2X 4. ay3Xa a4 X @,(0 Gy — CB XSt gen yo ) (2-1)ay - Gu ay
\That successfully eliminates the first element @,; from the second equation. By going backand multiplying (1,1) by @3, and subtracting the resulting equation from (3), a3: can beeliminated from equation (3). Now column 1 looks like
100,
Column two or any column can be operated upon in the same manner as the first column:1. Divide row n by the element in position n (for-example, @,n), thereby setting@ag = 1.2. Multipiy row 7 by the corresponding element in row 7 # 7, SO that Grn X Gin = Gin.3. Subtract row m from row i, resulting in a,, = 0, and ail other elements reduced bythe corresponding element in rown.
Continue this procedure for each row é until all elements .
Bin Grn ++) Ain Bisiny +1 Amn FO and — Aw = 1APPENDIX 3 - 653
 
For example, beginning with
2 4 8 346 5 2 223 6 5 30
Divide row 1 by the value 2 (position @;):
124176 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 17
0 -7 -22 -80
3 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 17O 1 +22/7 +80/70 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 -167 -41/7O 1 +22/7 +80/70 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 7 —0 1 +22/7 +80/7 ~0 0 1 3
Multiply row 3 by -16/7 and subtract the result (0 0 -16/7 -48/7) from row 1:
10 0 1 .0 1 422/77 +80/7 . L0 0 1 3654 APPENDIX 3
 
Multiply row 3 by +22/7 and subtract (0 0 +22/7 +66/7) from row 2:
010 21 3
The results show that x, = 1, x2 = 2, x3=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.
oororeo
Solution to Weather Probabilities Expressed as a Markov Chain
Let A= P(clear);,; =P(clear),B= P(cloudy);,, = P(cloudy),C= P(rainy),,; = P(rainy),
because when they converge all ith elements will equal (é + 1)th elements. The equationsare
A=.7A+.2B+.2C  ()).. 'B=.25A+.6B+.4C (2)C=.05A+.2B+.4C (3) ¢Tn additionA+B+C=1 (4)To solve this system of equations using matrices, convert and add equation (3) to (4),-3A+.2B4.26=0 (1).25A-.4B+.4C=0 (24)1.054+1.2B+.4C=1 (3)which becomes the matrix( -3 2 2 | qa)25 -4 4 0 (2+)105 12 41 B+)The following are key steps in the solution: :1. Reduce the first row, ~
1 -.6667 -.6667 0 L25 —4 4 0} ° “1.05 1.2 41 -
and make the leacing entries of rows 2 and 3 zero:
{1 ~6667 -.6667 00 ~.2333 .5667 0
0 1.9009 1.1000 1APPENDIX 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 00 1 -2.4291 00 0 5.7143 1
3. Reduce the third row,
1 0 2.2862 00 1 -2.4291 00 0 1.1750
and make the third entry of rows 1 and 2 zero:
1 0 0 .40000 1° 0.4250001
+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 MATRIXMatrix solution to simultaneous Tinear equationsC--+- Copyright 1986 PJ KaufmanDIMENSION A(10,10), €(10)OPEN(6,FILE="PRN' )WRITE(*, 7000)7000 FORMAT(' Enter matrix size (n)>*\)READ(* ,5000)N5000 FORMAT(BN,14)IF(N.GT.1LO)STOP ‘Matrix limited to 10 x 10WRITE(*,7001) (1,1=1,N)7001 FORMAT(' Enter matrix elements row by row under headings
 
_ + ‘do not include constant to right of+ 7X, 10(12, *-xxxxx"))dO 20 I = 1,N
- WRITE(*,7003)17003 FORMAT(' Row’,12,':'\)READ(*,5003) (A(I,d), J=1,N)5003 FORMAT(B8N, LOF8.C)20 CONTINUEWRITE(*,7004) (J,J=2,N) . 7WRITE(6,7004) (J,J™1,N)7004 FORMAT(/* Input matrix is:'//9X,10(12,*-col ‘))656 APPENDIX 3
ee
00 30 [= 1,N .WRITE(6,7005)1,(A(1,J),J=1,N)30 WRITE(*,7005)1,(ACI,J),d=1,N)
7005 FORMAT(/‘ Row',12,':',10F8.3)WRITE(*,7008)(1,[=1,N)
7008 FORMAT(/' Enter constant vector under headings..'/
+ LX, LOC TL, Xxxxxx '))READ(*,5008)(C(I),f=1,N)
5008 FORMATCBN, 1OF8.0}WRITE(*,7009)(C{1),I=1,N)WRITE(6,7009)¢C{I),I=1,N}
7009 FORMAT(/' Constant vector is:'/10F8.3)
C----- Process row by row (Gaussian Elimination)CO 100 1 = 1,NOly = ACT,1)00 40 J = 1,N40 ACT,J) = ACT, J)/01Vc(t) = C(I)/Olv.C--+-- Zero out column [ for each row00 60 J = 1,N1F(J.E0.1)G0TO 60FACTOR = A(J,!)
00 50 kK = 1,N
50 ACI, K} = ACI,K) = ACT, K)*FACTORC(J) = C(I) - CCL)*FACTOR
60 CONTINUE .
100 CONTINUEWRITEC*, 7007) (CCT), = 1,N)WRITE(6,7007)(CC(T),T = 1,N)7007 FORMAT(/' Solution vector is: '/10F8.3)CALL EXITEND
Sample Computer Printout
MATRIXEnter matrix size (N)>3 . xEnter matrix elements row by row under headings...do not include constant te right of =T+ XXXXX_2°XXKXK BOXXX
Row l: 2 4 8Row 2: 6 5 2Row 3: 3 6 5 -Input matrix is:"= iscol 2-co} 3-colRow 1: 2,000 4.000 8.000Row 2: 6.000 5.000 2.000 ~ _Row 3: 3.000 6.000 » 5,000 Te
€nter constant vector under headings... -LXXXXXX 2XXXXXX BXXXXXK34 22 30
Constant vector is:34.000 22.000 30,000 ~
Solution vector is:1.000 2.000 3.000APPENDIX 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 neutra/ 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:
 
 
Next Day Price ChangeUp Neutral 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 row, and get the probability ofeach occurrence in a transition matrix T. .
 
Next Day Price ChangeUp Neutral Down
 
Previous day trend Up 500 285 214Neutral 400 .240 360Down 241 310 A448
 
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 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
C21 = G21 X By + Aza X Bay + Gay X B35,The general formula for this is
cy= >) Ga by)
aotFor spreadsheet users, there is no general forrnula to copy from one cell to another. Formatrix A of 3 columns and 3 rows located in columns 4, 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 theformula for ¢,, in cell GI 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).65a” APPENDIX 3
 
iterative 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 /,,=TxT
2. Multiply the iterative matrix by the transition matrix to get the next iterative matrix,Lh=I,xT
3. Continue to multiply the last iterative matrix by the transition matrix until the newiterative matrix is unchanged (or very close) to the previous iterative matrix,
I=. XT‘When each element satisfies the condition
@abs(Iy(r1) ~ I(t - 1)) <.001then 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 7RIGI 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 5 = .267,there is an inflationary bias of +.267¢ per quarter.3. The detrended data resulting from subtracting the line values (2) from the originaldata(i). .4, Intermediate values for &, , and T.5. The constant values @ 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:
1. Dominated by other supply-demand characteristics, such as stocks, or,
2. Distorted by the nonseasonal rallies of 1973 and 1980, which each took three yearsto return to the traditional seasonal pattern.PROGRAM TRIGL -
C---- Single Frequency Trigonometric RegressionC---- Copyright 1986 PJ Kaufman
DIMENSION X(250), ¥(259), D¢250), R(250)DOUBLE PRECISION SC2,SCD
DATA MAX/250/OPEN(6,FILE="PRN")
C---- Read data, set X to incremental timeTeiWRITE(*,7000)660 -
ee
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)Y¥(1)5000 FORMAT(BN,FB.2)X{1) = 1IF(Y(1) .NE.O)THENIF(L.GT.MAX)STOP ‘Maximum data exceeded1 = [+1GOTO 10ENOIFN= 1-1WRITE(6, 6000)6000 FORMAT('1Single Frequency Trigonometric Regression’ ,20X,+ “Data Input'//' Time',16X,'Prices')D0 40 J = 1,N,440 WRITE(6,6001)J,(Y(1),1=d,J+3)6001 FORMAT(14,4F8.2)
C---- Linear regression analysis for detrendingCALL LINREG(N,Y,A,B,S0)WRITE(6,6002)A,BFORMAT(/*Linear regression results: A =',F8.3,', B =',F8.3)Detrend data into D and computer sums for equation (4)00 60 I = 1,N60 OU) = Yet) - (A+B*1)C---- Print detrended dataWRITE(6, 6003)6003 FORMAT(/’ Detrended data'/)00 65 I = 1,N,465 WRITE(6,6001)1,(0(J),J=1,1+3)sc2 = 0scd = 0C---- Solve equation (4) using detrended data00 70 I = 2,N-1 °Ol = OCI)S$C2 = $C2 + DI*DI70 SCD = SCD + DI*(D{1-1)+D¢1+1))
 
ALPHA = SCD/SC2WRITE(6,6004)SC2,SCD,ALPHA6004 FORMAT(/'Sum C-squared =',F8.1,', Sum C x D =',F8.1,', Alpha ="+ F8.3) . ~C---- Solve for omega .W = ACOS{ALPHA/2)T = 360/WWRITE(6 ,6014)W,T6014 FORMAT(/" Omega (W) =',F5.1,' degrees, Period (1 =',F6.2,+ "time units’)
C---- Sums for normal equationscos2 = 0COSSIN = 0 .Ycos = 0 -~SINCOS = 9 . .SIN2 = 0YSIN = 0APPENDIX 4 661
 
 
6005C----
90
6006
00 80 1 = 1,NC = COS(W*I)S = SIN(W*I)Ccos2 = COS2 + C*CCOSSIN = COSSIN + C*SYCOS = YCOS + Y(1)*CSINCOS = SINCOS + S*CSIN2 = SIN2 + S*SYSIN = YSIN + Y(I)*S
Solve normal equations
TB = (YSIN*COS2 - YCOS)/(SIN2*COS2 - COSSIN)
TA = (YCOS - B*COSSIN)/COS2
WRITE(6 ,6005)TA, TB
FORMAT(/* Solution to norma) equations: A =',F8.3,', B =',F8.3)
Values of fitted curve using detrended data00 90 1 = 1,NRCI) = TA*COS(W*I) + TB*SIN(W*I)WRITE(6, 6006)FORMAT(/' Trigonometric regression results using detrended data’,
+ /)
100C---
“110
6007
120
ee
C----
100
00 100 I = 1,N,4WRITE(6,6001)1,(R(J),J=1, 1+3) '
Add trend back to result00 110 [ = 1,NRCL) = RCI) + A + BI
WRITE(6 , 6007)FORMAT(/' Final regression results with trend added back'/00 120 1 = 1,N,4
WRITE(6,6001)1, (R(J),d=1,1+3)
CALL EXITEND
SUBROUTINE LINREG(N,DATA,A,B,SD)Generalized simple linear. regression
OLMENSION DATA(2)
Initialize sums
SX=0.
SY=0.
SXY=0.
SX2=0.
A=0o
B=0.
SD=0.
IF(N.LE.2)RETURN -
00 100 I=2,N ~X= 1 7Y = DATA(I)SX=SX+X - -SY=SY+YSXY=SXY+Y *XSX2=SX2+X*XCONTINUEM=N-1 weB=(M*SXY-SX*SY) / (M*SX2-SX*SX)A=(SY-B*SX) / MAPPENDIX 4
A UES RRC
200
Single
Time
Linear regression results: A = 28.889, 8 = .267
Detrended data
61
Sum C-squared = 6338.4, Sum C x D = 9282.2, Alpha = 1.464Omega (W) = .7 degrees, Period (T) =480.50 time units
Solution to normal equations: A = -.603, 8 = 1.831
Frequency Trigonometric Regression
ResidualsSSR=0DO 200 1=2,NY=DATA(I)R=Y-(A+8*1)SSR=SSR+R*RCONTINUESD=SQRT(SSR/M)RETURNENDPrices22.12 22.46 22.23.18 24.56 25,28.23 33.77 35.46.22 51.48 * 40.36.51 29.30 30,39.75 30.07 | 29.38.94 42,95 43.47.70 46.98 35.25.40 29.45 . 27.32.74 33,53 30.36.82 45.07 ° 55.66.56 70.06 27.32.06 31.46 35.38.22 43.24 45.37.08 38.72 34,35.07 40.23 41.$1.12 63.71 59.
“7,77.“3,13,3.5.3.ll.-12.-6.“3.25.“9,“4,“7.-1