indiTraders - Forum for the Active Indian Trader  

Go Back   indiTraders - Forum for the Active Indian Trader > Tools > Software > Matlab

FXPrimus
Reply
 
Bookmark and Share LinkBack (2) Thread Tools Display Modes
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 19-08-2010, 05:50 PM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,830 Times in 1,156 Posts
Oxymoron has a reputation beyond repute
Oxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond repute
Default 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 White-Noise%
[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;
Returns p,n and q from AR(p), I(n) and MA(q) values along with the model definition in case the series is not white noise. The only downside is that it doesn't check for AR 0 MA 1 models, which you will have to do manually.

For more information on ARIMA, check out Hamilton. It's mostly theoretical junk anyway. Did it as a coding exercise.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 11 Users Say Thank You to Oxymoron For This Useful Post:
  #2 (permalink)  
Old 19-08-2010, 07:28 PM
musterion's Avatar
musterion musterion is offline
Professor Satchafunkilus
 
Join Date: Mar 2009
Location: planet earth
Posts: 649
Thanks: 633
Thanked 1,313 Times in 466 Posts
musterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to behold
Default

oxy,,
on a serious note,hats off to u
__________________
:meditating: Calculation must measure the incalculable. :meditating:
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following User Says Thank You to musterion For This Useful Post:
  #3 (permalink)  
Old 22-08-2010, 09:10 PM
musterion's Avatar
musterion musterion is offline
Professor Satchafunkilus
 
Join Date: Mar 2009
Location: planet earth
Posts: 649
Thanks: 633
Thanked 1,313 Times in 466 Posts
musterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to beholdmusterion is a splendid one to behold
Default

oxy,
how u have picked up programing in matlab,,how to learn coding in matlab
__________________
:meditating: Calculation must measure the incalculable. :meditating:
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 3 Users Say Thank You to musterion For This Useful Post:
  #4 (permalink)  
Old 22-08-2010, 09:52 PM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,830 Times in 1,156 Posts
Oxymoron has a reputation beyond repute
Oxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond repute
Default

Originally Posted by musterion View Post
oxy,
how u have picked up programing in matlab,,how to learn coding in matlab

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 4 Users Say Thank You to Oxymoron For This Useful Post:
  #5 (permalink)  
Old 22-12-2010, 07:50 AM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,830 Times in 1,156 Posts
Oxymoron has a reputation beyond repute
Oxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond repute
Default 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, AR-MA 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 White-Noise%
[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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 2 Users Say Thank You to Oxymoron For This Useful Post:
  #6 (permalink)  
Old 13-05-2011, 07:08 PM
cincin cincin is offline
Newbie
 
Join Date: May 2011
Posts: 2
Thanks: 2
Thanked 2 Times in 2 Posts
cincin is on a distinguished road
Default

Hi oxymoron
Could you tell me where can i download nifty.mat?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following User Says Thank You to cincin For This Useful Post:
  #7 (permalink)  
Old 13-05-2011, 07:25 PM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,830 Times in 1,156 Posts
Oxymoron has a reputation beyond repute
Oxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond repute
Default

Originally Posted by cincin View Post
Hi oxymoron
Could you tell me where can i download nifty.mat?

Hi cincin,
You can use any closing price series instead of Nifty.mat ; just be sure to change the name in the code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 13-05-2011, 07:59 PM
cincin cincin is offline
Newbie
 
Join Date: May 2011
Posts: 2
Thanks: 2
Thanked 2 Times in 2 Posts
cincin is on a distinguished road
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following User Says Thank You to cincin For This Useful Post:
  #9 (permalink)  
Old 13-05-2011, 09:05 PM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,830 Times in 1,156 Posts
Oxymoron has a reputation beyond repute
Oxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond repute
Default

Originally Posted by cincin View Post
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

Add this to Matlab Directory
Code:
http://hotfile.com/dl/117686622/bb54715/Nifty.mat.html


What are you using this for, btw?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following User Says Thank You to Oxymoron For This Useful Post:
  #10 (permalink)  
Old 02-09-2011, 11:02 AM
Saima Saima is offline
Newbie
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Saima is on a distinguished road
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following User Says Thank You to Saima For This Useful Post:
Reply

indiTraders - Forum for the Active Indian Trader > Tools > Software > Matlab


Tags
arima, arima automation, hamilton, matlab, monte carlo simulation, time series analysis

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


LinkBacks (?)
LinkBack to this Thread: http://www.inditraders.com/matlab/3672-matlab-code-arima-automation.html
Posted By For Type Date
Akaike Information Criterion Matlab Code at Askives This thread Refback 02-05-2013 03:45 AM
Mat Automation - Download This thread Refback 06-01-2012 08:46 AM

FXPrimus

All times are GMT +5.5. The time now is 09:57 AM.


vBulletin Copyright by vBulletin

Content Relevant URLs by vBSEO 3.3.2