2017-01-21 97 views
0

让我们假设我们有下面的代码,使用BIC标准,该标准选择最好的ARIMA模型优化代码,使其快速运行

function [AR_order,MA_order]= complete_arima(Y) 
%% if not specific input from user, use default time series 
if nargin<1 
    Y=xlsread('ARIMA','ARIMA','d6:d468'); 
end 
% Y is the observed time series 
%% graphical representation of time series 
subplot(3,1,1); 
plot(Y); 
title('original time series'); 
%%autocorrelation/partial autocorrelation of given signal 
subplot(3,1,2); 
autocorr(Y); 
subplot(3,1,3); 
parcorr(Y); 
hold off 
%% entering necessary parameters for Arima Testing 
d=input('enter necessary order of differencing  : '); 
M=input('maximum order of lag for ARMA simulation : '); 
%% simulate ARIMA model on the base of given input 
LOGL = zeros(M,M); %Initialize 
PQ = zeros(M,M); 
for p = 1:M 
    for q = 1:M 
     mod = arima(p,d,q);% for each pair generate new ARIMA model 
     [~,~,logL] = estimate(mod,Y,'print',false); 
     LOGL(p,q) = logL; 
     PQ(p,q) = p+q; 
    end 
end 
%% calculate BIC 
LOGL = reshape(LOGL,M*M,1); 
PQ = reshape(PQ,M*M,1); 
[~,bic] = aicbic(LOGL,PQ+1,100); 
bic=reshape(bic,M,M); 
%% determine order of ARIMA by finding minimum element from matrix bic and corresponding indeces 
[AR_order,MA_order]=find(bic==min(min(bic))); 
end 

,执行时间为

>> tic 
>> [AR_order,MA_order]=complete_arima(); 
enter necessary order of differencing  : 1 
maximum order of lag for ARMA simulation : 4 
>> toc 
Elapsed time is 36.499133 seconds. 

我怎样才能加快给定的代码?我应该使用parfor运行一个循环吗?

第一我已经建立并行池

parpool('local',4) 
Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers. 

ans = 

Pool with properties: 

      Connected: true 
      NumWorkers: 4 
       Cluster: local 
     AttachedFiles: {} 
      IdleTimeout: 30 minute(s) (30 minutes remaining) 
      SpmdEnabled: true 
>> [AR_order,MA_order]=complete_arima(); 
enter necessary order of differencing  : 1 
maximum order of lag for ARMA simulation : 4 
>> toc 
Elapsed time is 24.983676 seconds. 

为并行执行,我用下面的代码

function [AR_order,MA_order]= complete_arima(Y) 
%% if not specific input from user, use default time series 
if nargin<1 
    Y=xlsread('ARIMA','ARIMA','d6:d468'); 
end 
% Y is the observed time series 
%% graphical representation of time series 
subplot(3,1,1); 
plot(Y); 
title('original time series'); 
%%autocorrelation/partial autocorrelation of given signal 
subplot(3,1,2); 
autocorr(Y); 
subplot(3,1,3); 
parcorr(Y); 
hold off 
%% entering necessary parameters for Arima Testing 
d=input('enter necessary order of differencing  : '); 
M=input('maximum order of lag for ARMA simulation : '); 
%% simulate ARIMA model on the base of given input 
LOGL = zeros(M,M); %Initialize 
PQ = zeros(M,M); 
parfor p = 1:M 
    for q = 1:M 
     mod = arima(p,d,q);% for each pair generate new ARIMA model 
     [~,~,logL] = estimate(mod,Y,'print',false); 
     LOGL(p,q) = logL; 
     PQ(p,q) = p+q; 
    end 
end 
%% calculate BIC 
LOGL = reshape(LOGL,M*M,1); 
PQ = reshape(PQ,M*M,1); 
[~,bic] = aicbic(LOGL,PQ+1,100); 
bic=reshape(bic,M,M); 
%% determine order of ARIMA by finding minimum element from matrix bic and corresponding indeces 
[AR_order,MA_order]=find(bic==min(min(bic))); 
end 

我怎样才能加速更?是有在任何一行代码,我可以向量化?在此先感谢

回答

0

在这里我会发布另一个解决方案,持续120秒

function [AR_order,MA_order]= complete_arima(Y) 
%% created parallel loop 
parpool('local',4); 
tic; 
%% if not specific input from user, use default time series 

if nargin<1 
    Y=xlsread('ARIMA','ARIMA','d6:d468'); 
end 
% Y is the observed time series 
%% graphical representation of time series 
subplot(3,1,1); 
plot(Y); 
title('original time series'); 
%%autocorrelation/partial autocorrelation of given signal 
subplot(3,1,2); 
autocorr(Y); 
subplot(3,1,3); 
parcorr(Y); 
hold off 
%% entering necessary parameters for Arima Testing 
d=input('enter necessary order of differencing  : '); 
M=input('maximum order of lag for ARMA simulation : '); 
%% simulate ARIMA model on the base of given input 
LOGL = zeros(M,M); %Initialize 
PQ = zeros(M,M); 
parfor p = 1:M 
    for q = 1:M 
     mod = arima(p,d,q);% for each pair generate new ARIMA model 
     [~,~,logL] = estimate(mod,Y,'print',false); 
     LOGL(p,q) = logL; 
     PQ(p,q) = p+q; 
    end 
end 
%% calculate BIC 
LOGL = reshape(LOGL,M*M,1); 
PQ = reshape(PQ,M*M,1); 
[~,bic] = aicbic(LOGL,PQ+1,100); 
bic=reshape(bic,M,M); 
%% determine order of ARIMA by finding minimum element from matrix bic and corresponding indeces 
[AR_order,MA_order]=find(bic==min(min(bic))); 
toc 
delete(gcp('nocreate')); 
end 

>> [AR_order,MA_order]=complete_arima(); 
Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers. 
enter necessary order of differencing  : 1 
maximum order of lag for ARMA simulation : 10 
Elapsed time is 120.370494 seconds. 
Parallel pool using the 'local' profile is shutting down. 
>> 

我该如何解决这个问题?