Skip to content

Instantly share code, notes, and snippets.

@ThisIsTian
Last active December 10, 2018 17:58
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 ThisIsTian/f100674b1d4b50246e089cd55ae36074 to your computer and use it in GitHub Desktop.
Save ThisIsTian/f100674b1d4b50246e089cd55ae36074 to your computer and use it in GitHub Desktop.
Exponentioal Moving Variance and the standard Variance comparison
%==========================================================================
% Exponentioal Moving Variance and the standard Variance comparison
% Created by: Tiantian Xie (Dec. 10th, 2018)
%==========================================================================
clear;
close all;
NumberOfSamples = 500;
X = rand(NumberOfSamples,1);
WindowSize = 25;
% calculate the correct variance
for i=1:(NumberOfSamples-WindowSize+1)
Slice =X(i:i+WindowSize-1);
YVar(WindowSize-1+i) = var(Slice);
end
% calculate exponential moving variance
Mu=0;
V =0;
Alpha = 0.04;
for i=1:NumberOfSamples
%ref:http://people.ds.cam.ac.uk/fanf2/hermes/doc/antiforgery/stats.pdf
Diff = X(i)-Mu;
Incr = Alpha*Diff;
Mu = Mu + Incr;
V = (1-Alpha)*(V+Incr*Diff);
YEMV(i) = V;
end
% draw the plot
figure('Position',[0,0,800,400]);
plot(1:NumberOfSamples,YVar,1:NumberOfSamples,YEMV,'linewidth',2);
set(gca,'FontSize',20)
xlabel('Sample');
ylabel('Variance');
title('Exponential moving variance v.s. variance');
L=legend('VAR ($N=25$)','EMV ($\alpha=0.04$)');
set(L, 'interpreter', 'latex');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment