--- title: "Moving Average" id: 57016 type: "computer_media" slug: "moving-average" url: "http://localhost/computer_media/moving-average/" markdown_url: "http://localhost/computer_media/moving-average.md" published_at: "2024-10-02T07:39:44+00:00" modified_at: "2026-03-30T21:20:09+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/40_MvAv.png" excerpt: "A compact rolling-window statistics tool that continuously tracks mean and population standard deviation across a user-defined number of inputs." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_contents: - id: 56732 title: "Timex Sinclair Public Domain Library Tape 1001" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1001/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/10/40_MvAv.png" media_type_tags: "Mathematics" --- # Moving Average This program computes a rolling moving average alongside the mean and standard deviation of a user-defined set of numbers. The user specifies how many values to track (N), then enters them one at a time; after each full set the program displays all N values, their mean, and their population standard deviation. A sliding-window technique is implemented at line 370: each element X(J-1) is overwritten with X(J), shifting the array left so the oldest value is discarded when a new one is entered next cycle. The standard deviation uses the computational formula √(SS/N − (SX/N)²), avoiding a two-pass algorithm by accumulating both the sum and sum-of-squares in a single loop. *** ## Program Analysis ### Program Structure The program is divided into four logical phases, loosely reflected in its line-number bands: 1. **Initialisation (lines 5–10):** Title display and reset of counter `K`. 2. **Data entry (lines 100–240):** Prompts for the window size `N`, allocates array `X(N)`, and collects `N` values one at a time. 3. **Statistics and display (lines 300–410):** Computes sum `SX` and sum-of-squares `SS` in a single pass, prints all values, mean, and standard deviation. 4. **Loop-back (line 420):**`GOTO 210` jumps directly to the “NEXT NUMBER?” prompt, bypassing the dimension and initial fill steps, creating the rolling window. ### Rolling Window Mechanism The sliding-window behaviour is achieved entirely within the statistics loop at line 370: - `IF J>1 THEN LET X(J-1)=X(J)` copies each element one position to the left. - After the loop completes, `X(1)` through `X(N-1)` hold the values that were in positions 2 through N, and `X(N)` still holds the most recent entry. - When the program loops back to line 210, `K` is already equal to `N` (the counter was left at its final value), so `INPUT X(K)` overwrites only `X(N)` — the slot vacated conceptually by the shift — with the newest value. This is an elegant in-place shift that avoids any pointer arithmetic or modular indexing, using only basic array assignment. ### Statistical Method Mean and population standard deviation are computed using the one-pass computational formula: - Mean: `SX/N` where `SX` is the sum of all values. - Std Dev: `SQR(SS/N-(SX/N)**2)` where `SS` is the sum of squared values — equivalent to √(E[X²] − (E[X])²). Note that `**` is used for exponentiation (line 350 and 410), which is valid ZX81/TS1000 BASIC syntax as an alternative to `^`. This single-pass approach is numerically efficient for small N but can suffer from catastrophic cancellation with large values or large N — acceptable for the intended hobbyist use case. ### Key BASIC Idioms | Line | Idiom | Purpose | | --- | --- | --- | | `120` | `DIM X(N)` | Dynamic array sizing based on user input | | `320` | `PRINT "LAST ";N;" NUMBERS",,,,` | Trailing commas advance print position / add blank lines | | `410` | Trailing commas after std dev | Provides visual spacing before the next input prompt | | `420` | `GOTO 210` | Re-enters data entry loop without re-dimensioning | ### Counter Behaviour and Potential Anomaly Variable `K` is initialised to 0 at line 10 and incremented at line 200 before each input. After the first full pass through lines 200–240, `K` equals `N`. When the program loops back to line 210 via line 420, `K` is *not* reset. The input at line 220 therefore writes directly into `X(N)` on every subsequent cycle — which is exactly the intended behaviour given the left-shift at line 370. However, `K` never advances beyond `N` again, so the `IF K1 THEN LET X(J-1)=X(J) 380 NEXT J 400 PRINT ,,"MEAN = ";SX/N 410 PRINT ,,"STD DEV = ";SQR (SS/N-(SX/N)**2),,,,, 420 GOTO 210 500 SAVE "1004%0" 510 RUN ```