Skip to content

Instantly share code, notes, and snippets.

@swiesend
Last active September 11, 2019 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swiesend/0610dedb1eb8a0071cc3ec80f19d09f3 to your computer and use it in GitHub Desktop.
Save swiesend/0610dedb1eb8a0071cc3ec80f19d09f3 to your computer and use it in GitHub Desktop.
OnlineStats.jl - Exponentially Weighted Moving Average with a Window
cd(@__DIR__)
using OnlineStats
using Dates
using Plots
gr()
function test()
data = Plots.fakedata(250,2) .+ [2 1]
n,m= size(data)
ewmas = zeros(0,2)
labels = ["dogs","cats","ewma(dogs)","ewma(cats)"]
linetypes = [:scatter :scatter :path :path]
colors = [:red :blue :darkblue :darkred]
ewma = [
Mean(weight = ExponentialWeight(.01)),
Mean(weight = ExponentialWeight(.01))
]
anim = @animate for i=1:n
ys = @view data[1:i,:]
ewmas = vcat(ewmas, [value(fit!(ewma[j], ys[i,j])) for j in 1:m]')
plot(hcat(ys,ewmas),
line=(linetypes,:auto,2,colors),
labels=labels,
title="Exponentially Weighted Moving Average")
end every 1
gif(anim, "../data/anim/continually.gif")
end
test()
cd(@__DIR__)
using OnlineStats
using Dates
using Plots
gr()
function test(data, window=10)
n,m= size(data)
ewmas = zeros(0,2)
labels = ["dogs","cats","ewma(dogs,w=$window)","ewma(cats,w=$window)"]
linetypes = [:scatter :scatter :path :path]
colors = [:red :blue :darkblue :darkred]
anim = @animate for i=1:n
ys = @view data[max(1,i-window+1):i,:]
@show i, size(ys)
ewmas = vcat(ewmas, [value(fit!(Mean(weight = ExponentialWeight(.01)), ys[:,j])) for j in 1:m]')
is = @view data[1:i,:]
plot(hcat(is,ewmas),
line=(linetypes,:auto,2,colors),
labels=labels,
title="Exponentially Weighted Moving Average (window=$window)",
size=(600,400))
end every 1
gif(anim, "../data/anim/window_$window.gif")
end
data = Plots.fakedata(250,2) .+ [2 1]
test(data, 10)
test(data, 25)
test(data, 50)
test(data, 75)
test(data, 100)
@swiesend
Copy link
Author

swiesend commented Sep 11, 2019

continually

continually

@swiesend
Copy link
Author

window

window_10
window_25
window_50
window_75
window_100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment