

Matlab Code : ARIMA Automation
My first real work in Matlab!
Just change the "load" command to your .mat file's name and Input from Close to your variable name. It automatically checks for series stationarity, does the differencing to produce a stationary series, checks for white noise, if not white noise, runs the ARMA regression with different parameters and chooses the parameters that have lowest Akaike information criterion. Code:
%Loading Data% load NiftyFut.mat; %Inputs% Input = Close; WNLagPeriod = input(' Enter WNLagPeriod: '); %White Noise Lag Period% MaxI = 15; %Maximum integrand period% MaxAR = 10; %Maximum AR lookback% MaxMA = 10; %Maximum MA lookback% %Checking for I(n) process% VarianceMatrix = []; for I = 1: MaxI VarianceMatrix = [VarianceMatrix; var(diff(Input,I))]; end IMatrix = find(ismember(VarianceMatrix, min(VarianceMatrix))==1); if var(Input) < min(VarianceMatrix) I = 0; else I = IMatrix; end %Making Data Stationary% if I > 0 StationaryInput = diff(Input,I); else StationaryInput = Input; end; %Testing for WhiteNoise% [LBTestH,LBTestValue,LBTeststat,LBTestCriticalValue] = lbqtest(StationaryInput, WNLagPeriod,[],[]); clear LBTestValue LBTeststat LBTestCriticalValue WNLagPeriod if LBTestH == 0 disp(['The process is white noise at I =',num2str(I)]); else %Selecting AR & MA Process based on AIC% Y = []; for AR = 1: MaxAR for MA = 0: MaxMA Y = [Y; aic(armax(StationaryInput,[AR,MA])),AR,MA]; end end clear AR MA AIC = Y(:,1); AR = Y(:,2); MA = Y(:,3); clear Y; RowNumber = find(ismember(AIC, min(AIC))==1); ARp = AR(RowNumber); MAq = MA(RowNumber); model = armax(StationaryInput,[ARp,MAq]); end; clear LBTestH VarianceMatrix IMatrix MaxAR MaxMA MaxI AIC AR MA Input RowNumber StationaryInput; For more information on ARIMA, check out Hamilton. It's mostly theoretical junk anyway. Did it as a coding exercise. 
The Following 11 Users Say Thank You to Oxymoron For This Useful Post:  
The Following User Says Thank You to musterion For This Useful Post:  


I just picked up an objective and started coding it step by step. Each step posed a constraint and with each constraint, I learned something new. What you see here is a very polished code. The build up was rather inelegant. This code actually gave me a great deal of comfort with the matlab language. 


New and Improved
Fully automated ARIMA code. Does the following:
1) Checks for stationarity and if nonstationary, makes the difference adjustments for Ith order accordingly. 2) Checks for White Noise on the stationary series using Ljung Box 3) If not a White Noise, ARMA combination is run and the one with the best Bayesian Information Criterion is selected. With a slight tweak in garchfit code, explanatory variables can be included as well. Sections to customize: %Loading Data% %Inputs% Requires 'Econometrics Toolbox' Code:
tic %Loading Data% load Nifty.mat; %Inputs% Input = Close; WNLagPeriod = 10; %White Noise Lag Period% MaxI = 5; %Maximum integrand period% MaxAR = 5; %Maximum AR lookback% MaxMA = 5; %Maximum MA lookback% %Checking for I(n) process% VarianceMatrix = zeros(MaxI,1); for I = 1: MaxI VarianceMatrix(I,1) = var(diff(Input,I)); end IMatrix = find(ismember(VarianceMatrix, min(VarianceMatrix))==1); if var(Input) < min(VarianceMatrix) I = 0; else I = IMatrix; end %Making Data Stationary% if I > 0 StationaryInput = diff(Input,I); else StationaryInput = Input; end; [row1,~] = size(StationaryInput); %Testing for WhiteNoise% [LBTestH,~,~,~] = lbqtest(StationaryInput, WNLagPeriod,[],[]); clear LBTestValue LBTeststat LBTestCriticalValue WNLagPeriod %Start Iterations% if LBTestH == 0 disp(['The process is white noise at I = ', num2str(I)]); else %Selecting AR & MA Process based on AIC% Z = zeros(MaxAR + 1,MaxMA+1); for MA = 0: MaxMA for AR = 0: MaxAR i = AR + 1; j = MA + 1; spec = garchset('R', AR, 'M', MA, 'P', 0, 'Q', 0); [Coeff,~,LLF,~,~,~] = garchfit(spec,StationaryInput); Parameters = garchcount(Coeff) [~,BIC] = aicbic(LLF,Parameters,row1); Z(i,j) = BIC; end end clear AR MA spec Coeff LLF AIC CSize i j row1 [row,column] = find(ismember(Z, min(min(Z),[],2))); ARp = row  1; MAq = column  1; spec = garchset('R',ARp,'M',MAq,'P',0,'Q',0); [FinalCoeff,FinalErrors,~,FinalInnovations,~,FinalSummary] = garchfit(spec,StationaryInput); garchdisp(FinalCoeff, FinalErrors) RSquared = 1  corr(StationaryInput,FinalInnovations) end; clear row column Input RowNumber StationaryInput Parameters; clear LBTestH VarianceMatrix IMatrix MaxAR MaxMA MaxI AIC AR MA; toc 





Could you be so kind and send me an example of the input data file? I really need it!
Sorry i am not good at english Thank you 
The Following User Says Thank You to cincin For This Useful Post:  



The Following User Says Thank You to Oxymoron For This Useful Post:  


Hi oxymoron
I use the same code in Matlab with the Nifty.mat data. it gave me this message "clear LBTestH VarianceMatrix IMatrix MaxAR MaxMA MaxI AIC AR MA Input RowNumber StationaryInput;" what should I do next so that it can give me the valus of p,n and q also if I run the same code again and again would it give me the same ARIMA parameters or will gave other ARIMA models, as for one dataset one may have more than one ARIMA models. Thanks 
The Following User Says Thank You to Saima For This Useful Post:  
Tags 
arima, arima automation, hamilton, matlab, monte carlo simulation, time series analysis 
Thread Tools  
Display Modes  


LinkBacks (?)
LinkBack to this Thread: http://www.inditraders.com/matlab/3672matlabcodearimaautomation.html


Posted By  For  Type  Date  
Akaike Information Criterion Matlab Code at Askives  This thread  Refback  02052013 03:45 AM  
Mat Automation  Download  This thread  Refback  06012012 08:46 AM 