Averages/Simple moving average: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: removed OVERFLOW from the STYLE html tag in PRE.)
m ({{out}})
Line 6: Line 6:


'''Description'''<br>
'''Description'''<br>
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().
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 word stateful in the task description refers to the need for SMA() to remember certain information between calls to it:
Line 110: Line 111:
end Main;</lang>
end Main;</lang>


{{out}}
Output:
<pre>Inserting 1 into max-3: 1.00000E+00
<pre>Inserting 1 into max-3: 1.00000E+00
Inserting 1 into max-5: 1.00000E+00
Inserting 1 into max-5: 1.00000E+00
Line 228: Line 229:
sma(SMAFREE(h5))
sma(SMAFREE(h5))
#
#
)</lang>Output:
)</lang>
{{out}}
<pre>
<pre>
next number 1.000000, SMA_3 = 1.000000, SMA_5 = 1.000000
next number 1.000000, SMA_3 = 1.000000, SMA_5 = 1.000000
Line 311: Line 313:
IF window%(period%)<period% window%(period%) += 1
IF window%(period%)<period% window%(period%) += 1
= accum(period%) / window%(period%)</lang>
= accum(period%) / window%(period%)</lang>
{{out}}
Output:
<pre>
<pre>
Number = 1 SMA3 = 1 SMA5 = 1
Number = 1 SMA3 = 1 SMA5 = 1
Line 364: Line 366:
)
)
);</lang>
);</lang>
{{out}}
Output:
<pre>1 - sma3: 1 sma5: 1
<pre>1 - sma3: 1 sma5: 1
2 - sma3: 3/2 sma5: 3/2
2 - sma3: 3/2 sma5: 3/2
Line 421: Line 423:
}</lang>
}</lang>


{{out}}
Output:

<pre>1 - SMA3: 1 SMA5: 1
<pre>1 - SMA3: 1 SMA5: 1
2 - SMA3: 1.5 SMA5: 1.5
2 - SMA3: 1.5 SMA5: 1.5
Line 664: Line 665:
}</lang>
}</lang>


{{out}}
Output:
<pre>
<pre>
1 (sma3) 1 (sma5) 1
1 (sma3) 1 (sma5) 1
Line 741: Line 742:
console.log i, sma3(i), sma7(i), sma11(i)
console.log i, sma3(i), sma7(i), sma11(i)
</lang>
</lang>
{{out}}
output
<lang>
<pre>
> coffee moving_average.coffee
> coffee moving_average.coffee
1 1 1 1
1 1 1 1
Line 754: Line 755:
9 8 6 5
9 8 6 5
10 9 7 5.5
10 9 7 5.5
</lang>
</pre>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 811: Line 812:


===Using a Struct===
===Using a Struct===
This version avoids the heap allocation of the closure keeping the data in the stack frame of the main function. Same output:
This version avoids the heap allocation of the closure
keeping the data in the stack frame of the main function.
Same output:
<lang d>import std.stdio, std.traits, std.algorithm;
<lang d>import std.stdio, std.traits, std.algorithm;


Line 839: Line 842:
=={{header|E}}==
=={{header|E}}==


This implementation produces two (function) objects sharing state. It is idiomatic in E to separate input from output (read from write) rather than combining them into one object.
This implementation produces two (function) objects sharing state.
It is idiomatic in E to separate input from output (read from write)
rather than combining them into one object.


The structure is the same as the implementation of [[Standard Deviation#E]].
The structure is the same as the implementation of [[Standard Deviation#E]].
Line 1,001: Line 1,006:
end.</lang>
end.</lang>


{{out}}
Output:
<lang erlang>9> sma:main().
<lang erlang>9> sma:main().
Added 1, sma(3) -> 1.000000, sma(5) -> 1.000000
Added 1, sma(3) -> 1.000000, sma(5) -> 1.000000
Line 1,077: Line 1,082:
[ 1.;2.;3.;4.;5.;5.;4.;3.;2.;1.] |> sma 5 (printf "%.2f ")
[ 1.;2.;3.;4.;5.;5.;4.;3.;2.;1.] |> sma 5 (printf "%.2f ")
printfn ""</lang>
printfn ""</lang>
{{out}}
Output:<pre>sma3: 1.00 1.50 2.00 3.00 4.00 4.67 4.67 4.00 3.00 2.00
<pre>sma3: 1.00 1.50 2.00 3.00 4.00 4.67 4.67 4.00 3.00 2.00
sma5: 1.00 1.50 2.00 2.50 3.00 3.80 4.20 4.20 3.80 3.00</pre>
sma5: 1.00 1.50 2.00 2.50 3.00 3.80 4.20 4.20 3.80 3.00</pre>


Line 1,130: Line 1,136:
</lang>
</lang>


Sample output for a period of 5:
{{out}} for a period of 5:
<pre>
<pre>
After 0 numbers list is [,] average is 0.0
After 0 numbers list is [,] average is 0.0
Line 1,271: Line 1,277:
}
}
}</lang>
}</lang>
{{out}}
Output:
<pre>
<pre>
x sma3 sma5
x sma3 sma5
Line 1,303: Line 1,309:
(1..5).each{ printf( "%1.1f ", ma5(it)) }
(1..5).each{ printf( "%1.1f ", ma5(it)) }
(5..1).each{ printf( "%1.1f ", ma5(it)) }</lang>
(5..1).each{ printf( "%1.1f ", ma5(it)) }</lang>
{{out}}
Sample output:
<pre>1.0 1.5 2.0 2.5 3.0 3.8 4.2 4.2 3.8 3.0 </pre>
<pre>1.0 1.5 2.0 2.5 3.0 3.8 4.2 4.2 3.8 3.0 </pre>


