优化代码,使其快速运行

时间:2017-01-21 10:51:15

标签: algorithm matlab performance parallel-processing

让我们假设我们有以下代码,它们使用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

我怎样才能加快速度呢?代码中是否有任何可以向量化的行?提前感谢

1 个答案:

答案 0 :(得分: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.
>> 

我该如何解决这个问题?