Time Series Analysis: Exponential Smoothing

Date: 198x
Type: Program
Platform(s): TS 2068

This program performs exponential smoothing on a time series of observed values to forecast the next period’s value. It tests multiple smoothing constants (alpha = 0.01, 0.05, and 0.1 through 0.9 in steps of 0.1) and selects the best alpha by minimising the sum of squared errors over the non-initialisation periods. The subroutine at line 730 implements the standard single exponential smoothing recurrence F = α·V(i) + (1−α)·F, storing the final smoothed value and cumulative squared error for each alpha in parallel arrays M() and E(). A user-defined function at line 585 rounds display values to two decimal places using the INT(100*X+0.5)/100 idiom. The program allows the user to re-run the analysis with a different number of initialisation periods by RESTOREing the DATA pointer and looping back to line 140.


Program Analysis

Program Structure

The program is divided into four logical phases:

  1. Variable glossary (lines 2–20): A scrolling reference screen explains all variables used, paused for about 11 seconds before clearing.
  2. Data input and display (lines 30–155): Reads N observations and P (initialisation period count) from DATA statements at lines 900, 910, and 920, printing each observed value.
  3. Alpha search and best-fit selection (lines 160–530): Iterates over 11 alpha values, calling the smoothing subroutine at line 730 each time, then scans arrays E() and M() to find the minimum squared-error alpha.
  4. Forecast table and repeat option (lines 540–850): Prints forecast values for all alphas, then offers the user a chance to re-run with a different P via a RESTORE/GO TO loop.

DATA Layout

LineContentsRole
90010N — number of observations
9104.30, 4.10, 3.81, … 1.66V(I) — ten observed values (a declining series)
9209P — periods used to seed the initial average

Exponential Smoothing Subroutine (lines 730–840)

The subroutine implements the standard recurrence: F = A*V(I) + (1-A)*F. The initial smoothed value F is set to the arithmetic mean V2 of the first P observations. For each subsequent period from P+1 to N, the squared error (F-V(I))^2 is accumulated in E1. When I=N, the final smoothed value is saved into M(I4) and the cumulative error into E(I4) before returning.

The index variable I4 maps alpha values to array slots:

  • Index 2 → α = 0.01
  • Index 3 → α = 0.05
  • Indices 4–12 → α = 0.1, 0.2, … 0.9

Alpha Selection Logic (lines 360–500)

After all 11 subroutine calls, the program scans E(2) through E(12) to find the index I3 with the smallest accumulated squared error. A three-branch conditional then maps I3 back to its alpha value for display: I3=2 → 0.01, I3=3 → 0.05, I3>30.1*(I3-3).

Key BASIC Idioms and Techniques

  • User-defined function for rounding: DEF FN A(X)=INT(100*X+.5)/100 at line 585 rounds to two decimal places — a common idiom since BASIC’s default floating-point display can produce long trailing decimals.
  • RESTORE for re-run: Line 710 uses RESTORE : GO TO 140 to reset the DATA pointer and re-read P, allowing a fresh analysis with a different initialisation window without re-entering observations.
  • Parallel arrays: Arrays M(20) and E(20) are dimensioned to 20, though only indices 2–12 are used, leaving a safe margin.
  • Guard clause at line 180: IF P<1 OR P>=N THEN GO TO 140 prevents a degenerate initialisation window (P must be at least 1 and strictly less than N).

Bugs and Anomalies

  • Line 350 comment typo: The REM reads “ERROES SQUARED” instead of “ERRORS SQUARED” — a cosmetic issue only.
  • Line 11 typo: “SPECIFC” should be “SPECIFIC” — again cosmetic.
  • V1 not reset on re-run: The variable V1 (sum of initial observations) is initialised to 0 at line 170, but if the user chooses to re-run (line 710), execution jumps to line 140 which skips line 170. This means V1 accumulates across runs, corrupting V2 (the seed average) on any subsequent pass.
  • Array bounds: DIM V(10) is exactly sized for the 10 data points; any change to N in the DATA statement without updating the DIM would cause an error.
  • Forecast stored for period N, not N+1: The subroutine saves F after updating it with V(N), so M(I4) holds the one-step-ahead forecast for period N+1, which is the intended use.

Content

Appears On

One of a series of library tapes. Programs on these tapes were renamed to a number series. This tape contained

Related Products

Related Articles

Related Content

Image Gallery

Time Series Analysis: Exponential Smoothing

