September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,56 +1,54 @@
|
|||
import system'routines.
|
||||
import system'collections.
|
||||
import extensions.
|
||||
import system'routines;
|
||||
import system'collections;
|
||||
import extensions;
|
||||
|
||||
class SMA
|
||||
{
|
||||
object thePeriod.
|
||||
object theList.
|
||||
object thePeriod;
|
||||
object theList;
|
||||
|
||||
constructor new : aPeriod
|
||||
[
|
||||
thePeriod := aPeriod.
|
||||
theList := List new.
|
||||
]
|
||||
constructor new(period)
|
||||
{
|
||||
thePeriod := period;
|
||||
theList :=new List();
|
||||
}
|
||||
|
||||
append : aNumber
|
||||
[
|
||||
theList append:aNumber.
|
||||
append(n)
|
||||
{
|
||||
theList.append(n);
|
||||
|
||||
var aCount := theList length.
|
||||
aCount =>
|
||||
0 [ ^0.0r ];
|
||||
! [
|
||||
if (aCount > thePeriod)
|
||||
[
|
||||
theList removeAt:0.
|
||||
var count := theList.Length;
|
||||
count =>
|
||||
0 { ^0.0r }
|
||||
: {
|
||||
if (count > thePeriod)
|
||||
{
|
||||
theList.removeAt:0;
|
||||
|
||||
aCount := thePeriod
|
||||
].
|
||||
count := thePeriod
|
||||
};
|
||||
|
||||
var aSum := theList summarize(Real new).
|
||||
var sum := theList.summarize(new Real());
|
||||
|
||||
^ aSum / aCount
|
||||
]
|
||||
]
|
||||
^ sum / count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public program
|
||||
[
|
||||
var SMA3 := SMA new:3.
|
||||
var SMA5 := SMA new:5.
|
||||
public program()
|
||||
{
|
||||
var SMA3 := SMA.new:3;
|
||||
var SMA5 := SMA.new:5;
|
||||
|
||||
1 to:5 do(:i)
|
||||
[
|
||||
console printPaddingRight(30, "sma3 + ", i, " = ", SMA3 append:i).
|
||||
console printLine("sma5 + ", i, " = ", SMA5 append:i)
|
||||
].
|
||||
for (int i := 1, i <= 5, i += 1) {
|
||||
console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:i);
|
||||
console.printLine("sma5 + ", i, " = ", SMA5.append:i)
|
||||
};
|
||||
|
||||
5 to:1 do(:i)
|
||||
[
|
||||
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.
|
||||
]
|
||||
console.readChar()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
|
|
@ -1,19 +1,23 @@
|
|||
/*REXX program illustrates and displays a simple moving average using a constructed list*/
|
||||
parse arg p q n . /*obtain optional arguments from the CL*/
|
||||
if p=='' | p=="," then p= 3 /*Not specified? Then use the default.*/
|
||||
if q=='' | q=="," then q= 5 /* " " " " " " */
|
||||
if n=='' | n=="," then n=10 /* " " " " " " */
|
||||
@.=0 /*default value, only needed for odd N.*/
|
||||
do j=1 for n%2; @.j=j; end /*build 1st half of list, increasing #s*/
|
||||
do k=n%2 by -1 to 1; @.j=k; j=j+1; end /* " 2nd " " " decreasing " */
|
||||
if p=='' | p=="," then p= 3 /*Not specified? Then use the default.*/
|
||||
if q=='' | q=="," then q= 5 /* " " " " " " */
|
||||
if n=='' | n=="," then n= 10 /* " " " " " " */
|
||||
@.= 0 /*default value, only needed for odd N.*/
|
||||
do j=1 for n%2; @.j= j /*build 1st half of list, increasing #s*/
|
||||
end /*j*/
|
||||
|
||||
say ' ' " SMA with " ' SMA with '
|
||||
say ' number ' " period" p' ' ' period' q
|
||||
say ' ──────── ' "──────────" '──────────'
|
||||
do k=n%2 by -1 to 1; @.j= k; j= j+1 /* " 2nd " " " decreasing " */
|
||||
end /*k*/
|
||||
|
||||
do m=1 for n; say center(@.m, 10) left(SMA(p, m), 11) left(SMA(q, m), 11); end
|
||||
say ' number ' " SMA with period" p' ' " SMA with period" q
|
||||
say ' ──────── ' "───────────────────" '───────────────────'
|
||||
pad=' '
|
||||
do m=1 for n; say center(@.m, 10) pad left(SMA(p, m), 19) left(SMA(q, m), 19)
|
||||
end /*m*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
SMA: procedure expose @.; parse arg p,j; i=0 ; $=0
|
||||
do k=max(1, j-p+1) to j+p for p while k<=j; i=i+1; $=$+@.k; end
|
||||
return $/i
|
||||
SMA: procedure expose @.; parse arg p,j; i= 0 ; $= 0
|
||||
do k=max(1, j-p+1) to j+p for p while k<=j; i= i + 1; $= $ + @.k
|
||||
end /*k*/
|
||||
return $/i /*SMA ≡ simple moving average. */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
Class sma
|
||||
'to be stored in a class module with name "sma"
|
||||
Private n As Integer 'period
|
||||
Private arr() As Double 'circular list
|
||||
Private index As Integer 'pointer into arr
|
||||
Private oldsma As Double
|
||||
|
||||
Public Sub init(size As Integer)
|
||||
n = size
|
||||
ReDim arr(n - 1)
|
||||
index = 0
|
||||
End Sub
|
||||
|
||||
Public Function sma(number As Double) As Double
|
||||
sma = oldsma + (-arr(index) + number) / n
|
||||
oldsma = sma
|
||||
arr(index) = number
|
||||
index = (index + 1) Mod n
|
||||
End Function
|
||||
|
||||
Normal module
|
||||
Public Sub main()
|
||||
s = [{1,2,3,4,5,5,4,3,2,1}]
|
||||
Dim sma3 As New sma
|
||||
Dim sma5 As New sma
|
||||
sma3.init 3
|
||||
sma5.init 5
|
||||
For i = 1 To UBound(s)
|
||||
Debug.Print i, Format(sma3.sma(CDbl(s(i))), "0.00000"),
|
||||
Debug.Print Format(sma5.sma(CDbl(s(i))), "0.00000")
|
||||
Next i
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue