How to resolve the algorithm Averages/Simple moving average step by step in the Factor programming language
How to resolve the algorithm Averages/Simple moving average step by step in the Factor 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 Factor programming language
Source code in the factor programming language
USING: kernel interpolate io locals math.statistics prettyprint
random sequences ;
IN: rosetta-code.simple-moving-avg
:: I ( P -- quot )
V{ } clone :> v!
[ v swap suffix! P short tail* v! ] ;
: sma-add ( quot n -- quot' ) swap tuck call( x x -- x ) ;
: sma-query ( quot -- avg v ) first concat dup mean swap ;
: simple-moving-average-demo ( -- )
5 I 10 <iota> [
over sma-query unparse
[I After ${2} numbers Sequence is ${0} Mean is ${1}I] nl
100 random sma-add
] each drop ;
MAIN: simple-moving-average-demo
You may also check:How to resolve the algorithm Voronoi diagram step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm S-expressions step by step in the 11l programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Component Pascal programming language
You may also check:How to resolve the algorithm File modification time step by step in the Pascal programming language