%------------------------------------------------------------
% Tony Hyun Kim
% CS 246, PS 4, Problem 1(a)
% Mean reversion
%------------------------------------------------------------
dp = diff(p);
truth = sign(dp); % Did the price go up or down
pred  = zeros(size(truth));
Npred = length(truth);

pred(1) = 2*randi(2)-3; % Choose +1 or -1 randomly for initial guess
for i = 2:Npred
    if(truth(i-1)==0)
        pred(i) = pred(i-1);   % Maintain the previous forecast
    else % Price moved
        pred(i) = -truth(i-1); % Then predict other direction
    end
end

score = sum(truth'*pred)/Npred;
fprintf('Mean reversion score is %.4f\n',score);