Source Code

    2 REM "EXP SMOOTH"
    4 PRINT "VARIABLES :": PRINT 
    5 PRINT "A   CONSTANT ALPHA"
    6 PRINT "E(I)  QUADRATIC ERROR"
    7 PRINT "F   VALUE ESTIMATED BY"
    8 PRINT "   THE EXPONENTIAL SMOOTHING"
    9 PRINT "M(K)  VALUE ESTIMATED BY THE"
   10 PRINT "   EXPONENTIAL SMOOTHING FOR"
   11 PRINT "    A SPECIFC ALPHA"
   12 PRINT "N   NUMBER OF OBSERVATIONS-line      900"
   13 PRINT "P   NUMBER OF PERIODS TO"
   14 PRINT "    BASE THE SMOOTHING ON--line     920"
   15 PRINT "V(I)  OBSERVED VALUE FOR PERIOD      I--line 910"
   20 PAUSE 340: CLS : PRINT 
   30 PRINT "TIME SERIES ANALYSIS:"
   40 PRINT "EXPONENTIAL SMOOTHING"
   50 PRINT "-------------------------------"
   60 PRINT : PRINT 
   65 READ N
   70 PRINT "NUMBER OF OBSERVATIONS ";N
   80 DIM V(10): DIM M(20): DIM E(20)
   90 PRINT 
  100 FOR I=1 TO N
  105 READ V(I)
  110 PRINT "VALUE FOR PERIOD ";I;TAB ( 22);V(I)
  130 NEXT I
  140 PRINT 
  150 PRINT "NUMBER OF PERIODS RETAINED"
  154 READ P
  155 PRINT "FOR THE SMOOTHING? ";P
  160 REM    CALCULATION OF THE AVERAGES WITH DIFFERENT ALPHAS.
  170 LET V1=0
  180 IF P<1 OR P>=N THEN GO TO 140
  190 FOR I=1 TO 10
  200 LET M(I)=0: LET E(I)=0
  210 NEXT I
  220 FOR I=1 TO P
  230 LET V1=V1+V(I)
  240 NEXT I
  250 LET V2=V1/P
  260 LET A=0.01: LET I4=2
  270 GO SUB 730
  280 LET A=0.05: LET I4=3
  290 GO SUB 730
  300 FOR J=1 TO 9
  310 LET A=J/10
  320 LET I4=3+J
  330 GO SUB 730
  340 NEXT J
  350 REM   FIND THE MINIMUM SUM OF ERROES SQUARED
  360 LET I3=2
  370 LET E3=E(2)
  380 FOR I=3 TO 12
  390 IF E(I)>E3 THEN GO TO 420
  400 LET E3=E(I)
  410 LET I3=I
  420 NEXT I
  430 IF I3=3 THEN GO TO 470
  440 IF I3>3 THEN GO TO 490
  450 LET A=0.01
  460 GO TO 510
  470 LET A=0.05
  480 GO TO 510
  490 LET A=0.1*(I3-3)
  500 PRINT : PRINT : PRINT 
  510 PRINT "BEST SMOOTHING CONSTANT (ALPHA)"
  520 PRINT "(DETERMINED BY FINDING THE MINIMUM OF"
  525 PRINT "THE SUM OF THE ERRORS SQUARED) = ";A
  530 REM  FORECAST OF N+1
  540 PRINT : PRINT 
  550 PRINT "FORECAST FOR PERIOD ";N+1;":"
  560 PRINT "------------------------"
  570 PRINT "   ALPHA       VALUE"
  580 PRINT "------------------------"
  585 DEF FN A(X)=INT (100*X+.5)/100
  590 PRINT TAB ( 4);.01;TAB ( 15);FN A(M(2))
  600 PRINT TAB ( 4);.05;TAB ( 15);FN A(M(3))
  610 FOR I=1 TO 9
  620 LET A=I/10
  630 LET K=3+I
  640 PRINT TAB ( 4);A;TAB ( 15);FN A(M(K))
  650 NEXT I
  660 PRINT "-------------------------------"
  670 PRINT : PRINT 
  680 PRINT "CHANGE NUMBER OF PERIODS"
  690 INPUT "RETAINED FOR THE SMOOTHING? (Y/N) ";C$
  700 PRINT 
  710 IF C$<>"N" THEN RESTORE : GO TO 140
  720 GO TO 850
  730 REM   EXPONENTIAL SMOOTHING SUBROUTINE
  740 LET P1=P+1
  750 LET E1=0
  760 LET F=V2
  770 FOR I=P1 TO N
  775 LET Z=(F-V(I))^2
  780 LET E1=E1+Z
  790 LET F=A*V(I)+(1-A)*F
  800 IF I<>N THEN GO TO 830
  810 LET M(I4)=F
  820 LET E(I4)=E1
  830 NEXT I
  840 RETURN 
  850 STOP 
  900 DATA 10
  910 DATA 4.30,4.10,3.81,3.14,2.95,2.58,2.49,2.39,1.75,1.66
  920 DATA 9

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top