0.4.
6.78.
2.18,74,“4,
7.10.-8.“8.
4,28.
10.
“5.“5.17.
v75790763608
“7.“5.4.7.-3.“5.7.1.li.“9,14,“14.“6.1.-10.4,12.
22.30.40.40,36.32.46,27.4829.65.+6236.38.33.9563.
28
35
44
0059051642132335
2551
469600
38APPENDIX 4 - 663
 
Trigonometric regression resuits using detrended data
159131721252933Rr41454953576165Fina15913721252933374h454953$76165
1
BL 1.78 1,80 86-.54-1,66 -1.88 = -1.10
.27 1.50 1.92 1.32
6OL 0-131-192-1581-.28 1.09. -1.88)—1.66
56 -.85 1.80 -1.79- 82 591.68 1.871.06 -.32 0 -1.53 1.92“1.28 104 «1,34 1,921.47 22300 1.13 -1.89“1.64 +51 89 1.821.77 77 +640 -1.71“1.86  -1.02 237 1.561.914 1.24  -.09-1,381,693) 1440.19 1,171.90 1.61 4600-94*1,.83  -1.75 =.72 69
regression results with trend added back29,96 31.21 31.50 30.8229.68 28.83 28.88 29.9331.57 33.06 33.75 33.4132.37 31,32 30.98 31.6633.15 34.79 35.85 35.9035.06 33.92 33.23 33.5234.75 36.43 37.79 38.2437.70 36.58 35.64 35.5236.43 38.02 39.59 40.4340.25 39.28 38.18 37.6938.21 39.60 41.27 42.4642.68 41.95 40.81 40.0140.12 41.23 42.89 44.3444,96 44,56 43.49 42.4742.19 42.95 44,47 46.0947.09 47.07 46.18 45.0544.42 44,78 46.07 47.74
TWO-FREQUENCY TRIGONOMETRIC REGRESSION
The FORTRAN program 7RIG2 and its subroutines LINREG (found in Appendix 4) and MTXare used to find the two-frequency representation of the copper cycle. The program out-put is clearly separated into the following steps.
1.2.3.
w
Input data, where each time period is the average cash price for a calendar quarter.The solution to the linear regression, giving the detrending line.
The detrended data resulting from subtracting the line values (2) from the originaldata (1).
. Intermediate values for 0, O2, @:, and @.. Resulting values @1, 31, 42, and 52, which arederived from the matrix solution using
Gaussian elimination.The cycle resulting from the detrended data.The final cycle with the trend added back.
PROGRAM TRIG22-Frequency Trigonometric Regression
- Copyright 1986 PJ Kaufman
DIMENSION X(250), ¥(250); 0(250), R(250), A(4,4), B(4)EQUIVALENCE (A(1,1),C2W1),(A(1,2),CW1SW1),(AC1,3),CW1CW2),664
APPENDIX 4
eee
CACL,4) LWISW2), (ACZ2, 1), DUPL), (A(2,2},S2W1),CA(2,3), SWICW2),(A(2,4),SWISW2},(A(3, 1), DUP2),(A(3,2),DUP3) ,(A(3,3),C2W2},(A(3,4) ,CW2SW2},(A(4,1), DUP4), (AC4, 2), DUPS), (A(4, 3), DUPE),
tee ett
DATA MAX/250/ ,NDIM/4/OPEN(6,FILE="PRN*)
C---- Read data, set X to incremental timeI=lWRITE(*, 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(8N,F8.2)X(1I) = 1TFCY(1).NE.O) THENIFCI,GT.MAX)STOP ‘Maximum data exceeded’T= 14) ‘GOTO 10ENOIFN= 1-2WRITE(6, 6000)6000 FORMAT(‘'12-Frequency Trigonometric Regression'//+ ‘Time’ ,16X,‘Prices*)00 40 9 = 1,N,440 WRITE(6, 60010, (¥(1), 1-0 ,J+3)6001 FORMAT (14,4F8.2)
C---- Linear regression analysis for detrendingCALL LINREG(N,Y,ALIN,8LIN, SD)WRITE(6,6002)ALIN,8LIN6002 FORMAT(/' Linear regression results: A =',F8.3,', B =',F8.3)
C---- Detrend data into 0 and computer sums for equation (4)bo 60 I = 1,N60 OCT) = YCI} - CALIN+BLIN®I)C---+- Print detrended data
WRITE(6, 6003)6003 FORMAT(/' Detrended data'/}
00 65 I = 1,N,465 WRITE(6,6001)1,(D(J),J=I,1+3)
Sc2 = 0 _SCO = 0sce = 0sp2 = 0 7sp? = 0 ue- for alphal and alpha? using detrended dataIT = 2,N-3
 
