How to resolve the algorithm Averages/Simple moving average step by step in the Elena programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Simple moving average step by step in the Elena programming language

Table of Contents

Problem Statement

Computing the simple moving average of a series of numbers. 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. 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:

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:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Simple moving average step by step in the Elena programming language

Source code in the elena programming language

import system'routines;
import system'collections;
import extensions;

class SMA
{
    object thePeriod;
    object theList;
    
    constructor new(period)
    {
        thePeriod := period;
        theList :=new List();
    }
    
    append(n)
    {
        theList.append(n);

        var count := theList.Length;
        count =>
            0 { ^0.0r }
            : {
                if (count > thePeriod)
                {
                    theList.removeAt:0;
                    
                    count := thePeriod
                };
        
                var sum := theList.summarize(Real.new());
                
                ^ sum / count
            }
    }
}

public program()
{
    var SMA3 := SMA.new:3;
    var SMA5 := SMA.new:5;

    for (int i := 1, i <= 5, i += 1) {
        console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:i);
        console.printLine("sma5 + ", i, " = ", SMA5.append:i)
    };

    for (int i := 5, i >= 1, i -= 1) {
        console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:i);
        console.printLine("sma5 + ", i, " = ", SMA5.append:i)
    };
    
    console.readChar()
}

  

You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Draw a rotating cube step by step in the Processing programming language
You may also check:How to resolve the algorithm Pisano period step by step in the Python programming language
You may also check:How to resolve the algorithm Percolation/Mean run density step by step in the Nim programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the AArch64 Assembly programming language