Line 1,335: Line 1,341:
putStrLn $ "Next number = " ++ (show n) ++ ", SMA_3 = " ++ (show mm3) ++ ", SMA_5 = " ++ (show mm5)
putStrLn $ "Next number = " ++ (show n) ++ ", SMA_3 = " ++ (show mm3) ++ ", SMA_5 = " ++ (show mm5)
)</lang>
)</lang>
{{out}}
output:
<pre>Next number = 1.0, SMA_3 = 1.0, SMA_5 = 1.0
<pre>Next number = 1.0, SMA_3 = 1.0, SMA_5 = 1.0
Next number = 2.0, SMA_3 = 1.5, SMA_5 = 1.5
Next number = 2.0, SMA_3 = 1.5, SMA_5 = 1.5
Line 1,565: Line 1,571:
}
}
}</lang>
}</lang>
{{out}}
Output:
<pre>Next number = 1.0, SMA = 1.0
<pre>Next number = 1.0, SMA = 1.0
Next number = 2.0, SMA = 1.5
Next number = 2.0, SMA = 1.5
Line 1,613: Line 1,619:
WScript.Echo("Next number = " + n + ", SMA_3 = " + sma3(n) + ", SMA_5 = " + sma5(n));
WScript.Echo("Next number = " + n + ", SMA_3 = " + sma3(n) + ", SMA_5 = " + sma5(n));
}</lang>
}</lang>
{{out}}
output:
<pre>Next number = 1, SMA_3 = 1, SMA_5 = 1
<pre>Next number = 1, SMA_3 = 1, SMA_5 = 1
Next number = 2, SMA_3 = 1.5, SMA_5 = 1.5
Next number = 2, SMA_3 = 1.5, SMA_5 = 1.5
Line 1,939: Line 1,945:
return
return
</lang>
</lang>
{{out}}
'''Output:'''
<pre style="height: 25ex; overflow: scroll">
<pre style="height: 25ex; overflow: scroll">
Next number = 1.0, SMA = 1.000000000
Next number = 1.0, SMA = 1.000000000
Line 1,994: Line 2,000:
for i in 1..5: echo sma2(float(i))
for i in 1..5: echo sma2(float(i))
for i in countdown(5,1): echo sma2(float(i))</lang>
for i in countdown(5,1): echo sma2(float(i))</lang>
{{out}}
Output:
<pre>1.0000000000000000e+00
<pre>1.0000000000000000e+00
1.5000000000000000e+00
1.5000000000000000e+00
Line 2,067: Line 2,073:
</lang>
</lang>


{{out}}
Output:
<pre>
<pre>
Next number = 1.0, SMA = 1.0
Next number = 1.0, SMA = 1.0
Line 2,241: Line 2,247:
) periodLst</lang>
) periodLst</lang>


{{out}}
Output:
<pre>
<pre>
SIMPLE MOVING AVERAGE: PERIOD = 3
SIMPLE MOVING AVERAGE: PERIOD = 3
Line 2,338: Line 2,344:
return sum / queue~items
return sum / queue~items
</lang>
</lang>
{{out}}
Output:
<pre>
<pre>
Period size = 3
Period size = 3
Line 2,531: Line 2,537:
" (sma5) "
" (sma5) "
(format (sma5 N) *Scl) ) )</lang>
(format (sma5 N) *Scl) ) )</lang>
{{out}}
Output:
<pre>1.00 (sma3) 1.00 (sma5) 1.00
<pre>1.00 (sma3) 1.00 (sma5) 1.00
2.00 (sma3) 1.50 (sma5) 1.50
2.00 (sma3) 1.50 (sma5) 1.50
Line 2,626: Line 2,632:
print (" Next number = %-2g, SMA = %g " % (i, sma(i)))</lang>
print (" Next number = %-2g, SMA = %g " % (i, sma(i)))</lang>


{{out}}
'''Sample output'''
<pre>SIMPLE MOVING AVERAGE (procedural): PERIOD = 3
<pre>SIMPLE MOVING AVERAGE (procedural): PERIOD = 3
Next number = 1 , SMA = 1
Next number = 1 , SMA = 1
Line 2,770: Line 2,776:
end
end
return s/i</lang>
return s/i</lang>
'''output''' using the defaults
{{out}} using the defaults
<pre style="height:30ex">
<pre style="height:30ex">
item 1= 1
item 1= 1
Line 2,943: Line 2,949:
(map av '(1 2 3 4 5 5 4 3 2 1))
(map av '(1 2 3 4 5 5 4 3 2 1))
</lang>
</lang>
{{out}}
Output:
<pre>
<pre>
(1 3/2 2 3 4 14/3 14/3 4 3 2)
(1 3/2 2 3 4 14/3 14/3 4 3 2)
Line 3,060: Line 3,066:
puts "Next number = $n, SMA_3 = [averager3 val $n], SMA_5 = [averager5 val $n]"
puts "Next number = $n, SMA_3 = [averager3 val $n], SMA_5 = [averager5 val $n]"
}</lang>
}</lang>
{{out}}
Output:
<pre>Next number = 1, SMA_3 = 1.0, SMA_5 = 1.0
<pre>Next number = 1, SMA_3 = 1.0, SMA_5 = 1.0
Next number = 2, SMA_3 = 1.5, SMA_5 = 1.5
Next number = 2, SMA_3 = 1.5, SMA_5 = 1.5