Data update
This commit is contained in:
parent
52a6ef48dd
commit
157b70a810
604 changed files with 14253 additions and 2100 deletions
|
|
@ -0,0 +1,32 @@
|
|||
(* helper functions *)
|
||||
val sum = List.foldl (op +) 0.0
|
||||
fun mean xs = (sum xs) / (Real.fromInt (length xs))
|
||||
|
||||
type smastate = int * real list
|
||||
|
||||
(* initialize an SMA state with the given period *)
|
||||
fun smaInit period : smastate = (period, [])
|
||||
|
||||
(* update the SMA in the given state with the given number *)
|
||||
(* returns a tuple containing the new SMA and the new state *)
|
||||
fun sma (state : smastate) (num : real) : (real * smastate) =
|
||||
let
|
||||
val (period, buffer) = state
|
||||
val amt = Int.min(1 + List.length buffer, period)
|
||||
val newlist = List.take(num::buffer, amt)
|
||||
in
|
||||
(mean newlist, (period, newlist))
|
||||
end
|
||||
|
||||
fun printSMA' _ [] = ()
|
||||
| printSMA' state (x::xs) =
|
||||
let
|
||||
val (n, next) = sma state x
|
||||
in
|
||||
print ("Added " ^ (Real.toString x) ^ ": SMA=" ^ (Real.toString n) ^ "\n");
|
||||
printSMA' next xs
|
||||
end
|
||||
|
||||
(* print the SMA with given period at each step over the list xs *)
|
||||
fun printSMA period xs =
|
||||
printSMA' (smaInit period) xs
|
||||
Loading…
Add table
Add a link
Reference in a new issue