C = Del) + D(T+2}T= O(T+h}
P= D(i-i) + B(1+3}SC2 = SC2 + C*CSCO = SCD + C*T
SCP = SCP + C*P$02 = $02 + T*T
(AC4,4) S202), (BCL) ,YCWL) ,(B(2}, YSW1) ,(B(3},YCW2),(B(4),YSW2),(B(1) AL), (8(2},B1), (B(3),A2), (B(4) ,B2)APPENDIX 4
mn,
c-
70
100
6009
196010
SOPALPHA2ALPHA]T = SQRTCALPHAL*ALPHA1+8*( 1+ALPHA2/2))WL = ACOS((ALPHAI+T)/4)
W2 = ACOS((ALPHAL-1T)/4}
WRITE(6,6004)SC2,SCD,SCP,S02,SDP,ALPHA1,ALPHA2 ,W1,W26004 FORMAT(/*
- 665SOP + T*P
(SDP*SC2-SCP)/(S02*SC2-SCD)(SCP-ALPHA2*SCD}/SC2
Intermediate values:'//
+ " SUMS C2 =", F8.1,', C*D =',F8.1,', C#P =", F8.1/+ . D2 =", F8.1,', O*P =',F8.2//" Alphal = °,F8.3+ Alpha2 =’, F8.3,’ Omegal =',F5.2,', Omega2 =',F5.2)Sums for normal equations. . .to be used for matrix solutioncawl = 0CW1SW1 = 0CWLCW2 = 0CwWiSW2 = 0YCWwl = 9Sawi = 9SWICW2 = 0SW1SW2 = 0YSWl = 0Caw2 = 0cwW2sW2 = 0Ycw2 = 0sew2 = 0Ysw2 = 0CO 100 I = 1,NBI = B(I)SW1 = SIN(WI*T)CW1 = COS(W1*I)SW2 = SINCW2*I)CW2 = COS(W2*1)C2Wi = C2Wi + CW1*SW1CW1SW1 = CWISW1 + CWL*SW1CW1CW2 = CWICW2 + CW1*CW2CWLSW2 = CWISW2 + CW1*SW2 -YCWL = YCW1 + DI*CW1S2WL = S2W1 + SW1*SWLSWICW2 = SWISW2 + SWL*CWZSW1SW2 = SWLSW2 + SW1*SW2YSWL = YSW + DICaW2 = C2W2 + CW2*CW2CW2SW2 = CW2SW2 + CW2*SW2ycw2 = YCW2 + DI*CW2S2W2 = S2W2 + SW2*SW2YSW2 = YSW2 + DI*SW2Duplicate calculations for matrixbuPL = CWLSW1OUP2 = CWICW2DUP3 = SWiCW2bup4 = CWLSW2DUPS = SWiSW2DUP6 = CW2SW2WRITE(6, 6009)FORMAT(/' Coefficient matrix:'/)
BO 110 T = 1,NDiM™WRITE(5, 5010) (ACI, 0), G=2,4), 861)FORMAT (5FB.3)666 APPENDIX 4_—
C---~ Solve using matrix Gaussian EliminationCALL MTXCA,B,NDIM)
C--:
Solution vectorWRITE(6 ,6011)(B(1),1=1,NDIM)6011 FORMAT(/' Solution vector:'/4FB.3)
C---- Values of fitted curve using detrended dataDO 90 I =1,N90 R(T) = Al*COSCWL*I) + BI*SINCW1*I) + A2*COSCW2*I) ++ B2*SIN(W2*I}
WRITE(6,6006)6006 FORMAT(/' Trigonometric regression results using detrended data’,+ ‘)00 105 I = 1,N,4105 WRITE(6,6001)1,(R(J) ,J=I,1+3)
C---- Add trend back to result00 115 I =1,N115 R(T) = R(T) + ALIN + BLIN*I
WRITE(6, 6007) .6007 FORMAT(/' Final results with trend added back'/)BO 120 I = 1,N,4120 WRITE(6,6001)1,(R(J) ,J=2, 1+3)
CALL EXITENDSUBROUTINE MTX(A,C,N)C---- Matrix solution to simultaneous linear equationsC---- Copyright 1986 9J KaufmanDIMENSION A(4,4), C4), Al(4,4), C1(4)C---- Process row by row (Gaussian Elimination)BO 100 I = 1,NOIV = A(1,1)D0 40 9 = 1,N40 A(T,d) = ACT,J)/DIV .C(I) = C(T)/01VC---- Zero out column I for each row00 60 J = 1,N
1F(J.E0.1)GOTO 60FACTOR = A(d,1)
00 50 K = I,N50 ACJ,K) = ACO,K) > ACI, K)*FACTOR- C(J) = Cid) - CCT)*FACTOR60 CONTINUE100 CONTINUERETURN _
END 72-Frequency Trigonametric Regression
Time Prices1 22.12 22.46 22.17 22.005 23.18 24.55 25.57 30.599 228.23 33.77 35.99 40:0513 45.22 51.48. 40.76 40.1617 35.51 29.30 30.36 36.42aL 39.75 30.07 29.08 32.13APPENDIX 4
 
25 38.94 42.95 43,38 46.2329 47,70 46.98 35.78 27.3533 25.40 29.45 27.15 28.4837 32.74 33.53 39.01 29,2541 36.82 45.07 55.13 65.5145 66.56 70.06 27.30 35.6249 32.06 31.46 35.75 36.4653 38.22 43.24 45.46 38.9657 37.08 38.72 34.01 33.0061 35.07 40.23 41.63 44,9565 $1.12 63.71 59.56 63.38
Linear regression results: A = 28.889, 8 = .267
Detrended data
1 “7.04 -6.96 “7.52 -7.965 “7,05 -§.93 “$5.19 -.449 -3.06 2.21 4.07 7.9513 13.86 18.85 7.86 7.0017 3.08 -4.40 -3.61 2.192 5.25 -4.70 -§.96 “3.1725 3.37 7.11 7.28 9.8629 11,06 10.07 -1.39 -10.0933 -12.31 -8.52 “12.09 -10.0337 -6.04 -5.51 -9.30 +10.334. 73.02 4.96 14,75 24.8645 25,65 28.88 “14.15 -6.1049 +9.92 _ -10.79 -6.77 -6.3253 74,83 -.08 1.87 4,8957 77.04 “5.67 -10.64 -11.9261 -10.12 -§.23 “4.09 1.0465 4786 17.19. 12.77 16.32
Intermediate values:
SUMS C2 = 17396.7, C*D = 8753.0, C*P = 10475.102 = 6126.9, D*P = 5499.2
Alphal = .15r, Alpha2 = .898, Omegal = .47, Omega2 = 2.52
Coefficient matrix:
34.206 790 - 461 426 124.829«799 33.794 - 454 479 -7.036-.461 ~ 454 33.742 781 33.500+426 479 -.781 34.258 28.219
Solution vector: _3.635 -.317 ~ 939 762
Trigonometric regression results using detrended dete
1 4,29 84 69 71.205 -3.72 +2.38 “4.57 -2.189 71.06 -.43 3.23 2.1213 4.21 3.45 1.39 1.8217 -2.17 “1,93 -3.36 74,3921 -1.80 72,78 67 1.7225 2.23 4.76 2.31 3.2429 1.05 +1.35 “1.09 -4.4533 -2.90 73.25 -2.67 797 12 3.49 3.41 2.9741 3.91 27 59 -2.04
45 -3.65 -2.57 74.56 -1.50668 APPENDIX 449-106 38 3444 2.1953 4.54 2.79 1.38 1.2057 -2.70 “1.91 -3.91 “3.91611.68 = -2.44 1.44 1.6965 2.86 4.60 2.08 3.19
Final results with trend added back
1 33.44 30.26 30.38 28.755 26.50 28.12 26.19 28.859 30.23 31.13 35.05 34.2113 36.58 36.08 34.29 34,98ws 31.26 31.77 30.61 29.85APPENDIX 5a
Fourier Transformation
The original computer program that solves the Fourier transformation as described by JackHutson and Anthony Warren (Technical Analysis of Stocks & Commodities) is based on theCooley-Tukey Fast Fourier Transform Algorithm (1965), developed at the IBM Thomas J.Watson Research Center. A full explanation of this program and its interpretation can befound in the two articles that appeared in the January 1983 edition of Technical Analysisof Stocks & Commodities, then reprinted in the September 1986 issue.
The following program appeared in the September 1986 issue of Technical Analysis ofStocks & Commodities. It was written by John Ehlers and appended to the article “A Com-parison of the Fourier and Maximum Entropy Methods.”
FAST FOURIER TRANSFORM PROGRAM
10 REM "FAST FOURIER TRANSFORM"20 REM FOR APPLE J{ WITH 2 DISKS USING3O0.REM DATA IN CST OR COMPU-TRAC FORMAT40 REM BY JOHN F. EHLERS45 REM CDPYRIGHT (CY 1986 BY TECHNICAL ANALYSIS, INC.50 HOME60 DIM DF${20)7O HTAB 5: INVERSE : PRINT ** FAST FOURIER TRANSFORM *" :NORMAL71 HTAB 19: PRINT “BY”72 HTAB 14; PRINT "JOHN EHLERS"73 HTAB 16: PRINT “BOX 1801"74 HTAB 12: PRINT "GDLETA, CA 93116": PRINT80 LET D$ = CHR$(4)90 PRINT D$ + “QPEN MASTER, L40,02"100 FOR I = 1 T0 20110 PRINT D$ + “READ MASTER,R";T120 INPUT DF$(I)130 IF LEFT$ (DF$(I),5) = "99999" GOTO 150140 NEXT I150 PRINT D$ + “CLOSE”160 FOR J = 1 TO I-1170 PRINT "<"; CHRS (J +-64); ">"; MIDS (DFS(J),4,16)180 NEXT J190 PRINT "<¥> VIEW NEW DATA DISK"200 PRINT “<X> EXIT TO MENU": PRINT -210 PRINT * SELECT <>": CHRS(8); CHRS$(8);220 POKE - 16368,0: GET X$: PRINT X$; ">"; CHRS{(8}; CHRS$(8);230 IF X$ = "V" THENHOME : VTAB 10:PRINT "INSERT NEW DATA DISK IN ORIVE 2":PRINT: PRINT "PRESS";: INVERSE: PRINT "RETURN"; :NORMAL: PRINT "TO CONTINUE”: VTAB 22:POKE - 16368,0: GET S$: GOTO 30240 fF X$ = "X” THEN HOME250 LET SF = ASC (X$) - 64260 IF SF > 2 AND SF <= J - 1 THEN 290270 HTAB 13: PRINT *": CHR$(8);: GOTO 220280 IF K < 120 OR K > LR THENPRINT "OOPS ~ RUN AGAIN": END670 APPENDIX 5eee
290 HOME : VTAB 10: INVERSE: PRINT MIDS (OFS{SF),4,16):NORMAL: PRINT: PRINT
300 PRINT: PRINT D$ + "OPEN" + MIDS (OFS(SF},4,163 + ",L40":PRINT O$ + "READ" + MIDS (DFS$(SF),4,16) +",RO":INPUT X$: LET LR = YAL (XS)
310 PRINT OS + “CLOSE”
320 IF LR < 32 THEN 370
330 IF LR < 1024 THEN 410
340 PRINT "CONTAINS"; LR; " RECORDS"
350 PRINT "A MAXIMUM 1024 RECORDS ARE ALLOWED": PRINT
360 PRINT "PRESS";: INVERSE: PRINT “RETURN";: NORMAL:PRINT " TO CONTINUE": YTAB 22
37G PRINT "CONTAINS ONLY"; LR; “RECORDS”
380 PRINT "AT LEAST 32 RECORDS ARE REQUIRED": PRINT
390 PRINT "PRESS";: INVERSE: PRINT “RETURN";: NORMAL:PRINT "TO CONTINUE": VTAB 22
400 POKE - 16368,0: GET S$: HOME: CLEAR: GOTO 20
410 PRINT "CONTAINS "; LR; " RECORDS": PRINT
420 PRINT “ENTER ONLY 32, 64, 128, OR 256"
430 PRINT "FOR ANALYSIS. THIS NUMBER MUST BE"
440 PRINT “LESS THAN THE FILE LENGTH.": PRINT
450 INPUT "RECORDS FOR ANALYSIS? *; N
460 IF (N = 32 OR N = 64 OR N = 128 OR N = 256 OR N = 512)
AND N < LR THEN 486470 PRINT: PRINT "OOPS -- RUN PROGRAM AGAIN": ENO
480 DIM DAS(1024), F(1024), A(257,8)490 PRINT: PRINT O$ + “OPEN” + MID$ (OFS(SF),4,16) + *,L40"500 FOR T= 1 TON:PRINT OS + “READ * + MIDS (OFS(SF),4,16) + "(LR — N+ I): INPUT DAS(I):NEXT I510 PRINT 0$ + “CLOSE”520 REM *** DATA CONVERTER ***530 IF MID$ (DA$(1),23,5) <> "99999" THEN 550540 LET OA$(1) = DAS{2)550 FOR I = | TON560 LET FEL) = VAL (MIDS (DAS(1),23,5)) .570 IF MID$ (DA$(T),23,5) = "99999" THENLET FCI) = FOE - 1)580 NEXT I
590 REM *** SPECTRUM ANALYSIS ***
600 LET PI = 3.1415926
610 LET PO = INT (N / 4): LET SO = 8620 FOR} = 1 TO RO +1: -
 
FOR J = 1 TO 8: LET A(I,J) = O: NEXT J:
NEXT I630 FOR 1 = 1 TO RO -640 FOR J = 1 TO SO STEP 2650 LET K= 4 * (PF - 1) + 5 * (J+) us660 LET ACE,J) = F(K) °670 LET ACI,d + 1) = 90680 NEXT J620 NEXT I700 LET M = LOG (N) / LOG (2): LET N2=N #/ 2:
LET NL = N- I: LET J = 1
710 FOR i = 1 TO NI -720 TF 1 > J THEN 850
730 LET R = INT ((J - 1) / 4)740 LET S =2* 39 -8*RAPPENDIX 5 ° 67ane
750 LET RE = INT ((L - 1) / 4)760 LET SL=2*1-8* RL
770 LET RI = RL +1780 LET R =R+1790 LET T = ACR,S)
800 LET A{R,S) = A(R1,SI)810 LET ACRI,S1) = T
820 LET T = ACR,S = 1)
830 LET ACR,S - 1) = ACRE,SL - 1)840 LET A(RI,S] - 1) =T850 LET K = N2
860 IF K > d THEN 910
870 LET d =J-K
880 LET K =K/2
890 GOTO 860
300 NEXT I
910 LET J = +K
920 NEXT 1
930 LET LO = 1
940 FOR L = 1 70M
950 LET LI = LO
960 LET LO = 2 * LO
970 LET Vo= 2 ‘980 LET W = 0
990 lET Z = PL / it1000 LET W1 = COS (Z)1010 LET W2 = SIN (2)
1020 FOR J = 1 TO L1
1030 FOR I = J TO N STEP 10
1040 LET K = T+ li
1050 LET RI = INT ((K - 1) / 4)1060 LET SL =2* K-8* Ri1070 LET Ri = RL +]
1080 LET Al = ACR1,Si - 1)
1090 LET Bl = A{R1,S1)
1100 LET T = AL* V- BI *W1110 LET U = AL* W+ Bl * ¥1120 LET R = INT ((T - 1)/ 4)1130 LETS =2*1-8*R
1140 LET R =Redo-
1150 LET ACRI,S1 - 1) = AC(R,S - 1) -T1160 LET AC(R1,S1) = A(R,S) ~ U1170 LET AC(R,S - 1) = A(R,S - 1) +T1180 LET A(R,S) = ACR,S) + U1190 NEXT I -
1200 LET Us ¥ * Wi ~