mat=readmat('data.txt');
mat=mat(180:1000,633:635);
rmat=flipud(mat);
rmat=diff(log(rmat));
rmat=delzeros(rmat);
h=250;


i=1
for Col = rmat
    %%%%%%%%%%%%%%%%% GARCH FIT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    [Coeff,Errors,LLF,Innovations,Sigmas,Summary] = garchfit(Col);
    %%%%%%%%%%%%%% GETGARCH PARAM-- COMPUTE MEAN REVERSION RATE%%%%%%%%%%%%%%%%%
    beta = garchget(Coeff, 'GARCH');
    alpha= garchget(Coeff, 'ARCH');
    w = garchget(Coeff, 'K');
    lambda = alpha + beta;
    rateMeanRev = 1/(1-lambda);
    halflife = 1- log(2)/log(lambda);
    
    %%%%%%%%%%%%%%%%%%%%%GARCH PREDICTOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    allsigs(1:length(Sigmas),i) = Sigmas;
    s= garchfit(Col);
    future=garchpred(s, Col, h);
    futmat(1:length(future),i) = future;
    
    x2=[length(rmat)+1:length(Col)+h];
    x1=[1:length(Col)];

    ylabel('volatility / return')
    subplot(2,1,1)
    plot(Innovations)
    axis([0, 900, -Inf, Inf]);
    subplot(2,1,2);
    plot(x1,allsigs(1:length(allsigs),i),'-',x2,futmat(1:length(futmat),i),'-'); 
    axis([0,900, -Inf, Inf])
    i = i+1;
end
