Last active
September 11, 2019 21:02
-
-
Save swiesend/0610dedb1eb8a0071cc3ec80f19d09f3 to your computer and use it in GitHub Desktop.
OnlineStats.jl - Exponentially Weighted Moving Average with a Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
continually