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,