Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,22 @@
|
|||
pragma.enable("accumulator")
|
||||
def makeMovingAverage(period) {
|
||||
def values := ([null] * period).diverge()
|
||||
var index := 0
|
||||
var count := 0
|
||||
|
||||
def insert(v) {
|
||||
values[index] := v
|
||||
index := (index + 1) %% period
|
||||
count += 1
|
||||
}
|
||||
|
||||
/** Returns the simple moving average of the inputs so far, or null if there
|
||||
have been no inputs. */
|
||||
def average() {
|
||||
if (count > 0) {
|
||||
return accum 0 for x :notNull in values { _ + x } / count.min(period)
|
||||
}
|
||||
}
|
||||
|
||||
return [insert, average]
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
? for period in [3, 5] {
|
||||
> def [insert, average] := makeMovingAverage(period)
|
||||
> println(`Period $period:`)
|
||||
> for value in [1,2,3,4,5,5,4,3,2,1] {
|
||||
> insert(value)
|
||||
> println(value, "\t", average())
|
||||
> }
|
||||
> println()
|
||||
> }
|
||||
|
||||
Period 3:
|
||||
1 1.0
|
||||
2 1.5
|
||||
3 2.0
|
||||
4 3.0
|
||||
5 4.0
|
||||
5 4.666666666666667
|
||||
4 4.666666666666667
|
||||
3 4.0
|
||||
2 3.0
|
||||
1 2.0
|
||||
|
||||
Period 5:
|
||||
1 1.0
|
||||
2 1.5
|
||||
3 2.0
|
||||
4 2.5
|
||||
5 3.0
|
||||
5 3.8
|
||||
4 4.2
|
||||
3 4.2
|
||||
2 3.8
|
||||
1 3.0
|
||||
Loading…
Add table
Add a link
Reference in a new issue