%% Test Stock Data for a Random Walk
% This example shows how to assess whether a time series is a random walk. It uses market data for daily returns of stocks and cash (money market) from the period January 1, 2000 to November 7, 2005.
% Load the data.

load CAPMuniverse

%
% Extract two series to test. The first column of data is the daily return of a technology stock. The last (14th) column is the daily return for cash (the daily money market rate).

tech1 = Data(:,1); 
money = Data(:,14);

% The returns are the logs of the ratios of values at the end of a day over the values at the beginning of the day.
%
% Convert the data to prices (values) instead of returns. vratiotest takes prices as inputs, as opposed to returns.

tech1 = cumsum(tech1);
money = cumsum(money);

%
% Plot the data to see whether they appear to be stationary.

subplot(2,1,1)
plot(Dates,tech1);
title('Log(relative stock value)')
datetick('x')
hold on
subplot(2,1,2);
plot(Dates,money)
title('Log(accumulated cash)')
datetick('x')
hold off

% Cash has a small variability, and appears to have long-term trends. The stock series has a good deal of variability, and no definite trend, though it appears to increase towards the end.
%
% Test whether the stock series matches a random walk.

[h,pValue,stat,cValue,ratio] = vratiotest(tech1)

% vratiotest does not reject the hypothesis that a random walk is a reasonable model for the stock series.
%
% Test whether an i.i.d. random walk is a reasonable model for the stock series.

[h,pValue,stat,cValue,ratio] = vratiotest(tech1,'IID',true)

% vratiotest rejects the hypothesis that an i.i.d. random walk is a reasonable model for the tech1 stock series at the 5% level. Thus, vratiotest indicates that the most appropriate model of the tech1 series is a heteroscedastic random walk.
%
% Test whether the cash series matches a random walk.

[h,pValue,stat,cValue,ratio] = vratiotest(money)

% vratiotest emphatically rejects the hypothesis that a random walk is a reasonable model for the cash series (pValue = 4.6093e-145). The removal of a trend from the series does not affect the resulting statistics.
% Copyright 2015 The MathWorks, Inc.