%% Assess Whether a Series Is a Random Walk
% Test whether a US equity index is a random walk using various step sizes. Perform the test with and without the assumption that the innovations are independent and identically distributed.
% Load the global large-cap equity indices data set. Focus on the daily S & P 500 index (SP).

load Data_GlobalIdx1
logSP = log(DataTable.SP);

figure
plot(diff(logSP))
axis tight

% The plot indicates possible conditional heteroscedasticity.
%
% Test whether the series is a random walk using various periods and whether the innovations are independent and identically distributed.

q = [2 4 8 2 4 8];
flag = logical([1 1 1 0 0 0]);
[h,pValue,stat,cValue,ratio] = ...
        vratiotest(logSP,'period',q,'IID',flag)
rho1 = ratio(1)-1 % First-order autocorrelation of returns

% h indicates that the test fails to reject that the series is a random walk at 5% level, except in the case where period = 8 and IID = true. This rejection is likely due to the test not accounting for the heteroscedasticity.
% Copyright 2015 The MathWorks, Inc.