Computing the simple moving average of a series of numbers.

Task[edit]
Create a stateful function/class/instance that takes a period and returns a routine that takes a number as argument and returns a simple moving average of its arguments so far.

Description
A simple moving average is a method for computing an average of a stream of numbers by only averaging the last   P   numbers from the stream,   where   P   is known as the period.

It can be implemented by calling an initialing routine with   P   as its argument,   I(P),   which should then return a routine that when called with individual, successive members of a stream of numbers, computes the mean of (up to), the last   P   of them, lets call this   SMA().

The word   stateful   in the task description refers to the need for   SMA()   to remember certain information between calls to it:

   The period,   P
   An ordered container of at least the last   P   numbers from each of its individual calls.

Stateful   also means that successive calls to   I(),   the initializer,   should return separate routines that do   not   share saved state so they could be used on two independent streams of data.

Pseudo-code for an implementation of   SMA   is:

function SMA(number: N):
    stateful integer: P
    stateful list:    stream
    number:           average

    stream.append_last(N)
    if stream.length() > P:
        # Only average the last P elements of the stream
        stream.delete_first()
    if stream.length() == 0:
        average = 0
    else:    
        average = sum( stream.values() ) / stream.length()
    return average

See also
Tasks for calculating statistical measures



in one go
moving (sliding window)
moving (cumulative)

Mean
Arithmetic

Statistics/Basic
Averages/Arithmetic mean
Averages/Pythagorean means


Averages/Simple moving average




Geometric

Averages/Pythagorean means




Harmonic

Averages/Pythagorean means




Quadratic

Averages/Root mean square




Circular

Averages/Mean angle
Averages/Mean time of day




Median

Averages/Median




Mode

Averages/Mode




Standard deviation

Statistics/Basic



Cumulative standard deviation




*        Averages/Simple moving average  26/08/2015
AVGSMA   CSECT
         USING  AVGSMA,R12
         LR     R12,R15
         ST     R14,SAVER14
         ZAP    II,=P'0'           ii=0
         LA     R7,1
         LH     R3,NA
         SRA    R3,1               na/2
LOOPA    CR     R7,R3              do i=1 to na/2
         BH     ELOOPA
         AP     II,=P'1000'        ii=ii+1000
         LR     R1,R7              i
         MH     R1,=H'6'
         LA     R4,A-6(R1)
         MVC    0(6,R4),II         a(i)=ii
         LH     R1,NA              na
         SR     R1,R7              -i
         MH     R1,=H'6'
         LA     R4,A(R1)
         MVC    0(6,R4),II         a(na+1-i)=ii
         LA     R7,1(R7)
         B      LOOPA
ELOOPA   XPRNT  =CL30' n     sma3        sma5       ',30
         XPRNT  =CL30' ----- ----------- -----------',30
         LA     R7,1               i=1
LOOP     CH     R7,NA              do i=1 to na
         BH     RETURN
         STH    R7,N               n=i
         XDECO  R7,C               i
         MVC    BUF+1(5),C+7
         MVC    P,=H'3'            p=3
         BAL    R14,SMA
         MVC    C(13),EDMASK
         ED     C(13),SS           sma(3,i)
         MVC    BUF+7(11),C+2
         MVC    P,=H'5'            p=5
         BAL    R14,SMA
         MVC    C(13),EDMASK
         ED     C(13),SS           sma(5,i)
         MVC    BUF+19(11),C+2
         XPRNT  BUF,30             output i,sma3,sma5
         LA     R7,1(R7)
         B      LOOP
*        *****  sub sma(p,n) returns(PL6)
SMA      LH     R5,N
         SH     R5,P
         A      R5,=F'1'           ia=n-p+1
         C      R5,=F'1'
         BH     OKIA
         LA     R5,1               ia=1
OKIA     LH     R6,NA              ib=na
         CH     R6,N
         BL     OKIB
         LH     R6,N               ib=n
OKIB     ZAP    II,=P'0'           ii=0
         ZAP    SS,=P'0'           ss=0
         LR     R3,R5              k=ia
LOOPK    CR     R3,R6              do k=ia to ib
         BH     ELOOPK
         AP     II,=P'1'           ii=ii+1
         LR     R1,R3
         MH     R1,=H'6'
         LA     R4,A-6(R1)
         MVC    C(6),0(R4)         ss=ss+a(k)
         AP     SS,C(6)
         LA     R3,1(R3)
         B      LOOPK
ELOOPK   ZAP    C,SS
         DP     C,II
         ZAP    SS,C(10)           ss=ss/ii
         BR     R14
RETURN   L      R14,SAVER14        restore caller address
         XR     R15,R15
         BR     R14
SAVER14  DS     F
NN       EQU    10
NA       DC     AL2(NN)
A        DS     (NN)PL6
II       DS     PL6
SS       DS     PL6
P        DS     H
N        DS     H
C        DS     CL16
BUF      DC     CL30'                              '  buffer
EDMASK   DC     X'4020202020202021204B202020'  CL13
         YREGS
         END    AVGSMA