Skip to content

Commit 97bdfef

Browse files
authored
Merge pull request #230 from labmlai/smooth
Smoothing
2 parents 9c2773b + b67fb7d commit 97bdfef

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

app/ui/src/components/charts/smoothing/two_sided_exponential_average.ts

+33-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {PointValue} from "../../../models/run"
33

44
export class TwoSidedExponentialAverage extends SeriesSmoothing {
55
protected smooth(): void {
6-
let smoothingFactor = 1 - this.smoothValue / 100
6+
let smoothingFactor = this.smoothValue / 100
77

88
for (let i = 0; i < this.indicators.length; i++) {
99
let ind = this.indicators[i]
@@ -13,27 +13,45 @@ export class TwoSidedExponentialAverage extends SeriesSmoothing {
1313
}
1414

1515
let result: PointValue[] = []
16-
let forward_pass: number[] = []
17-
let lastSmoothed = ind.series[0].value
16+
17+
let f_sum: number[] = []
18+
let b_sum: number[] = []
19+
let f_weights: number[] = []
20+
let b_weights: number[] = []
21+
22+
let last_sum = 0
23+
let last_weight = 0
24+
let last_step = ind.series[0].step
1825
for (let j = 0; j < ind.series.length; j++) {
19-
let smoothed = lastSmoothed * (1 - smoothingFactor) + ind.series[j].value * smoothingFactor
20-
forward_pass.push(smoothed)
21-
lastSmoothed = smoothed
26+
let smooth_gap = Math.pow(smoothingFactor, Math.abs(ind.series[j].step - last_step))
27+
f_sum.push(last_sum * smooth_gap + ind.series[j].value)
28+
f_weights.push(last_weight * smooth_gap + 1)
29+
30+
last_sum = f_sum[j]
31+
last_weight = f_weights[j]
32+
last_step = ind.series[j].step
2233
}
2334

24-
let backward_pass: number[] = []
25-
lastSmoothed = ind.series[ind.series.length - 1].value
35+
last_sum = 0
36+
last_weight = 0
37+
last_step = ind.series[ind.series.length - 1].step
2638
for (let j = ind.series.length - 1; j >= 0; j--) {
27-
let smoothed = lastSmoothed * (1 - smoothingFactor) + ind.series[j].value * smoothingFactor
28-
backward_pass.push(smoothed)
29-
lastSmoothed = smoothed
39+
let smooth_gap = Math.pow(smoothingFactor, Math.abs(ind.series[j].step - last_step))
40+
b_sum.push(last_sum * smooth_gap + ind.series[j].value)
41+
b_weights.push(last_weight * smooth_gap + 1)
42+
43+
last_sum = b_sum[b_sum.length - 1]
44+
last_weight = b_weights[b_weights.length - 1]
45+
last_step = ind.series[j].step
3046
}
31-
backward_pass = backward_pass.reverse()
47+
b_weights.reverse()
48+
b_sum.reverse()
3249

3350
for (let j = 0; j < ind.series.length; j++) {
34-
let smoothed = (forward_pass[j] + backward_pass[j]) / 2
35-
result.push({step: ind.series[j].step, value: ind.series[j].value, smoothed: smoothed,
36-
lastStep: ind.series[j].lastStep})
51+
let smoothed = (f_sum[j] + b_sum[j] - ind.series[j].value) /
52+
(f_weights[j] + b_weights[j] - 1)
53+
result.push({step: ind.series[j].step, value: ind.series[j].value,
54+
smoothed: smoothed, lastStep: ind.series[j].lastStep})
3755
}
3856

3957
ind.series = result

0 commit comments

Comments
 (0)