2017-01-21 96 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. 
>> 

我該如何解決這個問題?