SlideShare a Scribd company logo
1 of 5
%% Estimating ARMA models
clear all; clc; close all
load('Term');
% AR(1) Estimation
[parameters1, ~, ~, ~, ~, ~, ~] = armaxfilter(term, 1, 1);
disp(parameters1)
T=1000
constant=parameters1(1)
ar_proc = armaxfilter_simulate(T, constant, 1, parameters1(2), 0, 0);
% compute standard errors for parameters
stderror1=std(ar_proc)/sqrt(T)
% MA(3) Estimation
[parameters2, ~, ~, ~, ~, ~, ~] = armaxfilter(term, 1, [], 1:3);
disp(parameters2)
T=1000
constant=parameters2(1)
ma_proc = armaxfilter_simulate(T, constant, 0, 0, 3, parameters2(2:4));
% compute standard errors for parameters
stderror2=std(ma_proc)/sqrt(T)
% ARMA(1,1) estimation combines the two inputs.
[parameters3, ~, ~, ~, ~, ~, ~] = armaxfilter(term, 1, 1, 1);
disp(parameters3)
T=1000
constant=parameters3(1)
arma_proc = armaxfilter_simulate(T, constant, 1, parameters3(2), 1,
parameters3(3));
% compute standard errors for parameters
stderror3=std(arma_proc)/sqrt(T)
%% Model Selection Exercise
T=1000; P=4; Q=4;
AIC_MODELS=zeros(P+1,Q+1);
SBIC_MODELS=zeros(P+1,Q+1);
% Estimate ARMA models
for p=0:P
for q=0:Q
[~, ~, ~, ~, diagnostics]=
armaxfilter(term,1,1:p,1:q,[],[],[],P);
AIC_MODELS(p+1,q+1)= diagnostics.AIC ;
SBIC_MODELS(p+1,q+1)=diagnostics.SBIC;
end
end
disp(AIC_MODELS(:));
disp(SBIC_MODELS(:));
[~,I]=min(SBIC_MODELS(:));
[ar_coeff,ma_coeff]=ind2sub(size(SBIC_MODELS),I);
ar_coeff=ar_coeff-1;
ma_coeff=ma_coeff-1;
[parameters, ~, errors]= armaxfilter(term,1,1:ar_coeff,1:ma_coeff);
display ([parameters' 0 0 ar_coeff ma_coeff])
disp(errors)
% Ljung-Box and LM Test
% The LJ and LM test look at multiple correlations simultaneously.
These
% results output the test statistic and the p-value.
disp('Lung-Box Test (Test stat, p-value)' )
[ts,pv]=ljungbox(errors,12);
disp([ts,pv])
disp('LM Test (Test stat, p-value)' )
[ts,pv]=lmtest1(errors,12);
disp([ts,pv])
% Ljung-Box and LM Test for the random walk model
errors_rw=term(2:end)-term(1:end-1);
disp(errors_rw)
disp('Lung-Box Test (Test stat, p-value)' )
[ts,pv]=ljungbox(errors_rw,12);
disp([ts,pv])
disp('LM Test (Test stat, p-value)' )
[ts,pv]=lmtest1(errors_rw,12);
disp([ts,pv])
%% Forecasting AR, MA and ARMA models:
% Estimating the parameters on the first "T-hold_out" observations and
% forecasting out-of-sample the last "hold_out" observations.
% Generating the parameters of the model: T observations
T=729;
hold_out=364;
phi = parameters3(2);
theta= parameters3(3);
ARorder = 1;
MAorder=1;
constant= parameters3(1);
Last_in_sample_obs=T-hold_out;
% Compute the roots to guarantee stationarity
roots_check=armaroots([constant; phi;theta],1,1,1);
display(roots_check)
% Simulating the Model
sim_time_series = armaxfilter_simulate(T, constant, ARorder, phi,
MAorder, theta);
% Estimating the model on the observations before the holdout period
starts
[parameters, ~, ~]=
armaxfilter(sim_time_series(1:Last_in_sample_obs),1,1,1);
display(parameters)
%%%%%
forecast=zeros(T-Last_in_sample_obs,1);
forecast_rw=zeros(T-Last_in_sample_obs,1);
realization=zeros(T-Last_in_sample_obs,1);
% Generating forecasts at horizons 1 through "T-holdout"
for s=1:hold_out
[forecast_temp,realization_temp,~,~]=...
arma_forecaster(sim_time_series,parameters,1,1,1,Last_in_sample_obs,s);
[forecast_temp_rw,~,~,~]=...
arma_forecaster(sim_time_series,1,0,1,0,Last_in_sample_obs,s);
forecast(s)=forecast_temp(Last_in_sample_obs);
forecast_rw(s)=forecast_temp_rw(Last_in_sample_obs);
realization(s)=realization_temp(Last_in_sample_obs);
end
% Concatenating the forecasts with the in-sample observations
forecasts=[sim_time_series(1:Last_in_sample_obs); forecast];
forecasts_rw=[sim_time_series(1:Last_in_sample_obs); forecast_rw];
%% Evaluating the forecasts of AR, MA and ARMA models:
% Estimating the model on the observations before the holdout period
starts
[parameters, LL, ~]=
armaxfilter(sim_time_series(1:Last_in_sample_obs),0,1,1);
display(parameters)
% Generate 1-step out-sample-forecasts
[forecasts,realizations,~]=...
arma_forecaster(sim_time_series,parameters,0,1,1,Last_in_sample_obs,1);
[forecasts_rw,~,errors]=...
arma_forecaster(sim_time_series,1,0,1,0,Last_in_sample_obs,1);
% Keep realizations and forecasts available
valid = all(~isnan(errors),2);
forecasts=forecasts(valid,:);
forecasts_rw=forecasts_rw(valid,:);
realizations=realizations(valid,:);
% MZ Regressions
% Computing the tests for the ARMA forecasts
% Run regression, plain OLS with white errors
[beta,~,~,~,vcv_white] = ols(realizations,forecasts,1);
% Display parameters and t-stats
disp(' Params T-stats')
disp([beta beta./sqrt(diag(vcv_white))]);
% Display the F-test
r=2;
A=[1 0;0 1];
c =[0;1];
disp('Wald Stat')
% Here I construct the F-test
disp((A*beta-c)'*...
inv(A*vcv_white*A')*...
(A*beta-c));
% Here I construct the p-value
disp('P-value (Wald)')
disp(1-chi2cdf((A*beta-c)'*...
inv(A*vcv_white*A')*...
(A*beta-c),2))
% Computing the tests for the RW forecasts
% Run regression, plain OLS with white errors
[beta,~,~,~,vcv_white] = ols(realizations,forecasts_rw,1);
% Display parameters and t-stats
disp(' Params T-stats')
disp([beta beta./sqrt(diag(vcv_white))]);
% Display the F-test
r=2;
A=[1 0;0 1];
c =[0;1];
disp('Wald Stat')
% Here I construct the F-test
disp((A*beta-c)'*...
inv(A*vcv_white*A')*...
(A*beta-c));
% Here I construct the p-value
disp('P-value (Wald)')
disp(1-chi2cdf((A*beta-c)'*...
inv(A*vcv_white*A')*...
(A*beta-c),2))
% Diebold Mariano tests
% DM tests use the squarred errors and a OLS regression with NW errors.
% Positive indicate the random walk model is preferred
% Negative indicate the ARMA model is preferred,
[dm,tstat,~,~,vcv_white]=...
ols((realizations-forecasts).^2 - (realizations-
forecasts_rw).^2,[],1);
disp('DM , MSE (if positive: it prefers Random Walk, If negative:
prefers ARMA), h=1')
disp(dm./sqrt(vcv_white))

More Related Content

What's hot

Stability of Control System
Stability of Control SystemStability of Control System
Stability of Control Systemvaibhav jindal
 
Time response analysis
Time response analysisTime response analysis
Time response analysisKaushal Patel
 
Isen 614 project report
Isen 614 project reportIsen 614 project report
Isen 614 project reportVanshaj Handoo
 
SERENE 2014 Workshop: Paper "Verification and Validation of a Pressure Contro...
SERENE 2014 Workshop: Paper "Verification and Validation of a Pressure Contro...SERENE 2014 Workshop: Paper "Verification and Validation of a Pressure Contro...
SERENE 2014 Workshop: Paper "Verification and Validation of a Pressure Contro...SERENEWorkshop
 
Modern Control - Lec 06 - PID Tuning
Modern Control - Lec 06 - PID TuningModern Control - Lec 06 - PID Tuning
Modern Control - Lec 06 - PID TuningAmr E. Mohamed
 
Time domain analysis
Time domain analysisTime domain analysis
Time domain analysisHussain K
 
Time response of discrete systems 4th lecture
Time response of discrete systems 4th lectureTime response of discrete systems 4th lecture
Time response of discrete systems 4th lecturekhalaf Gaeid
 
Solution Manual for Mechanics of Flight – Warren Phillips
Solution Manual for Mechanics of Flight – Warren PhillipsSolution Manual for Mechanics of Flight – Warren Phillips
Solution Manual for Mechanics of Flight – Warren PhillipsHenningEnoksen
 

What's hot (10)

Control chap1
Control chap1Control chap1
Control chap1
 
Stability of Control System
Stability of Control SystemStability of Control System
Stability of Control System
 
Time response analysis
Time response analysisTime response analysis
Time response analysis
 
time response analysis
time response analysistime response analysis
time response analysis
 
Isen 614 project report
Isen 614 project reportIsen 614 project report
Isen 614 project report
 
SERENE 2014 Workshop: Paper "Verification and Validation of a Pressure Contro...
SERENE 2014 Workshop: Paper "Verification and Validation of a Pressure Contro...SERENE 2014 Workshop: Paper "Verification and Validation of a Pressure Contro...
SERENE 2014 Workshop: Paper "Verification and Validation of a Pressure Contro...
 
Modern Control - Lec 06 - PID Tuning
Modern Control - Lec 06 - PID TuningModern Control - Lec 06 - PID Tuning
Modern Control - Lec 06 - PID Tuning
 
Time domain analysis
Time domain analysisTime domain analysis
Time domain analysis
 
Time response of discrete systems 4th lecture
Time response of discrete systems 4th lectureTime response of discrete systems 4th lecture
Time response of discrete systems 4th lecture
 
Solution Manual for Mechanics of Flight – Warren Phillips
Solution Manual for Mechanics of Flight – Warren PhillipsSolution Manual for Mechanics of Flight – Warren Phillips
Solution Manual for Mechanics of Flight – Warren Phillips
 

Similar to Assignment1code1

I need help understanding the Pan Tompkins algorythm, and Id also .pdf
I need help understanding the Pan Tompkins algorythm, and Id also .pdfI need help understanding the Pan Tompkins algorythm, and Id also .pdf
I need help understanding the Pan Tompkins algorythm, and Id also .pdfeyewatchsystems
 
Write a Matlab code (a computerized program) for calculating plane st.docx
 Write a Matlab code (a computerized program) for calculating plane st.docx Write a Matlab code (a computerized program) for calculating plane st.docx
Write a Matlab code (a computerized program) for calculating plane st.docxajoy21
 
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdfConsider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdfmeerobertsonheyde608
 
Matlab kod taslağı
Matlab kod taslağıMatlab kod taslağı
Matlab kod taslağıMerve Cvdr
 
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfIncorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfaartechindia
 
Systemic Arterial Pulse Pressure Analysis
Systemic Arterial Pulse Pressure AnalysisSystemic Arterial Pulse Pressure Analysis
Systemic Arterial Pulse Pressure AnalysisCody Pilot
 
R/Finance 2009 Chicago
R/Finance 2009 ChicagoR/Finance 2009 Chicago
R/Finance 2009 Chicagogyollin
 
arimamodel-170204090012.pdf
arimamodel-170204090012.pdfarimamodel-170204090012.pdf
arimamodel-170204090012.pdfssuserdca880
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdfsowmya koneru
 
Arima model
Arima modelArima model
Arima modelJassika
 
Fourier series example
Fourier series exampleFourier series example
Fourier series exampleAbi finni
 
Adding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMPAdding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMPJacques Rioux
 
Time series Modelling Basics
Time series Modelling BasicsTime series Modelling Basics
Time series Modelling BasicsAshutosh Kumar
 

Similar to Assignment1code1 (20)

I need help understanding the Pan Tompkins algorythm, and Id also .pdf
I need help understanding the Pan Tompkins algorythm, and Id also .pdfI need help understanding the Pan Tompkins algorythm, and Id also .pdf
I need help understanding the Pan Tompkins algorythm, and Id also .pdf
 
Write a Matlab code (a computerized program) for calculating plane st.docx
 Write a Matlab code (a computerized program) for calculating plane st.docx Write a Matlab code (a computerized program) for calculating plane st.docx
Write a Matlab code (a computerized program) for calculating plane st.docx
 
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdfConsider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
 
matlab codes.pdf
matlab codes.pdfmatlab codes.pdf
matlab codes.pdf
 
AGC PROGRAM.docx
AGC PROGRAM.docxAGC PROGRAM.docx
AGC PROGRAM.docx
 
Matlab kod taslağı
Matlab kod taslağıMatlab kod taslağı
Matlab kod taslağı
 
array
arrayarray
array
 
PSOGlobalSearching
PSOGlobalSearchingPSOGlobalSearching
PSOGlobalSearching
 
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfIncorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
 
Systemic Arterial Pulse Pressure Analysis
Systemic Arterial Pulse Pressure AnalysisSystemic Arterial Pulse Pressure Analysis
Systemic Arterial Pulse Pressure Analysis
 
R/Finance 2009 Chicago
R/Finance 2009 ChicagoR/Finance 2009 Chicago
R/Finance 2009 Chicago
 
arimamodel-170204090012.pdf
arimamodel-170204090012.pdfarimamodel-170204090012.pdf
arimamodel-170204090012.pdf
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
Mnistauto 5
Mnistauto 5Mnistauto 5
Mnistauto 5
 
Arima model
Arima modelArima model
Arima model
 
Fourier series example
Fourier series exampleFourier series example
Fourier series example
 
Adding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMPAdding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMP
 
ETSATPWAATFU
ETSATPWAATFUETSATPWAATFU
ETSATPWAATFU
 
Time series Modelling Basics
Time series Modelling BasicsTime series Modelling Basics
Time series Modelling Basics
 
Fpga Design Project
Fpga Design ProjectFpga Design Project
Fpga Design Project
 

Assignment1code1

  • 1. %% Estimating ARMA models clear all; clc; close all load('Term'); % AR(1) Estimation [parameters1, ~, ~, ~, ~, ~, ~] = armaxfilter(term, 1, 1); disp(parameters1) T=1000 constant=parameters1(1) ar_proc = armaxfilter_simulate(T, constant, 1, parameters1(2), 0, 0); % compute standard errors for parameters stderror1=std(ar_proc)/sqrt(T) % MA(3) Estimation [parameters2, ~, ~, ~, ~, ~, ~] = armaxfilter(term, 1, [], 1:3); disp(parameters2) T=1000 constant=parameters2(1) ma_proc = armaxfilter_simulate(T, constant, 0, 0, 3, parameters2(2:4)); % compute standard errors for parameters stderror2=std(ma_proc)/sqrt(T) % ARMA(1,1) estimation combines the two inputs. [parameters3, ~, ~, ~, ~, ~, ~] = armaxfilter(term, 1, 1, 1); disp(parameters3) T=1000 constant=parameters3(1) arma_proc = armaxfilter_simulate(T, constant, 1, parameters3(2), 1, parameters3(3)); % compute standard errors for parameters stderror3=std(arma_proc)/sqrt(T) %% Model Selection Exercise T=1000; P=4; Q=4; AIC_MODELS=zeros(P+1,Q+1); SBIC_MODELS=zeros(P+1,Q+1);
  • 2. % Estimate ARMA models for p=0:P for q=0:Q [~, ~, ~, ~, diagnostics]= armaxfilter(term,1,1:p,1:q,[],[],[],P); AIC_MODELS(p+1,q+1)= diagnostics.AIC ; SBIC_MODELS(p+1,q+1)=diagnostics.SBIC; end end disp(AIC_MODELS(:)); disp(SBIC_MODELS(:)); [~,I]=min(SBIC_MODELS(:)); [ar_coeff,ma_coeff]=ind2sub(size(SBIC_MODELS),I); ar_coeff=ar_coeff-1; ma_coeff=ma_coeff-1; [parameters, ~, errors]= armaxfilter(term,1,1:ar_coeff,1:ma_coeff); display ([parameters' 0 0 ar_coeff ma_coeff]) disp(errors) % Ljung-Box and LM Test % The LJ and LM test look at multiple correlations simultaneously. These % results output the test statistic and the p-value. disp('Lung-Box Test (Test stat, p-value)' ) [ts,pv]=ljungbox(errors,12); disp([ts,pv]) disp('LM Test (Test stat, p-value)' ) [ts,pv]=lmtest1(errors,12); disp([ts,pv]) % Ljung-Box and LM Test for the random walk model errors_rw=term(2:end)-term(1:end-1); disp(errors_rw) disp('Lung-Box Test (Test stat, p-value)' ) [ts,pv]=ljungbox(errors_rw,12); disp([ts,pv]) disp('LM Test (Test stat, p-value)' ) [ts,pv]=lmtest1(errors_rw,12); disp([ts,pv]) %% Forecasting AR, MA and ARMA models: % Estimating the parameters on the first "T-hold_out" observations and % forecasting out-of-sample the last "hold_out" observations. % Generating the parameters of the model: T observations T=729; hold_out=364; phi = parameters3(2); theta= parameters3(3); ARorder = 1; MAorder=1; constant= parameters3(1);
  • 3. Last_in_sample_obs=T-hold_out; % Compute the roots to guarantee stationarity roots_check=armaroots([constant; phi;theta],1,1,1); display(roots_check) % Simulating the Model sim_time_series = armaxfilter_simulate(T, constant, ARorder, phi, MAorder, theta); % Estimating the model on the observations before the holdout period starts [parameters, ~, ~]= armaxfilter(sim_time_series(1:Last_in_sample_obs),1,1,1); display(parameters) %%%%% forecast=zeros(T-Last_in_sample_obs,1); forecast_rw=zeros(T-Last_in_sample_obs,1); realization=zeros(T-Last_in_sample_obs,1); % Generating forecasts at horizons 1 through "T-holdout" for s=1:hold_out [forecast_temp,realization_temp,~,~]=... arma_forecaster(sim_time_series,parameters,1,1,1,Last_in_sample_obs,s); [forecast_temp_rw,~,~,~]=... arma_forecaster(sim_time_series,1,0,1,0,Last_in_sample_obs,s); forecast(s)=forecast_temp(Last_in_sample_obs); forecast_rw(s)=forecast_temp_rw(Last_in_sample_obs); realization(s)=realization_temp(Last_in_sample_obs); end % Concatenating the forecasts with the in-sample observations forecasts=[sim_time_series(1:Last_in_sample_obs); forecast]; forecasts_rw=[sim_time_series(1:Last_in_sample_obs); forecast_rw]; %% Evaluating the forecasts of AR, MA and ARMA models: % Estimating the model on the observations before the holdout period starts [parameters, LL, ~]= armaxfilter(sim_time_series(1:Last_in_sample_obs),0,1,1); display(parameters) % Generate 1-step out-sample-forecasts [forecasts,realizations,~]=... arma_forecaster(sim_time_series,parameters,0,1,1,Last_in_sample_obs,1); [forecasts_rw,~,errors]=... arma_forecaster(sim_time_series,1,0,1,0,Last_in_sample_obs,1); % Keep realizations and forecasts available valid = all(~isnan(errors),2);
  • 4. forecasts=forecasts(valid,:); forecasts_rw=forecasts_rw(valid,:); realizations=realizations(valid,:); % MZ Regressions % Computing the tests for the ARMA forecasts % Run regression, plain OLS with white errors [beta,~,~,~,vcv_white] = ols(realizations,forecasts,1); % Display parameters and t-stats disp(' Params T-stats') disp([beta beta./sqrt(diag(vcv_white))]); % Display the F-test r=2; A=[1 0;0 1]; c =[0;1]; disp('Wald Stat') % Here I construct the F-test disp((A*beta-c)'*... inv(A*vcv_white*A')*... (A*beta-c)); % Here I construct the p-value disp('P-value (Wald)') disp(1-chi2cdf((A*beta-c)'*... inv(A*vcv_white*A')*... (A*beta-c),2)) % Computing the tests for the RW forecasts % Run regression, plain OLS with white errors [beta,~,~,~,vcv_white] = ols(realizations,forecasts_rw,1); % Display parameters and t-stats disp(' Params T-stats') disp([beta beta./sqrt(diag(vcv_white))]); % Display the F-test r=2; A=[1 0;0 1]; c =[0;1]; disp('Wald Stat') % Here I construct the F-test disp((A*beta-c)'*... inv(A*vcv_white*A')*... (A*beta-c)); % Here I construct the p-value disp('P-value (Wald)') disp(1-chi2cdf((A*beta-c)'*... inv(A*vcv_white*A')*... (A*beta-c),2)) % Diebold Mariano tests % DM tests use the squarred errors and a OLS regression with NW errors. % Positive indicate the random walk model is preferred % Negative indicate the ARMA model is preferred,
  • 5. [dm,tstat,~,~,vcv_white]=... ols((realizations-forecasts).^2 - (realizations- forecasts_rw).^2,[],1); disp('DM , MSE (if positive: it prefers Random Walk, If negative: prefers ARMA), h=1') disp(dm./sqrt(vcv_white))