# Exponential smoothing for non-constant time steps

**URL:** https://discourse.julialang.org/t/exponential-smoothing-for-non-constant-time-steps/121236
**Category:** General Usage
**Tags:** smoothing
**Created:** [October 12, 2024, 10:13pm UTC](https://discourse.julialang.org/t/exponential-smoothing-for-non-constant-time-steps/121236 "2024-10-12T22:13:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [October 12, 2024, 10:13pm UTC](https://discourse.julialang.org/t/exponential-smoothing-for-non-constant-time-steps/121236/1 "2024-10-12T22:13:26Z")

</div>

I played around with moving averages, but for an **iterative** algorithm and **low memory** requirements (storing a lot of values), exponential smoothing seems the way to go.

To calculate the smoothed value over time I came up with this monster:

```julia
smooth(t,x,w) = getindex.(
  accumulate(
    (a,b)->(b[1],(a[2]*exp(-a[1]/w)+(1-exp(-a[1]/w))*b[2])),
    Iterators.zip(vcat(diff(t),0),x)
  ),2)

```

Is there a package that does something like this nicer / faster?

Here is an example:

```julia
t = rand(500)
t[170:200] .*=20
t = cumsum(t)
x = rand(500)
x[200:250] .*=10
x = cumsum(x)

using GLMakie
plot(t,x)
w = 20
plot!(t.-w,smooth(t,x,w))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/dec7f782780db7636b42749bd716fba7c40c62ff.png)

```julia
julia> using BenchmarkTools
julia> @benchmark smooth(t,x,w)
@benchmark smooth(t,x,w)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 13.840 μs … 97.470 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 19.480 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 20.006 μs ± 2.835 μs ┊ GC (mean ± σ): 0.00% ± 0.00%

                                   ▅█▆▂                        
  ▂▂▂▂▂▁▂▁▁▂▂▂▂▂▂▂▁▁▂▂▂▂▁▁▂▁▂▂▂▂▂▂▄████▇▅▇▆▆▄▃▃▃▂▄▅▅▄▃▃▃▃▃▃▂▂ ▃
  13.8 μs Histogram: frequency by time 22.9 μs <

 Memory estimate: 20.12 KiB, allocs estimate: 4.

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 12, 2024, 10:34pm UTC](https://discourse.julialang.org/t/exponential-smoothing-for-non-constant-time-steps/121236/2 "2024-10-12T22:34:48Z")

</div>

Check [this post](https://discourse.julialang.org/t/exponential-smoothing/41020/5).

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [October 12, 2024, 10:40pm UTC](https://discourse.julialang.org/t/exponential-smoothing-for-non-constant-time-steps/121236/3 "2024-10-12T22:40:33Z")

</div>

I don’t see the option to use time values in the discussed cases.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 12, 2024, 10:48pm UTC](https://discourse.julialang.org/t/exponential-smoothing-for-non-constant-time-steps/121236/4 "2024-10-12T22:48:01Z")

</div>

OK, but maybe it’s worth trying with linear interpolation on a constant time step?

Another package:  
[Moving Averages · MarketTechnicals.jl (juliahub.com)](https://docs.juliahub.com/MarketTechnicals/2Q1wL/0.6.0/ma/)
